IQRF Gateway Daemon
ClientServicePm.h
Go to the documentation of this file.
1 
17 #pragma once
18 
19 #include "JsonUtils.h"
20 #include "PrfPulseMeter.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 <memory>
30 
31 class IDaemon;
32 
33 typedef std::basic_string<unsigned char> ustring;
34 
35 template <typename T>
36 class ScheduleDpaTask
37 {
38 public:
39  ScheduleDpaTask() = delete;
40  ScheduleDpaTask(const T& dpt, IScheduler* schd)
41  :m_scheduler(schd)
42  , m_dpa(dpt)
43  , m_sync(false)
44  {}
45 
47  : m_taskHandle(other.m_taskHandle)
48  , m_scheduler(other.m_scheduler)
49  , m_dpa(other.m_dpa)
50  , m_sync((bool)other.m_sync)
51  {
52  }
53 
54  virtual ~ScheduleDpaTask() {};
55 
56  bool isSync() const { return m_sync; }
57  void setSync(bool val) { m_sync = val; }
58 
59  void scheduleTaskPeriodic(const std::string& clientId, const std::string& task, const std::chrono::seconds& sec,
60  const std::chrono::system_clock::time_point& tp = std::chrono::system_clock::now())
61  {
62  removeSchedule(clientId);
63  m_taskHandle = m_scheduler->scheduleTaskPeriodic(clientId, task, sec, tp);
64  }
65 
66  bool isScheduled()
67  {
68  return m_taskHandle != IScheduler::TASK_HANDLE_INVALID;
69  }
70 
71  void removeSchedule(const std::string& clientId)
72  {
73  m_scheduler->removeTask(clientId, m_taskHandle);
74  m_taskHandle = IScheduler::TASK_HANDLE_INVALID;
75  }
76 
77  T& getDpa() { return m_dpa; }
78 private:
79  std::atomic_bool m_sync{false};
81  IScheduler* m_scheduler;
82  T m_dpa;
83 };
84 
86 
87 class ClientServicePm : public IService
88 {
89 public:
90  ClientServicePm() = delete;
91  ClientServicePm(const std::string& name);
92  virtual ~ClientServicePm();
93 
94  void updateConfiguration(const rapidjson::Value& cfg);
95 
96  void setDaemon(IDaemon* daemon) override;
97  virtual void setSerializer(ISerializer* serializer) override;
98  virtual void setMessaging(IMessaging* messaging) override;
99  const std::string& getName() const override {
100  return m_name;
101  }
102 
103  void update(const rapidjson::Value& cfg) override;
104  void start() override;
105  void stop() override;
106 
107 private:
108  void handleMsgFromMessaging(const ustring& msg);
109  void handleTaskFromScheduler(const std::string& task);
110 
111  void processFrcFromScheduler(const std::string& task);
112  void processPulseMeterFromTaskQueue(PrfPulseMeterSchd& pm);
113  void processPulseMeterFromScheduler(const std::string& task);
114 
115  bool getFrc() { return m_frcActive; }
116  void setFrc(bool val);
117 
118  std::string m_name;
119 
120  uint16_t m_frcPeriod = 5;
121  uint16_t m_sleepPeriod = 60;
122 
123  IMessaging* m_messaging;
124  IDaemon* m_daemon;
125  ISerializer* m_serializer;
126 
127  std::vector<PrfPulseMeterSchd> m_watchedPm;
128  IScheduler::TaskHandle m_frcHandle;
129  bool m_frcActive = false;
130 
131  std::unique_ptr<TaskQueue<PrfPulseMeterSchd*>> m_taskQueue;
132 
133  rapidjson::Value m_dpaRequestJsonPattern;
134 };
virtual ~ScheduleDpaTask()
Definition: ClientServicePm.h:54
long TaskHandle
Task handle is task identification.
Definition: IScheduler.h:32
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: ClientServicePm.h:57
std::basic_string< unsigned char > ustring
Definition: ClientServicePm.h:31
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
const std::string & getName() const override
Get name of the instance.
Definition: ClientServicePm.h:99
IScheduler interface.
Definition: IScheduler.h:28
IService interface.
Definition: IService.h:30
bool isSync() const
Definition: ClientServicePm.h:56
Definition: ClientServicePm.h:87
IMessaging interface.
Definition: IMessaging.h:29
bool isScheduled()
Definition: ClientServicePm.h:66
ScheduleDpaTask(const T &dpt, IScheduler *schd)
Definition: ClientServicePm.h:40
std::basic_string< unsigned char > ustring
Definition: ProtocolBridgeClientService.h:33
ScheduleDpaTask()=delete
ScheduleDpaTask(const ScheduleDpaTask &other)
Definition: ClientServicePm.h:46
ScheduleDpaTask< PrfPulseMeterJson > PrfPulseMeterSchd
Definition: ClientServicePm.h:85
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: ClientServicePm.h:59
T & getDpa()
Definition: ClientServicePm.h:77