-
Notifications
You must be signed in to change notification settings - Fork 0
/
picam-stream.py
executable file
·66 lines (45 loc) · 1.77 KB
/
picam-stream.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
#!/usr/bin/python3
import json
import signal
import logging
from observer import Observer
from streamer import Streamer
class Main:
def __init__(self):
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s %(threadName)s\t%(levelname)-8s\t%(message)s')
signal.signal(signal.SIGINT, self.exit)
signal.signal(signal.SIGTERM, self.exit)
self.is_restarted = False
self.config = self.load_config()
host = self.config['host']
stream_token = self.config['stream_token']
stream_config = self.config['stream_config']
self.previous_status = ""
logging.info('### PICAM-STREAM ###')
logging.info('streaming to \'{}\''.format(host))
self.s = Streamer(host, stream_token, stream_config)
self.o = Observer(host, self.observer_event_handler)
def load_config(self, config_path='./config.json'):
with open(config_path, 'r') as f:
config = json.load(f)
return config
def observer_event_handler(self, status):
if self.previous_status != status: # status has changed
logging.debug('observer reported status \'{}\''.format(status))
self.previous_status = status
if status in ['disconnected', 'stopped', 'error']:
if not self.is_restarted:
logging.warning('(re)starting stream ...'.format(status))
self.s.restart_stream()
self.is_restarted = True
else:
self.is_restarted = False
def start(self):
self.o.start()
def exit(self, signum, frame):
logging.debug('SIGTERM was sent, exiting')
self.o.stop()
self.s.stop_stream()
logging.info('bye!')
if __name__ == '__main__':
Main().start()