diff --git a/thingsboard_gateway/gateway/tb_gateway_service.py b/thingsboard_gateway/gateway/tb_gateway_service.py index 96be8b4f..eb86506a 100644 --- a/thingsboard_gateway/gateway/tb_gateway_service.py +++ b/thingsboard_gateway/gateway/tb_gateway_service.py @@ -64,10 +64,10 @@ class TBGatewayService: log = logging.getLogger('service') log.info("Gateway starting...") self.__updater = TBUpdater() - self.__updates_check_period_ms = 3000 + self.__updates_check_period_ms = 300000 self.__updates_check_time = 0 self.version = self.__updater.get_version() - log.info("ThingsBoard IoT gateway version: %s", self.version) + log.info("ThingsBoard IoT gateway version: %s", self.version["current_version"]) self.available_connectors = {} self.__connector_incoming_messages = {} self.__connected_devices = {} @@ -107,6 +107,7 @@ class TBGatewayService: "ping": self.__rpc_ping, "stats": self.__form_statistics, "devices": self.__rpc_devices, + "update": self.__rpc_update, } self.__sheduled_rpc_calls = [] self.__self_rpc_sheduled_methods_functions = { @@ -177,9 +178,6 @@ class TBGatewayService: if cur_time - self.__updates_check_time >= self.__updates_check_period_ms: self.__updates_check_time = time()*1000 self.version = self.__updater.get_version() - if self.version.get("latest_version") is not None: - log.info("\n\n[===UPDATE===]\n\n New version %s is available! \n\n[===UPDATE===]\n", - self.version["latest_version"]) except KeyboardInterrupt: self.__stop_gateway() except Exception as e: @@ -480,7 +478,7 @@ class TBGatewayService: log.info("Gateway %s sheduled in %i seconds", method_to_call, seconds_to_restart/1000) result = {"success": True} elif arguments is not None: - result = self.__gateway_rpc_methods[method_to_call]() + result = self.__gateway_rpc_methods[method_to_call](arguments) else: result = self.__gateway_rpc_methods[method_to_call]() log.debug(result) @@ -496,6 +494,17 @@ class TBGatewayService: data_to_send[device] = self.__connected_devices[device]["connector"].get_name() return {"code": 200, "resp": data_to_send} + def __rpc_update(self, *args): + try: + result = {"resp": self.__updater.update(), + "code": 200, + } + except Exception as e: + result = {"error": str(e), + "code": 500 + } + return result + def rpc_with_reply_processing(self, topic, content): req_id = self.__rpc_requests_in_progress[topic][0]["data"]["id"] device = self.__rpc_requests_in_progress[topic][0]["device"] diff --git a/thingsboard_gateway/gateway/tb_updater.py b/thingsboard_gateway/gateway/tb_updater.py index 3b661983..3739d4c6 100644 --- a/thingsboard_gateway/gateway/tb_updater.py +++ b/thingsboard_gateway/gateway/tb_updater.py @@ -21,10 +21,12 @@ from threading import Thread from time import time, sleep from simplejson import loads +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://127.0.0.1:8090" +UPDATE_SERVICE_BASE_URL = "https://updates.thingsboard.io" +# UPDATE_SERVICE_BASE_URL = "http://127.0.0.1:8090" class TBUpdater(Thread): @@ -66,8 +68,9 @@ class TBUpdater(Thread): if content is not None and content.get("updateAvailable", False): new_version = content["message"].replace("New version ", "").replace(" is available!", "") log.info(content["message"]) - if new_version != self.__version: - self.__version["latest_version"] = new_version + self.__version["latest_version"] = new_version + log.info("\n\n[===UPDATE===]\n\n New version %s is available! \n\n[===UPDATE===]\n", + self.__version["latest_version"]) except ConnectionRefusedError: log.warning("Cannot connect to the update service. PLease check your internet connection.") except Exception as e: @@ -89,6 +92,12 @@ class TBUpdater(Thread): } return request_args + def update(self): + if self.__version["latest_version"] is not None: + result = TBUtility.install_package("thingsboard-gateway", self.__version["latest_version"]) + else: + result = "Congratulations! You have the latest version." + return result if __name__ == '__main__': - updater = TBUpdater() \ No newline at end of file + updater = TBUpdater() diff --git a/thingsboard_gateway/tb_utility/tb_utility.py b/thingsboard_gateway/tb_utility/tb_utility.py index 26eb884a..ac418dab 100644 --- a/thingsboard_gateway/tb_utility/tb_utility.py +++ b/thingsboard_gateway/tb_utility/tb_utility.py @@ -154,8 +154,9 @@ class TBUtility: def install_package(package, version="upgrade"): from sys import executable from subprocess import check_call + result = False if version.lower() == "upgrade": - check_call([executable, "-m", "pip", "install", package, "--upgrade", "--user"]) + result = check_call([executable, "-m", "pip", "install", package, "--upgrade", "--user"]) else: from pkg_resources import get_distribution current_package_version = None @@ -164,4 +165,5 @@ class TBUtility: except Exception: pass if current_package_version is None or current_package_version != version: - check_call([executable, "-m", "pip", "install", package + "==" + version, "--user"]) + result = check_call([executable, "-m", "pip", "install", package + "==" + version, "--user"]) + return result