Created
August 8, 2019 08:09
-
-
Save ipconfiger/6c2cecedd401ba53c2c40797a9356770 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#coding=utf8 | |
import sys | |
import os | |
import time | |
import RPi.GPIO as GPIO | |
from fabric import SerialGroup as Group | |
GPIO.setmode(GPIO.BOARD) | |
GPIO.setup(40, GPIO.IN) | |
connect_kwargs = { | |
'key_filename': '/home/pi/.ssh/id_rsa' | |
} | |
class PowerState(object): | |
def __init__(self): | |
self.off_count = 0 | |
def update_count(self, state): | |
if state: | |
self.off_count = 0 | |
else: | |
self.off_count += 1 | |
#print "state:", state, "count:", self.off_count | |
if self.off_count > 60: | |
self.power_off() | |
def power_off(self): | |
#print "off!" | |
try: | |
Group( | |
'10.0.1.100', | |
'10.0.1.101', | |
'10.0.1.102', | |
user="root", | |
connect_kwargs=connect_kwargs | |
).run('halt -p') | |
except: | |
pass | |
os.system('halt -p') | |
def check_pin(): | |
return GPIO.input(40) | |
def main(): | |
powerstate = PowerState() | |
try: | |
while True: | |
powerstate.update_count(check_pin()) | |
time.sleep(1) | |
except KeyboardInterrupt: | |
GPIO.cleanup() | |
sys.exit(1) | |
if __name__=="__main__": | |
main() | |
GPIO.cleanup() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment