-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpictrl.py
More file actions
executable file
·385 lines (339 loc) · 10.2 KB
/
pictrl.py
File metadata and controls
executable file
·385 lines (339 loc) · 10.2 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
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
#!/usr/bin/env python2
import functools
import pyudev
import signal
import sys
import RPi.GPIO as GPIO
import threading
import time
import subprocess
import os
import shutil
import datetime
import logging
import traceback
# --- constants ---
GpioNextBtn = 15
GpioPauseBtn = 17
GpioPrevBtn = 18
GpioLed = 11
GpioPowerOut = 25
DiskBkp = 'rpi_trip'
FotoDir = 'photos'
UmountScript = '/usr/local/sbin/udev-auto-umount.sh'
IgnoredPartitions = ['boot']
# --- log mgnt ---
logger = None
def logError(errType, error, trace):
global logger
logger.error(error.message+"\n"+"\n\t".join(traceback.format_tb(trace)))
def daemonize():
fileScript = os.path.basename(__file__).replace('.py', '', 1)
sys.excepthook = logError
if os.path.exists('/var/run/'+fileScript+'.pid'):
sys.stderr.write("pid file already exists, abording\n")
sys.exit(1)
os.chdir("/")
sys.stdout.flush()
sys.stderr.flush()
si = file('/dev/null', 'r')
so = file('/dev/null', 'a+')
se = file('/dev/null', 'a+')
os.dup2(si.fileno(), sys.stdin.fileno())
os.dup2(so.fileno(), sys.stdout.fileno())
os.dup2(se.fileno(), sys.stderr.fileno())
pid = os.fork()
if pid > 0:
sys.exit(0)
pidfile = file('/var/run/'+fileScript+'.pid', 'w')
pidfile.write(str(os.getpid()))
pidfile.write("\n")
pidfile.close()
def delPidFile():
fileScript = os.path.basename(__file__).replace('.py', '', 1)
if os.path.exists('/var/run/'+fileScript+'.pid'):
os.remove('/var/run/'+fileScript+'.pid')
# --- app starting ---
if "--daemon" in sys.argv:
fileScript = os.path.basename(__file__).replace('.py', '', 1)
logging.basicConfig(filename='/var/log/'+fileScript+'.log', filemode='a',
format='%(asctime)s: %(levelname)s: %(message)s',
level=logging.INFO)
logger = logging.getLogger()
daemonize()
else:
logging.basicConfig(format='%(message)s', level=logging.INFO)
logger = logging.getLogger()
logger.info('Starting...')
lock = threading.Lock()
bigDisk = False
sdCard = False
copying = False
sdCardName = None
class LedThread(threading.Thread):
def __init__(self, pin):
threading.Thread.__init__(self)
self._time = -1
self._on = False
self._pin = pin
self._stop = False
GPIO.setmode(GPIO.BCM)
GPIO.setup(pin, GPIO.OUT)
self._updateEvent = threading.Event( )
def _isFinished(self):
global lock
lock.acquire()
finish = self._stop
lock.release()
return finish
def run(self):
while not self._isFinished():
self._on = not self._on
GPIO.output(self._pin, self._time == -1 or self._on)
if(self._time == -1):
self._updateEvent.wait()
else:
self._updateEvent.wait(self._time)
self._updateEvent.clear()
def stop(self):
global lock
lock.acquire()
self._stop = True
lock.release()
self._updateEvent.set()
def update(self, time):
global lock
lock.acquire()
self._time = time
lock.release()
self._updateEvent.set()
class CopyThread(threading.Thread):
def __init__(self, devName):
threading.Thread.__init__(self)
self._dev = devName
self._stopEvent = threading.Event( )
def run(self):
global lock
global copying
global sdCard
global bigDisk
global logger
lock.acquire()
copying = True
lock.release()
onChange()
context = pyudev.Context()
for device in context.list_devices(subsystem='block', DEVTYPE='partition'):
if device.get('ID_FS_LABEL_ENC') == DiskBkp:
df = subprocess.Popen(["/bin/df", device.get('DEVNAME')], stdout=subprocess.PIPE)
output = df.communicate()[0]
destSpace = int(output.split("\n")[1].split()[3])
if device.get('ID_FS_LABEL_ENC') == self._dev:
srcName = device.get('DEVNAME')
df = subprocess.Popen(["/bin/df", srcName], stdout=subprocess.PIPE)
output = df.communicate()[0]
srcSize = int(output.split("\n")[1].split()[2])
if srcSize > destSpace:
logger.warning("Not enought space! Unable to save disk")
lock.acquire()
copying = False
lock.release()
onChange()
return;
tstamp = os.path.getmtime('/media/'+self._dev)
fileList = os.listdir('/media/'+self._dev)
for file in fileList:
if file[0] != '.':
newtstamp = os.path.getmtime('/media/'+self._dev+'/'+file)
if newtstamp > tstamp:
tstamp = newtstamp
fileDate = datetime.date.fromtimestamp(tstamp)
dirName = fileDate.strftime('%d_%m_%Y')
if os.path.exists('/media/'+DiskBkp+'/'+FotoDir+'/'+dirName):
i = 2
newDir = dirName + ' ('+str(i)+')'
while os.path.exists('/media/'+DiskBkp+'/'+FotoDir+'/'+newDir):
++i
newDir = dirName + ' ('+str(i)+')'
dirName = newDir
logger.info('Copying '+ self._dev + ' to ' + dirName)
shutil.copytree('/media/'+self._dev, '/media/'+DiskBkp+'/'+FotoDir+'/'+dirName)
lock.acquire()
copying = False
sdCard = False
lock.release()
logger.info("Unmounting "+ srcName)
subprocess.call([UmountScript, srcName], shell=False)
onChange()
def stop(self):
self._stopEvent.set()
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
def onAdd(devName):
global sdCard
global bigDisk
global led
global sdCardName
if devName == DiskBkp:
lock.acquire()
bigDisk = True
lock.release()
onChange()
elif (devName not in IgnoredPartitions) and (devName != None):
time.sleep(0.5)
if(os.path.exists('/media/'+devName)):
lock.acquire()
sdCard = True
sdCardName = devName
lock.release()
onChange()
def onRemove(devName):
global sdCard
global bigDisk
global led
if devName == DiskBkp:
lock.acquire()
bigDisk = False
lock.release()
onChange()
elif (devName not in IgnoredPartitions) and (devName != None):
lock.acquire()
sdCard = False
lock.release()
onChange()
def onChange():
global sdCard
global bigDisk
global led
global copying
global lock
lock.acquire()
if(bigDisk and sdCard):
if copying:
lock.release()
led.update(0.1)
else:
lock.release()
led.update(0.5)
else:
lock.release()
led.update(-1)
led = LedThread(GpioLed)
led.start()
context = pyudev.Context()
monitor = pyudev.Monitor.from_netlink(context)
monitor.filter_by('block')
for device in context.list_devices(subsystem='block', DEVTYPE='partition'):
onAdd(device.get('ID_FS_LABEL_ENC'))
if not bigDisk:
logger.warning('Main hard drive is not connected')
GPIO.setup(GpioNextBtn, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(GpioPauseBtn, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(GpioPrevBtn, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(GpioPowerOut, GPIO.IN, pull_up_down=GPIO.PUD_UP)
def evt_udev(action, device):
if 'ID_FS_TYPE' in device:
if action == 'add':
onAdd(device.get('ID_FS_LABEL_ENC'))
else:
onRemove(device.get('ID_FS_LABEL_ENC'))
observer = pyudev.MonitorObserver(monitor, evt_udev)
observer.start()
def onBtn1(arg):
global bigDisk
global sdCard
global copying
global sdCardName
global lock
lock.acquire()
if bigDisk == False:
lock.release()
return;
if sdCard and (copying == False):
lock.release()
cp = CopyThread(sdCardName)
cp.start()
else:
lock.release()
subprocess.call(['/usr/bin/mpc', 'toggle', '--quiet'], shell=False)
def onBtn2(arg):
global bigDisk
global sdCard
global copying
global sdCardName
global lock
lock.acquire()
if bigDisk == False:
lock.release()
return;
if sdCard and (copying == False):
lock.release()
cp = CopyThread(sdCardName)
cp.start()
else:
lock.release()
subprocess.call(['/usr/bin/mpc', 'next', '--quiet'], shell=False)
def onBtn3(arg):
global bigDisk
global sdCard
global copying
global sdCardName
global lock
lock.acquire()
if bigDisk == False:
lock.release()
return;
if sdCard and (copying == False):
lock.release()
cp = CopyThread(sdCardName)
cp.start()
else:
lock.release()
subprocess.call(['/usr/bin/mpc', 'prev', '--quiet'], shell=False)
def cleanAll():
global observer
global led
global logger
global GpioPauseBtn
global GpioNextBtn
global GpioPrevBtn
global GpioPowerOut
led.stop()
GPIO.remove_event_detect(GpioPauseBtn)
GPIO.remove_event_detect(GpioNextBtn)
GPIO.remove_event_detect(GpioPrevBtn)
GPIO.remove_event_detect(GpioPowerOut)
GPIO.output(GpioLed, False)
observer.stop()
if "--daemon" in sys.argv:
delPidFile()
def onPowerOff2(arg):
global observer
global led
global logger
global GpioPowerOut
logger.info('Power off2')
GPIO.remove_event_detect(GpioPowerOut)
def onPowerOff(arg):
global observer
global led
global logger
global GpioPowerOut
logger.info('Power off')
cleanAll()
cmd = subprocess.Popen(["/sbin/poweroff"], stdout=subprocess.PIPE)
output = cmd.communicate()[0]
sys.exit(0)
def exitBySignal(signum = None, frame = None):
logger.info('Signal received, exiting')
# cleanAll()
# sys.exit(0)
for sig in [signal.SIGTERM, signal.SIGINT, signal.SIGHUP, signal.SIGQUIT]:
signal.signal(sig, exitBySignal)
GPIO.add_event_detect(GpioPauseBtn, GPIO.RISING, callback = onBtn1, bouncetime = 300)
GPIO.add_event_detect(GpioNextBtn, GPIO.RISING, callback = onBtn2, bouncetime = 300)
GPIO.add_event_detect(GpioPrevBtn, GPIO.RISING, callback = onBtn3, bouncetime = 300)
GPIO.add_event_detect(GpioPowerOut, GPIO.RISING, callback = onPowerOff, bouncetime = 300)
if "--daemon" not in sys.argv:
logger.info('Press Ctrl-C to quit')
signal.pause()