-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgateway.py
executable file
·215 lines (177 loc) · 5.67 KB
/
gateway.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
#!/usr/bin/env python3
import logging
import dbus
import dbus.service
import dbus.mainloop.glib
from gi.repository import GLib
import dbluez
import parser
import threading
from threading import Lock
import time
import getopt, sys
import pickle
DBUS_OBJ_MAN = 'org.freedesktop.DBus.ObjectManager'
DBUS_PROPS = 'org.freedesktop.DBus.Properties'
BLUEZ = 'org.bluez'
BLUEZ_ADAPTER = 'org.bluez.Adapter1'
BLUEZ_DEVICE = 'org.bluez.Device1'
BLUEZ_GATTSERV = 'org.bluez.GattService1'
BLUEZ_GATTCHAR = 'org.bluez.GattCharacteristic1'
loop = None
timer = None
scanner = None
log_level = None
daemon = False
def usage():
print('Usage:')
print(' gateway.py [options]')
print('Options:')
print(' -h, --help Show help')
print(' -i, --adapter=hciX Specify local adapter interface')
print(' -d, --daemon Enable daemonized mode')
print(' -V, --verbose Be verbose and show debug log')
print(' -p, --device_path=<path> Specify path to store temp. device information')
def parse_options():
global adapter
global daemon
global log_level
global device_path
try:
opts, args = getopt.getopt(sys.argv[1:], "dhi:Vp:", ["help", "adapter="])
except getopt.GetoptError as err:
# print help information and exit:
print(err) # will print something like "option -a not recognized"
usage()
sys.exit(2)
adapter = 'hci0'
log_level = logging.INFO
for o, a in opts:
if o in ('-h', '--help'):
usage()
sys.exit()
elif o in ('-d', '--daemon'):
daemon = True
elif o in ('-i', '--adapter'):
adapter = a
elif o in ('-V', '--verbose'):
log_level = logging.DEBUG
elif o in ('-p', '--device_path'):
device_path = a
else:
assert False, "unhandled option"
def new_device_cb(adapter, address, frametype, power, url):
global logger
global addresses
global addresses_to_process
global devices
global t_started
global daemon
logger.info('Found new device: {} with \'{}\''.format(address, url))
if url == 'http://www.afarcloud.eu/':
device = dbluez.Device(adapter, address, frametype, power, url)
afcdev = parser.Thingsboard(device)
if address in addresses:
if addresses[address]['last_sync'] + 3600 > t_started:
logger.debug('Device {} was synced within last {} seconds.'.format(address, 3600))
return
if address not in addresses_to_process:
addresses_to_process.append(address)
if address not in devices.keys():
devices[address] = afcdev
if daemon == True:
if devices[address].locked() != None and devices[address].locked() == True:
return
logger.info('Start synchronization of {} in background'.format(address))
lock = Lock()
lock.acquire()
devices[address].startSynchronization(lock, 900)
def quit():
global logger
global loop
global scanner
global timer
global devices
global addresses_to_process
global lock
global addresses
global t_started
def sync():
if len(addresses_to_process) > 0:
address = addresses_to_process.pop()
lock.acquire()
devices[address].startSynchronization(lock)
thread = threading.Thread(target=sync)
thread.daemon = True
thread.start()
elif len(devices) > 0:
logger.info('Waiting for last device to finish')
lock.acquire()
logger.info('Finished')
lock.release()
loop.quit()
elif len(devices) == 0:
loop.quit()
lock = Lock()
logger.info('Scan timeout')
GLib.source_remove(timer)
thread = threading.Thread(target=sync)
thread.daemon = True
thread.start()
def main():
global args
global adapter
global devices
global addresses_to_process
global addresses
global loop
global scanner
global timer
global logger
global log_level
global t_started
global device_path
global daemon
device_path = ''
t_started = time.time()
t_started = int(t_started) - int(t_started) % 60
parse_options()
logging.basicConfig(stream=sys.stderr, level=log_level)
logger = logging.getLogger(__name__)
dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
addresses = {}
addresses_to_process = []
devices = {}
try:
if device_path != '':
with open(device_path, 'rb') as config_dictionary_file:
addresses = pickle.load(config_dictionary_file)
logger.debug(addresses)
except Exception:
logger.debug('{} not found')
scanner = dbluez.Scanner(adapter, new_device_cb)
scanner.startScan()
GLib.threads_init()
if daemon == False:
timer = GLib.timeout_add(5000, quit)
loop = GLib.MainLoop()
try:
loop.run()
except KeyboardInterrupt:
logger.info('Interrupted via keyboard')
scanner.stopScan()
for address in devices.keys():
try:
devices[address].removeDevice()
if devices[address].syncedSuccessfully() == True:
addresses[address] = { 'last_sync': t_started }
else:
addresses[address] = { 'last_sync': 0 }
except Exception as e:
None
if device_path != '':
with open(device_path, 'wb') as device_dict_file:
pickle.dump(addresses, device_dict_file)
logger.info('Exit')
if __name__ == '__main__':
main()