-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
320 lines (264 loc) · 9.87 KB
/
server.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
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
import os
import sys
import threading
import cherrypy
from ipaddress import IPv4Interface
from jinja2 import Environment
from jinja2 import FileSystemLoader
import netifaces
import dbus
import dbus.mainloop.glib
from gi.repository import GLib
current_dir = os.path.dirname(os.path.abspath(__file__))
class BlueZDbus(cherrypy.process.plugins.SimplePlugin):
def __init__(self, bus):
cherrypy.process.plugins.SimplePlugin.__init__(self, bus)
# Get the system bus
try:
dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
self._system_bus = dbus.SystemBus()
except Exception as ex:
cherrypy.log(
'Unable to get the system dbus: "{}".'.format(ex.message))
cherrypy.log('Exiting. Is dbus running?')
sys.exit(1)
self._mainloop = GLib.MainLoop()
self.adapter_path = None
self.adapter = None
class Adapter:
def __init__(self, system_bus, path):
self._interface = dbus.Interface(
system_bus.get_object('org.bluez', path),
'org.freedesktop.DBus.Properties')
@property
def address(self):
val = self._interface.Get('org.bluez.Adapter1', 'Address')
return val
@property
def powered(self):
_val = self._interface.Get('org.bluez.Adapter1', 'Powered')
return _val == dbus.Boolean(True)
@powered.setter
def powered(self, val):
_val = dbus.Boolean(val)
self._interface.Set('org.bluez.Adapter1', 'Powered', _val)
class Device:
def __init__(self, system_bus):
self._manager = dbus.Interface(
system_bus.get_object('org.bluez', '/'),
'org.freedesktop.DBus.ObjectManager')
self._system_bus = system_bus
self._system_bus.add_signal_receiver(
self._interfaces_added,
dbus_interface='org.freedesktop.DBus.ObjectManager',
signal_name='InterfacesAdded')
self._system_bus.add_signal_receiver(
self._properties_changed,
dbus_interface='org.freedesktop.DBus.Properties',
signal_name='PropertiesChanged',
arg0='org.bluez.Device1',
path_keyword='path')
def _print_properties(self, path, properties):
cherrypy.log('Path {}'.format(path))
for key in properties.keys():
value = properties[key]
if (key == 'Class'):
cherrypy.log(' %s = 0x%06x' % (key, value))
else:
cherrypy.log(' %s = %s' % (key, value))
def devices(self):
cherrypy.log('Devices')
_devices = []
obj = self._manager.GetManagedObjects()
for path, interface in obj.items():
if 'org.bluez.Device1' not in interface:
continue
properties = interface['org.bluez.Device1']
self._print_properties(path, properties)
_device = {
'Name': properties['Name'],
'Address': properties['Address'],
'Connected': properties['Connected'],
'path': path,
}
_devices.append(_device)
return _devices
def _interfaces_added(self, path, interfaces):
if 'org.bluez.Device1' not in interfaces:
return
properties = interfaces['org.bluez.Device1']
cherrypy.log('Interfaces added')
self._print_properties(path, properties)
def _properties_changed(self, interface, changed, invalidated, path):
if interface != 'org.bluez.Device1':
return
cherrypy.log('Properties changed')
self._print_properties(path, changed)
def connect(self, path):
cherrypy.log('Connect to {}'.format(path))
interface = dbus.Interface(
self._system_bus.get_object('org.bluez', path),
'org.bluez.Device1')
interface.Connect()
def disconnect(self, path):
cherrypy.log('Disconnect {}'.format(path))
interface = dbus.Interface(
self._system_bus.get_object('org.bluez', path),
'org.bluez.Device1')
interface.Disconnect()
def _run(self):
cherrypy.log('Dbus mainloop.run')
try:
self._mainloop.run()
except KeyboardInterrupt:
pass
def start(self):
if self.adapter is None:
self.adapter = BlueZDbus.Adapter(
self._system_bus, self.adapter_path)
self.device = BlueZDbus.Device(self._system_bus)
self._thread = threading.Thread(target=self._run)
self._thread.start()
def stop(self):
self._mainloop.quit()
cherrypy.log('Dbus mainloop.quit')
class Poweroff(cherrypy.process.plugins.SimplePlugin):
def __init__(self, bus):
cherrypy.process.plugins.SimplePlugin.__init__(self, bus)
self._thread = None
self._restart = False
self._time_delay = 10
@property
def restart(self):
return self._restart
@restart.setter
def restart(self, val):
self._restart = val
@property
def time_delay(self):
return self._time_delay
@time_delay.setter
def time_delay(self, val):
self._time_delay = val
def _run(self):
action = 'poweroff'
if self._restart:
action = 'reboot'
os.system('sudo /bin/systemctl {}'.format(action))
def delay_start(self):
if self._thread and self._thread.is_alive():
raise cherrypy.HTTPError(429, 'Too Many Requests')
self._thread = threading.Timer(self._time_delay, self._run)
self._thread.start()
def stop(self):
if self._thread and self._thread.is_alive():
self._thread.cancel()
self._thread = None
def is_alive(self):
return self._thread.is_alive()
class Template:
def __init__(self):
self.env = None
cherrypy.config.namespaces['template'] = self._namespace
def _namespace(self, k, v):
if k == 'dir':
self.env = Environment(loader=FileSystemLoader(v))
class Root:
def __init__(self, template):
self._template = template
self._bluez = cherrypy.engine.bluez_dbus
def _addresses(self):
addrs = []
for iface in netifaces.interfaces():
ip4addrs = []
for inet in netifaces.ifaddresses(iface)[netifaces.AF_INET]:
ip4addrs.append(IPv4Interface(
'{addr}/{netmask}'.format(**inet)).with_prefixlen)
addrs.append({'interface': iface, 'ipv4': ','.join(ip4addrs)})
return addrs
def _dhcp_leases(self):
addrs = []
with open('/var/lib/misc/dnsmasq.leases') as f:
for l in f.readlines():
e, m, a, n, i = l.split()
addrs.append({'ipv4': a, 'name': n})
return addrs
@cherrypy.expose
def index(self):
return self._template.env.get_template(
'index.html'
).render(
nodename=os.uname().nodename,
addrs=self._addresses(),
dhcp_leases=self._dhcp_leases(),
bluetooth_powered=self._bluez.adapter.powered,
)
def _poweroff(self, restart):
cherrypy.engine.poweroff.restart = restart
cherrypy.engine.poweroff.delay_start()
return self._template.env.get_template(
'restart.html' if restart else 'poweroff.html',
).render(
nodename=os.uname().nodename,
)
@cherrypy.expose
def poweroff(self):
return self._poweroff(restart=False)
@cherrypy.expose
def restart(self):
return self._poweroff(restart=True)
class Bluetooth:
def __init__(self, template):
self._template = template
self._bluez = cherrypy.engine.bluez_dbus
@cherrypy.expose
def index(self):
return self._template.env.get_template(
'bluetooth/index.html'
).render(
nodename=os.uname().nodename,
powered=self._bluez.adapter.powered,
devices=self._bluez.device.devices(),
)
@cherrypy.expose
def power(self, on_off=None):
if on_off == 'on':
val = True
elif on_off == 'off':
val = False
else:
raise cherrypy.HTTPError('400 Bad Request')
self._bluez.adapter.powered = val
return self._template.env.get_template(
'bluetooth/power.html'
).render(
on_off=on_off,
)
@cherrypy.expose
def connect(self, address=None):
for device in self._bluez.device.devices():
if address != device['Address']:
continue
self._bluez.device.connect(device['path'])
raise cherrypy.HTTPRedirect('/bluetooth')
raise cherrypy.NotFound()
@cherrypy.expose
def disconnect(self, address=None):
for device in self._bluez.device.devices():
if address != device['Address']:
continue
self._bluez.device.disconnect(device['path'])
raise cherrypy.HTTPRedirect('/bluetooth')
raise cherrypy.NotFound()
if __name__ == '__main__':
cherrypy.engine.drop_privileges = \
cherrypy.process.plugins.DropPrivileges(cherrypy.engine)
cherrypy.engine.bluez_dbus = BlueZDbus(cherrypy.engine)
cherrypy.engine.poweroff = Poweroff(cherrypy.engine)
template = Template()
config = os.path.join(current_dir, 'server.conf')
cherrypy.config.update(config)
cherrypy.tree.mount(Root(template), '/', config)
cherrypy.tree.mount(Bluetooth(template), '/bluetooth', config)
cherrypy.engine.start()
cherrypy.engine.block()