mirror of
https://github.com/thingsboard/thingsboard-gateway
synced 2025-10-26 22:31:42 +08:00
Improvements for performance
This commit is contained in:
@@ -80,21 +80,26 @@ class BytesModbusUplinkConverter(ModbusConverter):
|
||||
log.warning("unsupported register count for double data type in response for tag %s",
|
||||
data_sent["tag"])
|
||||
continue
|
||||
elif type_of_data == "bit":
|
||||
if "bit" in data_sent:
|
||||
if type(result) == list:
|
||||
if len(result) > 1:
|
||||
log.warning("with bit parameter only one register is expected, got more then one in response for tag %s",
|
||||
data_sent["tag"])
|
||||
continue
|
||||
result = result[0]
|
||||
position = 15 - data_sent["bit"] # reverse order
|
||||
# transform result to string representation of a bit sequence, add "0" to make it longer >16
|
||||
result = "0000000000000000" + str(bin(result)[2:])
|
||||
# get length of 16, then get bit, then cast it to int(0||1 from "0"||"1", then cast to boolean)
|
||||
result = bool(int((result[len(result) - 16:])[15 - position]))
|
||||
else:
|
||||
log.error("Bit address not found in config for modbus connector for tag: %s", data_sent["tag"])
|
||||
|
||||
else:
|
||||
log.warning("unknown data type, not string, long or double in response for tag %s",
|
||||
data_sent["tag"])
|
||||
continue
|
||||
if "bit" in data_sent:
|
||||
if len(result) > 1:
|
||||
log.warning("with bit parameter only one register is expected, got more then one in response for tag %s",
|
||||
data_sent["tag"])
|
||||
continue
|
||||
result = result[0]
|
||||
position = 15 - data_sent["bit"] # reverse order
|
||||
# transform result to string representation of a bit sequence, add "0" to make it longer >16
|
||||
result = "0000000000000000" + str(bin(result)[2:])
|
||||
# get length of 16, then get bit, then cast it to int(0||1 from "0"||"1", then cast to boolean)
|
||||
result = bool(int((result[len(result) - 16:])[15 - position]))
|
||||
try:
|
||||
self.__result[config_data].append({tag: int(result)})
|
||||
except ValueError:
|
||||
|
||||
@@ -22,7 +22,7 @@ from thingsboard_gateway.connectors.connector import Connector, log
|
||||
from thingsboard_gateway.connectors.mqtt.json_mqtt_uplink_converter import JsonMqttUplinkConverter
|
||||
from threading import Thread
|
||||
from thingsboard_gateway.tb_utility.tb_utility import TBUtility
|
||||
from json import loads
|
||||
from simplejson import loads
|
||||
|
||||
|
||||
class MqttConnector(Connector, Thread):
|
||||
|
||||
@@ -13,7 +13,7 @@
|
||||
# limitations under the License.
|
||||
|
||||
import re
|
||||
from json import dumps
|
||||
from simplejson import dumps
|
||||
import time
|
||||
from threading import Thread
|
||||
from random import choice
|
||||
|
||||
@@ -178,13 +178,11 @@ class TBGatewayService:
|
||||
if events:
|
||||
for event in events:
|
||||
current_event = loads(event)
|
||||
# time.sleep(.001)
|
||||
if current_event.get("telemetry"):
|
||||
telemetry = {}
|
||||
if type(current_event["telemetry"]) == list:
|
||||
for item in current_event["telemetry"]:
|
||||
for key in item:
|
||||
telemetry[key] = item[key]
|
||||
telemetry.update(item.items())
|
||||
else:
|
||||
telemetry = current_event["telemetry"]
|
||||
filtered_telemetry = {}
|
||||
@@ -199,21 +197,19 @@ class TBGatewayService:
|
||||
attributes = {}
|
||||
if type(current_event["attributes"]) == list:
|
||||
for item in current_event["attributes"]:
|
||||
for key in item:
|
||||
attributes[key] = item[key]
|
||||
attributes.update(item.items())
|
||||
else:
|
||||
attributes = current_event["attributes"]
|
||||
filtered_attributes = {}
|
||||
for attribute_key in attributes:
|
||||
if attributes[attribute_key] is not None:
|
||||
filtered_attributes[attribute_key] = attributes[attribute_key]
|
||||
if filtered_attributes != {}:
|
||||
if filtered_attributes:
|
||||
self.__published_events.append(self.tb_client.client.gw_send_attributes(current_event["deviceName"],
|
||||
filtered_attributes))
|
||||
success = True
|
||||
for event in range(len(self.__published_events)):
|
||||
result = self.__published_events[event].get()
|
||||
success = result == self.__published_events[event].TB_ERR_SUCCESS
|
||||
success = self.__published_events[event].get() == self.__published_events[event].TB_ERR_SUCCESS
|
||||
if success:
|
||||
self.__event_storage.event_pack_processing_done()
|
||||
except Exception as e:
|
||||
|
||||
@@ -15,7 +15,6 @@
|
||||
from thingsboard_gateway.storage.event_storage import EventStorage
|
||||
import queue
|
||||
from logging import getLogger
|
||||
from time import time
|
||||
|
||||
log = getLogger("storage")
|
||||
|
||||
@@ -23,7 +22,7 @@ log = getLogger("storage")
|
||||
class MemoryEventStorage(EventStorage):
|
||||
def __init__(self, config):
|
||||
self.__queue_len = config.get("max_records_count", 10000)
|
||||
self.__events_per_time = config.get("read_records_count", 10)
|
||||
self.__events_per_time = config.get("read_records_count", 1000)
|
||||
self.__events_queue = queue.Queue(self.__queue_len)
|
||||
self.__event_pack = []
|
||||
|
||||
@@ -38,8 +37,7 @@ class MemoryEventStorage(EventStorage):
|
||||
if self.__event_pack:
|
||||
return self.__event_pack
|
||||
elif not self.__events_queue.empty():
|
||||
for x in range(min(self.__events_per_time, self.__events_queue.qsize())):
|
||||
self.__event_pack.append(self.__events_queue.get(False))
|
||||
self.__event_pack = [self.__events_queue.get(False) for _ in range(min(self.__events_per_time, self.__events_queue.qsize()))]
|
||||
return self.__event_pack
|
||||
|
||||
def event_pack_processing_done(self):
|
||||
|
||||
@@ -16,7 +16,7 @@ import logging
|
||||
import queue
|
||||
import ssl
|
||||
import time
|
||||
from json import loads, dumps
|
||||
from simplejson import loads, dumps
|
||||
from threading import RLock
|
||||
from threading import Thread
|
||||
|
||||
|
||||
@@ -14,7 +14,7 @@
|
||||
|
||||
import logging
|
||||
import time
|
||||
from json import dumps
|
||||
from simplejson import dumps
|
||||
|
||||
from thingsboard_gateway.tb_client.tb_device_mqtt import TBDeviceMqttClient, DEVICE_TS_KV_VALIDATOR, KV_VALIDATOR
|
||||
|
||||
|
||||
@@ -17,7 +17,7 @@ from inspect import getmembers, isclass
|
||||
from importlib import util
|
||||
import jsonpath_rw_ext as jp
|
||||
from logging import getLogger
|
||||
from json import dumps, loads
|
||||
from simplejson import dumps, loads
|
||||
from re import search
|
||||
from time import time
|
||||
|
||||
|
||||
Reference in New Issue
Block a user