BeeeOn Gateway  v2020.3.1-2-g6f737dc
Platform to interconnect the IoT world
DeviceStatusFetcher.h
1 #pragma once
2 
3 #include <map>
4 #include <set>
5 
6 #include <Poco/Clock.h>
7 #include <Poco/SharedPtr.h>
8 #include <Poco/Timespan.h>
9 
10 #include "core/CommandSender.h"
11 #include "core/DeviceStatusHandler.h"
12 #include "loop/StoppableRunnable.h"
13 #include "loop/StopControl.h"
14 #include "util/Loggable.h"
15 
16 namespace BeeeOn {
17 
25  public CommandSender,
26  public StoppableRunnable,
27  Loggable {
28 public:
29  typedef Poco::SharedPtr<DeviceStatusFetcher> Ptr;
30 
32 
36  void setIdleDuration(const Poco::Timespan &duration);
37 
41  void setWaitTimeout(const Poco::Timespan &timeout);
42 
47  void setRepeatTimeout(const Poco::Timespan &timeout);
48 
54  void registerHandler(DeviceStatusHandler::Ptr handler);
55 
59  void clearHandlers();
60 
61  void run() override;
62  void stop() override;
63 
64 protected:
69  class PrefixAnswer : public Answer {
70  public:
71  typedef Poco::AutoPtr<PrefixAnswer> Ptr;
72 
74  AnswerQueue &queue,
75  const DevicePrefix &prefix);
76 
77  DevicePrefix prefix() const;
78 
79  private:
80  const DevicePrefix m_prefix;
81  };
82 
83  class PrefixStatus {
84  public:
85  PrefixStatus();
86 
91  void startRequest();
92 
99  void deliverResponse(bool successful);
100 
105  bool needsRequest() const;
106 
117  bool shouldRepeat(const Poco::Timespan &repeatTimeout) const;
118 
119  private:
120  Poco::Clock m_lastRequested;
121  bool m_started;
122  bool m_successful;
123  };
124 
128  enum FetchStatus {
129  NOTHING,
130  WOULD_REPEAT,
131  ACTIVE
132  };
133 
134 protected:
142 
149  PrefixAnswer::Ptr handleDirtyAnswer(Answer::Ptr answer);
150 
154  std::set<DeviceStatusHandler::Ptr> matchHandlers(
155  const PrefixAnswer::Ptr answer) const;
156 
166  void processAnswer(
167  PrefixAnswer::Ptr answer,
168  std::set<DeviceStatusHandler::Ptr> handler);
169 
175  void collectPaired(
176  std::set<DeviceID> &paired,
177  const std::vector<DeviceID> &received,
178  const DevicePrefix &prefix) const;
179 
180 private:
181  StopControl m_stopControl;
182  Poco::Timespan m_idleDuration;
183  Poco::Timespan m_waitTimeout;
184  Poco::Timespan m_repeatTimeout;
185  std::map<DevicePrefix, std::set<DeviceStatusHandler::Ptr>> m_handlers;
186  std::map<DevicePrefix, PrefixStatus> m_status;
187 };
188 
189 }
std::set< DeviceStatusHandler::Ptr > matchHandlers(const PrefixAnswer::Ptr answer) const
Definition: DeviceStatusFetcher.cpp:191
Definition: Answer.h:30
Definition: AnswerQueue.h:22
To simplify Answer management, include the prefix of the connected ServerDeviceListCommand inside the...
Definition: DeviceStatusFetcher.h:69
void collectPaired(std::set< DeviceID > &paired, const std::vector< DeviceID > &received, const DevicePrefix &prefix) const
Process paired devices as given in a single Answer result. Only devices matching the given prefix wou...
Definition: DeviceStatusFetcher.cpp:338
FetchStatus
Denote certain situations of the status fetching.
Definition: DeviceStatusFetcher.h:128
void setIdleDuration(const Poco::Timespan &duration)
Set duration how long to sleep while there is nothing to do.
Definition: DeviceStatusFetcher.cpp:82
void registerHandler(DeviceStatusHandler::Ptr handler)
Register the given device status handler. The DeviceStatusFetcher would request a remote pairing regi...
Definition: DeviceStatusFetcher.cpp:106
bool needsRequest() const
Definition: DeviceStatusFetcher.cpp:58
void setRepeatTimeout(const Poco::Timespan &timeout)
Set timeout to wait until a request is re-issued after its unsuccessful finish (Answer was not fully ...
Definition: DeviceStatusFetcher.cpp:98
void startRequest()
Update the status of an associated prefix that the request for the associated prefix has been initiat...
Definition: DeviceStatusFetcher.cpp:44
void deliverResponse(bool successful)
Update the status of an associated prefix that an appropriate response has been delivered. The parameter denotes whether the response has been fully successful or not. This influences whether the request would be repeated.
Definition: DeviceStatusFetcher.cpp:51
DeviceStatusFetcher is responsible for fetching pairing state of devices for the registered status ha...
Definition: DeviceStatusFetcher.h:24
StopControl implements the stop mechanism generically.
Definition: StopControl.h:12
void clearHandlers()
Unregister all registered device status handlers.
Definition: DeviceStatusFetcher.cpp:115
Definition: CommandSender.h:20
Definition: Loggable.h:19
void stop() override
Definition: DeviceStatusFetcher.cpp:363
bool shouldRepeat(const Poco::Timespan &repeatTimeout) const
When the response was not fully successful, the request should be repeated. However, we do not want to DoS the remote server. Thus, the repeated requested would be performed after the given repeat timeout. The method returns true only, if the last response was not fully successful and the timeout exceeded sice the last request.
Definition: DeviceStatusFetcher.cpp:63
FetchStatus fetchUndone()
Determine status handlers for which no fully successful request was made and dispatch ServerDeviceLis...
Definition: DeviceStatusFetcher.cpp:120
Definition: StoppableRunnable.h:8
void processAnswer(PrefixAnswer::Ptr answer, std::set< DeviceStatusHandler::Ptr > handler)
Process results of the given answer and the matching status handler. When any of the results is unsuc...
Definition: DeviceStatusFetcher.cpp:275
Definition: DeviceStatusFetcher.h:83
PrefixAnswer::Ptr handleDirtyAnswer(Answer::Ptr answer)
Check status of the given answer and if it is not pending and castable to PrefixAnswer, it performs cast and returns it.
Definition: DeviceStatusFetcher.cpp:161
void setWaitTimeout(const Poco::Timespan &timeout)
Set timeout for AnswerQueue::wait() call.
Definition: DeviceStatusFetcher.cpp:90