communicationconfig.cpp
Go to the documentation of this file.
1 
8 
10 
12  : configPath(configPath)
13 {
16  readChannels();
17  readOptions();
18  readCommands();
20 }
21 
22 // Syntax-struct assign operator
23 void operator>>(const YAML::Node& node, Syntax& syntax)
24 {
25  syntax.endOfMessage = node["end_of_message"].as<std::string>();
26  syntax.endOfFrame = node["end_of_frame"].as<std::string>();
27  syntax.textMsgPrefix = node["text_msg_prefix"].as<std::string>();
28  syntax.answerOnCmdPrefix = node["answer_on_command_prefix"].as<std::string>();
29  syntax.channelGrpMsgPrefix =
30  node["channel_group_msg_prefix"].as<std::string>();
31  syntax.cmdErrorPrefix = node["cmd_error_prefix"].as<std::string>();
32  syntax.genErrorPrefix = node["gen_error_prefix"].as<std::string>();
33  syntax.optionsPrefix = node["options_prefix"].as<std::string>();
34  const YAML::Node& errorAsciiNode = node["grp_errors_ascii"];
35  if (errorAsciiNode.IsSequence() && errorAsciiNode.size() > 0)
36  {
37  for (auto item : errorAsciiNode)
38  {
39  syntax.grpErrorsAscii.insert(item.as<std::string>());
40  }
41  }
42  const YAML::Node& errorBinaryNode = node["grp_errors_binary"];
43  if (errorBinaryNode.IsSequence() && errorBinaryNode.size() > 0)
44  {
45  for (auto item : errorBinaryNode)
46  {
47  int c = 0;
48  if (item.IsSequence() && item.size() > 0)
49  {
50  std::string type;
51  std::unordered_set<unsigned int> errorSet;
52  for (auto item2 : item)
53  {
54  if (c != 0)
55  {
56  errorSet.insert(item2.as<unsigned int>());
57  c++;
58  }
59  else
60  {
61  type = item2.as<std::string>();
62  c++;
63  }
64  }
65  syntax.grpErrorsBinary.insert(std::make_pair(type, errorSet));
66  }
67  }
68  }
69 }
70 
71 // SerialInferfaceConfig-struct assign operator
72 void operator>>(const YAML::Node& node, SerialInterfaceParams& serialParams)
73 {
74  serialParams.baudRate = node["baudrate"].as<unsigned int>();
75  serialParams.deviceTag = node["device_tag"].as<std::string>();
76  serialParams.serialTimeout = node["serial_timeout"].as<unsigned int>();
77  serialParams.maxLineLength = node["max_line_length"].as<unsigned int>();
78  serialParams.serialDevicesFolder =
79  node["serial_devices_folder"].as<std::string>();
80 }
81 
82 // Channel-struct assign operator
83 void
84 operator>>(const YAML::Node& node,
85  std::unordered_map<std::string, std::shared_ptr<Channel>>& channels)
86 {
87  Channel ch;
88  ch.chName = node["ch_name"].as<std::string>();
89  ch.dataType = node["datatype"].as<std::string>();
90  ch.conversionNeeded = node["conversion_needed"].as<bool>();
91  ch.conversionFactor = 1.0;
92  if (node["conversion_factor"].IsScalar() &&
93  !node["conversion_factor"].IsNull())
94  {
95  ch.conversionFactor = node["conversion_factor"].as<double>();
96  }
97  channels.insert(std::make_pair(ch.chName, std::make_shared<Channel>(ch)));
98 }
99 
100 // CommandOptions-struct assign operator
102  const YAML::Node& node,
103  std::unordered_map<std::string, std::shared_ptr<CommandOptions>>& options)
104 {
105  CommandOptions cmdOpt;
106  cmdOpt.optName = node["name"].as<std::string>();
107  cmdOpt.optHasParams = node["opt_has_params"].as<bool>();
108  cmdOpt.optReturnsParams = node["opt_returns_params"].as<bool>();
109  cmdOpt.addsRespToGrps = node["adds_response_to_groups"].as<bool>();
110  if (node["opt"].IsScalar() && !node["opt"].IsNull())
111  {
112  cmdOpt.opt = node["opt"].as<std::string>();
113  }
114  if (node["response"].IsScalar() && !node["response"].IsNull())
115  {
116  cmdOpt.response = node["response"].as<std::string>();
117  }
118  const YAML::Node& paramsNode = node["params"];
119  if (paramsNode.IsSequence() && paramsNode.size() > 0)
120  {
121  for (auto item : paramsNode)
122  {
123  std::string param = item.as<std::string>();
124  std::vector<std::string> split;
125  boost::split(split, param, boost::is_any_of(":"));
126  std::string type = split[0];
127  std::string name = split[1];
128  cmdOpt.params.push_back(std::make_pair(name, type));
129  }
130  }
131  options.insert(
132  std::make_pair(cmdOpt.optName, std::make_shared<CommandOptions>(cmdOpt)));
133 }
134 
135 // CommandObject insertion/creation method
136 void CommunicationConfig::insertCommand(const YAML::Node& node)
137 {
138  CommandParams cmd;
139  cmd.name = node["cmd_name"].as<std::string>();
140  cmd.cmdHasParams = node["cmd_has_params"].as<bool>();
141  cmd.cmdHasResponse = node["cmd_has_response"].as<bool>();
142  cmd.respHasParams = node["response_contains_params"].as<bool>();
143  if (node["command"].IsScalar() && !node["command"].IsNull())
144  {
145  cmd.cmd = node["command"].as<std::string>();
146  }
147  if (node["response"].IsScalar() && !node["response"].IsNull())
148  {
149  cmd.response = node["response"].as<std::string>();
150  }
151  const YAML::Node& paramsNode = node["params"];
152  if (paramsNode.IsSequence() && paramsNode.size() > 0)
153  {
154  for (auto item : paramsNode)
155  {
156  std::string param = item.as<std::string>();
157  std::vector<std::string> split;
158  boost::split(split, param, boost::is_any_of(":"));
159  std::string type = split[0];
160  std::string name = split[1];
161  cmd.params.push_back(std::make_pair(name, type));
162  }
163  }
164  commands.insert(std::make_pair(
165  cmd.name,
166  std::make_shared<Command>(Command(cmd, getSyntax(), options))));
167 }
168 
169 // SensorGroupObject insertion/creation method
170 void CommunicationConfig::insertSensorGroup(const YAML::Node& node)
171 {
173  grp.grpNumber = node["grp_nr"].as<unsigned int>();
174  if (node["name"].IsScalar() && !node["name"].IsNull())
175  {
176  grp.grpName = node["name"].as<std::string>();
177  }
178  else
179  {
180  grp.grpName = "Unspecified Group";
181  }
182  const YAML::Node& channelNode = node["channels"];
183  if (channelNode.IsSequence() && channelNode.size() > 0)
184  {
185  for (auto item : channelNode)
186  {
187  std::string channelName = item.as<std::string>();
188  // insert check if channel exists later ..
189  grp.channels.push_back(channels[channelName]);
190  }
191  }
192  const YAML::Node& optionsNode = node["options"];
193  if (optionsNode.IsSequence() && optionsNode.size() > 0)
194  {
195  for (auto item : optionsNode)
196  {
197  std::string option = item.as<std::string>();
198  std::string name = "";
199  std::string params = "";
200  if (option.find(':') != std::string::npos)
201  {
202  std::vector<std::string> split;
203  boost::split(split, option, boost::is_any_of(":"));
204  name = split[0];
205  params = split[1];
206  }
207  else
208  {
209  name = option;
210  }
211 
212  grp.options.push_back(std::make_pair(options[name], params));
213  }
214  }
215  std::string encoding = node["encoding"].as<std::string>();
216  if (encoding.compare(SensorGroup::ENCODING_ASCII) == 0 ||
217  encoding.compare(SensorGroup::ENCODING_B64) == 0 ||
218  encoding.compare(SensorGroup::ENCODING_HEX) == 0)
219  grp.encoding = encoding;
220  // else: unkonwn encoding for grp nr xy <- should be an error/exception
221  sensorGroups.insert(std::make_pair(
222  grp.grpNumber,
223  std::make_shared<SensorGroup>(SensorGroup(grp, getSyntax()))));
224 }
225 
227 {
228  YAML::Node interfaceYaml =
229  YAML::LoadFile(configPath + "serial_interface_config.yml");
230  interfaceYaml >> serialParams;
231 }
232 
234 {
235  YAML::Node syntaxYaml = YAML::LoadFile(configPath + "general_syntax.yml");
236  syntaxYaml >> syntax;
237 }
238 
240 {
241  YAML::Node channelsYaml = YAML::LoadFile(configPath + "channels.yml");
242 
243  for (auto node : channelsYaml)
244  {
245  node >> channels;
246  }
247 }
248 
250 {
251  YAML::Node commandOpt = YAML::LoadFile(configPath + "command_options.yml");
252 
253  for (auto node : commandOpt)
254  {
255  node >> options;
256  }
257 }
258 
260 {
261  YAML::Node commandsYaml = YAML::LoadFile(configPath + "commands.yml");
262 
263  for (auto node : commandsYaml)
264  {
265  insertCommand(node);
266  }
267 }
268 
270 {
271  YAML::Node groupsYaml = YAML::LoadFile(configPath + "sensor_groups.yml");
272 
273  for (auto node : groupsYaml)
274  {
275  insertSensorGroup(node);
276  }
277 }
278 
279 const std::shared_ptr<Syntax> CommunicationConfig::getSyntax() const
280 {
281  return std::make_shared<Syntax>(syntax);
282 }
283 const std::shared_ptr<SerialInterfaceParams>
285 {
286  return std::make_shared<SerialInterfaceParams>(serialParams);
287 }
288 
289 const std::unordered_map<std::string, std::shared_ptr<Command>>&
291 {
292  return commands;
293 }
294 
295 const std::unordered_map<unsigned char, std::shared_ptr<SensorGroup>>&
297 {
298  return sensorGroups;
299 }
static const std::string ENCODING_B64
Definition: sensorgroup.h:161
Header file for the CommunicationConfig class.
std::vector< std::pair< std::shared_ptr< CommandOptions >, std::string > > options
Definition: sensorgroup.h:49
std::string encoding
Definition: sensorgroup.h:51
CommunicationConfig()
CommunicationConfig default constructor.
std::unordered_map< std::string, std::shared_ptr< Command > > commands
void insertSensorGroup(const YAML::Node &node)
Parses SensorGroup parameters from a YAML::Node and inserts a new SensorGroup object into the SensorG...
std::string name
Definition: command.h:33
bool cmdHasParams
Definition: command.h:34
std::unordered_map< std::string, std::unordered_set< unsigned int > > grpErrorsBinary
Definition: syntax.h:32
std::string cmd
Definition: command.h:38
The Channel class serves as a data struct to store sensor channel parameter.
Definition: sensorgroup.h:24
The Syntax class serves as a data struct to provide information about important symbols for the commu...
Definition: syntax.h:21
void readSensorGroups()
Parses all SensorGroup definitions from file.
friend void operator>>(const YAML::Node &node, Syntax &syntax)
Assing operator for YAML::Node to Syntax object.
std::string textMsgPrefix
Definition: syntax.h:25
The SensorGroupParameter class serves as a data struct to configure SensorGroup objects.
Definition: sensorgroup.h:42
The SensorGroup class builds the syntactic template for its specific command that results from the se...
Definition: sensorgroup.h:82
void readOptions()
Parses all CommandOptions definitions from file.
bool cmdHasResponse
Definition: command.h:40
bool respHasParams
Definition: command.h:41
void insertCommand(const YAML::Node &node)
Parses Command parameters from a YAML::Node and inserts a new Command object into the Command map...
std::string answerOnCmdPrefix
Definition: syntax.h:26
void readGeneralSyntax()
Parses Syntax defnition from file.
bool optReturnsParams
Definition: command.h:64
std::string genErrorPrefix
Definition: syntax.h:29
std::string grpName
Definition: sensorgroup.h:44
std::string optionsPrefix
Definition: syntax.h:30
std::string channelGrpMsgPrefix
Definition: syntax.h:27
void readChannels()
Parses all Channel defnitions from file.
void readSerialInterfaceConfig()
Parses serial interface coniguration from file.
The CommandOptions class serves as a data struct to inform Command objects about all modifiers that c...
Definition: command.h:55
std::string opt
Definition: command.h:62
std::unordered_map< std::string, std::shared_ptr< CommandOptions > > options
std::string chName
Definition: sensorgroup.h:26
bool conversionNeeded
Definition: sensorgroup.h:29
const std::unordered_map< unsigned char, std::shared_ptr< SensorGroup > > & getSensorGroups() const
SensorGroup map object getter.
std::string endOfFrame
Definition: syntax.h:24
std::string endOfMessage
Definition: syntax.h:23
The SerialInterfaceParams class serves as a data struct to configure the setup of a serial connection...
bool optHasParams
Definition: command.h:58
static const std::string ENCODING_ASCII
Definition: sensorgroup.h:160
static const std::string ENCODING_HEX
Definition: sensorgroup.h:162
const std::shared_ptr< SerialInterfaceParams > getSerialInterfaceParams() const
SerialInterfaceParams object getter.
std::vector< std::shared_ptr< Channel > > channels
Definition: sensorgroup.h:47
std::vector< std::pair< std::string, std::string > > params
Definition: command.h:36
std::unordered_map< unsigned char, std::shared_ptr< SensorGroup > > sensorGroups
bool addsRespToGrps
Definition: command.h:69
std::unordered_map< std::string, std::shared_ptr< Channel > > channels
std::string response
Definition: command.h:42
std::unordered_set< std::string > grpErrorsAscii
Definition: syntax.h:31
The CommandParams class serves as a data struct to configure Command objects.
Definition: command.h:31
const std::shared_ptr< Syntax > getSyntax() const
Syntax object getter.
std::vector< std::pair< std::string, std::string > > params
Definition: command.h:60
SerialInterfaceParams serialParams
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
void readCommands()
Parses all Command definitions from file.
std::string cmdErrorPrefix
Definition: syntax.h:28
std::string response
Definition: command.h:66
std::string optName
Definition: command.h:57
std::string dataType
Definition: sensorgroup.h:27
double conversionFactor
Definition: sensorgroup.h:30
const std::unordered_map< std::string, std::shared_ptr< Command > > & getCommands() const
Command map getter.


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