-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlisten-for-shutdown.py
More file actions
executable file
·48 lines (37 loc) · 1.46 KB
/
listen-for-shutdown.py
File metadata and controls
executable file
·48 lines (37 loc) · 1.46 KB
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
43
44
45
46
47
48
#!/usr/bin/env python3
import RPi.GPIO as GPIO
import subprocess
import os
import sys
pin=21 # GPIO number, not pin number
direction="FALLING"
if len(sys.argv) > 1:
pin=int(sys.argv[1])
print ("Pin {} from command line".format(str(pin)), flush=True)
elif "ENV_GPIO" in os.environ:
pin=int(os.getenv("ENV_GPIO"))
print ("Pin {} from environment".format(str(pin)), flush=True)
else:
print ("Using default pin {}".format(str(pin)), flush=True)
if len(sys.argv) > 2:
direction=sys.argv[2]
print ("Detect state {} from command line".format(direction), flush=True)
elif "ENV_STATE" in os.environ:
direction=os.getenv("ENV_STATE")
print ("Detect state {} environment".format(direction), flush=True)
else:
print ("Detect default state {}".format(direction), flush=True)
GPIO.setmode(GPIO.BCM)
if direction=="RISING":
print ("Waiting for RISING on GPIO pin {}".format(str(pin)), flush=True)
GPIO.setup(pin, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.wait_for_edge(pin, GPIO.RISING)
print ("Detected state RISING on pin {}".format(str(pin)), flush=True)
else:
print ("Waiting for FALLING on GPIO pin {}".format(str(pin)), flush=True)
GPIO.setup(pin, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.wait_for_edge(pin, GPIO.FALLING)
print ("Detected state FALLING on pin {}".format(str(pin)), flush=True)
print ("GPIO state detected. Shuting down", flush=True)
subprocess.call(['shutdown', '-h', 'now'], shell=False)
sys.exit(0)