BeeeOn Gateway  v2020.3.1-2-g6f737dc
Platform to interconnect the IoT world
BLESmartDeviceManager.h
1 #pragma once
2 
3 #include <map>
4 #include <vector>
5 
6 #include <Poco/Mutex.h>
7 #include <Poco/Thread.h>
8 #include <Poco/Timespan.h>
9 #include <Poco/UUID.h>
10 
11 #include "bluetooth/BLESmartDevice.h"
12 #include "bluetooth/HciInterface.h"
13 #include "commands/DeviceAcceptCommand.h"
14 #include "commands/DeviceSetValueCommand.h"
15 #include "core/AbstractSeeker.h"
16 #include "core/DongleDeviceManager.h"
17 #include "core/PollingKeeper.h"
18 #include "loop/StopControl.h"
19 #include "model/DeviceID.h"
20 #include "model/RefreshTime.h"
21 #include "net/MACAddress.h"
22 #include "util/AsyncWork.h"
23 
24 namespace BeeeOn {
25 
32 public:
33  static const Poco::UUID CHAR_MODEL_NUMBER;
34 
39  class BLESmartSeeker : public AbstractSeeker {
40  public:
41  typedef Poco::SharedPtr<BLESmartSeeker> Ptr;
42 
44  BLESmartDeviceManager& parent,
45  const Poco::Timespan& duration);
46 
47  protected:
48  void seekLoop(StopControl &control) override;
49 
50  private:
51  BLESmartDeviceManager& m_parent;
52  };
53 
55 
56  void dongleAvailable() override;
57 
62  std::string dongleMatch(const HotplugEvent &e) override;
63 
67  void dongleFailed(const FailDetector &dongleStatus) override;
68 
72  bool dongleMissing() override;
73  void stop() override;
74 
75  void setDevicePoller(DevicePoller::Ptr poller);
76  void setScanTimeout(const Poco::Timespan &timeout);
77  void setDeviceTimeout(const Poco::Timespan &timeout);
78  void setRefresh(const Poco::Timespan &refresh);
79  void setNumberOfExaminationThreads(const int numberOfExaminationThreads);
80  void setHciManager(HciInterfaceManager::Ptr manager);
81 
82 protected:
86  void notifyDongleRemoved() override;
87  void handleAccept(const DeviceAcceptCommand::Ptr cmd) override;
88  AsyncWork<>::Ptr startDiscovery(const Poco::Timespan &timeout) override;
90  const DeviceID &id,
91  const Poco::Timespan &timeout) override;
92  AsyncWork<double>::Ptr startSetValue(
93  const DeviceID &id,
94  const ModuleID &module,
95  const double value,
96  const Poco::Timespan &timeout) override;
97 
101  void eraseAllDevices();
102 
107  void processAsyncData(
108  const MACAddress& address,
109  std::vector<unsigned char> &data);
110 
115  void seekPairedDevices();
116 
126  void seekDevices(
127  std::vector<BLESmartDevice::Ptr>& foundDevices,
128  const StopControl& stop);
129 
136  const std::map<MACAddress, std::string>& devices,
137  Poco::FastMutex& foundDevicesMutex,
138  std::vector<BLESmartDevice::Ptr>& foundDevices,
139  const StopControl& stop);
140 
147  const std::map<MACAddress, std::string>& devices,
148  Poco::FastMutex& foundDevicesMutex,
149  std::vector<BLESmartDevice::Ptr>& foundDevices,
150  const StopControl& stop);
151 
155  BLESmartDevice::Ptr createDevice(const MACAddress& address) const;
156 
157  void processNewDevice(BLESmartDevice::Ptr newDevice);
158 
159 private:
160  Poco::FastMutex m_devicesMutex;
161  std::map<DeviceID, BLESmartDevice::Ptr> m_devices;
162  Poco::SharedPtr<HciInterface::WatchCallback> m_watchCallback;
163 
164  PollingKeeper m_pollingKeeper;
165  Poco::Timespan m_scanTimeout;
166  Poco::Timespan m_deviceTimeout;
167  RefreshTime m_refresh;
168  uint32_t m_numberOfExaminationThreads;
169  HciInterfaceManager::Ptr m_hciManager;
170  HciInterface::Ptr m_hci;
171 };
172 
173 }
void threadedExaminationOfDevices(const std::map< MACAddress, std::string > &devices, Poco::FastMutex &foundDevicesMutex, std::vector< BLESmartDevice::Ptr > &foundDevices, const StopControl &stop)
Splits the map of devices into smaller maps whose number is determined by the attribute m_numberOfExa...
Definition: BLESmartDeviceManager.cpp:352
BLESmartDevice::Ptr createDevice(const MACAddress &address) const
Creates BLE device based on its Model ID.
Definition: BLESmartDeviceManager.cpp:421
AsyncWork< double >::Ptr startSetValue(const DeviceID &id, const ModuleID &module, const double value, const Poco::Timespan &timeout) override
Starts set-value operation in a technology specific way. The method is always called inside a critica...
Definition: BLESmartDeviceManager.cpp:214
Definition: FailDetector.h:8
RefreshTime represents time of refreshing values from sensors. E.g. sensors are polled periodically o...
Definition: RefreshTime.h:24
void eraseAllDevices()
Clears m_devices.
Definition: BLESmartDeviceManager.cpp:158
void processAsyncData(const MACAddress &address, std::vector< unsigned char > &data)
Processes the asynchronous data of the paired device. If the message is correct it is already shipped...
Definition: BLESmartDeviceManager.cpp:239
void dongleAvailable() override
Definition: BLESmartDeviceManager.cpp:104
void notifyDongleRemoved() override
Wakes up the main thread.
Definition: BLESmartDeviceManager.cpp:130
PollingKeeper takes care of devices that are being polled. It cancels all polled devices it manages u...
Definition: PollingKeeper.h:17
Provides searching BLE devices on network in own thread.
Definition: BLESmartDeviceManager.h:39
void seekDevices(std::vector< BLESmartDevice::Ptr > &foundDevices, const StopControl &stop)
Seeks for new devices on Bluetooth LE network.
Definition: BLESmartDeviceManager.cpp:311
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
AbstractSeeker represents an asynchronous process that seeks for new devices in a certain network...
Definition: AbstractSeeker.h:25
Definition: MACAddress.h:8
AsyncWork::Ptr startDiscovery(const Poco::Timespan &timeout) override
Starts device discovery process in a technology-specific way. This method is always called inside a c...
Definition: BLESmartDeviceManager.cpp:165
void handleAccept(const DeviceAcceptCommand::Ptr cmd) override
Generic handler of the DeviceAcceptCommand. It might be helpful to override this method in case we ne...
Definition: BLESmartDeviceManager.cpp:199
StopControl implements the stop mechanism generically.
Definition: StopControl.h:12
Definition: ModuleID.h:12
Poco::Timespan duration() const
Definition: AbstractSeeker.cpp:17
AsyncWork< std::set< DeviceID > >::Ptr startUnpair(const DeviceID &id, const Poco::Timespan &timeout) override
Starts device unpair process in a technology-specific way. This method is always called inside a crit...
Definition: BLESmartDeviceManager.cpp:172
std::string dongleMatch(const HotplugEvent &e) override
Recognizes compatible dongle by testing HotplugEvent property as bluetooth.BEEEON_DONGLE == bluetooth...
Definition: BLESmartDeviceManager.cpp:135
Definition: HotplugEvent.h:10
void stop() override
Definition: BLESmartDeviceManager.cpp:146
Definition: DongleDeviceManager.h:14
The class implements the work with Bluetooth Low Energy devices. Allows us to process and execute the...
Definition: BLESmartDeviceManager.h:31
bool dongleMissing() override
Erases all instances of the device.
Definition: BLESmartDeviceManager.cpp:152
void dongleFailed(const FailDetector &dongleStatus) override
Erases all instances of the device.
Definition: BLESmartDeviceManager.cpp:140
void seekPairedDevices()
Tries to find not found paired devices. Each found device is added to parametr devices and attribute ...
Definition: BLESmartDeviceManager.cpp:264
Definition: DeviceID.h:17
void examineBatchOfDevices(const std::map< MACAddress, std::string > &devices, Poco::FastMutex &foundDevicesMutex, std::vector< BLESmartDevice::Ptr > &foundDevices, const StopControl &stop)
Finds out if the given devices are supported. In the positive case, a specific instance of the device...
Definition: BLESmartDeviceManager.cpp:390