-
Notifications
You must be signed in to change notification settings - Fork 3
/
sid.py
executable file
·190 lines (157 loc) · 5.8 KB
/
sid.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#!/usr/bin/env python
#Original Code Martin Bateman 2013
#Modified by Simon Walters
#GPLv2 applies
#V0.54 28Oct13 for scratchgpio4
#V0.6 5Dec13 - change patterns to SID and OK and use os.system call to launch scratchgpio
#V0.7 28Dec13 - cope with plus version vs standard
import sys
from socket import *
import subprocess
#from subprocess import Popen, call
import shlex
import os
import sys
import time
import threading
import getopt
import shlex
class Blink(threading.Thread):
def __init__(self, on,off):
super(Blink, self).__init__()
self.on = on
self.off = off
self.terminated = False
self.toTerminate = False
self.sequence = [1,2,1,2,1,2]
def start(self):
self.thread = threading.Thread(None, self.run, None, (), {})
self.thread.start()
def run(self):
while self.toTerminate == False:
#print self.sequence
for i in range(len(self.sequence)):
if self.sequence[i] == 0:
#print self.sequence[i]
os.system("echo 0 >/sys/class/leds/led0/brightness")
time.sleep(self.off )
if self.sequence[i] == 1:
#print self.sequence[i]
os.system("echo 1 >/sys/class/leds/led0/brightness")
time.sleep(self.on )
os.system("echo 0 >/sys/class/leds/led0/brightness")
time.sleep(self.off )
if self.sequence[i] == 2:
#print self.sequence[i]
os.system("echo 1 >/sys/class/leds/led0/brightness")
time.sleep(3 * self.on )
os.system("echo 0 >/sys/class/leds/led0/brightness")
time.sleep(self.off )
self.terminated = True
def stop(self):
self.toTerminate = True
while self.terminated == False:
# Just wait
time.sleep(0.01)
def set_delays(self,on,off):
self.on = on
self.off = off
def set_sequence(self,sequence):
self.sequence = sequence
def getserial():
# Extract serial from cpuinfo file
cpuserial = "0000000000000000"
try:
f = open('/proc/cpuinfo','r')
for line in f:
if line[0:6]=='Serial':
cpuserial = line[10:26]
f.close()
except:
cpuserial = "ERROR000000000"
return cpuserial
myserial = getserial()
print myserial
print myserial[-4:]
if __name__ == "__main__":
timeout = '300'
try:
opts, args = getopt.getopt(sys.argv[1:],"ht:",["timeout="])
except getopt.GetoptError:
print 'sudo sid.py -t <timeout>'
sys.exit(2)
for opt, arg in opts:
if opt == '-h':
print 'sudo sid.py -t <timeout>'
sys.exit()
elif opt in ("-t", "--timeout"):
timeout = arg
timeout = timeout.strip()
print 'Timeout is:', timeout
s = socket(AF_INET, SOCK_DGRAM)
s.bind(('', 50000))
s.settimeout(int(float(timeout)))
os.system("echo none >/sys/class/leds/led0/trigger")
blinkthread = Blink(0.2,0.2)
blinkthread.set_sequence([1,1,1,0,0,0,1,1,0,0,0,2,1,1,0,0,0,0,0])
blinkthread.start()
#blinkthread.join()
while 1:
try:
#print "try reading socket"
os.system("echo 1 >/sys/class/leds/led0/brightness")
data, wherefrom = s.recvfrom(1500, 0) # get the data from the socket
except (KeyboardInterrupt, SystemExit):
print "Program Ending - please wait a few secs"
break
except timeout:
print "No data received: socket timeout"
#print sys.exc_info()[0]
break
except:
print "Unknown error occured with receiving data"
break
data = data.lower()
print (data + " " + repr(wherefrom[0]))
if (data.find("start sid" + myserial[-4:]) != -1):
os.system('sudo pkill -f scratchgpio')
os.system('sudo python /home/pi/scratchgpio4/scratchgpio_handler4.py '+ str(repr(wherefrom[0])) +' standard &')
#print shlex.split("""x-terminal-emulator -e 'bash -c "sudo python /home/pi/simplesi_scratch_handler/scratch_gpio_handler2.py """ + str(repr(wherefrom[0])) + """"'""")
#process = subprocess.Popen(shlex.split("""x-terminal-emulator -e 'bash -c "sudo python /home/pi/scratchgpio4/scratchgpio_handler4.py """ + str(repr(wherefrom[0])) + """"'"""), stdout=subprocess.PIPE)
print "stop blinking"
# try:
# blinkthread.stop()
# print "blinking stopped"
# except:
# pass
# print "restart blinking"
blinkthread.set_sequence([2,2,2,0,0,0,2,1,2,0,0,0,0,0,0,0,0,0,0])
#blinkthread.start()
time.sleep(30)
break
if (data.find("start sidplus" + myserial[-4:]) != -1):
os.system('sudo pkill -f scratchgpio')
os.system('sudo python /home/pi/scratchgpio4/scratchgpio_handler4.py '+ str(repr(wherefrom[0])) +' &')
#print shlex.split("""x-terminal-emulator -e 'bash -c "sudo python /home/pi/simplesi_scratch_handler/scratch_gpio_handler2.py """ + str(repr(wherefrom[0])) + """"'""")
#process = subprocess.Popen(shlex.split("""x-terminal-emulator -e 'bash -c "sudo python /home/pi/scratchgpio4/scratchgpio_handler4.py """ + str(repr(wherefrom[0])) + """"'"""), stdout=subprocess.PIPE)
print "stop blinking"
# try:
# blinkthread.stop()
# print "blinking stopped"
# except:
# pass
# print "restart blinking"
blinkthread.set_sequence([2,2,2,0,0,0,2,1,2,0,0,0,0,0,0,0,0,0,0])
#blinkthread.start()
time.sleep(30)
break
try:
blinkthread.stop()
except:
pass
time.sleep(1)
os.system("echo 0 >/sys/class/leds/led0/brightness")
time.sleep(1)
os.system("echo mmc0 >/sys/class/leds/led0/trigger")
s.close()
sys.exit()