diff --git a/thingsboard_gateway/gateway/tb_gateway_service.py b/thingsboard_gateway/gateway/tb_gateway_service.py index c1835747..83d1d823 100644 --- a/thingsboard_gateway/gateway/tb_gateway_service.py +++ b/thingsboard_gateway/gateway/tb_gateway_service.py @@ -49,6 +49,7 @@ main_handler = logging.handlers.MemoryHandler(-1) class TBGatewayService: def __init__(self, config_file=None): + self.stopped = False self.__lock = RLock() if config_file is None: config_file = path.dirname(path.dirname(path.abspath(__file__))) + '/config/tb_gateway.yaml'.replace('/', path.sep) @@ -133,7 +134,7 @@ class TBGatewayService: try: gateway_statistic_send = 0 - while True: + while not self.stopped: cur_time = time()*1000 if self.__sheduled_rpc_calls: for rpc_call_index in range(len(self.__sheduled_rpc_calls)): @@ -172,10 +173,7 @@ class TBGatewayService: gateway_statistic_send = time()*1000 # self.__check_shared_attributes() except KeyboardInterrupt: - log.info("Stopping...") - self.__close_connectors() - log.info("The gateway has been stopped.") - self.tb_client.stop() + self.__stop_gateway() except Exception as e: log.exception(e) self.__close_connectors() @@ -191,7 +189,11 @@ class TBGatewayService: log.exception(e) def __stop_gateway(self): - pass + self.stopped = True + log.info("Stopping...") + self.__close_connectors() + log.info("The gateway has been stopped.") + self.tb_client.stop() def _attributes_parse(self, content, *args): try: diff --git a/thingsboard_gateway/gateway/tb_updater.py b/thingsboard_gateway/gateway/tb_updater.py index 598be885..5e9d5c0b 100644 --- a/thingsboard_gateway/gateway/tb_updater.py +++ b/thingsboard_gateway/gateway/tb_updater.py @@ -26,16 +26,15 @@ from thingsboard_gateway.tb_utility.tb_utility import TBUtility log = getLogger("service") -UPDATE_SERVICE_BASE_URL = "https://updates.thingsboard.io" -# UPDATE_SERVICE_BASE_URL = "http://0.0.0.0:8090" +# UPDATE_SERVICE_BASE_URL = "https://updates.thingsboard.io" +UPDATE_SERVICE_BASE_URL = "http://0.0.0.0:8090" class TBUpdater(Thread): def __init__(self, gateway, auto_updates_enabled): super().__init__() self.__gateway = gateway - # self.__version = get_distribution('thingsboard_gateway').version - self.__version = "1.2.1" #For test + self.__version = get_distribution('thingsboard_gateway').version self.__instance_id = str(uuid1()) self.__platform = "deb" self.__os_version = platform() @@ -45,7 +44,7 @@ class TBUpdater(Thread): self.start() def run(self): - while True: + while not self.__gateway.stopped: self.check_for_new_version() sleep(self.__check_period) @@ -63,6 +62,8 @@ class TBUpdater(Thread): new_version = content["message"].replace("New version ", "").replace(" is available!", "") log.info(content["message"]) TBUtility.install_package("thingsboard-gateway", new_version) + except ConnectionRefusedError: + log.warning("Cannot connect to the update service. PLease check your internet connection.") except Exception as e: log.exception(e) diff --git a/thingsboard_gateway/tb_utility/tb_utility.py b/thingsboard_gateway/tb_utility/tb_utility.py index 61ca15d3..cff0a70e 100644 --- a/thingsboard_gateway/tb_utility/tb_utility.py +++ b/thingsboard_gateway/tb_utility/tb_utility.py @@ -157,4 +157,4 @@ class TBUtility: if version.lower() == "upgrade": check_call([executable, "-m", "pip", "install", package, "--upgrade", "--user"]) else: - check_call([executable, "-m", "pip", "install", package + version, "--user"]) + check_call([executable, "-m", "pip", "install", package + "==" + version, "--user"])