-
Notifications
You must be signed in to change notification settings - Fork 1
/
power_monitor.py
executable file
·42 lines (39 loc) · 1.42 KB
/
power_monitor.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#!/usr/bin/python
import os
import syslog
import time
import RPi.GPIO as GPIO
# Number of minutes to wait before shutting down
SHUTDOWN_GRACE = 2
# Pin connected to power detect line
POWER_PIN = 17
GPIO.setmode(GPIO.BCM)
GPIO.setup(POWER_PIN,GPIO.IN)
try:
POWER_AT_START = GPIO.input(POWER_PIN)
if not POWER_AT_START:
syslog.syslog("Power detection line isn't connected. Will monitor for connection.")
GPIO.wait_for_edge(POWER_PIN, GPIO.RISING)
POWER_AT_START = True
syslog.syslog("Power status line detected. Monitoring for power interruptions....")
while POWER_AT_START:
GPIO.wait_for_edge(POWER_PIN, GPIO.FALLING)
# The next 2 lines weed out false positives and chatter.
time.sleep(5)
if not GPIO.input(POWER_PIN):
out = "Power disconnected. Waiting "+str(SHUTDOWN_GRACE)+" minutes until shutting down."
syslog.syslog(syslog.LOG_ALERT, out)
os.system("echo \""+out+"\" | wall")
time_power_lost = time.time()
while not GPIO.input(POWER_PIN):
if time.time() > time_power_lost+SHUTDOWN_GRACE*60:
os.system("shutdown -h now")
time.sleep(60)
time.sleep(1)
time_left = time_power_lost+SHUTDOWN_GRACE*60-time.time()
out = "Power connected. Aborting countdown with "+str(time_left)+" seconds to spare."
syslog.syslog(syslog.LOG_ALERT, out)
os.system("echo \""+out+"\" | wall")
syslog.syslog("Power detection line isn't connected. Aborting power monitor.")
finally:
GPIO.cleanup()