Commit a55da276 authored by nickolay.kovalev's avatar nickolay.kovalev
Browse files

float/double/string support

parent ae4b3ac6
......@@ -16,6 +16,7 @@ void parseWriteParameters(const char *input, int32_t *nmSpace, uint32_t *nodeId,
const UA_DataType *type, UA_Variant *value);
void parseType(UA_Variant *value, char *destination, uint32_t *len);
const char * const boolToString(bool b);
UA_String stringify(const char *chars, size_t len);
void errorLogger(void *context, UA_LogLevel level, UA_LogCategory category,
const char *msg, va_list args);
......@@ -323,10 +324,10 @@ void parseType(UA_Variant *value, char *destination, uint32_t *len){
*len = float_resp.size();
}
else if(UA_Variant_hasScalarType(value, &UA_TYPES[UA_TYPES_DOUBLE])) {
UA_Double float_val = *(UA_UInt64 *) value->data;
std::string float_resp = "float|" + std::to_string(float_val);
float_resp.copy(destination, float_resp.size(), 0);
*len = float_resp.size();
UA_Double double_val = *(UA_Double *) value->data;
std::string double_resp = "double|" + std::to_string(double_val);
double_resp.copy(destination, double_resp.size(), 0);
*len = double_resp.size();
}
/* String type */
else if(UA_Variant_hasScalarType(value, &UA_TYPES[UA_TYPES_STRING])) {
......@@ -371,20 +372,23 @@ void parseWriteParameters(const char *input, int32_t *nmSpace, uint32_t *nodeId,
std::size_t nmSpaceLen = identity.find(delimiter);
std::size_t nodeIdLen = identity.find(delimiter, nmSpaceLen+1);
std::size_t typeLen = identity.find(delimiter, nodeIdLen+1);
std::size_t endPos = identity.find(delimiter, typeLen+1);
//std::cerr << "nmSpaceLen: " << nmSpaceLen << std::endl;
//std::cerr << "nodeIdLen: " << nodeIdLen << std::endl;
//std::cerr << "typeLen: " << typeLen << std::endl;
//std::cerr << "endPost: " << endPos << std::endl;
//std::cerr.flush();
if (nmSpaceLen != std::string::npos && nodeIdLen != std::string::npos && typeLen != std::string::npos)
if (nmSpaceLen != std::string::npos && nodeIdLen != std::string::npos && typeLen != std::string::npos && endPos != std::string::npos)
{
std::string name_string(identity, 0, nmSpaceLen);
std::string node_string(identity, nmSpaceLen+1, nodeIdLen-nmSpaceLen-1);
std::string type_string(identity, nodeIdLen+1, typeLen-nodeIdLen-1);
std::string value_string(identity, typeLen+1, identity.size()-typeLen-1);
std::string value_string(identity, typeLen+1, endPos-typeLen-1);
//std::cerr << "input[nmSpaceLen]=" << input[nmSpaceLen] << std::endl;
//std::cerr << "input[nodeIdLen]=" << input[nodeIdLen] << std::endl;
//std::cerr << "input[typeLen]=" << input[typeLen] << std::endl;
//std::cerr << "input[nodeIdLen]=" << input[nodeIdLen] << std::endl;
//std::cerr << "input[typeLen]=" << input[typeLen] << std::endl;
//std::cerr << "input[endPos]=" << input[endPos] << std::endl;
//std::cerr << "nmSpace=" << name_string.c_str() << std::endl;
//std::cerr << "nodeId=" << node_string.c_str() << std::endl;
......@@ -435,23 +439,21 @@ void parseWriteParameters(const char *input, int32_t *nmSpace, uint32_t *nodeId,
UA_UInt64 int_val = std::stoi(value_string);
UA_Variant_setScalarCopy(val, &int_val, type);
}
/* todo make this live
else if (type_string.compare("float") == 0){
*type = UA_TYPES[UA_TYPES_FLOAT];
UA_Float float_val = std::stoi(value_string),
UA_Variant_setScalarCopy(value, &float_val, &UA_TYPES[UA_TYPES_INT32]);
type = &UA_TYPES[UA_TYPES_FLOAT];
UA_Float float_val = std::stof(value_string.c_str());
UA_Variant_setScalarCopy(val, &float_val, type);
}
else if (type_string.compare("double") == 0){
*type = UA_TYPES[UA_TYPES_DOUBLE];
UA_Double int_val = std::stoi(value_string),
UA_Variant_setScalarCopy(value, &int_val, &UA_TYPES[UA_TYPES_INT32]);
type = &UA_TYPES[UA_TYPES_DOUBLE];
UA_Double double_val = std::stod(value_string.c_str());
UA_Variant_setScalarCopy(val, &double_val, type);
}
else if (type_string.compare("string") == 0){
*type = UA_TYPES[UA_TYPES_STRING];
UA_Int16 int_val = std::stoi(value_string),
UA_Variant_setScalarCopy(value, &int_val, &UA_TYPES[UA_TYPES_INT32]);
type = &UA_TYPES[UA_TYPES_STRING];
UA_String string_val = stringify(value_string.c_str(), value_string.length());
UA_Variant_setScalarCopy(val, &string_val, type);
}
*/
else{
*nmSpace = -1;
}
......@@ -464,4 +466,11 @@ void parseWriteParameters(const char *input, int32_t *nmSpace, uint32_t *nodeId,
inline const char * const boolToString(bool b){
return b ? "true" : "false";
}
inline UA_String stringify(const char *chars, size_t len) {
UA_String s;
s.length = len;
s.data = (UA_Byte*) chars;
return s;
}
\ No newline at end of file
......@@ -151,7 +151,7 @@ handle_call({write_node, {Namespace, NodeId, Type, Value}}, _From, #{port := Por
NamespaceBin/binary, $|,
NodeIdBin/binary, $|,
Type/binary, $|,
ValueBin/binary
ValueBin/binary, $|
>>,
{reply, sync_read(Port, WriteValue), State}.
......@@ -187,6 +187,8 @@ sync_read(Port, Data) when is_binary(Data) ->
{integer, erlang:binary_to_integer(Value)};
{Port, {data, <<"float|", Value/binary>>}} ->
{float, convert_to_float(Value)};
{Port, {data, <<"double|", Value/binary>>}} ->
{double, convert_to_float(Value)};
{Port, {data, <<"string|", Value/binary>>}} ->
{string, Value};
{Port, {data, <<"bool|true">>}} ->
......@@ -214,5 +216,25 @@ convert_to_float(Value) when is_binary(Value) ->
end
end.
value_per_type(_, Value) when is_integer(Value) ->
erlang:integer_to_binary(Value).
\ No newline at end of file
value_per_type(<<"byte">>, Value) when is_integer(Value) ->
erlang:integer_to_binary(Value);
value_per_type(<<"sbyte">>, Value) when is_integer(Value) ->
erlang:integer_to_binary(Value);
value_per_type(<<"int16">>, Value) when is_integer(Value) ->
erlang:integer_to_binary(Value);
value_per_type(<<"int32">>, Value) when is_integer(Value) ->
erlang:integer_to_binary(Value);
value_per_type(<<"int64">>, Value) when is_integer(Value) ->
erlang:integer_to_binary(Value);
value_per_type(<<"uint16">>, Value) when is_integer(Value) ->
erlang:integer_to_binary(Value);
value_per_type(<<"uint32">>, Value) when is_integer(Value) ->
erlang:integer_to_binary(Value);
value_per_type(<<"uint64">>, Value) when is_integer(Value) ->
erlang:integer_to_binary(Value);
value_per_type(<<"float">>, Value) when is_float(Value) ->
erlang:float_to_binary(Value);
value_per_type(<<"double">>, Value) when is_float(Value) ->
erlang:float_to_binary(Value);
value_per_type(<<"string">>, Value) when is_binary(Value) ->
Value.
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment