forked from LibreOffice/libresign
/
signd.py
executable file
·117 lines (91 loc) · 3.13 KB
/
signd.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
#!/usr/bin/python3
#
# libreoffice sign main program
#
# intended purpose:
# start libo controller
# detect network connection
# start and stop web server
#
import time, logging, signal, queue, os
import web, config
from playlist import Playlist
from request import Request
from locontrol import LibreOfficeController
class Sign():
def __init__(self):
self.running = True
self.messages = queue.Queue()
self.playlist = Playlist()
self.locontrol = LibreOfficeController(self)
# the interface we are using
self.net_iiface = ""
def network_found(self):
# logging.info("network found")
if not web.running:
web.start(self, self.messages)
self.locontrol.start_info_screen()
def network_lost(self):
# logging.info("network lost")
web.stop()
self.locontrol.stop_info_screen()
def check_interface (self, path):
# check if loopback (linux/include/linux/if_arp.h)
with open(path+'/type', 'r') as fd:
if int(fd.readline()) == 772:
return False
# TODO cable only? -- i think the purpose of the "cable only" idea
# was to make sure that the web control panel is only accessible
# when a person with physical access to the computer connects a
# cable
with open(path+'/carrier', 'r') as fd:
state = bool(int(fd.readline()))
return state
def poll_network (self):
state = False
for iface in os.listdir('/sys/class/net/'):
if os.path.isdir('/sys/class/net/'+iface):
state = self.check_interface('/sys/class/net/'+iface)
if state:
self.net_iface = iface
break
return state
def main(self):
while self.running:
if config.HTTP_CABLE_ONLY:
if self.poll_network():
self.network_found()
else:
self.network_lost()
else:
# repeated invocations do nothing
self.network_found()
# get requests from web control panel, 1 second timeout
try:
msg = self.messages.get(True, 1)
self.handle_web_request(msg)
except queue.Empty:
pass
self.locontrol.run()
self.network_lost()
def setup(self):
def sighand(signal, frame):
self.running = False
signal.signal(signal.SIGINT, sighand)
logging.basicConfig(level=logging.DEBUG)
self.playlist.load_files()
self.playlist.load_playlist()
self.main()
def handle_web_request(self, msg):
self.playlist.handle_web_request(msg)
self.locontrol.handle_web_request(msg)
logging.debug(msg)
# playlist info for the front-end
def get_playlist (self):
return self.playlist.playlist
# all files info for the front-end
def get_all_files (self):
return self.playlist.all_files
if __name__ == "__main__":
sign = Sign()
sign.setup()