Skip to content

Commit 7f6b9b0

Browse files
authored
Merge pull request #1958 from samson0v/master
Fixed data type conversion in RPC processing and method name parsing
2 parents c70b303 + aee7b31 commit 7f6b9b0

File tree

2 files changed

+27
-14
lines changed

2 files changed

+27
-14
lines changed

thingsboard_gateway/connectors/opcua/entities/rpc_request.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,12 +77,12 @@ def _get_rpc_type(self, content: dict) -> OpcUaRpcType:
7777

7878
def _is_connector_rpc(self) -> bool:
7979
try:
80-
(connector_type, rpc_method_name) = self.rpc_method.split('_')
80+
(connector_type, *rpc_method_name) = self.rpc_method.split('_')
8181
if connector_type == "opcua":
82-
self.rpc_method = rpc_method_name
82+
self.rpc_method = '_'.join(rpc_method_name)
8383
return True
8484

85-
except(ValueError, AttributeError):
85+
except (ValueError, AttributeError):
8686
return False
8787

8888
@staticmethod

thingsboard_gateway/connectors/opcua/opcua_connector.py

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@
6161
from asyncua import ua, Node
6262
from asyncua.ua import NodeId, UaStringParsingError
6363
from asyncua.common.ua_utils import value_to_datavalue
64-
from asyncua.ua.uaerrors import BadWriteNotSupported
64+
from asyncua.ua.uaerrors import BadWriteNotSupported, BadTypeMismatch
6565
from asyncua.crypto.security_policies import SecurityPolicyBasic256Sha256, SecurityPolicyBasic256, \
6666
SecurityPolicyBasic128Rsa15
6767
from asyncua.ua.uaerrors import UaStatusCodeError, BadNodeIdUnknown, BadConnectionClosed, \
@@ -1124,7 +1124,7 @@ def on_attributes_update(self, content: Dict):
11241124
return
11251125

11261126
for (key, value) in content["data"].items():
1127-
if not key in device.shared_attributes_keys_value_pairs:
1127+
if key not in device.shared_attributes_keys_value_pairs:
11281128
self.__log.warning("Attribute key %s not found in device attribute section %s", key, device.name)
11291129
continue
11301130

@@ -1346,8 +1346,8 @@ async def __process_rpc_request(self, identifier: Node | str, rpc_request: OpcUa
13461346
return result
13471347

13481348
async def __write_value(self, path, value):
1349-
13501349
result = {}
1350+
13511351
try:
13521352
var = path
13531353
if isinstance(path, str):
@@ -1357,38 +1357,51 @@ async def __write_value(self, path, value):
13571357

13581358
try:
13591359
await var.write_value(value)
1360-
except BadWriteNotSupported:
1360+
except (BadWriteNotSupported, BadTypeMismatch):
1361+
value = self.__guess_type_and_cast(value)
13611362
data_value = ua.DataValue(ua.Variant(value))
13621363
await var.write_value(data_value)
13631364

13641365
result['value'] = value
13651366
return result
1366-
1367-
except UaStringParsingError as e:
1367+
except UaStringParsingError:
13681368
error_response = f"Could not find identifier in string {path}"
13691369
result['error'] = error_response
13701370
self.__log.error(error_response)
13711371
return result
1372-
13731372
except Exception as e:
13741373
result['error'] = e.__str__()
13751374
self.__log.error("Can not find node for provided path %s ", path)
13761375
return result
13771376

1378-
async def __read_value(self, path):
1377+
@staticmethod
1378+
def __guess_type_and_cast(value):
1379+
if isinstance(value, str):
1380+
if value.lower() in ['true', 'false']:
1381+
return value.lower() == 'true'
1382+
1383+
try:
1384+
if '.' in value:
1385+
return float(value)
1386+
1387+
return int(value)
1388+
except ValueError:
1389+
return value
13791390

1391+
return value
1392+
1393+
async def __read_value(self, path):
13801394
result = {}
1395+
13811396
try:
13821397
var = self.__client.get_node(path)
13831398
result['value'] = await var.read_value()
13841399
return result
1385-
1386-
except UaStringParsingError as e:
1400+
except UaStringParsingError:
13871401
error_response = f"Could not find identifier in string {path}"
13881402
result['error'] = error_response
13891403
self.__log.error(error_response)
13901404
return result
1391-
13921405
except Exception as e:
13931406
result['error'] = e.__str__()
13941407
self.__log.error("Can not find node for provided path %s ", path)

0 commit comments

Comments
 (0)