diff --git a/main.py b/main.py index 4fbd7af..92846e0 100644 --- a/main.py +++ b/main.py @@ -85,7 +85,7 @@ def network_check(): network_status = 0 -def wait_for_system_on(): +def wait_for_system_off(): global debiankammer_status global network_status global some_one_at_home @@ -95,7 +95,13 @@ def wait_for_system_on(): network_check() some_one_at_home = smartphones.check_online(ping_cycle) if network_status == 1: + if not debiankammer.check_autostart(): + debiankammer_status = 0 + else: + debiankammer_status = 1 + if some_one_at_home == 0: + logging_for_me("Nobody at home") logging_for_me(">>> System goes off >>>") logging_for_me("debian-kammer and stammen go down") debiankammer_status = debiankammer.off() @@ -112,7 +118,7 @@ def wait_for_system_on(): sleep(5) -def wait_for_system_off(): +def wait_for_system_on(): global debiankammer_status global network_status global some_one_at_home @@ -159,13 +165,13 @@ def system_start(): logging_for_me("Network status: OFF") if some_one_at_home == 1: - logging_for_me("System status: ON -> Someone at home") + logging_for_me("Someone at home") if debiankammer_status == 0: logging_for_me("debian-kammer is down") logging_for_me("debian-kammer goes on") debiankammer.on() else: - logging_for_me("debian-kammer is running") + logging_for_me("debian-kammer is already up") else: logging_for_me("System status: OFF -> Nobody at home") @@ -184,11 +190,11 @@ def system_start(): try: sleep(0.2) if some_one_at_home == 1: - wait_for_system_on() + wait_for_system_off() sleep(0.2) if some_one_at_home == 0: - wait_for_system_off() + wait_for_system_on() except Exception as e: logging.error(">>>Exception>>>") diff --git a/network/devices.py b/network/devices.py index 3803407..e61f1ab 100644 --- a/network/devices.py +++ b/network/devices.py @@ -53,9 +53,12 @@ def __init__(self, ip_address=None, rf_on=None, rf_off=None, username=None, maca self.status = 0 # on/off self.reboot_protection = 0 - self.missed = 1 # because of start log "pi3kitchen is up" + self.missed = 0 # because of start log "pi3kitchen is up" def check_online(self, ping_cycle=1): + """ + If missed goes in if condition. + """ if not isinstance(self.ip_address, list): if os.system("ping -c 1 -W 1 " + self.ip_address + " > /dev/null 2>&1"): @@ -93,11 +96,11 @@ def wake_on_lan(self): os.system("wakeonlan -i " + self.macaddress + " " + self.macaddress) """ # TODO: check wakeonlan works with sudo or not! + os.system("wakeonlan " + self.macaddress) logging_for_me("Send wake-on-LAN package to " + self.macaddress) - # os.system("wakeonlan " + self.macaddress) def shutdown_fn(self): - # os.system("/usr/bin/ssh -i ~/.ssh/id_rsa -l " + self.username + " " + self.ip_address) + os.system("/usr/bin/ssh -i ~/.ssh/id_rsa -l " + self.username + " " + self.ip_address) logging_for_me("Sent shutdown command to: " + self.ip_address) def on(self): @@ -138,27 +141,35 @@ def off(self, socket=0): sleep(0.1) return 0 - def check_autostart(self, ping_cycle=4): - + def check_autostart(self): + """ + If missed goes in if condition. + """ if os.system("ping -c 1 -W 1 " + self.ip_address + " > /dev/null 2>&1"): - self.missed += 1 self.reboot_protection += 1 - if self.reboot_protection == 5 or self.reboot_protection == 10 or self.reboot_protection > 120: + if self.reboot_protection == 2 or self.reboot_protection == 10: self.missed = 0 if self.reboot_protection > 120: # counter reset self.reboot_protection = 0 + # self.missed should stay after if conditions + self.missed += 1 + else: self.status = 1 self.missed = 0 - if self.missed == ping_cycle: - self.off() # pi3kitchen is off, should just electric off - sleep(3) - self.on() + if self.missed == 5: + if self.macaddress is not None: + self.wake_on_lan() + + if self.rf_off is not None: + self.off() # pi3kitchen is off, should just electric off + sleep(3) + self.on() + self.status = 1 - self.missed += 1 # so that not again goes in this loop in this loop return self.status