tdaq-develop-2025-02-12
EpicsInterface.h
1 #ifndef _ots_EpicsInterface_h
2 #define _ots_EpicsInterface_h
3 
4 #include <ctime>
5 #include <fstream>
6 #include <map>
7 #include <queue>
8 #include <thread>
9 #include <utility>
10 #include <vector>
11 #include "cadef.h"
12 
13 #include <arpa/inet.h>
14 #include <dirent.h>
15 #include <errno.h>
16 #include <netdb.h>
17 #include <netinet/in.h>
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <string.h>
21 #include <sys/socket.h>
22 #include <sys/types.h>
23 #include <unistd.h>
24 #include <string>
25 
26 #include <libpq-fe.h>
27 
28 #include "otsdaq/SlowControlsCore/SlowControlsVInterface.h"
29 
30 // clang-format off
31 
32 struct dbr_ctrl_char;
33 
34 
35 namespace ots
36 {
37 class EpicsInterface;
38 
40 {
41  PVHandlerParameters(std::string pv, EpicsInterface* client)
42  {
43  pvName = pv;
44  webClient = client;
45  }
46  std::string pvName;
47  EpicsInterface* webClient;
48 };
49 
50 struct PVAlerts
51 {
52  PVAlerts(time_t t, const char* c, const char* d)
53  {
54  status = c;
55  severity = d;
56  time = t;
57  }
58  std::string status;
59  std::string severity;
60  time_t time;
61 };
62 
63 struct PVInfo
64 {
65  PVInfo(chtype tmpChannelType)
66  {
67  channelType = tmpChannelType;
69  for(int i = 0; i < circularBufferSize; i++)
70  {
71  std::pair<time_t, std::string> filler(0, "");
72  dataCache[i] = filler;
73  }
74  // mostRecentBufferIndex = 0;
75  }
76 
77  chid channelID = NULL;
78  PVHandlerParameters* parameterPtr = NULL;
79  evid eventID = NULL;
80  chtype channelType;
81  std::string pvValue;
82  int circularBufferSize = 10;
83  unsigned int mostRecentBufferIndex = -1;
84  std::vector<std::pair<time_t, std::string>>
86  bool valueChange = true;
89  std::queue<PVAlerts> alerts;
90  //struct dbr_ctrl_char settings;
91  struct dbr_ctrl_double settings;
92 
93 };
94 
96 PGconn *dcsArchiveDbConn;
97 PGconn *dcsAlarmDbConn;
98 PGconn *dcsLogDbConn;
99 int dcsArchiveDbConnStatus_;
100 int dcsAlarmDbConnStatus_;
101 int dcsLogDbConnStatus_;
102 
103 class EpicsInterface : public SlowControlsVInterface
104 {
105  public:
107  const std::string& pluginType,
108  const std::string& interfaceUID,
109  const ConfigurationTree& theXDAQContextConfigTree,
110  const std::string& controlsConfigurationPath);
111  ~EpicsInterface();
112 
113  static const std::string EPICS_NO_ALARM;
114  static const std::string EPICS_INVALID_ALARM;
115  static const std::string EPICS_MINOR_ALARM;
116  static const std::string EPICS_MAJOR_ALARM;
117 
118  void initialize (void) override;
119  void destroy (void);
120 
121  std::vector<std::string> getChannelList (void) override;
122  std::string getList (const std::string& format) override;
123  void subscribe (const std::string& pvName) override;
124  void subscribeJSON (const std::string& JSONpvList) override;
125  void unsubscribe (const std::string& pvName) override;
126  std::array<std::string, 4> getCurrentValue (const std::string& pvName) override;
127  std::array<std::string, 9> getSettings (const std::string& pvName) override;
128  std::vector<std::vector<std::string>> getChannelHistory (const std::string& pvName, int startTime, int endTime) override;
129  std::vector<std::vector<std::string>> getLastAlarms (const std::string& pvName) override;
130  std::vector<std::vector<std::string>> getAlarmsLog (const std::string& pvName) override;
131  std::vector<std::vector<std::string>> checkAlarmNotifications (void) override;
132  std::vector<std::string> checkAlarm (const std::string& pvName, bool ignoreMinor = false);
133 
134  void dbSystemLogin (void);
135  void dbSystemLogout (void);
136 
137  private:
138  void handleAlarmsForFSM (const std::string& fsmTransitionName, ConfigurationTree LinkToAlarmsToMonitor);
139 
140  public:
141 
142  virtual void configure (void) override;
143  virtual void halt (void) override { handleAlarmsForFSM("halt", getSelfNode().getNode("LinkToHaltAlarmsToMonitorTable")); }
144  virtual void pause (void) override { handleAlarmsForFSM("pause", getSelfNode().getNode("LinkToPauseAlarmsToMonitorTable")); }
145  virtual void resume (void) override { handleAlarmsForFSM("resume", getSelfNode().getNode("LinkToResumeAlarmsToMonitorTable")); }
146  virtual void start (std::string /*runNumber*/) override { handleAlarmsForFSM("start",getSelfNode().getNode("LinkToStartAlarmsToMonitorTable")); }
147  virtual void stop (void) override { handleAlarmsForFSM("stop", getSelfNode().getNode("LinkToStopAlarmsToMonitorTable")); }
148 
150  virtual bool running (void) override { handleAlarmsForFSM("running", getSelfNode().getNode("LinkToRunningAlarmsToMonitorTable")); sleep(1); return true;}
153  private:
154  bool checkIfPVExists (const std::string& pvName);
155  void loadListOfPVs (void);
156  void getControlValues (const std::string& pvName);
157  void createChannel (const std::string& pvName);
158  void destroyChannel (const std::string& pvName);
159  void subscribeToChannel (const std::string& pvName, chtype subscriptionType);
160  void cancelSubscriptionToChannel(const std::string& pvName);
161  void readValueFromPV (const std::string& pvName);
162  void writePVValueToRecord (const std::string& pvName, const std::string& pdata);
163  //void writePVControlValueToRecord(std::string pvName, struct dbr_ctrl_char* pdata);
164  void writePVControlValueToRecord(const std::string& pvName, struct dbr_ctrl_double* pdata);
165  void writePVAlertToQueue (const std::string& pvName, const char* status, const char* severity);
166  void readPVRecord (const std::string& pvName);
167  void debugConsole (const std::string& pvName);
168  static void eventCallback (struct event_handler_args eha);
169  static void eventCallbackAlarm (struct event_handler_args eha);
170  static void staticChannelCallbackHandler(struct connection_handler_args cha);
171  static void accessRightsCallback (struct access_rights_handler_args args);
172  static void printChidInfo (chid chid, const std::string& message);
173  void channelCallbackHandler (struct connection_handler_args& cha);
174  void popQueue (const std::string& pvName);
175 
176  private:
177  // std::map<chid, std::string> mapOfPVs_;
178  std::map<std::string, PVInfo*> mapOfPVInfo_;
179  int status_;
180  std::string loginErrorMsg_;
181 };
182 // clang-format on
183 } // namespace ots
184 
185 #endif
std::vector< std::string > checkAlarm(const std::string &pvName, bool ignoreMinor=false)
virtual void configure(void) override
Configure override for Epics.
virtual bool running(void) override
States.
std::vector< std::vector< std::string > > checkAlarmNotifications(void) override
Check Alarms from Epics.
std::vector< std::pair< time_t, std::string > > dataCache
(10, std::pair<time_t, std::string> (0, ""));
int circularBufferSize
Default Guess.