sensorgroupthread.cpp
Go to the documentation of this file.
1 
8 
10  std::shared_ptr<Syntax> syntax, ThreadDispatcher* dispatcher,
11  const std::unordered_map<unsigned char, std::shared_ptr<SensorGroup>>&
12  sensorGroups)
13  : dispatcher(dispatcher), syntax(syntax), sensorGroups(sensorGroups)
14 {
15  errorCBregistered = false;
16 }
17 
19 {
20  active = true;
21  worker = std::thread(&SensorGroupThread::workerFunction, this);
22 }
23 
25 {
26  active = false;
27  wakeUp();
28  worker.join();
29 }
30 
32 {
33  this->error = error;
34  errorCBregistered = true;
35 }
36 
38 {
39  while (active)
40  {
41  sleep();
43  {
44  std::string msg, original;
46  original = msg;
47  if (msg.size() < 1)
48  continue;
49  // temporary fix for detecting encodings other than ascii
50  if (std::count(msg.begin(), msg.end(), '#') == 1)
51  {
52  msg.erase(boost::remove_if(
53  msg, boost::is_any_of(syntax->channelGrpMsgPrefix)),
54  msg.end());
55  if (msg.size() < 3)
56  continue;
57  int grpNum = base64_decode(msg, 0, 0, 8, false);
58  // check if requested sensor group exists
59  if (sensorGroups.find(grpNum) == sensorGroups.end())
60  {
62  error("Received sensor group id unknown: " +
63  std::to_string(grpNum));
64  continue;
65  }
66  boost::trim(msg);
67  if (msg.size() < 1)
68  continue;
69  // call the corresponding group parser and send
70  try
71  {
72  sensorGroups[grpNum]->processResponse(msg);
73  }
74  catch (std::exception& e)
75  {
77  error("An exception occured while processing a group message.\n "
78  "Description: " +
79  std::string(e.what()) + "\n Message was: " + original);
80  }
81  }
82  else
83  {
84  msg.erase(boost::remove_if(
85  msg, boost::is_any_of(syntax->channelGrpMsgPrefix)),
86  msg.end());
87  // temporary fix
88  int indx = msg.find(syntax->answerOnCmdPrefix);
89  if (indx == std::string::npos)
90  continue;
91  msg.at(indx) = ' ';
92  // fix end
93  boost::trim_left(msg);
94  if (msg.size() < 1)
95  continue;
96  std::vector<std::string> split;
97  boost::split(split, msg, boost::is_any_of(" "));
98  if (split.size() < 2)
99  continue;
100  if (split[0].size() < 1)
101  continue;
102  unsigned char grpNum = 0;
103  try
104  {
105  grpNum = std::stoul(split[0]);
106  }
107  catch (std::exception& e)
108  {
109  if (errorCBregistered)
110  error("Received sensor group id unreadable: " + split[0]);
111  continue;
112  }
113  int startIdx = split[0].size();
114  // check if requested sensor group exists
115  if (sensorGroups.find(grpNum) == sensorGroups.end())
116  {
117  if (errorCBregistered)
118  error("Received sensor group id unknown: " +
119  std::to_string(grpNum));
120  continue;
121  }
122  msg = msg.substr(startIdx, std::string::npos);
123  boost::trim(msg);
124  if (msg.size() < 1)
125  continue;
126  // call the corresponding group parser and send
127  try
128  {
129  sensorGroups[grpNum]->processResponse(msg);
130  }
131  catch (std::exception& e)
132  {
133  if (errorCBregistered)
134  error("An exception occured while processing a group message.\n "
135  "Description: " +
136  std::string(e.what()) + "\n Message was: " + original);
137  }
138  }
139  }
140  }
141 }
SensorGroupThread(std::shared_ptr< Syntax > syntax, ThreadDispatcher *dispatcher, const std::unordered_map< unsigned char, std::shared_ptr< SensorGroup >> &sensorGroups)
SensorGroupThread constructor.
Header file for the SensorGroupThread class.
The ThreadDispatcher class coordinates other communication threads and handles start up and shut down...
boost::function< void(const std::string &)> errorCallbackPtr
Definition: readingthread.h:24
void sleep()
Locks the current worker thread until wakeUp() is called.
void workerFunction()
Worker function, responsible for parsing sensor messages and calling the correct sensor group process...
ThreadDispatcher * dispatcher
std::shared_ptr< Syntax > syntax
void stopThread()
Stop parsing incoming sensor group messages.
void registerErrorCallback(errorCallbackPtr error)
Register an error callback that can be called to treat occuing communication and parsing errors...
const bool IsMessageQueueEmpty() const
Is there a message in the message queue?
void wakeUp()
Unlocks the current worker thread.
std::unordered_map< unsigned char, std::shared_ptr< SensorGroup > > sensorGroups
errorCallbackPtr error
long base64_decode(const std::string &in, const unsigned int startByteIndex, const unsigned int endByteIndex, const unsigned int size, bool isSigned)
Function decodes a Base64 string and returns the decoded string (or only parts of it) up to a maximum...
void startThread()
Start parsing incoming sensor group messages.
void dequeueSensorGroupMessage(std::string &response)
Pop a received sensor group message from the message queue.


pses_ucbridge
Author(s): Sebastian Ehmes
autogenerated on Sat Oct 28 2017 19:16:13