from sys import stderr
from subprocess import run
from time import sleep
import json
from time import time
from hashlib import sha256
import hmac
from base64 import b64encode
from uuid import uuid4
from typing import Dict
from requests import request
def eprint(msg: str) -> None:
print(msg, file=stderr)
#
# switchbot
#
token: str = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
secret: str = "yyyyyyyyyyyyyyyyyyyyyyyyyyyy"
devid: str = "zzzzzzz"
ip: str = "000.000.000.000"
def build_header() -> Dict[str, str]:
global token, secret
nonce: str = uuid4()
stamp: int = int(round(time() * 1000))
data: str = f"{token}{stamp}{nonce}"
data_b: bytes = bytes(data, "utf-8")
secret_b = bytes(secret, "utf-8")
sign = b64encode(hmac.new(secret_b, msg=data_b, digestmod=sha256).digest())
return {
"Authorization": token,
"Content-Type": "application/json",
"charset": "utf8",
"t": str(stamp),
"sign": sign,
"nonce": str(nonce),
}
def power_on() -> None:
global devid
request(
"POST",
f"https://api.switch-bot.com/v1.1/devices/{devid}/commands",
headers=build_header(),
data=json.dumps({"commandType": "command", "command": "turnOn", "parameter": "default"}),
)
def power_off() -> None:
global devid
request(
"POST",
f"https://api.switch-bot.com/v1.1/devices/{devid}/commands",
headers=build_header(),
data=json.dumps({"commandType": "command", "command": "turnOff", "parameter": "default"}),
)
def is_alive() -> bool:
result: str = run(f"ping {ip} -n 1 -w 10000", shell=True, text=True, capture_output=True).stdout
return "100% の損失" not in result
def samba_on() -> None:
# if NAS is powered off, boot
if not is_alive():
eprint("NAS is shutdowned. Re-power with PlugMini")
power_off()
power_on()
eprint("NAS has been rebooted")
eprint("waiting...")
sleep(300)
eprint("NAS has been (re)booted")
while True:
if is_alive():
break
eprint("NAS is powered ON now")
if __name__ == "__main__":
samba_on()
input("Press Any Key...")