BeeeOn Gateway  v2020.3.1-2-g6f737dc
Platform to interconnect the IoT world
JablotronController.h
1 #pragma once
2 
3 #include <queue>
4 #include <string>
5 
6 #include <Poco/Event.h>
7 #include <Poco/Mutex.h>
8 #include <Poco/Nullable.h>
9 #include <Poco/RunnableAdapter.h>
10 #include <Poco/SharedPtr.h>
11 #include <Poco/Thread.h>
12 #include <Poco/Timespan.h>
13 
14 #include "io/SerialPort.h"
15 #include "jablotron/JablotronReport.h"
16 #include "loop/StopControl.h"
17 #include "util/Joiner.h"
18 #include "util/Loggable.h"
19 
20 namespace BeeeOn {
21 
28 public:
29  enum Beep {
30  BEEP_NONE,
31  BEEP_SLOW,
32  BEEP_FAST,
33  };
34 
36 
41  void setMaxProbeAttempts(int count);
42 
47  void setProbeTimeout(const Poco::Timespan &timeout);
48 
52  void setIOJoinTimeout(const Poco::Timespan &timeout);
53 
59  void setIOReadTimeout(const Poco::Timespan &timeout);
60 
66  void setIOErrorSleep(const Poco::Timespan &delay);
67 
77  void probe(const std::string &dev);
78 
84  void release(const std::string &dev);
85 
90  void dispose();
91 
97  JablotronReport pollReport(const Poco::Timespan &timeout);
98 
104  Poco::Nullable<uint32_t> readSlot(
105  unsigned int i,
106  const Poco::Timespan &timeout);
107 
113  void registerSlot(
114  unsigned int i,
115  uint32_t address,
116  const Poco::Timespan &timeout);
117 
123  void unregisterSlot(
124  unsigned int i,
125  const Poco::Timespan &timeout);
126 
132  void eraseSlots(const Poco::Timespan &timeout);
133 
141  void sendTX(
142  bool x,
143  bool y,
144  bool alarm,
145  Beep beep,
146  const Poco::Timespan &timeout);
147 
153  void sendEnroll(const Poco::Timespan &timeout);
154 
155 protected:
162  void handleOkError(const std::string &response) const;
163 
168  std::string command(const std::string &request,
169  const Poco::Timespan &timeout);
170 
175  void startIO();
176 
183  void stopIO(const std::string &dev);
184 
190  void probePort(const std::string &dev);
191 
196  bool receivedVersion(const std::string &response);
197 
205  std::string popResponse();
206 
212 
219  void readAndProcess();
220 
229  void processMessage(const std::string &message);
230 
238  void ioLoop();
239 
240  void writePort(const std::string &request);
241  std::string readPort(const Poco::Timespan &timeout);
242 
243 private:
244  SerialPort m_port;
245  std::queue<std::string> m_responses;
246  Poco::Event m_requestEvent;
247  std::queue<JablotronReport> m_reports;
248  Poco::Event m_pollEvent;
249 
250  size_t m_maxProbeAttempts;
251  Poco::Timespan m_probeTimeout;
252  Poco::Timespan m_ioJoinTimeout;
253  Poco::Timespan m_ioReadTimeout;
254  Poco::Timespan m_ioErrorSleep;
255 
256  Poco::SharedPtr<Poco::Thread> m_ioThread;
257  Poco::SharedPtr<Joiner> m_joiner;
258  Poco::RunnableAdapter<JablotronController> m_ioLoop;
259  StopControl m_stopControl;
260 
261  Poco::FastMutex m_lock;
262  Poco::FastMutex m_requestLock;
263 };
264 
265 }
void release(const std::string &dev)
Release the serial port and stop the I/O thread if running. If the argument dev does not match the cu...
Definition: JablotronController.cpp:133
Report with data coming from a sensor. Each report comes in format [AAAAAAAA] TYPE PAYLOAD...
Definition: JablotronReport.h:13
void setIOReadTimeout(const Poco::Timespan &timeout)
Configure timeout to use while reading from the serial port inside the I/O loop. The value can be neg...
Definition: JablotronController.cpp:99
void unregisterSlot(unsigned int i, const Poco::Timespan &timeout)
Unregister the given slot.
Definition: JablotronController.cpp:233
void handleOkError(const std::string &response) const
Check whether the response is &quot;OK&quot; or &quot;ERROR&quot;.
Definition: JablotronController.cpp:260
std::string command(const std::string &request, const Poco::Timespan &timeout)
Issue a command and return its result unless the timeout exceeds.
Definition: JablotronController.cpp:271
JablotronReport pollReport(const Poco::Timespan &timeout)
Poll for reports coming from Jablotron sensors. If the timeout exceeds, an invalid report is returned...
Definition: JablotronController.cpp:327
bool receivedVersion(const std::string &response)
Parse the response to be the version string.
Definition: JablotronController.cpp:502
void sendEnroll(const Poco::Timespan &timeout)
Send enroll packet with PGX, PGY and ALARM as 0 and no beeping.
Definition: JablotronController.cpp:255
void stopIO(const std::string &dev)
Stop the I/O thread is any and if the argument dev matches the currently used serial port...
Definition: JablotronController.cpp:169
void readAndProcess()
Read the serial port and process all received messages in order.
Definition: JablotronController.cpp:409
void startIO()
Start the I/O thread without any checks and clear any thread-related status.
Definition: JablotronController.cpp:153
void setIOErrorSleep(const Poco::Timespan &delay)
Set time interval to sleep for when there is an I/O error. It reduces the number of logged error mess...
Definition: JablotronController.cpp:107
std::string popResponse()
Pop the most recent received response. All older responses are dropped because only 1 command can be ...
Definition: JablotronController.cpp:313
void eraseSlots(const Poco::Timespan &timeout)
Unregister all slots at once.
Definition: JablotronController.cpp:240
StopControl implements the stop mechanism generically.
Definition: StopControl.h:12
void processMessage(const std::string &message)
Process the given message. The call recognizes 2 kinds of messages: reports from sensors and other (u...
Definition: JablotronController.cpp:370
void setIOJoinTimeout(const Poco::Timespan &timeout)
Configure join timeout when waiting for the I/O thread to finish.
Definition: JablotronController.cpp:91
Definition: Loggable.h:19
Definition: SerialPort.h:9
Poco::Nullable< uint32_t > readSlot(unsigned int i, const Poco::Timespan &timeout)
Read address of the given slot.
Definition: JablotronController.cpp:188
void sendTX(bool x, bool y, bool alarm, Beep beep, const Poco::Timespan &timeout)
Send status packet with PGX and PGY set to either 1 or 0 accodring to the given values. Alarm is disabled and beeping set to FAST.
Definition: JablotronController.cpp:245
void registerSlot(unsigned int i, uint32_t address, const Poco::Timespan &timeout)
Register the given slot with the given address.
Definition: JablotronController.cpp:225
JablotronController provides access to the Turris Dongle that is connected via a serial port...
Definition: JablotronController.h:27
JablotronReport popReport()
Pop the oldest report from the associated queue. If the queue is empty, it returns an invalid report...
Definition: JablotronController.cpp:350
void probe(const std::string &dev)
Probe the given serial port (e.g. &quot;/dev/ttyUSB0&quot;) and if it proves to be a Jablotron control station...
Definition: JablotronController.cpp:115
void ioLoop()
The entry into the I/O thread loop. It periodically calls readAndProcess() and checks whether a stop ...
Definition: JablotronController.cpp:434
void setMaxProbeAttempts(int count)
Configure number of attempts to initiate communication with the serial port connected to a Turris Don...
Definition: JablotronController.cpp:75
void probePort(const std::string &dev)
Probe the given port and test whether it is an appropriate Turris Dongle. Any obsolete messages in th...
Definition: JablotronController.cpp:463
void setProbeTimeout(const Poco::Timespan &timeout)
Configure timeout to wait for response from Turris Dongle while probing it.
Definition: JablotronController.cpp:83
void dispose()
Release the serial port and stop the I/O thread if running.
Definition: JablotronController.cpp:143