Skip to content

Commit d840bf8

Browse files
authored
[Flashscript] version 2.5.6 (#550)
* tweak wait_for_reboot for stock firmware * add ability to append logging messages. * [FlashScript] version 2.5.6
1 parent 97fb784 commit d840bf8

File tree

1 file changed

+51
-23
lines changed

1 file changed

+51
-23
lines changed

tools/flash-shelly.py

Lines changed: 51 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,28 @@
8080
import time
8181
import zipfile
8282

83+
class MFileHandler(logging.FileHandler):
84+
"""Handler that controls the writing of the newline character"""
85+
special_code = '[!n]'
86+
def emit(self, record) -> None:
87+
if self.special_code in record.msg:
88+
record.msg = record.msg.replace( self.special_code, '' )
89+
self.terminator = ''
90+
else:
91+
self.terminator = '\n'
92+
return super().emit(record)
93+
94+
class MStreamHandler(logging.StreamHandler):
95+
"""Handler that controls the writing of the newline character"""
96+
special_code = '[!n]'
97+
def emit(self, record) -> None:
98+
if self.special_code in record.msg:
99+
record.msg = record.msg.replace( self.special_code, '' )
100+
self.terminator = ''
101+
else:
102+
self.terminator = '\n'
103+
return super().emit(record)
104+
83105
logging.TRACE = 5
84106
logging.addLevelName(logging.TRACE, 'TRACE')
85107
logging.Logger.trace = functools.partialmethod(logging.Logger.log, logging.TRACE)
@@ -589,7 +611,7 @@ def write_hap_setup_code(wifi_ip, hap_setup_code):
589611

590612
def wait_for_reboot(device_info, preboot_uptime=-1, reboot_only=False):
591613
logger.debug(f"{PURPLE}[Wait For Reboot]{NC}")
592-
logger.info(f"waiting for {device_info.friendly_host} to reboot...")
614+
logger.info(f"waiting for {device_info.friendly_host} to reboot[!n]")
593615
onlinecheck = None
594616
info = None
595617
time.sleep(1) # wait for time check to fall behind
@@ -598,25 +620,30 @@ def wait_for_reboot(device_info, preboot_uptime=-1, reboot_only=False):
598620
n = 1
599621
logger.trace(f'check1: {current_uptime > preboot_uptime}')
600622
logger.trace(f'check2: {n < 60}')
601-
while reboot_only == False and (current_uptime == -1 or current_uptime > preboot_uptime) and n < 60 and onlinecheck == None:
602-
logger.trace(f'current_uptime: {current_uptime}')
603-
logger.trace(f'preboot_uptime: {preboot_uptime}')
604-
605-
logger.trace(f'check1: {current_uptime > preboot_uptime}')
606-
logger.trace(f'check2: {n < 60}')
607-
if n == 15:
608-
logger.info(f"still waiting for {device_info.friendly_host} to reboot...")
609-
elif n == 30:
610-
logger.info(f"we'll wait just a little longer for {device_info.friendly_host} to reboot...")
611-
time.sleep(1) # wait 1 second befor retrying.
612-
current_uptime = device_info.get_uptime(True)
613-
logger.trace(f"current_uptime: {current_uptime}")
614-
n += 1
615-
logger.trace(f"n: {n}")
616-
onlinecheck = device_info.get_current_version(is_flashing=True)
617-
logger.debug(f"onlinecheck {onlinecheck}")
618-
while reboot_only and device_info.get_uptime(True) < 3:
619-
time.sleep(1) # wait 1 second befor retrying.
623+
if reboot_only == False:
624+
while current_uptime > preboot_uptime and n < 60 or onlinecheck == None:
625+
logger.info(f"...[!n]")
626+
logger.trace(f'current_uptime: {current_uptime}')
627+
logger.trace(f'preboot_uptime: {preboot_uptime}')
628+
logger.trace(f'check3: {current_uptime > preboot_uptime}')
629+
logger.trace(f'check4: {n < 60}')
630+
if n == 15:
631+
logger.info(f"\nstill waiting for {device_info.friendly_host} to reboot[!n]")
632+
elif n == 30:
633+
logger.info(f"\nwe'll wait just a little longer for {device_info.friendly_host} to reboot[!n]")
634+
time.sleep(1) # wait 1 second befor retrying.
635+
current_uptime = device_info.get_uptime(True)
636+
logger.trace(f"current_uptime: {current_uptime}")
637+
n += 1
638+
logger.trace(f"n: {n}")
639+
onlinecheck = device_info.get_current_version(is_flashing=True)
640+
logger.debug(f"onlinecheck {onlinecheck}")
641+
else:
642+
while device_info.get_uptime(True) < 3:
643+
time.sleep(1) # wait 1 second befor retrying.
644+
logger.info(f"")
645+
logger.trace(f'current_uptime: {current_uptime}')
646+
logger.trace(f'preboot_uptime: {preboot_uptime}')
620647
return onlinecheck
621648

622649
def write_flash(device_info):
@@ -938,6 +965,7 @@ def probe_device(device, action, dry_run, quiet_run, silent_run, mode, exclude,
938965
elif got_info:
939966
parse_info(deviceinfo, action, dry_run, quiet_run, silent_run, flashmode, exclude, hap_setup_code, requires_upgrade, network_type, ipv4_ip, ipv4_mask, ipv4_gw, ipv4_dns)
940967
if requires_upgrade:
968+
time.sleep(10) # need to allow time for previous flash reboot to fully boot.
941969
requires_upgrade = 'Done'
942970
deviceinfo.get_info()
943971
if deviceinfo.flash_fw_version != '0.0.0' and not is_newer(deviceinfo.flash_fw_version, deviceinfo.fw_version):
@@ -1026,11 +1054,11 @@ def device_scan(hosts, action, dry_run, quiet_run, silent_run, mode, type, exclu
10261054
args.mode = 'stock' if args.mode == 'revert' else args.mode
10271055
args.hap_setup_code = f"{args.hap_setup_code[:3]}-{args.hap_setup_code[3:-3]}-{args.hap_setup_code[5:]}" if args.hap_setup_code and '-' not in args.hap_setup_code else args.hap_setup_code
10281056

1029-
sh = logging.StreamHandler()
1057+
sh = MStreamHandler()
10301058
sh.setFormatter(logging.Formatter('%(message)s'))
10311059
sh.setLevel(log_level[args.verbose])
10321060
if args.log_filename:
1033-
fh = logging.FileHandler(args.log_filename, mode='w', encoding='utf-8')
1061+
fh = MFileHandler(args.log_filename, mode='w', encoding='utf-8')
10341062
fh.setFormatter(logging.Formatter('%(asctime)s %(levelname)s %(lineno)d %(message)s'))
10351063
fh.setLevel(log_level[args.verbose])
10361064
logger.addHandler(fh)
@@ -1056,7 +1084,7 @@ def device_scan(hosts, action, dry_run, quiet_run, silent_run, mode, type, exclu
10561084

10571085
homekit_release_info = None
10581086
stock_release_info = None
1059-
app_version = "2.5.5"
1087+
app_version = "2.5.6"
10601088

10611089
logger.debug(f"OS: {PURPLE}{arch}{NC}")
10621090
logger.debug(f"app_version: {app_version}")

0 commit comments

Comments
 (0)