BeeeOn Gateway  v2020.3.1-2-g6f737dc
Platform to interconnect the IoT world
OZWNetwork.h
1 #pragma once
2 
3 #include <list>
4 #include <map>
5 #include <set>
6 #include <string>
7 
8 #include <Poco/AtomicCounter.h>
9 #include <Poco/Mutex.h>
10 #include <Poco/Path.h>
11 #include <Poco/Timespan.h>
12 
13 #include "hotplug/HotplugListener.h"
14 #include "loop/StoppableLoop.h"
15 #include "util/EventSource.h"
16 #include "util/PeriodicRunner.h"
17 #include "zwave/AbstractZWaveNetwork.h"
18 #include "zwave/OZWCommand.h"
19 #include "zwave/ZWaveListener.h"
20 #include "zwave/ZWaveNode.h"
21 
22 namespace OpenZWave {
23 
24 class Notification;
25 class ValueID;
26 
27 }
28 
29 namespace BeeeOn {
30 
54 class OZWNetwork :
55  public HotplugListener,
56  public AbstractZWaveNetwork {
57  friend class OZWCommand;
58 public:
59  OZWNetwork();
60  ~OZWNetwork();
61 
68  void setConfigPath(const std::string &path);
69 
76  void setUserPath(const std::string &path);
77 
81  void setPollInterval(const Poco::Timespan &interval);
82 
86  void setIntervalBetweenPolls(bool enable);
87 
91  void setRetryTimeout(const Poco::Timespan &timeout);
92 
96  void setAssumeAwake(bool awake);
97 
101  void setDriverMaxAttempts(int attempts);
102 
107  void setNetworkKey(const std::list<std::string> &bytes);
108 
112  void setStatisticsInterval(const Poco::Timespan &interval);
113 
118  void setControllersToReset(const std::list<std::string> &homes);
119 
124  void setExecutor(AsyncExecutor::Ptr executor);
125 
129  void registerListener(ZWaveListener::Ptr listener);
130 
135  void onNotification(const OpenZWave::Notification *n);
136 
141  void onAdd(const HotplugEvent &event) override;
142 
147  void onRemove(const HotplugEvent &event) override;
148 
154  void configure();
155 
159  void cleanup();
160 
161 protected:
166  class OZWNode : public ZWaveNode {
167  public:
168  OZWNode(const Identity &id, bool controller = false);
169 
175  void add(const CommandClass &cc, const OpenZWave::ValueID &id);
176 
181  OpenZWave::ValueID operator[](const CommandClass &cc) const;
182 
183  private:
184  std::map<CommandClass, OpenZWave::ValueID> m_valueIDs;
185  };
186 
191  void checkDirectory(const Poco::Path &path);
192 
197  void prepareDirectory(const Poco::Path &path);
198 
207  void fireStatistics();
208 
213  static bool matchEvent(const HotplugEvent &event);
214 
220  static void ozwNotification(
221  OpenZWave::Notification const *n,
222  void *context);
223 
229  bool ignoreNotification(const OpenZWave::Notification *n) const;
230 
239  void resetController(const uint32_t home);
240 
247  void driverReady(const OpenZWave::Notification *n);
248 
253  void driverFailed(const OpenZWave::Notification *n);
254 
261  void driverRemoved(const OpenZWave::Notification *n);
262 
267  bool checkNodeIsController(const uint32_t home, const uint8_t node) const;
268 
272  void nodeNew(const OpenZWave::Notification *n);
273 
281  void nodeAdded(const OpenZWave::Notification *n);
282 
296  void nodeNaming(const OpenZWave::Notification *n);
297 
306  void nodeProtocolInfo(const OpenZWave::Notification *n);
307 
323  void nodeReady(const OpenZWave::Notification *n);
324 
337  void nodeRemoved(const OpenZWave::Notification *n);
338 
348  void valueAdded(const OpenZWave::Notification *n);
349 
360  void valueChanged(const OpenZWave::Notification *n);
361 
377  void nodeQueried(const OpenZWave::Notification *n);
378 
395  void awakeNodesQueried(const OpenZWave::Notification *n);
396 
412  void allNodesQueried(const OpenZWave::Notification *n);
413 
417  static std::string homeAsString(uint32_t home);
418 
424  const OpenZWave::ValueID &id);
425 
430  void startInclusion() override;
431 
436  void cancelInclusion() override;
437 
442  void startRemoveNode() override;
443 
448  void cancelRemoveNode() override;
449 
454  void interrupt() override;
455 
467  void postValue(const ZWaveNode::Value &value) override;
468 
472  std::string valueForList(const OpenZWave::ValueID &valueID, const int value);
473 
474 private:
475  Poco::Path m_configPath;
476  Poco::Path m_userPath;
477  Poco::Timespan m_pollInterval;
478  bool m_intervalBetweenPolls;
479  Poco::Timespan m_retryTimeout;
480  bool m_assumeAwake;
481  unsigned int m_driverMaxAttempts;
482  std::set<uint32_t> m_controllersToReset;
483  std::vector<uint8_t> m_networkKey;
484 
488  std::map<uint32_t, std::map<uint8_t, OZWNode>> m_homes;
489 
495  Poco::AtomicCounter m_configured;
496 
500  mutable Poco::FastMutex m_managerLock;
501 
505  mutable Poco::FastMutex m_lock;
506 
511  OZWCommand m_command;
512  EventSource<ZWaveListener> m_eventSource;
513  AsyncExecutor::Ptr m_executor;
514  PeriodicRunner m_statisticsRunner;
515 };
516 
517 }
OZWCommand handles OpenZWave command management. It allows to request a command to be executed and ta...
Definition: OZWCommand.h:25
void fireStatistics()
Fire Z-Wave statistics. This is called periodically by the m_statisticsRunner.
Definition: OZWNetwork.cpp:992
std::string valueForList(const OpenZWave::ValueID &valueID, const int value)
Definition: OZWNetwork.cpp:1119
Command class representation of a Z-Wave value. We support only a subset of command classes that are ...
Definition: ZWaveNode.h:48
OZWNetwork manages the Z-Wave network by using the OpenZWave library (OZW). Its purpose is to handle ...
Definition: OZWNetwork.h:54
Definition: HotplugListener.h:9
void awakeNodesQueried(const OpenZWave::Notification *n)
Called when OZW believes that all awaken node have been queried fully. Here we analyze some stats abo...
Definition: OZWNetwork.cpp:793
void setExecutor(AsyncExecutor::Ptr executor)
Set asynchronous executor used for asynchronous tasks and events reporting.
Definition: OZWNetwork.cpp:185
void cleanup()
Deinitialize OZW library. Stop the statistics reporter.
Definition: OZWNetwork.cpp:282
bool checkNodeIsController(const uint32_t home, const uint8_t node) const
Find out whether the given node ID represents a controller of the given home.
Definition: OZWNetwork.cpp:554
Executes some function periodacally based on the given interval.
Definition: PeriodicRunner.h:18
void setNetworkKey(const std::list< std::string > &bytes)
Set OZW NetworkKey option. The key is expected to be 16 bytes long.
Definition: OZWNetwork.cpp:144
Value coming from the Z-Wave network. It holds some data (usually sensor data) and metadata to identi...
Definition: ZWaveNode.h:102
void setRetryTimeout(const Poco::Timespan &timeout)
Set OZW RetryTimeout option.
Definition: OZWNetwork.cpp:123
void driverFailed(const OpenZWave::Notification *n)
Called when OZW driver fails to become ready. The associated home ID is uninstalled.
Definition: OZWNetwork.cpp:531
EventSource implements common logic for firing events to listeners.
Definition: EventSource.h:24
void startRemoveNode() override
Start the removal mode on the primary controller(s).
Definition: OZWNetwork.cpp:946
void postValue(const ZWaveNode::Value &value) override
Post the given value into the Z-Wave network. The call is non-blocking and there is no direct feedbac...
Definition: OZWNetwork.cpp:1051
void resetController(const uint32_t home)
Initiate asynchronous reset of controller associated with the given home ID.
Definition: OZWNetwork.cpp:488
void startInclusion() override
Start the inclusion mode on the primary controller(s).
Definition: OZWNetwork.cpp:914
OZWNode wraps the ZWaveNode to be able to hold specific data related to the OpenZWave library...
Definition: OZWNetwork.h:166
void setStatisticsInterval(const Poco::Timespan &interval)
Set the interval of reporting OZW statistics.
Definition: OZWNetwork.cpp:164
bool ignoreNotification(const OpenZWave::Notification *n) const
Certain notifications coming from OZW are to be ignored because they are uninteresting or known to sc...
Definition: OZWNetwork.cpp:365
void driverRemoved(const OpenZWave::Notification *n)
Called when OZW driver is removed from the system.
Definition: OZWNetwork.cpp:542
void cancelInclusion() override
Cancel the inclusion mode if active.
Definition: OZWNetwork.cpp:927
void registerListener(ZWaveListener::Ptr listener)
Register a ZWaveListener that would be receiving events.
Definition: OZWNetwork.cpp:180
void valueAdded(const OpenZWave::Notification *n)
Called when OZW discoveres a new value associated with a certain Z-Wave node.
Definition: OZWNetwork.cpp:720
void checkDirectory(const Poco::Path &path)
Check that the given directory exists and is readable.
Definition: OZWNetwork.cpp:191
void setAssumeAwake(bool awake)
Set OZW AssumeAwake option.
Definition: OZWNetwork.cpp:131
void setUserPath(const std::string &path)
Set OZW userPath (cache of device definitions). This directory would be created if it does not exist...
Definition: OZWNetwork.cpp:102
void nodeReady(const OpenZWave::Notification *n)
Called when OZW believes that the given Z-Wave node is ready for standard operations.
Definition: OZWNetwork.cpp:679
bool controller() const
Definition: ZWaveNode.cpp:283
void allNodesQueried(const OpenZWave::Notification *n)
Called when OZW believes that all nodes have been queried fully. This call is similar to awakeNodesQu...
Definition: OZWNetwork.cpp:852
ZWaveNode represents information from the Z-Wave network about a particular node. Each Z-Wave node is...
Definition: ZWaveNode.h:22
static void ozwNotification(OpenZWave::Notification const *n, void *context)
Definition: OZWNetwork.cpp:353
void setDriverMaxAttempts(int attempts)
Set OZW DriverMaxAttempts option.
Definition: OZWNetwork.cpp:136
void nodeRemoved(const OpenZWave::Notification *n)
Called when OZW assumes that a Z-Wave node was removed from the network and thus is unreachable...
Definition: OZWNetwork.cpp:697
void setPollInterval(const Poco::Timespan &interval)
Set OZW PollInterval option.
Definition: OZWNetwork.cpp:108
void cancelRemoveNode() override
Cancel the removal mode if active.
Definition: OZWNetwork.cpp:959
void onNotification(const OpenZWave::Notification *n)
Handle incoming OZW notifications in the context of the OZWNetwork instance.
Definition: OZWNetwork.cpp:390
Definition: HotplugEvent.h:10
void nodeQueried(const OpenZWave::Notification *n)
Called when OZW finishes discovering of a Z-Wave node. At this moment, we are sure to know all dynami...
Definition: OZWNetwork.cpp:775
void add(const CommandClass &cc, const OpenZWave::ValueID &id)
Register the command class together with ValueID representation as provided by the OpenZWave library...
Definition: OZWNetwork.cpp:58
void onAdd(const HotplugEvent &event) override
If the event represents a compatible Z-Wave dongle, an appropriate driver is added into the OZW runti...
Definition: OZWNetwork.cpp:314
void driverReady(const OpenZWave::Notification *n)
Called when the OZW driver becomes ready to work for the given home ID. The OZWNetwork installs the h...
Definition: OZWNetwork.cpp:500
void configure()
Initialize OZW library, set options and register self as a watcher for handling notifications. The statistics reporter is started.
Definition: OZWNetwork.cpp:221
void valueChanged(const OpenZWave::Notification *n)
Called when OZW received data associated with a Z-Wave node&#39;s value. Such data are processed by the u...
Definition: OZWNetwork.cpp:742
Abstract implementation of the ZWaveNetwork class. It provides a pre-implemented polling mechanism...
Definition: AbstractZWaveNetwork.h:19
void interrupt() override
Cancel the current OZW command and interrupt an active pollEvent() call.
Definition: OZWNetwork.cpp:978
void prepareDirectory(const Poco::Path &path)
Create the directory represented by the given path. If it already exists, it must be writable and rea...
Definition: OZWNetwork.cpp:204
void nodeAdded(const OpenZWave::Notification *n)
Called when the OZW added a Z-Wave node to its list.
Definition: OZWNetwork.cpp:572
void nodeNew(const OpenZWave::Notification *n)
Called when the OZW discovered a new Z-Wave node.
Definition: OZWNetwork.cpp:560
void setConfigPath(const std::string &path)
Set OZW configPath (contains definitions, XML files, etc.). The directory should exist prior to calli...
Definition: OZWNetwork.cpp:96
void nodeNaming(const OpenZWave::Notification *n)
Called when OZW resolves more details about a Z-Wave node.
Definition: OZWNetwork.cpp:593
void nodeProtocolInfo(const OpenZWave::Notification *n)
Called when OZW resolves properties of a Z-Wave node.
Definition: OZWNetwork.cpp:637
void setIntervalBetweenPolls(bool enable)
Set OZW IntervalBetweenPolls option.
Definition: OZWNetwork.cpp:118
void onRemove(const HotplugEvent &event) override
If the event represents a compatible Z-Wave dongle, the appropriate driver is removed from the OZW ru...
Definition: OZWNetwork.cpp:342
void setControllersToReset(const std::list< std::string > &homes)
Set controllers (list of home IDs) to be reset upon their first appearance in the network...
Definition: OZWNetwork.cpp:174
static bool matchEvent(const HotplugEvent &event)
Determine hotplugged devices compatible with the OZWNetwork. The property tty.BEEEON_DONGLE is tested...
Definition: OZWNetwork.cpp:306
OpenZWave::ValueID operator[](const CommandClass &cc) const
Return the appropriate ValueID for the given command class.
Definition: OZWNetwork.cpp:64
static std::string homeAsString(uint32_t home)
Helper method to print the home ID.
Definition: OZWNetwork.cpp:890
static ZWaveNode::CommandClass buildCommandClass(const OpenZWave::ValueID &id)
Helper method to build a CommandClass instance directly from the OZW ValueID instance.
Definition: OZWNetwork.cpp:895
Identity of a Z-Wave node that can be used separately without any instance of the ZWaveNode class...
Definition: ZWaveNode.h:28