opcua_helpers.h
Go to the documentation of this file.
1 
23 #include "ros_opcua_msgs/Address.h"
24 #include "ros_opcua_msgs/TypeValue.h"
25 
26 #include <opc/ua/node.h>
28 
30 
31 std::map<int, std::string> _TypeToStringMap = {
32 // {0, "null"},
33  {1, "bool"},
34  {2, "int8"},
35  {3, "uint8"},
36  {4, "int16"},
37  {5, "uint16"},
38  {6, "int32"},
39  {7, "uint32"},
40  {8, "int64"},
41  {9, "uint64"},
42  {10, "float32"},
43  {11, "float64"},
44  {12, "string"},
45 // {13, "date_time"},
46 // {14, "guid"},
47 // {15, "byte_string"},
48 // {16, "xml_element"},
49 // {17, "id"},
50 // {18, "expanded_node_id"},
51 // {19, "status_code"},
52 // {20, "qualified_name"},
53 // {21, "localized_text"},
54 // {22, "extenstion_object"},
55 // {23, "data_value"},
56 // {24, "OpcUa::Variant"},
57 // {25, "diagnostic_info"}
58 };
59 
65 //OpcUa::NodeID convertAddressToNodeID(const ros_opcua_msgs::Address address) {
66 //
67 // if (address.id_type == "num") {
68 // return OpcUa::NumericNodeID(address.id_num, address.namespace_index);
69 // }
70 // else if (address.id_type == "str") {
71 // return OpcUa::StringNodeID(address.id_str, address.namespace_index);
72 // }
73 // else if (address.id_type == "guid") {
74 // return OpcUa::GuidNodeID(OpcUa::ToGuid(address.id_guid), address.namespace_index);
75 // }
76 // else {
77 // ROS_ERROR("OPC-UA client node %s: ID type %s does not exist!!", ros::this_node::getName().c_str(), address.id_type.c_str());
78 // throw std::exception();
79 // }
80 //}
81 
87 //ros_opcua_msgs::Address convertNodeIDToAddress(const OpcUa::NodeID nodeID) {
88 //
89 // ros_opcua_msgs::Address address;
90 // address.namespace_index = nodeID.GetNamespaceIndex();
91 //
92 // if (nodeID.IsInteger()) {
93 // address.id_type = "num";
94 // address.id_num = nodeID.GetIntegerIdentifier();
95 // }
96 // else if (nodeID.IsString()) {
97 // address.id_type = "str";
98 // address.id_str = nodeID.GetStringIdentifier();
99 // }
100 // else if (nodeID.IsGuid()) {
101 // address.id_type = "guid";
102 // address.id_guid = OpcUa::ToString(nodeID.GetGuidIdentifier());
103 // }
104 // else {
105 // ROS_ERROR("OPC-UA client node %s: ID type %s does not exist!!", ros::this_node::getName().c_str(), address.id_type.c_str());
106 // throw std::exception();
107 // }
108 //
109 // return address;
110 //}
111 
112 // OpcUa::NodeID convertAddressToString(const ros_opcua_msgs::Address address) {
113 //
114 // std::stringstream stream;
115 //
116 // stream << "ns=" << address.namespace_index << ";";
117 //
118 // if (address.id_type == "num") {
119 // stream << "i=" << address.id_num << ";";
120 // }
121 // else if (address.id_type == "str") {
122 // stream << "s=" << address.id_str << ";";
123 // }
124 // else if (address.id_type == "guid") {
125 // stream << "g=" << address.id_guid << ";";
126 // }
127 // else {
128 // ROS_ERROR("OPC-UA client node %s: ID type %s does not exist!!", ros::this_node::getName().c_str(), req.node.id_type.c_str());
129 // throw std::exception("Read above!");
130 // }
131 //
132 // return stream.str();
133 // }
134 
140 ros_opcua_msgs::TypeValue convertVariantToTypeValue(const OpcUa::Variant& variant) {
141 
142  ros_opcua_msgs::TypeValue typeValue;
143 
144  typeValue.type = _TypeToStringMap[(int)variant.Type()];
145 
146  if (typeValue.type == "bool") {
147  typeValue.bool_d = (bool)variant;
148  }
149  else if (typeValue.type == "int8") {
150  typeValue.int8_d = (int8_t)variant;
151  }
152  else if (typeValue.type == "uint8") {
153  typeValue.uint8_d = (uint8_t)variant;
154  }
155  else if (typeValue.type == "int16") {
156  typeValue.int16_d = (int16_t)variant;
157  }
158  else if (typeValue.type == "uint16") {
159  typeValue.uint16_d = (uint16_t)variant;
160  }
161  else if (typeValue.type == "int32") {
162  typeValue.int32_d = (int32_t)variant;
163  }
164  else if (typeValue.type == "uint32") {
165  typeValue.uint32_d = (uint32_t)variant;
166  }
167  else if (typeValue.type == "int64") {
168  typeValue.int64_d = (int64_t)variant;
169  }
170  else if (typeValue.type == "uint64") {
171  typeValue.uint64_d = (uint64_t)variant;
172  }
173  else if (typeValue.type == "float" || typeValue.type == "float32") {
174  typeValue.float_d = (float)variant;
175  }
176  else if (typeValue.type == "double" || typeValue.type == "float64") {
177  typeValue.double_d = (double)variant;
178  }
179  else if (typeValue.type == "string") {
180  typeValue.string_d = std::string(variant);
181  }
182  else {
183  typeValue.type = "Unknown";
184  }
185 
186  return typeValue;
187 }
188 
194 OpcUa::Variant convertTypeValueToVariant(ros_opcua_msgs::TypeValue& typeValue)
195 {
196  OpcUa::Variant variant;
197 
198  if (typeValue.type == "bool") {
199  variant = bool(typeValue.bool_d);
200  }
201  else if (typeValue.type == "int8") {
202  variant = typeValue.int8_d;
203  }
204  else if (typeValue.type == "uint8") {
205  variant = typeValue.uint8_d;
206  }
207  else if (typeValue.type == "int16") {
208  variant = typeValue.int16_d;
209  }
210  else if (typeValue.type == "uint16") {
211  variant = typeValue.uint16_d;
212  }
213  else if (typeValue.type == "int32") {
214  variant = typeValue.int32_d;
215  }
216  else if (typeValue.type == "uint32") {
217  variant = typeValue.uint32_d;
218  }
219  else if (typeValue.type == "int64") {
220  variant = typeValue.int64_d;
221  }
222  else if (typeValue.type == "uint64") {
223  variant = typeValue.uint64_d;
224  }
225  else if (typeValue.type == "float" || typeValue.type == "float32") {
226  variant = typeValue.float_d;
227  }
228  else if (typeValue.type == "double" || typeValue.type == "float64") {
229  variant = typeValue.double_d;
230  }
231  else if (typeValue.type == "string") {
232  variant = typeValue.string_d;
233  }
234 
235  return variant;
236 }
VariantType Type() const
std::map< int, std::string > _TypeToStringMap
Remapping between ROS types and OpenOpcUa types.
Definition: opcua_helpers.h:31
ros_opcua_msgs::TypeValue convertVariantToTypeValue(const OpcUa::Variant &variant)
OpcUa::Variant convertTypeValueToVariant(ros_opcua_msgs::TypeValue &typeValue)


ros_opcua_impl_freeopcua
Author(s): Denis Štogl
autogenerated on Tue Jan 19 2021 03:12:07