-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbutton_01-ecmlink
executable file
·103 lines (81 loc) · 2.4 KB
/
button_01-ecmlink
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#!/usr/bin/env python3
# -*- coding:utf-8 -*-
import os
import sys
import time
import atexit
import argparse
import subprocess
from mydaemon import Daemon
import RPi.GPIO as GPIO
DAEMON_NAME = 'My Background Process (id: #ID#)'
DAEMON_STOP_TIMEOUT = 10
PIDFILE = '/tmp/myproc_#ID#.pid'
RUNFILE = '/tmp/myproc_#ID#.run'
DEBUG = 0
def button_callback(channel):
subprocess.call(["bash", "/opt/github/DSMPi/launcher_link.sh"])
def kickit():
gpio_pin = 13
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD)
GPIO.setup(gpio_pin, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.add_event_detect(gpio_pin,GPIO.RISING,callback=button_callback)
def get_args():
'''
>>> get_args()
('start', 5)
>>> get_args()
('stop', 4)
'''
try:
parser = argparse.ArgumentParser()
parser.add_argument('action', help='action',
choices=['start', 'stop', 'restart'])
parser.add_argument('uniqid', help='Unique ID')
args = parser.parse_args()
result = (args.action, args.uniqid)
except Exception as err:
if DEBUG:
raise
else:
sys.stderr.write('%s\n' % (err))
result = (None, None)
return result
# -----------------------------------------------------------------------------
class MyProc(Daemon):
def run(self):
kickit()
atexit.register(self.delrun)
n = 0
while os.path.exists(RUNFILE):
print('.')
n += 1
if (n > 60):
break
time.sleep(1)
# -----------------------------------------------------------------------------
if __name__ == '__main__':
try:
(action, uniqid) = get_args()
DAEMON_NAME = DAEMON_NAME.replace('#ID#', uniqid)
PIDFILE = PIDFILE.replace('#ID#', uniqid)
RUNFILE = RUNFILE.replace('#ID#', uniqid)
d = MyProc(name=DAEMON_NAME, pidfile=PIDFILE, runfile=RUNFILE,
stoptimeout=DAEMON_STOP_TIMEOUT, debug=DEBUG)
# Action requested.
if action == 'start':
d.start()
elif action == 'stop':
d.stop()
elif action == 'restart':
d.restart()
else:
raise NameError('Unknown action')
sys.exit(0)
except Exception as err:
if DEBUG:
raise
else:
sys.stderr.write('%s\n' % err)
sys.exit(1)