1
0
mirror of https://github.com/thingsboard/thingsboard-gateway synced 2025-10-26 22:31:42 +08:00

Added statistic about received and messages sent.

This commit is contained in:
zbeacon 2019-11-08 16:28:31 +02:00
parent fa1e03e5db
commit 60e902d7cd
9 changed files with 56 additions and 7 deletions

View File

@ -1,6 +1,6 @@
[metadata]
name = thingsboard-gateway
version = 2.0.1
version = 2.0.2
description = Thingsboard Gateway for IoT devices.
long_description = The Thingsboard IoT Gateway is an open-source solution that allows you to integrate devices connected to legacy and third-party systems with Thingsboard.
license = Apache Software License (Apache Software License 2.0)

View File

@ -34,7 +34,7 @@ setup(
'PyYAML',
'six'
],
download_url='https://github.com/thingsboard/thingsboard-gateway/archive/2.0.1rc-python.tar.gz',
download_url='https://github.com/thingsboard/thingsboard-gateway/archive/rc2.0.2-python.tar.gz',
entry_points={
'console_scripts': [
'thingsboard-gateway = thingsboard_gateway.tb_gateway:daemon'

Binary file not shown.

View File

@ -28,6 +28,8 @@ from thingsboard_gateway.connectors.modbus.bytes_modbus_downlink_converter impor
class ModbusConnector(Connector, threading.Thread):
def __init__(self, gateway, config):
self.statistics = {'MessagesReceived': 0,
'MessagesSent': 0}
super(Connector, self).__init__()
super(threading.Thread, self).__init__()
self.__gateway = gateway
@ -42,6 +44,9 @@ class ModbusConnector(Connector, threading.Thread):
self.__stopped = False
self.daemon = True
def is_connected(self):
return self.__connected
def open(self):
self.__stopped = False
self.start()
@ -119,7 +124,9 @@ class ModbusConnector(Connector, threading.Thread):
converted_data = self.__devices[device]["converter"].convert(device_responses)
if converted_data["telemetry"] != self.__devices[device]["telemetry"] or\
converted_data["attributes"] != self.__devices[device]["attributes"]:
converted_data["attributes"] != self.__devices[device]["attributes"] and\
self.__server_conf.get("sendDataOnlyOnChange") == True:
self.statistics['MessagesReceived'] += 1
to_send = {"deviceName": converted_data["deviceName"], "deviceType": converted_data["deviceType"]}
if converted_data["telemetry"] != self.__devices[device]["telemetry"]:
self.__devices[device]["last_telemetry"] = converted_data["telemetry"]
@ -128,6 +135,18 @@ class ModbusConnector(Connector, threading.Thread):
self.__devices[device]["last_telemetry"] = converted_data["attributes"]
to_send["attributes"] = converted_data["attributes"]
self.__gateway.send_to_storage(self.get_name(), to_send)
self.statistics['MessagesSent'] += 1
elif self.__server_conf.get("sendDataOnlyOnChange") == False:
self.statistics['MessagesReceived'] += 1
to_send = {"deviceName": converted_data["deviceName"], "deviceType": converted_data["deviceType"]}
if converted_data["telemetry"] != self.__devices[device]["telemetry"]:
self.__devices[device]["last_telemetry"] = converted_data["telemetry"]
to_send["telemetry"] = converted_data["telemetry"]
if converted_data["attributes"] != self.__devices[device]["attributes"]:
self.__devices[device]["last_telemetry"] = converted_data["attributes"]
to_send["attributes"] = converted_data["attributes"]
self.__gateway.send_to_storage(self.get_name(), to_send)
self.statistics['MessagesSent'] += 1
except ConnectionException:
log.error("Connection lost! Trying to reconnect")
except Exception as e:

View File

@ -28,6 +28,8 @@ from json import loads
class MqttConnector(Connector, Thread):
def __init__(self, gateway, config):
super().__init__()
self.statistics = {'MessagesReceived': 0,
'MessagesSent': 0}
self.__gateway = gateway
self.__broker = config.get('broker')
self.__mapping = config.get('mapping')
@ -58,8 +60,7 @@ class MqttConnector(Connector, Thread):
tls_version=ssl.PROTOCOL_TLSv1_2,
ciphers=None)
except Exception as e:
log.error("Cannot setup connection to broker %s using SSL. Please check your configuration.\n\
Error: %s",
log.error("Cannot setup connection to broker %s using SSL. Please check your configuration.\nError: %s",
self.get_name(),
e)
self._client.tls_insecure_set(False)
@ -73,6 +74,9 @@ class MqttConnector(Connector, Thread):
self.__stopped = False
self.daemon = True
def is_connected(self):
return self._connected
def open(self):
self.__stopped = False
self.start()
@ -140,8 +144,7 @@ class MqttConnector(Connector, Thread):
log.debug('Custom converter for topic %s - found!', mapping["topicFilter"])
converter = module(mapping)
else:
log.error("\n\nCannot find extension module for %s topic.\n\
Please check your configuration.\n", mapping["topicFilter"])
log.error("\n\nCannot find extension module for %s topic.\n\Please check your configuration.\n", mapping["topicFilter"])
except Exception as e:
log.exception(e)
else:
@ -200,6 +203,7 @@ class MqttConnector(Connector, Thread):
self.__attribute_updates = config[service_config]
def _on_message(self, client, userdata, message):
self.statistics['MessagesReceived'] += 1
content = self._decode(message)
regex_topic = [regex for regex in self.__sub_topics if re.fullmatch(regex, message.topic)]
if regex_topic:
@ -218,6 +222,7 @@ class MqttConnector(Connector, Thread):
self.__gateway.add_device(converted_content["deviceName"], {"connector": None})
self.__gateway.update_device(converted_content["deviceName"], "connector", self)
self.__gateway.send_to_storage(self.get_name(), converted_content)
self.statistics['MessagesSent'] += 1
else:
continue
else:

View File

@ -27,6 +27,8 @@ from thingsboard_gateway.connectors.opcua.opcua_uplink_converter import OpcUaUpl
class OpcUaConnector(Thread, Connector):
def __init__(self, gateway, config):
self.statistics = {'MessagesReceived': 0,
'MessagesSent': 0}
Thread.__init__(self)
self.__gateway = gateway
self.__server_conf = config.get("server")
@ -74,6 +76,9 @@ class OpcUaConnector(Thread, Connector):
self.__connected = False
self.daemon = True
def is_connected(self):
return self.__connected
def open(self):
self.__stopped = False
self.start()
@ -263,7 +268,9 @@ class SubHandler(object):
log.debug("Python: New data change event on node %s, with val: %s", node, val)
subscription = self.connector.subscribed[node]
converted_data = subscription["converter"].convert(subscription["path"], val)
self.connector.statistics['MessagesReceived'] += 1
self.connector.data_to_send.append(converted_data)
self.connector.statistics['MessagesSent'] += 1
log.debug("Data to ThingsBoard: %s", converted_data)
except Exception as e:
log.exception(e)

View File

@ -69,11 +69,26 @@ class TBGatewayService:
self.__send_thread.start()
try:
gateway_statistic_send = 0
while True:
for rpc_in_progress in self.__rpc_requests_in_progress:
if time.time() >= self.__rpc_requests_in_progress[rpc_in_progress][1]:
self.__rpc_requests_in_progress[rpc_in_progress][2](rpc_in_progress)
self.cancel_rpc_request(rpc_in_progress)
if time.time() - gateway_statistic_send >= 60000:
summary_messages = {"SummaryReceived": 0, "SummarySent": 0}
telemetry = {}
for connector in self.available_connectors:
# if self.available_connectors[connector].is_connected():
telemetry[(connector+' MessagesReceived').replace(' ', '')] = self.available_connectors[connector].statistics['MessagesReceived']
telemetry[(connector+' MessagesSent').replace(' ', '')] = self.available_connectors[connector].statistics['MessagesSent']
self.tb_client.client.send_telemetry(telemetry)
summary_messages['SummaryReceived'] += telemetry[(connector+' MessagesReceived').replace(' ', '')]
summary_messages['SummarySent'] += telemetry[(connector+' MessagesSent').replace(' ', '')]
self.tb_client.client.send_telemetry(summary_messages)
gateway_statistic_send = time.time()
time.sleep(.1)
except Exception as e:
log.exception(e)
@ -115,6 +130,9 @@ class TBGatewayService:
except Exception as e:
log.exception(e)
def __send_statistic(self):
self.tb_client.client.gw_send_telemetry()
def send_to_storage(self, connector_name, data):
self._send_to_storage(connector_name, data)