3 """Wrapper functions for calling tplink.py"""
11 from typing import Dict, Optional
14 from decorator_utils import timeout
16 logger = logging.getLogger(__name__)
19 @timeout(10.0, use_signals=False, error_message="Timed out waiting for tplink.py")
20 def tplink_command(command: str) -> bool:
21 result = os.system(command)
22 signal = result & 0xFF
24 msg = f'{command} died with signal {signal}'
26 logging_utils.hlog(msg)
29 exit_value = result >> 8
31 msg = f'{command} failed, exited {exit_value}'
33 logging_utils.hlog(msg)
35 logger.debug('%s succeeded.', command)
39 @timeout(10.0, use_signals=False, error_message="Timed out waiting for tplink.py")
40 def tplink_get_info(cmd: str) -> Optional[Dict]:
41 logger.debug('Getting tplink device status via "%s"', cmd)
43 out = subprocess.getoutput(cmd)
44 logger.debug('RAW OUT> %s', out)
45 out = re.sub("Sent:.*\n", "", out)
46 out = re.sub("Received: *", "", out)
47 info = json.loads(out)["system"]["get_sysinfo"]
48 logger.debug("%s", json.dumps(info, indent=4, sort_keys=True))
50 except Exception as e:
52 print(out, file=sys.stderr)