OpcUaCanOpen
CANopen OPC-UA server
UaCalcItem.hpp
Go to the documentation of this file.
1 #ifndef __UACALCITEM_H__
2 #define __UACALCITEM_H__
3 
4 #include <opcua_basedatavariabletype.h>
5 #include "NmBuildingAutomation.h"
6 #include "UaControlDeviceItem.h"
8 #include "uadatetime.h"
9 #include "uadatavalue.h"
10 #include <cmath>
11 #include <string>
12 #include "srvtrace.h"
13 
14 #include "muParser.h"
15 //#include "muParserDef.h"
16 
17 #ifndef isnan
18 #define isnan(x) std::isnan(x)
19 #endif
20 
21 #ifndef isinf
22 #define isinf(x) std::isinf(x)
23 #endif
24 
25 namespace AddressSpace
26 {
27  typedef std::map<string,vector<UaControlVariable *> > mapDataVariable ;
28 
29  typedef ::ITEM ITEM_type;
30  typedef ::xsd::cxx::tree::sequence< ITEM_type > ITEM_sequence;
31  typedef ITEM_sequence::iterator ITEM_iterator;
32  typedef ITEM_sequence::const_iterator ITEM_const_iterator;
33 
34  typedef ::REGEXPR REGEXPR_type;
35  typedef ::xsd::cxx::tree::sequence< REGEXPR_type > REGEXPR_sequence;
36  typedef REGEXPR_sequence::iterator REGEXPR_iterator;
37  typedef REGEXPR_sequence::const_iterator REGEXPR_const_iterator;
38 
45  class UaCalcItem: public UaControlVariable
46  {
48  public:
49 /*
50  struct UaControlVariableValue {
51  double value;
52  UaControlVariable *ucv;
53  bool active;
54  UaControlVariableValue(UaControlVariable *o, double d)
55  {
56  ucv = o; active = false; value = d;
57  };
58  };
59 */
60 // typedef map<UaNodeId, UaControlVariableValue *> variableSet;
61  typedef map<std::string, UaControlVariable*> variableSet;
62 
67  virtual void calculateOnChange(Session *pSession, const UaDataValue& dataValue, OpcUa_Boolean checkAccessLevel);
68 
70  virtual void initCalculation();
71 
83 // UaCalcItem( const UaString& name,UaNodeId& uaId,
84 // NmBuildingAutomation * pNodeConfig, UaVariant &dv,string &formul, string &whenCondition, string &statusCondition,UaMutexRefCounted* pSharedMutex );
85 
86  UaCalcItem(const UaString& name, UaNodeId& uaId, const UaNode *parent,
87  NmBuildingAutomation * pNodeConfig, UaVariant &dv, ITEM &citem, UaControlVariableMap &cvs, UaMutexRefCounted* pSharedMutex);
88 
89 // bool compile();
90 
92  virtual ~UaCalcItem(void) {}
93 
100  {
101  boost::arg<1> _1;
102  boost::arg<2> _2;
103  boost::arg<3> _3;
104 
105 // UaDataValue ud = obd->value(0);
106 // UaControlVariableValue * u = new UaControlVariableValue(obd, convertToDouble(ud));
107 // cout << "ADD " << name << obd->browseName().toString().toUtf8() << endl << flush;
108 
109  m_formul.DefineVar(name, &(obd->curValue));
110  m_var.insert(pair<string, UaControlVariable *>(name, obd));
111 
112  if ( m_sWhenCondition.length() == 0)
113  obd->valueChange.connect(boost::bind(&UaCalcItem::calculateOnChange,this,_1,_2,_3));
114  }
115 
122  {
123  boost::arg<1> _1;
124  boost::arg<2> _2;
125  boost::arg<3> _3;
126 // UaDataValue ud = obd->value(0);
127 // UaControlVariableValue * u = new UaControlVariableValue(obd, convertToDouble(ud));
128 
129  m_formul.DefineVar(name, &(obd->curValue));
130  m_whenVar.insert(pair<string, UaControlVariable *>(name, obd));
131  obd->valueChange.connect(boost::bind(&UaCalcItem::calculateOnChange,this,_1,_2,_3));
132  }
139  {
140 // boost::arg<1> _1;
141 // UaDataValue ud = obd->value(0);
142 // UaControlVariableValue * u = new UaControlVariableValue(obd, convertToDouble(ud));
143 
144  m_statusFormula.DefineVar(name, &(obd->curValue));
145  m_statusVar.insert(pair<string ,UaControlVariable *>(name,obd));
146  }
147 
148  OpcUa_StatusCode checkStatus(variableSet& vs)
149  {
150  OpcUa_StatusCode ret = OpcUa_Good;
151  for (UaControlVariableMap::const_iterator vit = vs.begin(); vit != vs.end(); vit++)
152  {
153  UaControlVariable *uac = (*vit).second;
154  ret = uac->value(0).statusCode();
155 // cout << "Status Code " << uac->browseName().toString().toUtf8() << " "<< hex << ret << " value "<< uac->curValue << endl << flush;
156  if (ret != OpcUa_Good)
157  break;
158  }
159  return ret;
160  }
161 
165  static mu::value_type* getValueFromItem(const mu::char_type* ind, void * pNode);
166  static mu::value_type* getValueFromWhenItem(const mu::char_type* ind, void* pNode);
167  static mu::value_type* getValueFromStatusItem(const mu::char_type* ind, void* pNode);
168 
169  string &getFormula() { return m_sFormul; }
170  string &getWhenCondition() { return m_sWhenCondition; }
172 
173  //UaDateTime UaGetSourceTimeStamp(); //! define the timestamps of latest control variable
174 
175  static double PowFunc(double a_fVal, double b_fVal)
176  {
177  double d = std::pow(a_fVal, b_fVal);
178  return d;
179  }
180 
181  private:
182 // double convertToDouble(variableSet::iterator &vit);
183 
187 
188  bool m_fExists;
189  bool m_cExists;
190  bool m_sExists;
191 
195 
196  string m_sFormul;
197  string m_sWhenCondition;
198  string m_sStatusCondition;
200  const UaNode *m_pParent;
201  };
202 
203  /*
204  class UaCalcCompilerItem
205  {
206  public:
207  typedef void(UaCalcItem::*pushUaVariable)(std::string& , UaControlVariable *);
208 
209  UaCalcCompilerItem(string name, REGEXPR_sequence &regex)
210  {
211  m_regex = regex;
212  m_pParentCalc = 0;
213  m_sName = name;
214  m_whenCondition = 0;
215  m_formul = 0;
216  m_statusFormula = 0;
217  m_pSharedMutex = 0;
218 
219  };
220 
221  UaCalcCompilerItem(string name, OpcUa::BaseObjectType *pcn, REGEXPR_sequence &regex)
222  {
223  m_regex = regex;
224  m_pParentCalc = pcn;
225  m_sName = name;
226  m_whenCondition = 0;
227  m_formul = 0;
228  m_statusFormula = 0;
229 
230  m_pSharedMutex = ((UaControlDeviceGeneric *)pcn)->getSharedMutex();
231 
232  };
233 
234  ~UaCalcCompilerItem()
235  {
236  m_vCalcItem.clear();
237  }
238 
239  void setFormula(string *formul) { m_formul = formul; }
240  void setWhenCondition(string *wformul) { m_whenCondition = wformul; }
241  void setStatusCondition(string *sformul) { m_statusFormula = sformul; }
242 
243  string &getName() { return m_sName; }
244 
245  string * getFormula() { return m_formul; }
246  string * getWhenCondition() { return m_whenCondition; }
247  string * getStatusCondition() { return m_statusFormula; }
248 
249  REGEXPR_sequence &getRegExp() { return m_regex ; }
250  OpcUa::BaseObjectType *getParentCalc() { return m_pParentCalc; }
251  UaMutexRefCounted *getItemSharedMutex() { return m_pSharedMutex; }
252 
253  void addTempItem(UaCalcItem * uci) { m_vCalcItem.push_back(uci); }
254  UaStatus CreateLinks(NmBuildingAutomation *pNodeManager);
255  UaStatus findLinkToUaVariable(UaCalcItem *,NmBuildingAutomation *,string& ,OpcUa_Int,pushUaVariable);
256 
258  virtual void initCalculation();
259  mapDataVariable &getMapDataVariable()
260  {
261  return pVariables;
262  }
263  bool isVariableMap(string name)
264  {
265  return (pVariables.find(name) != pVariables.end());
266  }
267 
268  private:
269  string m_sName;
270  REGEXPR_sequence m_regex;
271 
272  string m_whenCondition; //! logical formula if it s true the calculation will be executed
273  string m_formul; //! analytic formula to calculate
274  string m_statusFormula; //! logical formula to calculate the status of variable
275 
276  UaMutexRefCounted *m_pSharedMutex;
277  OpcUa::BaseObjectType *m_pParentCalc;
278  vector<UaCalcItem *> m_vCalcItem; //! the temporary list of items
279  mapDataVariable pVariables; //! temporary set of pointer to variables created
280  };
281  */
282 
288  /*
289  class UaCalcCompiler
290  {
291  public:
292  class isName { //! predicate class to select the pattern
293  public:
294  isName(std::string sn): n(sn) {}
295  inline bool operator()(REGEXPR &reg) { return reg.name() == n; }
296  private:
297  std::string n;
298  };
299 
300  class existRegName { //! predicate class to select the pattern
301  public:
302  existRegName(std::string sn) : n(sn) {}
303  bool operator()(UaCalcCompilerItem *o)
304  {
305  REGEXPR_sequence &regex = o->getRegExp();
306  REGEXPR_sequence::iterator it = find_if(regex.begin(), regex.end(), isName(n));
307  return (it != regex.end());
308  }
309  private:
310  std::string n;
311  };
312 
315  //~UaCalcCompiler()
316  //{
317  // for (OpcUa_UInt i = 0; i < m_vCCI.size(); i++) {
318 
319  // delete m_vCCI[i];
320  // }
321  // m_vCCI.clear();
322 
323  //}
324 
328  //UaCalcCompiler(NmBuildingAutomation *pManager)
329  //{
330  // m_vCCI.clear();
331  // m_pNodeManager = pManager;
332  //}
333 
339  //UaStatus CompileItems(OpcUa::BaseObjectType *pParent,
340  // ITEM_sequence &itnd,
341  // REGEXPR_sequence &regex);
342 
348 // UaStatus CompileItems( ITEM_sequence &itnd,REGEXPR_sequence &regex);
349 
353  //UaStatus CreateItems();
354 
358 // void initCalculation();
359 
363  // UaStatus addCalcCompilerItem(UaCalcCompilerItem *);
364 
365  //private:
366  // UaStatus CompileOneItem(string &name, REGEXPR_sequence &regex,ITEM_iterator &sit);
367  // UaStatus createRegVariable(mapDataVariable&, OpcUa::BaseObjectType *, /*CanOpenOpcServerConfig::*/REGEXPR_sequence &, OpcUa_Int32 &);
368 
369  // vector< UaCalcCompilerItem *> m_vCCI; //! set of formulas
370  // NmBuildingAutomation *m_pNodeManager;
371  //};
372 
373 }
374 #endif
375