IQRF Gateway Daemon
ProtocolBridgeClientService.h
Go to the documentation of this file.
1 
17 #pragma once
18 
19 #include "JsonUtils.h"
20 #include "ProtocolBridge.h"
21 #include "IService.h"
22 #include "ISerializer.h"
23 #include "IMessaging.h"
24 #include "IScheduler.h"
25 #include "TaskQueue.h"
26 #include <string>
27 #include <chrono>
28 #include <vector>
29 #include <list>
30 #include <memory>
31 #include <unordered_map>
32 
33 class IDaemon;
34 
35 typedef std::basic_string<unsigned char> ustring;
36 
37 template <typename T>
39 {
40 public:
41  ScheduleDpaTask() = delete;
42  ScheduleDpaTask(const T& dpt, IScheduler* schd)
43  :m_scheduler(schd)
44  , m_dpa(dpt)
45  , m_sync(false)
46  {}
47 
49  : m_taskHandle(other.m_taskHandle)
50  , m_scheduler(other.m_scheduler)
51  , m_dpa(other.m_dpa)
52  , m_sync((bool)other.m_sync)
53  {
54  }
55 
56  virtual ~ScheduleDpaTask() {};
57 
58  bool isSync() const { return m_sync; }
59  void setSync(bool val) { m_sync = val; }
60 
61  void scheduleTaskPeriodic(const std::string& clientId, const std::string& task, const std::chrono::seconds& sec,
62  const std::chrono::system_clock::time_point& tp = std::chrono::system_clock::now())
63  {
64  removeSchedule(clientId);
65  m_taskHandle = m_scheduler->scheduleTaskPeriodic(clientId, task, sec, tp);
66  }
67 
68  bool isScheduled()
69  {
70  return m_taskHandle != IScheduler::TASK_HANDLE_INVALID;
71  }
72 
73  void removeSchedule(const std::string& clientId)
74  {
75  m_scheduler->removeTask(clientId, m_taskHandle);
76  m_taskHandle = IScheduler::TASK_HANDLE_INVALID;
77  }
78 
79  T& getDpa() { return m_dpa; }
80 private:
81  std::atomic_bool m_sync{ false };
83  IScheduler* m_scheduler;
84  T m_dpa;
85 };
86 
88 
90 {
91 public:
92  ProtocolBridgeClientService() = delete;
93  ProtocolBridgeClientService(const std::string& name);
94  virtual ~ProtocolBridgeClientService();
95 
96  void updateConfiguration(const rapidjson::Value& cfg);
97 
98  void setDaemon(IDaemon* daemon) override;
99  virtual void setSerializer(ISerializer* serializer) override;
100  virtual void setMessaging(IMessaging* messaging) override;
101  const std::string& getName() const override {
102  return m_name;
103  }
104 
105  void update(const rapidjson::Value& cfg) override;
106  void start() override;
107  void stop() override;
108 
109 private:
110  void handleMsgFromMessaging(const ustring& msg);
111  void handleTaskFromScheduler(const std::string& task);
112 
113  // data inside FRC STAT response for each Protocol Bridge
114  struct BridgeStatusData {
115  bool isOn;
116  bool isData;
117  bool isNewVisible;
118  bool isNewInvisible;
119  };
120 
121  // length of manufacturer field of packet header
122  static const int MANUFACTURER_LEN = 2;
123 
124  // length of address field of packet header
125  static const int ADDRESS_LEN = 6;
126 
127  // parsed header of a Protocol Bridge packet
128  struct PacketHeader {
129  uint8_t manufacturer[MANUFACTURER_LEN];
130  uint8_t address[ADDRESS_LEN];
131  };
132 
133  std::map<uint8_t, BridgeStatusData> getActiveBridgesStatusMap();
134  std::list<uint8_t> getNewVisibleMetersIndexes(uint8_t bridgeAddress);
135  std::list<uint8_t> getNewInvisibleMetersIndexes(uint8_t bridgeAddress);
136  std::list<uint8_t> getNewDataMetersIndexes(uint8_t bridgeAddress);
137  ProtocolBridge::FullPacketResponse getFullPacketResponse(uint8_t bridgeAddress, uint8_t meterIndex);
138  void confirmVisibleMeters(uint8_t bridgeAddress, std::list<uint8_t> newVisibleMetersIndexes);
139  void confirmInvisibleMeters(uint8_t bridgeAddress, std::list<uint8_t> newInvisibleMetersIndexes);
140  PacketHeader parseFullPacketResponse(ProtocolBridge::FullPacketResponse fullPacketResponse);
141  void sendDataIntoAzure(PacketHeader packetHeader, uint8_t data[], int dataLen);
142  void sleepProtocolBridge(uint8_t bridgeAddress);
143  void timeoutWMBProtocolBridge(uint8_t bridgeAddress);
144 
145  void getAndProcessDataFromMeters(const std::string& task);
146 
147  std::string m_name;
148 
149  // how often to send frc status cmd in sec
150  uint16_t m_frcPeriod = 30;
151 
152  // sleeping period is this value * HW interval (sec)
153  uint16_t m_sleepPeriod = 10;
154 
155  // timeout for wmbus module, module is on for this time
156  uint16_t m_wmPeriod = 60;
157 
158  IMessaging* m_messaging;
159  IDaemon* m_daemon;
160  ISerializer* m_serializer;
161 
162  std::unordered_map<int, ProtocolBridgeSchd> m_watchedProtocolBridges;
163 };
virtual ~ScheduleDpaTask()
Definition: ProtocolBridgeClientService.h:56
long TaskHandle
Task handle is task identification.
Definition: IScheduler.h:32
const std::string & getName() const override
Get name of the instance.
Definition: ProtocolBridgeClientService.h:101
Definition: ProtocolBridgeClientService.h:38
virtual TaskHandle scheduleTaskPeriodic(const std::string &clientId, const std::string &task, const std::chrono::seconds &sec, const std::chrono::system_clock::time_point &tp=std::chrono::system_clock::now())=0
Schedule periodic task.
void setSync(bool val)
Definition: ProtocolBridgeClientService.h:59
ScheduleDpaTask< ProtocolBridgeJson > ProtocolBridgeSchd
Definition: ProtocolBridgeClientService.h:87
ISerializer interface.
Definition: ISerializer.h:31
static const TaskHandle TASK_HANDLE_INVALID
Invalid task handle.
Definition: IScheduler.h:35
void removeSchedule(const std::string &clientId)
Definition: ProtocolBridgeClientService.h:73
virtual void removeTask(const std::string &clientId, TaskHandle hndl)=0
Remove task for client.
IDaemon interface.
Definition: IDaemon.h:31
IScheduler interface.
Definition: IScheduler.h:28
IService interface.
Definition: IService.h:30
Definition: ProtocolBridgeClientService.h:89
bool isSync() const
Definition: ProtocolBridgeClientService.h:58
IMessaging interface.
Definition: IMessaging.h:29
bool isScheduled()
Definition: ProtocolBridgeClientService.h:68
ScheduleDpaTask(const T &dpt, IScheduler *schd)
Definition: ProtocolBridgeClientService.h:42
std::basic_string< unsigned char > ustring
Definition: ProtocolBridgeClientService.h:33
Definition: ProtocolBridge.h:152
ScheduleDpaTask()=delete
ScheduleDpaTask(const ScheduleDpaTask &other)
Definition: ProtocolBridgeClientService.h:48
void scheduleTaskPeriodic(const std::string &clientId, const std::string &task, const std::chrono::seconds &sec, const std::chrono::system_clock::time_point &tp=std::chrono::system_clock::now())
Definition: ProtocolBridgeClientService.h:61
T & getDpa()
Definition: ProtocolBridgeClientService.h:79