BeeeOn Gateway  v2020.3.1-2-g6f737dc
Platform to interconnect the IoT world
DevicePoller.h
1 #pragma once
2 
3 #include <Poco/Clock.h>
4 #include <Poco/Mutex.h>
5 
6 #include "core/DeviceCache.h"
7 #include "core/Distributor.h"
8 #include "core/PollableDevice.h"
9 #include "loop/StoppableRunnable.h"
10 #include "loop/StopControl.h"
11 #include "util/AsyncExecutor.h"
12 #include "util/Loggable.h"
13 
14 namespace BeeeOn {
15 
23 public:
24  typedef Poco::SharedPtr<DevicePoller> Ptr;
25 
26  DevicePoller();
27 
28  void setDistributor(Distributor::Ptr distributor);
29  void setPollExecutor(AsyncExecutor::Ptr executor);
30 
39  void setWarnThreshold(const Poco::Timespan &threshold);
40 
46  void schedule(
47  PollableDevice::Ptr device,
48  const Poco::Clock &now = {});
49 
55  void cancel(const DeviceID &id);
56 
60  void run() override;
61 
65  void stop() override;
66 
70  void cleanup();
71 
72 protected:
79  static Poco::Timespan grabRefresh(const PollableDevice::Ptr device);
80 
85  void reschedule(
86  PollableDevice::Ptr device,
87  const Poco::Clock &now = {});
88 
94  void doSchedule(
95  PollableDevice::Ptr device,
96  const Poco::Clock &now = {});
97 
103  Poco::Timespan pollNextIfOnSchedule(const Poco::Clock &now = {});
104 
110  void doPoll(PollableDevice::Ptr device);
111 
112 private:
113  Distributor::Ptr m_distributor;
114  AsyncExecutor::Ptr m_pollExecutor;
115  Poco::Timespan m_warnThreshold;
116 
117  typedef std::multimap<Poco::Clock, PollableDevice::Ptr> Schedule;
118  Schedule m_schedule;
119  std::map<DeviceID, Schedule::const_iterator> m_devices;
120  std::set<DeviceID> m_active;
121  Poco::FastMutex m_lock;
122 
123  StopControl m_stopControl;
124 };
125 
126 }
void doPoll(PollableDevice::Ptr device)
Invoke the PollableDevice::poll() method via the configured m_pollExecutor. Thus, the poll() is usual...
Definition: DevicePoller.cpp:205
Poco::Timespan pollNextIfOnSchedule(const Poco::Clock &now={})
Check the next device to be polled. If the next device is scheduled into the future, return the time difference. Otherwise return 0 as the device is currently polled.
Definition: DevicePoller.cpp:187
void stop() override
Stop polling of devices.
Definition: DevicePoller.cpp:239
void schedule(PollableDevice::Ptr device, const Poco::Clock &now={})
Schedule the given device relatively to the given time reference (usually meaning now)...
Definition: DevicePoller.cpp:52
void cancel(const DeviceID &id)
Cancel the device of the given ID if exists. If the device is currently being polled, it would not be interrupted but its will not be rescheduled.
Definition: DevicePoller.cpp:91
StopControl implements the stop mechanism generically.
Definition: StopControl.h:12
void cleanup()
Remove all scheduled devices (kind of reset).
Definition: DevicePoller.cpp:244
DevicePoller is a scheduler for PollableDevice instances. Any number of devices can be scheduled for ...
Definition: DevicePoller.h:22
Definition: Loggable.h:19
void setWarnThreshold(const Poco::Timespan &threshold)
Configure time threshold that would enable to fire a warning about a too slow device. Polling of device should take longer (or much longer) than its refresh time.
Definition: DevicePoller.cpp:34
void reschedule(PollableDevice::Ptr device, const Poco::Clock &now={})
Reschedule device after its PollableDevice::poll() method has been called. Only active devices are re...
Definition: DevicePoller.cpp:112
void doSchedule(PollableDevice::Ptr device, const Poco::Clock &now={})
Do the actual scheduling - registration into m_devices, and m_schedule containers. If the devices is already scheduled, its previous record is just updated. Not thread-safe.
Definition: DevicePoller.cpp:73
Definition: StoppableRunnable.h:8
Definition: DeviceID.h:17
static Poco::Timespan grabRefresh(const PollableDevice::Ptr device)
Get refresh time of the device and check whether it is usable for regular polling. If it is not, throw an exception.
Definition: DevicePoller.cpp:39
void run() override
Poll devices according to the schedule.
Definition: DevicePoller.cpp:138