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