DABC (Data Acquisition Backbone Core)  2.9.9
Player.cxx
Go to the documentation of this file.
1 // $Id: Player.cxx 4674 2021-01-28 15:52:10Z linev $
2 
3 /************************************************************
4  * The Data Acquisition Backbone Core (DABC) *
5  ************************************************************
6  * Copyright (C) 2009 - *
7  * GSI Helmholtzzentrum fuer Schwerionenforschung GmbH *
8  * Planckstr. 1, 64291 Darmstadt, Germany *
9  * Contact: http://dabc.gsi.de *
10  ************************************************************
11  * This software can be used under the GPL license *
12  * agreements as stated in LICENSE.txt file *
13  * which is part of the distribution. *
14  ************************************************************/
15 
16 #include "fesa/Player.h"
17 
18 #include <cstdlib>
19 #include <cmath>
20 
21 #include "dabc/Publisher.h"
22 
23 #ifdef WITH_FESA
24 #include <cmw-rda/RDAService.h>
25 #include <cmw-rda/Config.h>
26 #include <cmw-rda/DeviceHandle.h>
27 #include <cmw-rda/ReplyHandler.h>
28 #endif
29 
30 namespace fesa {
31 
32  struct BeamProfile {
33  enum { sizex = 16, sizey = 16 };
34 
35  uint32_t arr[sizex][sizey];
36 
37  void clear()
38  {
39  for (unsigned x=0;x<sizex;x++)
40  for (unsigned y=0;y<sizey;y++)
41  arr[x][y] = 0;
42  }
43 
44  void fill(int kind = 0)
45  {
46  for (unsigned x=0;x<sizex;x++)
47  for (unsigned y=0;y<sizey;y++)
48  switch (kind) {
49  case 0: arr[x][y] = x+y; break;
50  case 1: arr[x][y] = sizex - x + sizey-y; break;
51  case 2: arr[x][y] = sizex - x + y; break;
52  case 3: arr[x][y] = x + sizey-y; break;
53  default: {
54  arr[x][y] = (unsigned) (sizex + sizey - 3*sqrt((x-sizex/2)*(x-sizex/2) + (y-sizey/2)*(y-sizey/2)));
55  }
56  }
57  }
58  };
59 
60 }
61 
62 
63 
64 fesa::Player::Player(const std::string &name, dabc::Command cmd) :
65  dabc::ModuleAsync(name, cmd),
66  fCounter(0),
67  fSniffer(0),
68  fHist(0),
69  fRDAService(0),
70  fDevice(0)
71 {
72  fWorkerHierarchy.Create("FESA", true);
73 
74  // this is just emulation, later one need list of real variables
75  dabc::Hierarchy item = fWorkerHierarchy.CreateHChild("BeamProfile");
76  item.SetField(dabc::prop_kind, "FESA.2D");
77  item.SetField("_autoload", "dabcsys/plugins/fesa/fesa.js");
78  item.SetField("_can_draw", true);
79 
80 
81  fWorkerHierarchy.CreateHChild("BeamRate").SetField(dabc::prop_kind, "rate");
82 
83  item = fWorkerHierarchy.CreateHChild("BeamRate2");
84  item.SetField(dabc::prop_kind, "rate");
85  item.EnableHistory(100);
86 
87  item = fWorkerHierarchy.CreateHChild("TestRate");
88  item.SetField(dabc::prop_kind, "rate");
89  item.EnableHistory(100);
90 
91  CreateCmdDef("CmdReset").AddArg("counter", "int", true, "5");
92 
93  fServerName = Cfg("Server", cmd).AsStr();
94  fDeviceName = Cfg("Device", cmd).AsStr();
95  fCycles = Cfg("Cycles", cmd).AsStr();
96  fService = Cfg("Service", cmd).AsStr();
97  fField = Cfg("Field", cmd).AsStr();
98 
99  CreateTimer("update", 1.);
100 
101  fCounter = 0;
102 
103  #ifdef WITH_FESA
104  if (!fServerName.empty() && !fDeviceName.empty()) {
105  fRDAService = rdaRDAService::init();
106 
107  try {
108  fDevice = fRDAService->getDeviceHandle(fDeviceName.c_str(), fServerName.c_str());
109  } catch (...) {
110  EOUT("Device %s on server %s not found", fDeviceName.c_str(), fServerName.c_str());
111  }
112 
113  if ((fDevice!=0) && !fService.empty()) {
114  item = fWorkerHierarchy.CreateHChild(fService);
115  item.SetField(dabc::prop_kind,"rate");
116  item.EnableHistory(100);
117  }
118 
119  }
120  #endif
121 
122  Publish(fWorkerHierarchy, "FESA/Player");
123 }
124 
126 {
127 }
128 
129 void fesa::Player::ProcessTimerEvent(unsigned timer)
130 {
131 // DOUT0("Process timer");
132 
133  fCounter++;
134 
136 
138  rec->fill(fCounter % 7);
139 
140  dabc::Buffer buf2;
141 
142  if (fCounter == 10) {
143  DOUT0("Create BeamProfile2 10 seconds later");
144  // this is just emulation, later one need list of real variables
145  dabc::LockGuard lock(fWorkerHierarchy.GetHMutex());
146  dabc::Hierarchy item2 = fWorkerHierarchy.CreateHChild("BeamProfile2");
147  item2.SetField(dabc::prop_kind, "FESA.2D");
148  item2.SetField("_autoload", "dabcsys/plugins/fesa/fesa.js");
149  item2.SetField("_can_draw", true);
150  }
151 
152  if (fCounter > 10) {
155  rec2->fill((fCounter+4) % 7);
156  }
157 
158 
159 #ifdef WITH_FESA
160  if ((fDevice!=0) && !fService.empty()) {
161  double res = doGet(fService, fField);
162  dabc::LockGuard lock(fWorkerHierarchy.GetHMutex());
163  fWorkerHierarchy.GetHChild(fService).SetField("value", res);
164  // DOUT0("GET FESA field %s = %5.3f", fService.c_str(), res);
165  }
166 #endif
167 
168  dabc::LockGuard lock(fWorkerHierarchy.GetHMutex());
169 
170  dabc::Hierarchy item = fWorkerHierarchy.GetHChild("BeamProfile");
171 
172  // DOUT0("Set binary buffer %u to item %s %p", buf.GetTotalSize(), item.GetName(), item.GetObject());
173 
174  item()->bindata() = buf;
175  item.SetField(dabc::prop_hash, fCounter);
176 
177 
178  if (fCounter > 10) {
179  // fill also second profile
180  dabc::Hierarchy item2 = fWorkerHierarchy.GetHChild("BeamProfile2");
181  item2()->bindata() = buf2;
182  item2.SetField(dabc::prop_hash, fCounter);
183  }
184 
185  double v1 = 100. * (1.3 + sin(dabc::Now().AsDouble()/5.));
186  fWorkerHierarchy.GetHChild("BeamRate").SetField("value", dabc::format("%4.2f", v1));
187  // int64_t arr[5] = {1,7,4,2,3};
188  //fWorkerHierarchy.FindChild("BeamRate").Field("arr").SetArrInt(5, arr);
189 
190 // std::vector<int> res;
191 // res = fWorkerHierarchy.FindChild("BeamRate").Field("arr").AsIntVector();
192 // DOUT0("Get vector of size %u", res.size());
193 // for (unsigned n=0;n<res.size();n++) DOUT0(" arr[%u] = %d", n, res[n]);
194 
195  v1 = 100. * (1.3 + cos(dabc::Now().AsDouble()/8.));
196  fWorkerHierarchy.GetHChild("BeamRate2").SetField("value", dabc::format("%4.2f", v1));
197 
198  int test = fCounter % 100;
199  v1 = 20 + (test & 0xfffffc) + (test & 3)*0.01;
200  fWorkerHierarchy.GetHChild("TestRate").SetField("value", dabc::format("%4.2f", v1));
201 
202  fWorkerHierarchy.MarkChangedItems();
203 
204 #ifdef DABC_EXTRA_CHECKS
205 // if (fCounter % 10 == 0)
206 // dabc::Object::DebugObject();
207 #endif
208 }
209 
210 
212 {
213  if (cmd.IsName(dabc::CmdGetBinary::CmdName())) {
214 
215  std::string itemname = cmd.GetStr("subitem");
216  std::string binkind = cmd.GetStr("Kind");
217  std::string query = cmd.GetStr("Query");
218 
219  // DOUT0("fesa::Player CmdGetBinary item %s kind %s", itemname.c_str(), binkind.c_str());
220 
221  dabc::LockGuard lock(fWorkerHierarchy.GetHMutex());
222 
223  dabc::Hierarchy item = fWorkerHierarchy.GetHChild(itemname);
224 
225  if (item.null()) {
226  EOUT("Item %s not found to get binary", itemname.c_str());
227  return dabc::cmd_false;
228  }
229 
230  dabc::Buffer buf;
231 
232  if (binkind == "dabc.bin") {
233  buf = item()->bindata();
234  item.FillBinHeader("", cmd);
235  }
236 
237  if (buf.null()) {
238  EOUT("No find binary data for item %s", itemname.c_str());
239  return dabc::cmd_false;
240  }
241 
242  cmd.SetRawData(buf);
243 
244  return dabc::cmd_true;
245  } else
246  if (cmd.IsName("CmdReset")) {
247 
248  fCounter = cmd.GetInt("counter", 0);
249  DOUT0("****************** CmdReset counter=%d ****************", fCounter);
250  return dabc::cmd_true;
251  }
252 
254 }
255 
256 double fesa::Player::doGet(const std::string &service, const std::string &field)
257 {
258  double res = 0.;
259 
260  #ifdef WITH_FESA
261  if (fDevice==0) return 0.;
262  try {
263  rdaData context;
264  rdaData* value = fDevice->get(service.c_str(), fCycles.c_str(), context);
265  if (value!=0) {
266  res = value->extractDouble(field.c_str());
267  delete value;
268  }
269  }
270  catch (const rdaException& ex)
271  {
272  EOUT("Exception caught in doGet: %s %s", ex.getType(), ex.getMessage());
273  }
274  catch (...)
275  {
276  EOUT("Uncknown exception caught in doGet");;
277  }
278  #endif
279  return res;
280 }
281 
Reference on memory from memory pool.
Definition: Buffer.h:135
static Buffer CreateBuffer(BufferSize_t sz)
This static method create independent buffer for any other memory pools Therefore it can be used in s...
Definition: Buffer.cxx:419
void * SegmentPtr(unsigned n=0) const
Returns pointer on the segment, no any boundary checks.
Definition: Buffer.h:171
Represents command with its arguments.
Definition: Command.h:99
bool SetRawData(Buffer rawdata)
Set raw data to the command, which can be transported also between nodes.
Definition: Command.cxx:334
std::string GetStr(const std::string &name, const std::string &dflt="") const
Definition: Command.h:136
int GetInt(const std::string &name, int dflt=0) const
Definition: Command.h:139
Represents objects hierarchy of remote (or local) DABC process.
Definition: Hierarchy.h:285
bool FillBinHeader(const std::string &itemname, dabc::Command &cmd, uint64_t mhash=0, const std::string &dflt_master_name="")
Fill binary header with item and master versions.
Definition: Hierarchy.cxx:1070
Hierarchy CreateHChild(const std::string &name, bool allowslahes=false, bool sortorder=false)
Create child item in hierarchy with specified name If allowslahes enabled, instead of subfolders item...
Definition: Hierarchy.h:392
Hierarchy GetHChild(const std::string &name, bool allowslahes=false, bool force=false, bool sortorder=false)
Return child, if necessary creates with full subfolder If force specified, missing childs and folders...
Definition: Hierarchy.cxx:944
void EnableHistory(unsigned length=100, bool withchilds=false)
Activate history production for selected element and its childs.
Definition: Hierarchy.cxx:837
Lock guard for posix mutex.
Definition: threads.h:127
virtual int ExecuteCommand(Command cmd)
Main method where commands are executed.
Definition: Module.h:232
bool SetField(const std::string &name, const RecordField &v)
Definition: Record.h:516
bool IsName(const char *name) const
Returns true if object name is the same as specified one.
Definition: Reference.cxx:177
bool null() const
Returns true if reference contains nullptr.
Definition: Reference.h:151
virtual ~Player()
Definition: Player.cxx:125
virtual void ProcessTimerEvent(unsigned timer)
Method called by framework when timer event is produced.
Definition: Player.cxx:129
virtual int ExecuteCommand(dabc::Command cmd)
Main method where commands are executed.
Definition: Player.cxx:211
double doGet(const std::string &service, const std::string &field)
Definition: Player.cxx:256
Player(const std::string &name, dabc::Command cmd=nullptr)
Definition: Player.cxx:64
#define DOUT0(args ...)
Definition: logging.h:156
#define EOUT(args ...)
Definition: logging.h:150
Event manipulation API.
Definition: api.h:23
TimeStamp Now()
Definition: timing.h:260
std::string format(const char *fmt,...)
Definition: string.cxx:49
const char * prop_hash
Definition: Hierarchy.cxx:35
const char * prop_kind
Definition: Hierarchy.cxx:29
@ cmd_false
Definition: Command.h:37
@ cmd_true
Definition: Command.h:38
Support for FESA - the control system of GSI/FAIR accelerator.
Definition: Factory.h:25
void fill(int kind=0)
Definition: Player.cxx:44
uint32_t arr[sizex][sizey]
Definition: Player.cxx:35