4802 |
jacho |
1 |
#!/usr/bin/python |
|
|
2 |
import wiringpi2 as gpio |
|
|
3 |
import time |
|
|
4 |
import subprocess |
|
|
5 |
|
|
|
6 |
power_sense_pin = 83 |
|
|
7 |
|
|
|
8 |
gpio.wiringPiSetupGpio() |
|
|
9 |
gpio.pinMode(power_sense_pin, 0) |
|
|
10 |
gpio.pullUpDnControl(power_sense_pin, 0) |
|
|
11 |
|
|
|
12 |
time.sleep(30) # wait for complete startup. |
|
|
13 |
|
|
|
14 |
oldstav = gpio.digitalRead(power_sense_pin) |
|
|
15 |
|
|
|
16 |
while True: |
|
|
17 |
stav = gpio.digitalRead(power_sense_pin) |
|
|
18 |
|
|
|
19 |
if oldstav == 0 and stav == 0: |
|
|
20 |
subprocess.call("shutdown -P 0", shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) |
|
|
21 |
exit(0) |
|
|
22 |
else: |
|
|
23 |
print "power is OK." |
|
|
24 |
|
|
|
25 |
oldstav = stav |
|
|
26 |
|
|
|
27 |
time.sleep(2) |
|
|
28 |
|
|
|
29 |
|