BeeeOn Gateway  v2020.3.1-2-g6f737dc
Platform to interconnect the IoT world
ZWaveDeviceManager.h
1 #pragma once
2 
3 #include <map>
4 #include <set>
5 #include <string>
6 
7 #include <Poco/Clock.h>
8 #include <Poco/Mutex.h>
9 #include <Poco/Timespan.h>
10 
11 #include "core/DeviceManager.h"
12 #include "model/RefreshTime.h"
13 #include "model/SensorValue.h"
14 #include "util/DelayedAsyncWork.h"
15 #include "zwave/ZWaveMapperRegistry.h"
16 #include "zwave/ZWaveNetwork.h"
17 #include "zwave/ZWaveNode.h"
18 
19 namespace BeeeOn {
20 
45 public:
51  class Device {
52  public:
53  Device(const ZWaveNode &node);
54 
55  DeviceID id() const;
56 
57  std::string product() const;
58  std::string vendor() const;
59 
68  void updateNode(const ZWaveNode &node);
69  const ZWaveNode &node() const;
70 
76  bool resolveMapper(ZWaveMapperRegistry::Ptr registry);
77 
78  ZWaveMapperRegistry::Mapper::Ptr mapper() const;
79 
80  void setRefresh(const RefreshTime &refresh);
81  RefreshTime refresh() const;
82 
86  std::list<ModuleType> types() const;
87 
91  SensorValue convert(const ZWaveNode::Value &value) const;
92 
93  std::string toString() const;
94 
95  private:
96  ZWaveNode m_node;
97  ZWaveMapperRegistry::Mapper::Ptr m_mapper;
98  RefreshTime m_refresh;
99  };
100 
101  typedef std::map<DeviceID, Device> DeviceMap;
102  typedef std::map<ZWaveNode::Identity, DeviceMap::iterator> ZWaveNodeMap;
103 
105 
109  void setNetwork(ZWaveNetwork::Ptr network);
110 
115  void setRegistry(ZWaveMapperRegistry::Ptr registry);
116 
125  void setDispatchDuration(const Poco::Timespan &duration);
126 
131  void setPollTimeout(const Poco::Timespan &timeout);
132 
138  void run() override;
139 
143  void stop() override;
144 
145 protected:
146  void handleAccept(const DeviceAcceptCommand::Ptr cmd) override;
147  AsyncWork<>::Ptr startDiscovery(const Poco::Timespan &duration) override;
149  const DeviceID &id,
150  const Poco::Timespan &timeout) override;
151  AsyncWork<double>::Ptr startSetValue(
152  const DeviceID &id,
153  const ModuleID &module,
154  const double value,
155  const Poco::Timespan &timeout) override;
156 
161  void dispatchUnpaired();
162 
175  void dispatchDevice(const Device &device, bool enabled = true);
176 
188  std::set<DeviceID> recentlyUnpaired();
189 
198  void processValue(const ZWaveNode::Value &value);
199 
205  void newNode(const ZWaveNode &node, bool dispatch);
206  void newNodeUnlocked(const ZWaveNode &node, bool dispatch);
207 
215  void updateNode(const ZWaveNode &node, bool dispatch);
216 
221  void removeNode(const ZWaveNode &node);
222 
228  void registerDevice(const Device &device);
229 
237  Device unregisterDevice(ZWaveNodeMap::iterator it);
238 
242  void stopInclusion();
243 
247  void stopRemoveNode();
248 
249 private:
250  ZWaveNetwork::Ptr m_network;
251  ZWaveMapperRegistry::Ptr m_registry;
252  Poco::Timespan m_dispatchDuration;
253  Poco::Timespan m_pollTimeout;
254 
258  DeviceMap m_devices;
259 
263  ZWaveNodeMap m_zwaveNodes;
264 
271  std::set<DeviceID> m_recentlyUnpaired;
272 
276  Poco::FastMutex m_lock;
277 
282  DelayedAsyncWork<>::Ptr m_inclusionWork;
283 
288  DelayedAsyncWork<std::set<DeviceID>>::Ptr m_removeNodeWork;
289 };
290 
291 }
void updateNode(const ZWaveNode &node)
Update the underlying ZWaveNode instance by the given one. Their identities must match.
Definition: ZWaveDeviceManager.cpp:60
RefreshTime represents time of refreshing values from sensors. E.g. sensors are polled periodically o...
Definition: RefreshTime.h:24
bool resolveMapper(ZWaveMapperRegistry::Ptr registry)
Try to resolve Mapper for this Device based on the ZWaveNode contents unless a mapper is already reso...
Definition: ZWaveDeviceManager.cpp:75
void setNetwork(ZWaveNetwork::Ptr network)
Configure ZWaveNetwork to be used by this manager.
Definition: ZWaveDeviceManager.cpp:128
void setRegistry(ZWaveMapperRegistry::Ptr registry)
Set registry that are able to resolve the Z-Wave devices specific features.
Definition: ZWaveDeviceManager.cpp:133
void dispatchUnpaired()
Dispatch all registered devices that are not paired to the remote server.
Definition: ZWaveDeviceManager.cpp:435
Implementation of the AsyncWork interface that executes a given function once after the the given del...
Definition: DelayedAsyncWork.h:24
void dispatchDevice(const Device &device, bool enabled=true)
Dispatch the given device to the remote server.
Definition: ZWaveDeviceManager.cpp:450
void setDispatchDuration(const Poco::Timespan &duration)
Set maximal time window in which new devices are reported since the start of the inclusion mode...
Definition: ZWaveDeviceManager.cpp:138
Value coming from the Z-Wave network. It holds some data (usually sensor data) and metadata to identi...
Definition: ZWaveNode.h:102
Definition: SensorValue.h:14
void newNode(const ZWaveNode &node, bool dispatch)
If the given node is fully resolvable (we can determine its Mapper instance), it is registered as a n...
Definition: ZWaveDeviceManager.cpp:276
std::list< ModuleType > types() const
Definition: ZWaveDeviceManager.cpp:99
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: ZWaveDeviceManager.cpp:403
void dispatch(Poco::AutoPtr< Command > cmd, Poco::AutoPtr< Answer > answer)
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
SensorValue convert(const ZWaveNode::Value &value) const
Definition: ZWaveDeviceManager.cpp:105
void setPollTimeout(const Poco::Timespan &timeout)
Set poll timeout of the main loop polling the configured ZWaveNetwork instance.
Definition: ZWaveDeviceManager.cpp:146
Definition: ModuleID.h:12
void registerDevice(const Device &device)
Register a device to be available for the BeeeOn system. The device must have a resolved mapper...
Definition: ZWaveDeviceManager.cpp:371
ZWaveNode represents information from the Z-Wave network about a particular node. Each Z-Wave node is...
Definition: ZWaveNode.h:22
std::set< DeviceID > recentlyUnpaired()
Access cached device IDs of the Z-Wave nodes removed by the recent unpair process. The call clears that cache.
Definition: ZWaveDeviceManager.cpp:489
Definition: DeviceManager.h:48
void run() override
Run the loop that receives events from the configured ZWaveNetwork instance. The loop receives inform...
Definition: ZWaveDeviceManager.cpp:154
AsyncWork::Ptr startDiscovery(const Poco::Timespan &duration) override
Starts device discovery process in a technology-specific way. This method is always called inside a c...
Definition: ZWaveDeviceManager.cpp:422
void processValue(const ZWaveNode::Value &value)
Process a Z-Wave value received from the Z-Wave network. If there is a paired device for that value a...
Definition: ZWaveDeviceManager.cpp:221
ZWaveDeviceManager implements the logical layer on top of the ZWaveNetwork interface. It adapts the Z-Wave specifics to the BeeeOn system with the help of ZWaveMapperRegistry::Mapper.
Definition: ZWaveDeviceManager.h:44
void updateNode(const ZWaveNode &node, bool dispatch)
If the given node is already registered, update information about it and if it is not paired...
Definition: ZWaveDeviceManager.cpp:321
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: ZWaveDeviceManager.cpp:552
void stopInclusion()
Helper method to stop the Z-Wave inclusion mode.
Definition: ZWaveDeviceManager.cpp:522
Device unregisterDevice(ZWaveNodeMap::iterator it)
Unregister the device pointed to by the iterator from being available to the BeeeOn system...
Definition: ZWaveDeviceManager.cpp:385
High-level representation of a Z-Wave device. It enriches the ZWaveNode for information needed by the...
Definition: ZWaveDeviceManager.h:51
void stop() override
Stop the polling loop.
Definition: ZWaveDeviceManager.cpp:213
Definition: DeviceID.h:17
void removeNode(const ZWaveNode &node)
The given node is considered to be unpaired, its cached data is deleted.
Definition: ZWaveDeviceManager.cpp:345
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: ZWaveDeviceManager.cpp:499
void stopRemoveNode()
Helper method to stop the Z-Wave node removal mode.
Definition: ZWaveDeviceManager.cpp:537