def update_firmware(**kwargs): token = kwargs.get("token") remote_repository = kwargs.get("repository", None) mqtt_client = kwargs.get("mqtt_client", None) experimental_enabled = kwargs.get("experimental_enabled", False) def send_status(status): mqtt_client.publish( f"/device/{token}/hive", json.dumps({ "command": "update_status_firmware", "status": status })) send_status("git") if os.path.exists(LOCAL_FIRMWARE_FOLDER): os.system( f"cd {LOCAL_FIRMWARE_FOLDER} {'&& sudo git fetch && sudo git checkout dev' if experimental_enabled else ''} && sudo git pull" ) else: os.system( f"sudo git clone {remote_repository} {LOCAL_FIRMWARE_FOLDER} {'&& sudo git fetch && sudo git checkout dev' if experimental_enabled else ''}" ) latest_version = open( f"{LOCAL_FIRMWARE_FOLDER}/latest_version").read().strip() latest_firmware = f"{LOCAL_FIRMWARE_FOLDER}/sixfab_pms_firmware_{latest_version}.bin" try: current_firmware_version = get_metric("version") except: send_status("error") return #if current_firmware_version == latest_version[1:]: # send_status("finish") # return if not lock_distribution_service(): send_status("error") return if not os.path.exists(latest_firmware): send_status("firmware_not_exists") return hat_api = SixfabPower() try: last_step_cache = 0 for step in hat_api.update_firmware(latest_firmware): if last_step_cache < 100 and step - last_step_cache > 2: last_step_cache = step send_status(step) except: send_status("error") return send_status("finish") release_distribution_service()
#!/usr/bin/env python3 from power_api import SixfabPower, Definition import time import sys, os api = SixfabPower() firmware_path = "/opt/b0x/sixfab_pms_firmware_v0.3.3.bin" error_occured = 0 try: api.reset_mcu() api.reset_for_boot_update() time.sleep(5) for step in api.update_firmware(firmware_path): print(f"{step}%") except: error_occured = 1 print("raised error") finally: update_successful = 0 if error_occured is 0: counter = 0 for i in range(15): print('.', sep=' ', end=' ', flush=True) time.sleep(1)