sensorgroup.cpp
Go to the documentation of this file.
1 
8 
9 const std::string SensorGroup::ENCODING_ASCII = "ASCII";
10 const std::string SensorGroup::ENCODING_B64 = "B64";
11 const std::string SensorGroup::ENCODING_HEX = "HEX";
12 
14 
16  std::shared_ptr<Syntax> syntax)
17  : syntax(syntax)
18 {
19  grpNumber = sensorParams.grpNumber;
20  grpName = sensorParams.grpName;
21  channelList = sensorParams.channels;
22  responseEncoding = sensorParams.encoding;
23  callbackRegistered = false;
24  valueErrorCBSet = false;
25  // init channel values map
27  for (std::shared_ptr<Channel> ch : channelList)
28  {
29  channelValues.insertParameter(ch->chName, ch->dataType);
30  }
31  // init command params map
33  // insert grp number
34  cmdInputParams.insertParameter("grp_nr", "uint8_t", grpNumber);
35  // insert channel list
36  std::vector<std::string> channels;
37  // init channel map
38  channelMap = std::unordered_map<std::string, std::shared_ptr<Channel>>();
39  for (std::shared_ptr<Channel> ch : channelList)
40  {
41  channels.push_back(ch->chName);
42  channelMap.insert(std::make_pair(ch->chName, ch));
43  }
44  cmdInputParams.insertParameter("channels", "string_t[]", channels);
45  // init options list and adding option parameters
46  optionsList = std::vector<std::string>();
47  // init value returning options list
48  optionVariableList = std::vector<std::string>();
50  for (auto option : sensorParams.options)
51  {
52 
54  for (auto param : option.first->params)
55  {
56  tempPm.insertParameter(param.first, param.second);
57  }
58  // the first part is later needed for the command string
59  optionsList.push_back(option.first->optName);
60  std::vector<std::string> optionSplit;
61  boost::split(optionSplit, option.first->opt, boost::is_any_of(" ="));
62  std::vector<std::string> splitParamValues;
63  if (option.second.find(',') != std::string::npos)
64  {
65  boost::split(splitParamValues, option.second, boost::is_any_of(","));
66  }
67  else
68  {
69  splitParamValues.push_back(option.second);
70  }
71 
72  int valueCounter = 0;
73  for (std::string optString : optionSplit)
74  {
75  if (optString.at(0) == '$')
76  {
77  std::string paramName = optString.substr(1, std::string::npos);
79  paramName, tempPm.getParameter(paramName)->getType());
80 
81  if (valueCounter >= splitParamValues.size())
82  break; // DANGER! this has to be treated carefully -> not enough
83  // arguments error!
84 
86  paramName, splitParamValues[valueCounter]);
87  valueCounter++;
88  }
89  }
90  // this part is later needed for parsing
91  if (option.first->addsRespToGrps)
92  {
93  std::vector<std::string> split;
94  boost::split(split, option.first->response, boost::is_any_of(" "));
95  for (std::string s : split)
96  {
97  if (s.at(0) == '$')
98  {
99  std::string paramName = s.substr(1, std::string::npos);
100  optionVariableList.push_back(paramName);
101  optionValues.insertParameter(
102  paramName, tempPm.getParameter(paramName)->getType());
103  }
104  }
105  }
106  }
107 }
108 
109 void SensorGroup::processResponse(const std::string& response)
110 {
111  parseResponse(response);
112  if (callbackRegistered)
113  callbackFunction(this);
114 }
115 
116 void SensorGroup::parseResponse(const std::string& response)
117 {
118  if (responseEncoding.compare(ENCODING_ASCII) == 0)
119  {
120  std::vector<std::string> split;
121  boost::split(split, response, boost::is_any_of(" | "));
122  int splitIndex = 0;
123  for (std::string s : split)
124  {
125  s.erase(boost::remove_if(s, boost::is_any_of(" | ")), s.end());
126  if (s.size() <= 0)
127  continue;
128  if (splitIndex >= channelList.size())
129  break;
130 
131  if (syntax->grpErrorsAscii.find(s) != syntax->grpErrorsAscii.end() &&
133  {
134 
135  valueError("Group: " + std::to_string(grpNumber) + " ch.: " +
136  channelList[splitIndex]->chName +
137  " had a faulty value!\n Error Code: " + s);
138  channelValues.setParameterValueAsString(channelList[splitIndex]->chName, "0", false);
139  }
140  else
141  {
143  s);
144  }
145  splitIndex++;
146  }
147  // check for options
148  if (optionVariableList.size() <= 0)
149  return;
150  // parse options with returns
151  for (; splitIndex < optionVariableList.size(); splitIndex++)
152  {
154  split[splitIndex]);
155  }
156  }
157  else if (responseEncoding.compare(ENCODING_B64) == 0)
158  {
159 
160  int byteIndex = 1;
161  for (int i = 0; i < channelList.size(); i++)
162  {
163  std::string chName = channelList[i]->chName;
164  if (!channelValues.isParamInMap(chName))
165  return;
166 
167  int typeSize = channelValues.getParameter(chName)->getTypeByteSize();
168  bool isSigned = channelValues.getParameter(chName)->isTypeSigned();
169  std::string type = channelValues.getParameter(chName)->getType();
170  long tempValue = tempValue = base64_decode(
171  response, byteIndex, byteIndex + typeSize - 1, typeSize, isSigned);
172  // check for value errors;
173  if (syntax->grpErrorsBinary.find(type) != syntax->grpErrorsBinary.end())
174  {
175  // unsigned int errorCode = tempValue;
176 
177  if (syntax->grpErrorsBinary[type].find(tempValue) !=
178  syntax->grpErrorsBinary[type].end())
179  {
180 
181  valueError("Group: " + std::to_string(grpNumber) + " ch.: " + chName +
182  " had a faulty value!\n Error Code: " +
183  std::to_string(tempValue));
184  channelValues.setParameterValueAsString(channelList[i]->chName, "0", false);
185  }
186  }
187  std::string val = std::to_string(tempValue);
189  byteIndex += typeSize;
190  }
191  }
192 }
193 
195 {
196  this->callbackFunction = callbackFunction;
197  callbackRegistered = true;
198 }
199 
201 {
202  this->valueError = valueError;
203  valueErrorCBSet = true;
204 }
205 
206 const std::string& SensorGroup::getName() const { return grpName; }
207 
209  std::string& command) const
210 {
212 }
213 
214 const bool
216  const std::string& response) const
217 {
218  Parameter::ParameterMap outputParams;
219  return cmd.verifyResponse(cmdInputParams, optionsList, response,
220  outputParams);
221 }
222 
223 const bool SensorGroup::getChannelValueConverted(const std::string& name,
224  double& out) const
225 {
226  if (!channelValues.isParamInMap(name))
227  return false;
228  if (!channelValues.getParameter(name)->isValid()){
229  out = std::numeric_limits<double>::quiet_NaN();
230  return true;
231  }
232  const std::string& type = channelValues.getParameter(name)->getType();
233  int size = channelValues.getParameter(name)->getTypeByteSize();
234  if (type.compare("int8_t") == 0)
235  {
236  if (size > 1)
237  throw std::invalid_argument("Parameter typename \"" + type +
238  "\" doesn't match given variable type!");
239  char value;
240  channelValues.getParameterValue(name, value);
241  out = value * channelMap.at(name)->conversionFactor;
242  }
243  else if (type.compare("uint8_t") == 0)
244  {
245  if (size > 1)
246  throw std::invalid_argument("Parameter typename \"" + type +
247  "\" doesn't match given variable type!");
248  unsigned char value;
249  channelValues.getParameterValue(name, value);
250  out = value * channelMap.at(name)->conversionFactor;
251  }
252  else if (type.compare("int16_t") == 0)
253  {
254  if (size > 2)
255  throw std::invalid_argument("Parameter typename \"" + type +
256  "\" doesn't match given variable type!");
257  short value;
258  channelValues.getParameterValue(name, value);
259  out = value * channelMap.at(name)->conversionFactor;
260  }
261  else if (type.compare("uint16_t") == 0)
262  {
263  if (size > 2)
264  throw std::invalid_argument("Parameter typename \"" + type +
265  "\" doesn't match given variable type!");
266  unsigned short value;
267  channelValues.getParameterValue(name, value);
268  out = value * channelMap.at(name)->conversionFactor;
269  }
270  else if (type.compare("int32_t") == 0)
271  {
272  if (size > 4)
273  throw std::invalid_argument("Parameter typename \"" + type +
274  "\" doesn't match given variable type!");
275  int value;
276  channelValues.getParameterValue(name, value);
277  out = value * channelMap.at(name)->conversionFactor;
278  }
279  else if (type.compare("uint32_t") == 0)
280  {
281  if (size > 4)
282  throw std::invalid_argument("Parameter typename \"" + type +
283  "\" doesn't match given variable type!");
284  unsigned int value;
285  channelValues.getParameterValue(name, value);
286  out = value * channelMap.at(name)->conversionFactor;
287  }
288  else if (type.compare("int64_t") == 0)
289  {
290  if (size > 8)
291  throw std::invalid_argument("Parameter typename \"" + type +
292  "\" doesn't match given variable type!");
293  long value;
294  channelValues.getParameterValue(name, value);
295  out = value * channelMap.at(name)->conversionFactor;
296  }
297  else if (type.compare("uint64_t") == 0)
298  {
299  if (size > 8)
300  throw std::invalid_argument("Parameter typename \"" + type +
301  "\" doesn't match given variable type!");
302  unsigned long value;
303  channelValues.getParameterValue(name, value);
304  out = value * channelMap.at(name)->conversionFactor;
305  }
306  else if (type.compare("float32_t") == 0)
307  {
308  if (size > 4)
309  throw std::invalid_argument("Parameter typename \"" + type +
310  "\" doesn't match given variable type!");
311  float value;
312  channelValues.getParameterValue(name, value);
313  out = value * channelMap.at(name)->conversionFactor;
314  }
315  else if (type.compare("float64_t") == 0)
316  {
317  if (size > 8)
318  throw std::invalid_argument("Parameter typename \"" + type +
319  "\" doesn't match given variable type!");
320  double value;
321  channelValues.getParameterValue(name, value);
322  out = value * channelMap.at(name)->conversionFactor;
323  }
324  else
325  return false;
326 }
static const std::string ENCODING_B64
Definition: sensorgroup.h:161
SensorGroup()
SensorGroup default constructor.
Definition: sensorgroup.cpp:13
std::vector< std::pair< std::shared_ptr< CommandOptions >, std::string > > options
Definition: sensorgroup.h:49
const bool verifyResponseOnComand(Command &cmd, const std::string &response) const
Verfy the response on a constructed set sensor group command.
std::string encoding
Definition: sensorgroup.h:51
boost::function< void(SensorGroup *)> responseCallback
Definition: sensorgroup.h:63
std::string grpName
Definition: sensorgroup.h:166
valueErrorCallbackPtr valueError
Definition: sensorgroup.h:178
The Parameter::ParameterMap class provides a map functionality for Parameter::Parameter objects conta...
Definition: parameter.h:163
const bool isParamInMap(const std::string &name) const
Is a parameter with the given name contained within this map?.
Definition: parameter.h:178
void registerErrorCallback(valueErrorCallbackPtr valueError)
Register a value error function pointer to handle value errors during communication.
The SensorGroupParameter class serves as a data struct to configure SensorGroup objects.
Definition: sensorgroup.h:42
std::shared_ptr< Syntax > syntax
Definition: sensorgroup.h:177
Header file for the SensorGroup class.
std::vector< std::shared_ptr< Channel > > channelList
Definition: sensorgroup.h:168
std::string grpName
Definition: sensorgroup.h:44
std::vector< std::string > optionsList
Definition: sensorgroup.h:176
const bool verifyResponse(const Parameter::ParameterMap &inputParams, const std::string &responseOrig, Parameter::ParameterMap &outputParams)
Verifies a given response based on the internal template given all necessary input parameter values...
Definition: command.cpp:134
responseCallback callbackFunction
Definition: sensorgroup.h:173
bool callbackRegistered
Definition: sensorgroup.h:174
const bool getChannelValueConverted(const std::string &name, double &value) const
Get a received converted value from a certain channel.
std::unordered_map< std::string, std::shared_ptr< Channel > > channelMap
Definition: sensorgroup.h:169
void insertParameter(const std::string &name, const std::string &type, const T &value, const bool isValid=true)
Insert a new parameter in this map, with the given name, type and value.
Definition: parameter.h:219
static const std::string ENCODING_ASCII
Definition: sensorgroup.h:160
static const std::string ENCODING_HEX
Definition: sensorgroup.h:162
const std::shared_ptr< Parameter > & getParameter(const std::string &name) const
Definition: parameter.h:259
std::vector< std::shared_ptr< Channel > > channels
Definition: sensorgroup.h:47
Parameter::ParameterMap cmdInputParams
Definition: sensorgroup.h:175
Parameter::ParameterMap optionValues
Definition: sensorgroup.h:172
void setParameterValueAsString(const std::string &name, const std::string &input, const bool isValid=true)
Set the value of a parameter in this map with the given name from a string.
Definition: parameter.h:406
void createSensorGroupCommand(Command &cmd, std::string &command) const
Get the generated sensor group command, that can be used to register this sensor group on the serial ...
const std::string & getName() const
Get the name of this sensor group.
std::vector< std::string > optionVariableList
Definition: sensorgroup.h:171
std::string responseEncoding
Definition: sensorgroup.h:167
Parameter::ParameterMap channelValues
Definition: sensorgroup.h:170
void setResponseCallback(responseCallback callbackFunction)
Register a sensor group callback.
The Command class builds the syntactic template for a specific command defined in a CommandParams str...
Definition: command.h:90
unsigned char grpNumber
Definition: sensorgroup.h:45
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 processResponse(const std::string &response)
Parses an incoming sensor group message, extracts contained values and calls the registered sensor gr...
unsigned char grpNumber
Definition: sensorgroup.h:165
boost::function< void(const std::string &)> valueErrorCallbackPtr
Definition: sensorgroup.h:69
bool valueErrorCBSet
Definition: sensorgroup.h:179
void parseResponse(const std::string &response)
Parse a received sensor channel message and extract channel values, i.e. convert encoded data to "pro...
void generateCommand(const Parameter::ParameterMap &inputParams, std::string &out)
Generates a command string from the internal template given all necessary parameter values...
Definition: command.cpp:88
void getParameterValue(const std::string &name, T &value) const
Get the value of a parameter in this map with the given name.
Definition: parameter.h:235


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