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

Improvements for remote configurator

This commit is contained in:
zbeacon
2020-02-24 09:31:05 +02:00
parent fb91fa7041
commit 8750b2b7b3
2 changed files with 55 additions and 44 deletions

View File

@@ -17,6 +17,7 @@ from simplejson import dumps, loads, dump
from yaml import safe_dump
from time import time, sleep
from logging import getLogger
from re import findall
from logging.config import fileConfig
from logging.handlers import MemoryHandler
from os import remove
@@ -48,6 +49,9 @@ class RemoteConfigurator:
try:
if not self.in_process:
self.in_process = True
# while not self.__gateway._published_events.empty():
# log.debug("Waiting for end of the data processing...")
# sleep(1)
decoded_configuration = b64decode(configuration)
self.__new_configuration = loads(decoded_configuration)
self.__old_connectors_configs = self.__gateway.connectors_configs
@@ -243,22 +247,23 @@ class RemoteConfigurator:
def __update_logs_configuration(self):
try:
if self.__old_logs_configuration == self.__new_logs_configuration:
# if self.__old_logs_configuration != self.__new_logs_configuration:
global log
log = getLogger('service')
remote_handler_current_state = self.__gateway.remote_handler.activated
remote_handler_current_level = self.__gateway.remote_handler.current_log_level
logs_conf_file_path = self.__gateway._config_dir + 'logs.conf'
new_logging_level = findall(r'level=(.*)', self.__new_logs_configuration.replace("NONE", "NOTSET"))[-1]
with open(logs_conf_file_path, 'w') as logs:
logs.write(self.__new_logs_configuration+"\r\n")
logs.write(self.__new_logs_configuration.replace("NONE", "NOTSET")+"\r\n")
fileConfig(logs_conf_file_path)
self.__gateway.main_handler = MemoryHandler(-1)
self.__gateway.remote_handler = TBLoggerHandler(self.__gateway)
self.__gateway.main_handler.setTarget(self.__gateway.remote_handler)
if remote_handler_current_level != 'NOTSET':
self.__gateway.remote_handler.activate(remote_handler_current_level)
if not remote_handler_current_state:
if new_logging_level == "NOTSET":
self.__gateway.remote_handler.deactivate()
global log
log = getLogger('service')
else:
self.__gateway.remote_handler.activate(new_logging_level)
log.debug("Logs configuration has been updated.")
except Exception as e:
log.exception(e)

View File

@@ -87,7 +87,7 @@ class TBGatewayService:
if self.__remote_configurator is not None:
self.__remote_configurator.send_current_configuration()
self.__load_persistent_devices()
self.__published_events = Queue(0)
self._published_events = Queue(0)
self._send_thread = Thread(target=self.__read_data_from_storage, daemon=True,
name="Send data to Thingsboard Thread")
self._send_thread.start()
@@ -119,7 +119,7 @@ class TBGatewayService:
if cur_time - gateway_statistic_send > 5000.0 and self.tb_client.is_connected():
summary_messages = self.__form_statistics()
with self.__lock:
# with self.__lock:
self.tb_client.client.send_telemetry(summary_messages)
gateway_statistic_send = time.time()*1000
# self.__check_shared_attributes()
@@ -269,7 +269,9 @@ class TBGatewayService:
try:
if self.tb_client.is_connected():
size = getsizeof(devices_data_in_event_pack)
with self.__lock:
# with self.__lock:
events = []
if self.__remote_configurator is None or not self.__remote_configurator.in_process:
events = self._event_storage.get_event_pack()
if events:
for event in events:
@@ -308,12 +310,17 @@ class TBGatewayService:
if devices_data_in_event_pack:
if not self.tb_client.is_connected(): break
self.__send_data(devices_data_in_event_pack)
if self.tb_client.is_connected():
if self.tb_client.is_connected() and (self.__remote_configurator is None or not self.__remote_configurator.in_process):
success = True
while not self.__published_events.empty():
event = self.__published_events.get()
while not self._published_events.empty():
if self.__remote_configurator.in_process or not self.tb_client.is_connected() or self._published_events.empty():
break
event = self._published_events.get(True, 10)
try:
if self.tb_client.is_connected() and (self.__remote_configurator is None or not self.__remote_configurator.in_process):
success = event.get() == event.TB_ERR_SUCCESS
else:
break
except Exception as e:
log.exception(e)
success = False
@@ -333,21 +340,20 @@ class TBGatewayService:
def __send_data(self, devices_data_in_event_pack):
try:
with self.__lock:
for device in devices_data_in_event_pack:
if devices_data_in_event_pack[device].get("attributes"):
if device == self.name:
self.__published_events.put(self.tb_client.client.send_attributes(devices_data_in_event_pack[device]["attributes"]))
self._published_events.put(self.tb_client.client.send_attributes(devices_data_in_event_pack[device]["attributes"]))
else:
self.__published_events.put(self.tb_client.client.gw_send_attributes(device,
self._published_events.put(self.tb_client.client.gw_send_attributes(device,
devices_data_in_event_pack[
device][
"attributes"]))
if devices_data_in_event_pack[device].get("telemetry"):
if device == self.name:
self.__published_events.put(self.tb_client.client.send_telemetry(devices_data_in_event_pack[device]["telemetry"]))
self._published_events.put(self.tb_client.client.send_telemetry(devices_data_in_event_pack[device]["telemetry"]))
else:
self.__published_events.put(self.tb_client.client.gw_send_telemetry(device,
self._published_events.put(self.tb_client.client.gw_send_telemetry(device,
devices_data_in_event_pack[
device][
"telemetry"]))