-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSafeShutdown.py
88 lines (73 loc) · 2.15 KB
/
SafeShutdown.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import RPi.GPIO as GPIO
import os
import requests
import time
from multiprocessing import Process
import subprocess
#initialize pins
powerPin = 3 #pin 5
ledPin = 14 #TXD
resetPin = 2 #pin 13
powerenPin = 4 #pin 5
#initialize GPIO settings
def init():
GPIO.setmode(GPIO.BCM)
GPIO.setup(powerPin, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(resetPin, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(ledPin, GPIO.OUT)
GPIO.output(ledPin, GPIO.HIGH)
GPIO.setup(powerenPin, GPIO.OUT)
GPIO.output(powerenPin, GPIO.HIGH)
GPIO.setwarnings(False)
def api_request(command : str):
supervisor_address = os.environ.get('BALENA_SUPERVISOR_ADDRESS')
supervisor_api_key = os.environ.get('BALENA_SUPERVISOR_API_KEY')
uri = '{address}/v1/{cmd}?apikey={key}'
uri = uri.format(address=supervisor_address, cmd=command, key=supervisor_api_key)
headers = {'Content-Type': 'application/json'}
requests.post(uri, headers=headers)
def request_halt():
print('System halt')
api_request('halt')
def request_reboot():
print('System reboot')
api_request('reboot')
def request_shutdown():
print('System shutdown')
api_request('shutdown')
#waits for user to hold button up to 1 second before issuing poweroff command
def poweroff():
while True:
GPIO.wait_for_edge(powerPin, GPIO.FALLING)
request_shutdown()
#blinks the LED to signal button being pushed
def ledBlink():
while True:
GPIO.output(ledPin, GPIO.HIGH)
GPIO.wait_for_edge(powerPin, GPIO.FALLING)
start = time.time()
while GPIO.input(powerPin) == GPIO.LOW:
GPIO.output(ledPin, GPIO.LOW)
time.sleep(0.2)
GPIO.output(ledPin, GPIO.HIGH)
time.sleep(0.2)
#resets the pi
def reset():
while True:
#self.assertEqual(GPIO.input(resetPin), GPIO.LOW)
GPIO.wait_for_edge(resetPin, GPIO.FALLING)
request_reboot()
if __name__ == "__main__":
#initialize GPIO settings
init()
#create a multiprocessing.Process instance for each function to enable parallelism
powerProcess = Process(target = poweroff)
powerProcess.start()
ledProcess = Process(target = ledBlink)
ledProcess.start()
resetProcess = Process(target = reset)
resetProcess.start()
powerProcess.join()
ledProcess.join()
resetProcess.join()
GPIO.cleanup()