BeeeOn Gateway  v2020.3.1-2-g6f737dc
Platform to interconnect the IoT world
JablotronDeviceManager.h
1 #pragma once
2 
3 #include <list>
4 #include <set>
5 
6 #include <Poco/Mutex.h>
7 
8 #include "commands/DeviceAcceptCommand.h"
9 #include "commands/DeviceSetValueCommand.h"
10 #include "commands/DeviceUnpairCommand.h"
11 #include "commands/GatewayListenCommand.h"
12 #include "core/DeviceManager.h"
13 #include "hotplug/HotplugListener.h"
14 #include "jablotron/JablotronController.h"
15 #include "jablotron/JablotronGadget.h"
16 #include "jablotron/JablotronReport.h"
17 #include "model/ModuleType.h"
18 #include "model/RefreshTime.h"
19 #include "util/BackOff.h"
20 
21 namespace BeeeOn {
22 
50 public:
52 
60  void setUnpairErasesSlot(bool erase);
61 
68  void setTxBackOffFactory(BackOffFactory::Ptr factory);
69 
75  void setPGYEnrollGap(const Poco::Timespan &gap);
76 
81  void setEraseAllOnProbe(bool erase);
82 
87  void setRegisterOnProbe(const std::list<std::string> &addresses);
88 
92  void setMaxProbeAttempts(int count);
93 
97  void setProbeTimeout(const Poco::Timespan &timeout);
98 
102  void setIOJoinTimeout(const Poco::Timespan &timeout);
103 
107  void setIOReadTimeout(const Poco::Timespan &timeout);
108 
112  void setIOErrorSleep(const Poco::Timespan &delay);
113 
114  void onAdd(const HotplugEvent &e) override;
115  void onRemove(const HotplugEvent &e) override;
116 
117  void run() override;
118  void stop() override;
119 
124  void handleRemoteStatus(
125  const DevicePrefix &prefix,
126  const std::set<DeviceID> &devices,
127  const DeviceStatusHandler::DeviceValues &values) override;
128 
129 protected:
137  void handleAccept(const DeviceAcceptCommand::Ptr cmd) override;
138 
143  AsyncWork<>::Ptr startDiscovery(const Poco::Timespan &timeout) override;
144 
151  const Poco::Timespan &timeout,
152  const uint64_t serialNumber) override;
153 
161  const DeviceID &id,
162  const Poco::Timespan &timeout) override;
163 
169  AsyncWork<double>::Ptr startSetValue(
170  const DeviceID &id,
171  const ModuleID &module,
172  const double value,
173  const Poco::Timespan &timeout) override;
174 
179  std::string hotplugMatch(const HotplugEvent &e);
180 
184  void newDevice(
185  const DeviceID &id,
186  const std::string &name,
187  const std::list<ModuleType> &types,
188  const RefreshTime &refreshTime);
189 
193  void acceptGadget(const DeviceID &id);
194 
198  void enrollTX();
199 
203  void registerGadget(
204  std::set<unsigned int> &freeSlots,
205  const uint32_t address,
206  const Poco::Timespan &timeout);
207 
211  void unregisterGadget(const DeviceID &id, const Poco::Timespan &timeout);
212 
216  void shipReport(const JablotronReport &report);
217 
223  std::vector<JablotronGadget> readGadgets(const Poco::Timespan &timeout);
224 
229  void scanSlots(
230  std::set<uint32_t> &registered,
231  std::set<unsigned int> &freeSlots,
232  std::set<unsigned int> &unknownSlots);
233 
238  void initDongle();
239 
246  void syncSlots();
247 
253  static DeviceID buildID(uint32_t address);
254 
258  static uint32_t extractAddress(const DeviceID &id);
259 
260  static DeviceID pgxID();
261  static DeviceID pgyID();
262  static DeviceID sirenID();
263 
267  static void sleep(const Poco::Timespan &delay);
268 
269 private:
270  bool m_unpairErasesSlot;
271  BackOffFactory::Ptr m_txBackOffFactory;
272  Poco::Timespan m_pgyEnrollGap;
273  bool m_eraseAllOnProbe;
274  std::list<uint32_t> m_registerOnProbe;
275  JablotronController m_controller;
276  bool m_pgx;
277  bool m_pgy;
278  bool m_alarm;
279  JablotronController::Beep m_beep;
280  Poco::FastMutex m_lock;
281 };
282 
283 }
Report with data coming from a sensor. Each report comes in format [AAAAAAAA] TYPE PAYLOAD...
Definition: JablotronReport.h:13
RefreshTime represents time of refreshing values from sensors. E.g. sensors are polled periodically o...
Definition: RefreshTime.h:24
void enrollTX()
Enroll PGX, PGY or Siren.
Definition: JablotronDeviceManager.cpp:519
AsyncWork< double >::Ptr startSetValue(const DeviceID &id, const ModuleID &module, const double value, const Poco::Timespan &timeout) override
Set value of PGX, PGY or Siren. It sends the TX packet with ENROLL:0 and the appropriate states...
Definition: JablotronDeviceManager.cpp:711
void handleAccept(const DeviceAcceptCommand::Ptr cmd) override
Confirm pairing of the given device. If it is PGX, PGY or Siren, we send the TX ENROLL:1 packet(s)...
Definition: JablotronDeviceManager.cpp:524
void syncSlots()
Scan slots and synchronize with pairing cache. The devices that are paired in device cache and missin...
Definition: JablotronDeviceManager.cpp:378
void setIOErrorSleep(const Poco::Timespan &delay)
Definition: JablotronDeviceManager.cpp:155
void shipReport(const JablotronReport &report)
Report the asynchronous message via DeviceManager::ship().
Definition: JablotronDeviceManager.cpp:446
void setPGYEnrollGap(const Poco::Timespan &gap)
Configure gap between two TX ENROLL packets while pairing the PGY. At least 3 seconds gap was experim...
Definition: JablotronDeviceManager.cpp:110
Definition: HotplugListener.h:9
JablotronDeviceManager utilizes the JablotronController to communicate with a Turris Dongle to receiv...
Definition: JablotronDeviceManager.h:49
void setRegisterOnProbe(const std::list< std::string > &addresses)
Register the given list of address when the dongle is connected and probed successfully (after the po...
Definition: JablotronDeviceManager.cpp:123
void handleRemoteStatus(const DevicePrefix &prefix, const std::set< DeviceID > &devices, const DeviceStatusHandler::DeviceValues &values) override
Receive list of paired devices and reflect this in the connected dongle if any.
Definition: JablotronDeviceManager.cpp:266
AsyncWork is an interface allowing to wait until an asynchronous operation finishes. It is also possible to force-stop it by calling cancel().
Definition: AsyncWork.h:21
static DeviceID buildID(uint32_t address)
Definition: JablotronDeviceManager.cpp:160
static uint32_t extractAddress(const DeviceID &id)
Definition: JablotronDeviceManager.cpp:167
void setEraseAllOnProbe(bool erase)
Erase all slots registered in the dongle when connected and probed successfully.
Definition: JablotronDeviceManager.cpp:118
void setMaxProbeAttempts(int count)
Definition: JablotronDeviceManager.cpp:135
Definition: ModuleID.h:12
AsyncWork::Ptr startSearch(const Poco::Timespan &timeout, const uint64_t serialNumber) override
Search whether the gadget of the given serialNumber is registered. If it is not and the serialNumber ...
Definition: JablotronDeviceManager.cpp:605
void unregisterGadget(const DeviceID &id, const Poco::Timespan &timeout)
Unregister gadget from tis slot.
Definition: JablotronDeviceManager.cpp:649
void setIOJoinTimeout(const Poco::Timespan &timeout)
Definition: JablotronDeviceManager.cpp:145
void newDevice(const DeviceID &id, const std::string &name, const std::list< ModuleType > &types, const RefreshTime &refreshTime)
Dispatch information about the new device.
Definition: JablotronDeviceManager.cpp:543
Definition: DeviceManager.h:48
Definition: HotplugEvent.h:10
std::string hotplugMatch(const HotplugEvent &e)
Recognizes compatible dongle by testing HotplugEvent property as tty.BEEEON_DONGLE == jablotron...
Definition: JablotronDeviceManager.cpp:210
AsyncWork::Ptr startDiscovery(const Poco::Timespan &timeout) override
Discover all slots and report gadgets that are not paired. Also, report devices PGX, PGY and Siren.
Definition: JablotronDeviceManager.cpp:558
void setUnpairErasesSlot(bool erase)
The BeeeOn unpair operation marks a device as unpaired. It can also unregister the gadget from Turris...
Definition: JablotronDeviceManager.cpp:100
static void sleep(const Poco::Timespan &delay)
Sleep for at least the given delay and not less.
Definition: JablotronDeviceManager.cpp:172
void setIOReadTimeout(const Poco::Timespan &timeout)
Definition: JablotronDeviceManager.cpp:150
JablotronController provides access to the Turris Dongle that is connected via a serial port...
Definition: JablotronController.h:27
void initDongle()
Initialize the newly connected dongle. Apply the eraseAllOnProbe and registerOnProbe settings...
Definition: JablotronDeviceManager.cpp:343
void setProbeTimeout(const Poco::Timespan &timeout)
Definition: JablotronDeviceManager.cpp:140
std::vector< JablotronGadget > readGadgets(const Poco::Timespan &timeout)
Read all slots from the controller and return registered gadgets. Certain gadgets might be unresolved...
Definition: JablotronDeviceManager.cpp:277
void acceptGadget(const DeviceID &id)
Accept gadget device to be paired.
Definition: JablotronDeviceManager.cpp:491
void stop() override
Definition: JablotronDeviceManager.cpp:259
void registerGadget(std::set< unsigned int > &freeSlots, const uint32_t address, const Poco::Timespan &timeout)
Unregister gadget from tis slot.
Definition: JablotronDeviceManager.cpp:470
Definition: DeviceID.h:17
void scanSlots(std::set< uint32_t > &registered, std::set< unsigned int > &freeSlots, std::set< unsigned int > &unknownSlots)
Scan all slots and detect all registered gadgets, free (empty) slots and slots having unknown (unsupp...
Definition: JablotronDeviceManager.cpp:312
void setTxBackOffFactory(BackOffFactory::Ptr factory)
Set BackOffFactory to be used while sending TX packets to set PGX, PGY or Siren. The backoff controls...
Definition: JablotronDeviceManager.cpp:105
AsyncWork< std::set< DeviceID > >::Ptr startUnpair(const DeviceID &id, const Poco::Timespan &timeout) override
Unpair the given device. If it is PGX, PGY or Siren, then only the device cache is updated...
Definition: JablotronDeviceManager.cpp:686
DevicePrefix prefix() const override
Definition: DeviceManager.cpp:33