Skip to content

Commit

Permalink
Added RPC params processing for Modbus and removed orjson from packages
Browse files Browse the repository at this point in the history
  • Loading branch information
imbeacon committed Apr 7, 2020
1 parent 2ddf770 commit 9da60f3
Show file tree
Hide file tree
Showing 11 changed files with 70 additions and 60 deletions.
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[metadata]
name = thingsboard-gateway
version = 2.2.4.1
version = 2.2.4.2
description = Thingsboard Gateway for IoT devices.
long_description= file: README.md
license = Apache Software License (Apache Software License 2.0)
Expand Down
3 changes: 1 addition & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,11 @@
'pytz',
'PyYAML',
'simplejson',
'orjson>=2.6.3',
'pyrsistent',
'requests',
'python-can'
],
download_url='https://github.com/thingsboard/thingsboard-gateway/archive/2.2.4.1.tar.gz',
download_url='https://github.com/thingsboard/thingsboard-gateway/archive/2.2.4.2.tar.gz',
entry_points={
'console_scripts': [
'thingsboard-gateway = thingsboard_gateway.tb_gateway:daemon'
Expand Down
4 changes: 2 additions & 2 deletions thingsboard-gateway.spec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
%define name thingsboard-gateway
%define version 2.2.4.1
%define unmangled_version 2.2.4.1
%define version 2.2.4.2
%define unmangled_version 2.2.4.2
%define release 1

Summary: Thingsboard Gateway for IoT devices.
Expand Down
5 changes: 3 additions & 2 deletions thingsboard_gateway/config/modbus.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,12 @@
"value": false
},
"setCPUFanSpeed": {
"type": "int",
"value": 42,
"functionCode": 16,
"address": 0,
"address": 1,
"byteOrder": "BIG",
"registerCount": 1
"registerCount": 2
},
"getCPULoad": {
"type": "int",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,16 @@ def convert(self, config, data):
"16float": builder.add_16bit_float,
"32float": builder.add_32bit_float,
"64float": builder.add_64bit_float}

value = config["value"]
lower_type = config["tag"].lower()
value = None
if data.get("data") and data["data"].get("params") is not None:
value = data["data"]["params"]
else:
value = config["value"]
lower_type = config.get("type", config.get("tag", "error")).lower()
if lower_type == "error":
log.error('"type" and "tag" - not found in configuration.')
variable_size = config.get("registerCount", 1) * 8
if lower_type in ["integer", "dword", "dword/integer", "word"]:
if lower_type in ["integer", "dword", "dword/integer", "word", "int"]:
lower_type = str(variable_size) + "int"
assert builder_functions.get(lower_type) is not None
builder_functions[lower_type](value)
Expand All @@ -68,6 +73,8 @@ def convert(self, config, data):
log.debug(bits)
builder.add_bits(bits)
return builder.to_string()
else:
log.error("Unknown variable type")

builder_converting_functions = {5: builder.to_coils,
15: builder.to_coils,
Expand All @@ -77,6 +84,10 @@ def convert(self, config, data):
function_code = config["functionCode"]

if function_code in builder_converting_functions:
return builder_converting_functions[function_code]()
log.warning("Unsupported function code, for the device %s in the Modbus Downlink converter", self.__config["deviceName"])
builder = builder_converting_functions[function_code]()
if "Exception" in str(builder):
log.exception(builder)
builder = str(builder)
return builder
log.warning("Unsupported function code, for the device %s in the Modbus Downlink converter", config["device"])
return None
78 changes: 41 additions & 37 deletions thingsboard_gateway/connectors/modbus/modbus_connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,44 +231,48 @@ def __function_to_device(self, config, unit_id):
else:
log.error("Unknown Modbus function with code: %i", function_code)
log.debug("With result %s", str(result))

if "Exception" in str(result):
log.exception(result)
result = str(result)
return result

def server_side_rpc_handler(self, content):
log.debug("Modbus connector received rpc request for %s with content: %s", self.get_name(), content)
rpc_command_config = self.__devices[content["device"]]["config"]["rpc"].get(content["data"].get("method"))

if rpc_command_config is not None:
rpc_command_config["unitId"] = self.__devices[content["device"]]["config"]["unitId"]
if rpc_command_config.get('bit') is not None:
rpc_command_config["functionCode"] = 6
if rpc_command_config.get("functionCode") in (5, 6, 15, 16):
rpc_command_config["payload"] = self.__devices[content["device"]]["downlink_converter"].convert(rpc_command_config, content)
response = None
try:
response = self.__function_to_device(rpc_command_config, rpc_command_config["unitId"])
except Exception as e:
log.exception(e)

if isinstance(response, ReadRegistersResponseBase):
to_converter = {"rpc": {content["data"]["method"]: {"data_sent": rpc_command_config,
"input_data": response}}}
response = self.__devices[content["device"]]["converter"].convert(config=None, data=to_converter)
log.debug("Received RPC method: %s, result: %r", content["data"]["method"], response)
elif isinstance(response, (WriteMultipleRegistersResponse,
WriteMultipleCoilsResponse,
WriteSingleCoilResponse,
WriteSingleRegisterResponse)):
response = str(response)
log.debug("Write %r", response)
response = False if response is None else response
self.__gateway.send_rpc_reply(content["device"],
content["data"]["id"],
{content["data"]["method"]: response})
if content.get("device") is not None:
log.debug("Modbus connector received rpc request for %s with content: %s", content["device"], content)
rpc_command_config = self.__devices[content["device"]]["config"]["rpc"].get(content["data"].get("method"))
if rpc_command_config is not None:
rpc_command_config["unitId"] = self.__devices[content["device"]]["config"]["unitId"]
if rpc_command_config.get('bit') is not None:
rpc_command_config["functionCode"] = 6
if rpc_command_config.get("functionCode") in (5, 6, 15, 16):
rpc_command_config["payload"] = self.__devices[content["device"]]["downlink_converter"].convert(rpc_command_config, content)
response = None
try:
response = self.__function_to_device(rpc_command_config, rpc_command_config["unitId"])
except Exception as e:
log.exception(e)
if isinstance(response, ReadRegistersResponseBase):
to_converter = {"rpc": {content["data"]["method"]: {"data_sent": rpc_command_config,
"input_data": response}}}
response = self.__devices[content["device"]]["converter"].convert(config=None, data=to_converter)
log.debug("Received RPC method: %s, result: %r", content["data"]["method"], response)
elif isinstance(response, (WriteMultipleRegistersResponse,
WriteMultipleCoilsResponse,
WriteSingleCoilResponse,
WriteSingleRegisterResponse)):
response = str(response)
log.debug("Write %r", response)
response = False if response is None else response
response = str(response) if isinstance(response, Exception) else response
self.__gateway.send_rpc_reply(content["device"],
content["data"]["id"],
{content["data"]["method"]: response})
else:
log.error("Received rpc request, but method %s not found in config for %s.",
content["data"].get("method"),
self.get_name())
self.__gateway.send_rpc_reply(content["device"],
content["data"]["id"],
{content["data"]["method"]: "METHOD NOT FOUND!"})
else:
log.error("Received rpc request, but method %s not found in config for %s.",
content["data"].get("method"),
self.get_name())
self.__gateway.send_rpc_reply(content["device"],
content["data"]["id"],
{content["data"]["method"]: "METHOD NOT FOUND!"})
log.debug("Received RPC to connector: %r", content)
3 changes: 1 addition & 2 deletions thingsboard_gateway/gateway/tb_gateway_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@
from threading import Thread, RLock

from yaml import safe_load
from simplejson import load, dumps
from orjson import loads
from simplejson import load, dumps, loads

from thingsboard_gateway.gateway.tb_client import TBClient
from thingsboard_gateway.gateway.tb_logger import TBLoggerHandler
Expand Down
3 changes: 1 addition & 2 deletions thingsboard_gateway/storage/event_storage_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@
from os import remove
from os.path import exists
from base64 import b64decode
from simplejson import load, JSONDecodeError
from orjson import dumps
from simplejson import load, JSONDecodeError, dumps
from thingsboard_gateway.storage.file_event_storage import log
from thingsboard_gateway.storage.event_storage_files import EventStorageFiles
from thingsboard_gateway.storage.file_event_storage_settings import FileEventStorageSettings
Expand Down
3 changes: 1 addition & 2 deletions thingsboard_gateway/tb_client/tb_device_mqtt.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@

import paho.mqtt.client as paho

# from simplejson import dumps
from orjson import dumps
from simplejson import dumps
# from jsonschema import Draft7Validator
# from jsonschema import ValidationError

Expand Down
3 changes: 1 addition & 2 deletions thingsboard_gateway/tb_client/tb_gateway_mqtt.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@

import logging
import time
# from simplejson import dumps
from orjson import dumps
from simplejson import dumps

from thingsboard_gateway.tb_client.tb_device_mqtt import TBDeviceMqttClient
from thingsboard_gateway.tb_utility.tb_utility import TBUtility
Expand Down
3 changes: 1 addition & 2 deletions thingsboard_gateway/tb_utility/tb_utility.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@
from inspect import getmembers, isclass
from importlib import util
from logging import getLogger
# from simplejson import dumps, loads
from orjson import loads, dumps
from simplejson import dumps, loads
from jsonpath_rw import parse


Expand Down

0 comments on commit 9da60f3

Please sign in to comment.