BeeeOn Gateway  v2020.3.1-2-g6f737dc
Platform to interconnect the IoT world
FHEMClient.h
1 #pragma once
2 
3 #include <map>
4 #include <queue>
5 #include <string>
6 #include <vector>
7 
8 #include <Poco/Event.h>
9 #include <Poco/Mutex.h>
10 #include <Poco/SharedPtr.h>
11 #include <Poco/Timespan.h>
12 #include <Poco/Timestamp.h>
13 #include <Poco/Net/DialogSocket.h>
14 #include <Poco/Net/SocketAddress.h>
15 
16 #include "conrad/FHEMDeviceInfo.h"
17 #include "loop/StopControl.h"
18 #include "loop/StoppableRunnable.h"
19 #include "util/Loggable.h"
20 
21 namespace BeeeOn {
22 
29 public:
30  typedef Poco::SharedPtr<FHEMClient> Ptr;
31 
32  FHEMClient();
33 
34  void setRefreshTime(const Poco::Timespan &time);
35  void setReceiveTimeout(const Poco::Timespan &timeout);
36  void setReconnectTime(const Poco::Timespan &time);
37  void setFHEMAddress(const std::string &address);
38 
44  void run() override;
45  void stop() override;
46 
50  void sendRequest(const std::string& request);
51 
65  Poco::JSON::Object::Ptr receive(const Poco::Timespan &timeout);
66 
67 protected:
72  void initConnection();
73 
79  void cycle();
80 
85  Poco::JSON::Object::Ptr nextEvent();
86 
90  void retrieveHomeMaticDevices(std::vector<std::string>& devices);
91 
99  void processDevice(const std::string& device);
100 
106  const std::string& device,
107  const Poco::JSON::Object::Ptr internals) const;
108 
113  Poco::JSON::Object::Ptr internals,
114  std::map<std::string, std::string>& channels);
115 
119  std::string retrieveChannelState(const std::string& channel);
120 
121  void createNewDeviceEvent(
122  const std::string& device,
123  const std::string& model,
124  const std::string& type,
125  const std::string& serialNumber);
126 
127  void createStatEvent(
128  const std::string& event,
129  const std::string& device);
130 
131  void createMessageEvent(
132  const std::string& device,
133  const std::string& model,
134  const std::string& type,
135  const std::string& serialNumber,
136  const std::string& rawMsg,
137  const double rssi,
138  const std::map<std::string, std::string>& channels);
139 
140  void appendEventToQueue(const Poco::JSON::Object::Ptr event);
141 
148  Poco::JSON::Object::Ptr sendCommand(const std::string& command);
149 
150 private:
151  StopControl m_stopControl;
152  Poco::Timespan m_refreshTime;
153  Poco::Timespan m_receiveTimeout;
154  Poco::Timespan m_reconnectTime;
155  Poco::Net::SocketAddress m_fhemAddress;
156  Poco::Net::DialogSocket m_telnetSocket;
157  std::map<std::string, FHEMDeviceInfo> m_deviceInfos;
158  std::queue<Poco::JSON::Object::Ptr> m_eventsQueue;
159  Poco::FastMutex m_queueMutex;
160  Poco::Event m_receiveEvent;
161 };
162 
163 }
The class stores a statistic information about device maintained by FHEM server. According to these i...
Definition: FHEMDeviceInfo.h:14
void retrieveHomeMaticDevices(std::vector< std::string > &devices)
Retrieves all HomeMatic devices known to FHEM server.
Definition: FHEMClient.cpp:189
Poco::JSON::Object::Ptr nextEvent()
Returns message from queue. If the queue is empty it returns null.
Definition: FHEMClient.cpp:175
void sendRequest(const std::string &request)
Sends a request over a telnet connection to FHEM server.
Definition: FHEMClient.cpp:99
void processDevice(const std::string &device)
Retrieves information about given device and detects changes connected to this device. If some change is detected then an event is created and is appended to the queue.
Definition: FHEMClient.cpp:211
Poco::JSON::Object::Ptr sendCommand(const std::string &command)
Sends a command over telnet connection and returns a response.
Definition: FHEMClient.cpp:399
void run() override
It starts with creation of telnet connection with FHEM server and then it periodicly ask FHEM server ...
Definition: FHEMClient.cpp:68
void cycle()
Retrieves all HomeMatic devices known to FHEM server and processes each device. Processing of device ...
Definition: FHEMClient.cpp:158
StopControl implements the stop mechanism generically.
Definition: StopControl.h:12
Poco::JSON::Object::Ptr receive(const Poco::Timespan &timeout)
Waiting for a new event according to given timeout. Returns event, if some event is in queue...
Definition: FHEMClient.cpp:107
void initConnection()
Creates DialogSocket and connect it to a defined socket address m_fhemAddress.
Definition: FHEMClient.cpp:138
FHEMDeviceInfo assembleDeviceInfo(const std::string &device, const Poco::JSON::Object::Ptr internals) const
Creates DeviceInfo for a given device and part of message.
Definition: FHEMClient.cpp:277
std::string retrieveChannelState(const std::string &channel)
Retrieves a state of a given channel.
Definition: FHEMClient.cpp:326
The class communicates with FHEM server. It allows to search HomeMatic devices, gather data from Home...
Definition: FHEMClient.h:28
Definition: Loggable.h:19
void retrieveChannelsState(Poco::JSON::Object::Ptr internals, std::map< std::string, std::string > &channels)
For a given device it retrieves all channels and their states.
Definition: FHEMClient.cpp:305
Definition: StoppableRunnable.h:8
void stop() override
Definition: FHEMClient.cpp:93