pvDatabaseCPP  4.7.0
pvdbcrAddRecord.cpp
Go to the documentation of this file.
1 /*
2  * Copyright information and license terms for this software can be
3  * found in the file LICENSE that is included with the distribution
4  */
5 
10 #include <iocsh.h>
11 #include <pv/standardField.h>
12 #include <pv/standardPVField.h>
13 #include <pv/timeStamp.h>
14 #include <pv/pvTimeStamp.h>
15 #include <pv/alarm.h>
16 #include <pv/pvAlarm.h>
17 #include <pv/pvAccess.h>
18 #include <pv/serverContext.h>
19 #include <pv/rpcService.h>
20 
21 // The following must be the last include for code exampleLink uses
22 #include <epicsExport.h>
23 #define epicsExportSharedSymbols
24 #include "pv/pvDatabase.h"
25 #include "pv/pvdbcrAddRecord.h"
26 using namespace epics::pvData;
27 using namespace std;
28 
29 namespace epics { namespace pvDatabase {
30 
31 PvdbcrAddRecordPtr PvdbcrAddRecord::create(
32  std::string const & recordName,
33  int asLevel,std::string const & asGroup)
34 {
35  FieldCreatePtr fieldCreate = getFieldCreate();
36  PVDataCreatePtr pvDataCreate = getPVDataCreate();
37  StructureConstPtr topStructure = fieldCreate->createFieldBuilder()->
38  addNestedStructure("argument")->
39  add("recordName",pvString)->
40  addNestedUnion("union") ->
41  endNested()->
42  endNested()->
43  addNestedStructure("result") ->
44  add("status",pvString) ->
45  endNested()->
46  createStructure();
47  PVStructurePtr pvStructure = pvDataCreate->createPVStructure(topStructure);
48  PvdbcrAddRecordPtr pvRecord(
49  new PvdbcrAddRecord(recordName,pvStructure,asLevel,asGroup));
50  if(!pvRecord->init()) pvRecord.reset();
51  return pvRecord;
52 }
53 
54 PvdbcrAddRecord::PvdbcrAddRecord(
55  std::string const & recordName,
56  PVStructurePtr const & pvStructure,
57  int asLevel,std::string const & asGroup)
58 : PVRecord(recordName,pvStructure,asLevel,asGroup)
59 {
60 }
61 
62 bool PvdbcrAddRecord::init()
63 {
64  initPVRecord();
65  PVStructurePtr pvStructure = getPVStructure();
66  pvRecordName = pvStructure->getSubField<PVString>("argument.recordName");
67  if(!pvRecordName) return false;
68  pvResult = pvStructure->getSubField<PVString>("result.status");
69  if(!pvResult) return false;
70  return true;
71 }
72 
73 void PvdbcrAddRecord::process()
74 {
75  PVDataCreatePtr pvDataCreate = getPVDataCreate();
76  string name = pvRecordName->get();
77  PVRecordPtr pvRecord = PVDatabase::getMaster()->findRecord(name);
78  if(pvRecord) {
79  pvResult->put(name + " already exists");
80  return;
81  }
82  PVUnionPtr pvUnion = getPVStructure()->getSubField<PVUnion>("argument.union");
83  if(!pvUnion) {
84  pvResult->put(name + " argument.union is NULL");
85  return;
86  }
87  PVFieldPtr pvField(pvUnion->get());
88  if(!pvField) {
89  pvResult->put(name + " union has no value");
90  return;
91  }
92  if(pvField->getField()->getType()!=epics::pvData::structure) {
93  pvResult->put(name + " union most be a structure");
94  return;
95  }
96  StructureConstPtr st = std::tr1::static_pointer_cast<const Structure>(pvField->getField());
97  PVStructurePtr pvStructure = pvDataCreate->createPVStructure(st);
98  PVRecordPtr pvRec = PVRecord::create(name,pvStructure);
99  bool result = PVDatabase::getMaster()->addRecord(pvRec);
100  if(result) {
101  pvResult->put("success");
102  } else {
103  pvResult->put("failure");
104  }
105 }
106 }}
107 
108 static const iocshArg arg0 = { "recordName", iocshArgString };
109 static const iocshArg arg1 = { "asLevel", iocshArgInt };
110 static const iocshArg arg2 = { "asGroup", iocshArgString };
111 static const iocshArg *args[] = {&arg0,&arg1,&arg2};
112 
113 static const iocshFuncDef pvdbcrAddRecordFuncDef = {"pvdbcrAddRecord", 3,args};
114 
115 static void pvdbcrAddRecordCallFunc(const iocshArgBuf *args)
116 {
117  char *sval = args[0].sval;
118  if(!sval) {
119  throw std::runtime_error("pvdbcrAddRecord recordName not specified");
120  }
121  string recordName = string(sval);
122  int asLevel = args[1].ival;
123  string asGroup("DEFAULT");
124  sval = args[2].sval;
125  if(sval) {
126  asGroup = string(sval);
127  }
129  record->setAsLevel(asLevel);
130  record->setAsGroup(asGroup);
132  bool result = master->addRecord(record);
133  if(!result) cout << "recordname " << recordName << " not added" << endl;
134 }
135 
136 static void pvdbcrAddRecord(void)
137 {
138  static int firstTime = 1;
139  if (firstTime) {
140  firstTime = 0;
141  iocshRegister(&pvdbcrAddRecordFuncDef, pvdbcrAddRecordCallFunc);
142  }
143 }
144 
145 extern "C" {
146  epicsExportRegistrar(pvdbcrAddRecord);
147 }
STL namespace.
Base interface for a PVRecord.
Definition: pvDatabase.h:56
std::tr1::shared_ptr< PVDatabase > PVDatabasePtr
Definition: pvDatabase.h:43
std::tr1::shared_ptr< PvdbcrAddRecord > PvdbcrAddRecordPtr
PvdbcrAddRecord A record that adds a record to the master database.
static PVDatabasePtr getMaster()
Get the master database.
Definition: pvDatabase.cpp:38
std::tr1::shared_ptr< PVRecord > PVRecordPtr
Definition: pvDatabase.h:21
epicsExportRegistrar(pvdbcrAddRecord)
static PvdbcrAddRecordPtr create(std::string const &recordName, int asLevel=0, std::string const &asGroup=std::string("DEFAULT"))
Create a record.