The following were made to all special pvdbcr modules
1) Much cleaner implementation. 2) pvdbSpecialRegister.dbd is new
This commit is contained in:
@@ -7,47 +7,148 @@
|
||||
* @author mrk
|
||||
* @date 2021.03.12
|
||||
*/
|
||||
|
||||
|
||||
/* Author: Marty Kraimer */
|
||||
#include <epicsThread.h>
|
||||
#include <iocsh.h>
|
||||
#include <pv/event.h>
|
||||
#include <pv/pvAccess.h>
|
||||
#include <pv/serverContext.h>
|
||||
#include <pv/pvData.h>
|
||||
#include <pv/standardField.h>
|
||||
#include <pv/standardPVField.h>
|
||||
#include <pv/timeStamp.h>
|
||||
#include <pv/pvTimeStamp.h>
|
||||
#include <pv/rpcService.h>
|
||||
#include <pv/alarm.h>
|
||||
#include <pv/pvAlarm.h>
|
||||
#include <pv/pvDatabase.h>
|
||||
#include <pv/pvaClient.h>
|
||||
|
||||
// The following must be the last include for code pvDatabase uses
|
||||
// The following must be the last include for code exampleLink uses
|
||||
#include <epicsExport.h>
|
||||
#define epicsExportSharedSymbols
|
||||
#include "pv/pvStructureCopy.h"
|
||||
#include "pv/pvDatabase.h"
|
||||
#include "pv/pvdbcrAddRecord.h"
|
||||
|
||||
using namespace epics::pvData;
|
||||
using namespace epics::pvAccess;
|
||||
using namespace epics::pvDatabase;
|
||||
using namespace epics::pvaClient;
|
||||
using namespace std;
|
||||
|
||||
static const iocshArg testArg0 = { "recordName", iocshArgString };
|
||||
static const iocshArg testArg1 = { "asLevel", iocshArgInt };
|
||||
static const iocshArg *testArgs[] = {&testArg0,&testArg1};
|
||||
class PvdbcrAddRecord;
|
||||
typedef std::tr1::shared_ptr<PvdbcrAddRecord> PvdbcrAddRecordPtr;
|
||||
|
||||
static const iocshFuncDef pvdbcrAddRecordFuncDef = {"pvdbcrAddRecord", 2,testArgs};
|
||||
|
||||
class epicsShareClass PvdbcrAddRecord :
|
||||
public PVRecord
|
||||
{
|
||||
private:
|
||||
PvdbcrAddRecord(
|
||||
std::string const & recordName,
|
||||
epics::pvData::PVStructurePtr const & pvStructure);
|
||||
PVStringPtr pvRecordName;
|
||||
epics::pvData::PVStringPtr pvResult;
|
||||
public:
|
||||
POINTER_DEFINITIONS(PvdbcrAddRecord);
|
||||
|
||||
static PvdbcrAddRecordPtr create(
|
||||
std::string const & recordName);
|
||||
virtual bool init();
|
||||
virtual void process();
|
||||
};
|
||||
|
||||
PvdbcrAddRecordPtr PvdbcrAddRecord::create(
|
||||
std::string const & recordName)
|
||||
{
|
||||
FieldCreatePtr fieldCreate = getFieldCreate();
|
||||
PVDataCreatePtr pvDataCreate = getPVDataCreate();
|
||||
StructureConstPtr topStructure = fieldCreate->createFieldBuilder()->
|
||||
addNestedStructure("argument")->
|
||||
add("recordName",pvString)->
|
||||
addNestedUnion("union") ->
|
||||
endNested()->
|
||||
endNested()->
|
||||
addNestedStructure("result") ->
|
||||
add("status",pvString) ->
|
||||
endNested()->
|
||||
createStructure();
|
||||
PVStructurePtr pvStructure = pvDataCreate->createPVStructure(topStructure);
|
||||
PvdbcrAddRecordPtr pvRecord(
|
||||
new PvdbcrAddRecord(recordName,pvStructure));
|
||||
if(!pvRecord->init()) pvRecord.reset();
|
||||
return pvRecord;
|
||||
}
|
||||
|
||||
PvdbcrAddRecord::PvdbcrAddRecord(
|
||||
std::string const & recordName,
|
||||
PVStructurePtr const & pvStructure)
|
||||
: PVRecord(recordName,pvStructure)
|
||||
{
|
||||
}
|
||||
|
||||
bool PvdbcrAddRecord::init()
|
||||
{
|
||||
initPVRecord();
|
||||
PVStructurePtr pvStructure = getPVStructure();
|
||||
pvRecordName = pvStructure->getSubField<PVString>("argument.recordName");
|
||||
if(!pvRecordName) return false;
|
||||
pvResult = pvStructure->getSubField<PVString>("result.status");
|
||||
if(!pvResult) return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
void PvdbcrAddRecord::process()
|
||||
{
|
||||
PVDataCreatePtr pvDataCreate = getPVDataCreate();
|
||||
string name = pvRecordName->get();
|
||||
PVRecordPtr pvRecord = PVDatabase::getMaster()->findRecord(name);
|
||||
if(pvRecord) {
|
||||
pvResult->put(name + " already exists");
|
||||
return;
|
||||
}
|
||||
PVUnionPtr pvUnion = getPVStructure()->getSubField<PVUnion>("argument.union");
|
||||
if(!pvUnion) {
|
||||
pvResult->put(name + " argument.union is NULL");
|
||||
return;
|
||||
}
|
||||
PVFieldPtr pvField(pvUnion->get());
|
||||
if(!pvField) {
|
||||
pvResult->put(name + " union has no value");
|
||||
return;
|
||||
}
|
||||
if(pvField->getField()->getType()!=epics::pvData::structure) {
|
||||
pvResult->put(name + " union most be a structure");
|
||||
return;
|
||||
}
|
||||
StructureConstPtr st = static_pointer_cast<const Structure>(pvField->getField());
|
||||
PVStructurePtr pvStructure = pvDataCreate->createPVStructure(st);
|
||||
PVRecordPtr pvRec = PVRecord::create(name,pvStructure);
|
||||
bool result = PVDatabase::getMaster()->addRecord(pvRec);
|
||||
if(result) {
|
||||
pvResult->put("success");
|
||||
} else {
|
||||
pvResult->put("failure");
|
||||
}
|
||||
}
|
||||
|
||||
static const iocshArg arg0 = { "recordName", iocshArgString };
|
||||
static const iocshArg arg1 = { "asLevel", iocshArgInt };
|
||||
static const iocshArg arg2 = { "asGroup", iocshArgString };
|
||||
static const iocshArg *args[] = {&arg0,&arg1,&arg2};
|
||||
|
||||
static const iocshFuncDef pvdbcrAddRecordFuncDef = {"pvdbcrAddRecord", 3,args};
|
||||
|
||||
static void pvdbcrAddRecordCallFunc(const iocshArgBuf *args)
|
||||
{
|
||||
char *recordName = args[0].sval;
|
||||
if(!recordName) {
|
||||
throw std::runtime_error("pvdbcrAddRecord invalid number of arguments");
|
||||
char *sval = args[0].sval;
|
||||
if(!sval) {
|
||||
throw std::runtime_error("pvdbcrAddRecord recordName not specified");
|
||||
}
|
||||
string recordName = string(sval);
|
||||
int asLevel = args[1].ival;
|
||||
string asGroup("DEFAULT");
|
||||
sval = args[2].sval;
|
||||
if(sval) {
|
||||
asGroup = string(sval);
|
||||
}
|
||||
PvdbcrAddRecordPtr record = PvdbcrAddRecord::create(recordName);
|
||||
record->setAsLevel(asLevel);
|
||||
bool result = PVDatabase::getMaster()->addRecord(record);
|
||||
if(!result) cout << "recordname" << " not added" << endl;
|
||||
record->setAsGroup(asGroup);
|
||||
PVDatabasePtr master = PVDatabase::getMaster();
|
||||
bool result = master->addRecord(record);
|
||||
if(!result) cout << "recordname " << recordName << " not added" << endl;
|
||||
}
|
||||
|
||||
static void pvdbcrAddRecordRegister(void)
|
||||
|
||||
Reference in New Issue
Block a user