-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBLE_DATA.py
197 lines (147 loc) · 7.77 KB
/
BLE_DATA.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
'''Python API for using BLE_SCANNER and obtaining the
retrieved data from the scan: It will store into MySQL
the time and date it was retrieved from, mac and name of the
device
NOTE: This code includes table generator and thus, you do
not have to run BLE_TABLE.py seperately all the time.
This code should not be plagarized nor used for credential/academic purposes
unless stated otherwise by the owner. Modification and usage is accepted
upon approval.
For more details contact: yoshiki.shoji@mail.utoronto.ca
'''
import pymysql
from BLE_SCANNER import Bluetoothctl
from BLE_TABLE import MYSQL_TABLE
# Connect to database
connection = pymysql.connect(host = 'localhost',
user = 'root',
passwd = 'ditpwd',
db = 'DATA')
# Commit every data as by default it is False
connection.autocommit(True)
# Create Cursor Object
cursorObject = connection.cursor()
class DATA:
def refresh(self):
'''A function which removes all the devices from bluetoothctl
Necessary protocol whenever we execute the code again'''
print('Hold on please. Getting BLE SCAN ready...''')
bl = Bluetoothctl()
devices = bl.get_device_info_dict()
bl.remove_devices_KV(devices)
return
def newly_detected(self,devices_prior,devices_updated):
'''Conditions must be made for checking whether device is
available or not within its vicinity'''
DETECTED = 'DETECTED'
# First check if the device is new - detected new device
# Devices still commonly shared - still detected no update needed in table
available = list(set(devices_prior.keys()) & set(devices_updated.keys()))
# Uncommon devices between the two dicts
new_devices = list(set(devices_updated.keys()) ^ set(available))
# print(new_devices)
if new_devices == []:
print('no new devices seen')
print('-----')
else:
print('new devices seen - go to table for more info')
print('-----')
for new in new_devices:
insertStatement = ("INSERT INTO "+new+"(DATE,TIME,MAC,AVAILABILITY)"
"VALUES(CURDATE(),CURTIME()," +"'"+ devices_updated[new] +"'"+", '"+DETECTED+"')")
cursorObject.execute(insertStatement)
return
def check_disappearance(self,devices_prior,devices_updated):
'''To check disappearance, compare the prior and updated list lengths'''
delta = len(devices_prior.keys()) - len(devices_updated.keys())
NON_DETECTED = 'NON-DETECTED'
# Whenever we lose a device, the new one has smaller dimension
# Deal with other conditions
if delta <= 0:
return
else:
disp_dev = [dev for dev in devices_prior.keys() if dev not in devices_updated.keys()]
print('some devices have disappeared - go to table for more info')
print('-----')
for dev in disp_dev:
insertStatement = ("INSERT INTO "+dev+"(DATE,TIME,MAC,AVAILABILITY)"
"VALUES(CURDATE(),CURTIME()," +"'"+ devices_prior[dev] +"'"+", '"+NON_DETECTED+"')")
cursorObject.execute(insertStatement)
return
def data_gen(self,blscan_time,totalscan_time1,totalscan_time2):
try:
# Reset all pre-existing devices in bluetoothctl devices folder
self.refresh()
table_gen = MYSQL_TABLE()
while totalscan_time1 >= 0:
# Instantiate the table generator variables
# and initiate bluetoothctl
# Outer loop is used whenever the break statement is executed in the inner loop.
# Note the O(n^2) run time.
init = True
bl = Bluetoothctl()
print('scanning again as no device was detected')
print('-----')
while totalscan_time2 >= 0:
# Base case - always executed once
if init == True:
print('init bluetooth...')
for i in range(0,2):
bl.start_scan(blscan_time)
devices_dict = bl.get_device_info_dict()
if devices_dict == {}:
print('No devices seen. Scan again')
print('-----')
break
tablenames = table_gen.table_generator(devices_dict)[0]
devices_mydict = table_gen.table_generator(devices_dict)[1]
print(devices_mydict)
print('-----')
# Remove all devices from bluetoothctl - This is what will enable us to
# 'Dynamically' check the presence of the device
bl.remove_devices_VK(devices_mydict)
DETECTED = 'DETECTED'
# All the devices are available in the room (Insert Availability as DETECTED)
for table in tablenames:
insertStatement = ("INSERT INTO "+table+" (DATE,TIME,MAC,AVAILABILITY) "
"VALUES(CURDATE(),CURTIME(),"+"'"+devices_mydict[table]+"'"+" ,'"+DETECTED+"')")
cursorObject.execute(insertStatement)
# Build on from the base case - Check to see if devices_dict is changing
# Which consequently means tablenames is changing (ie. len difference)
else:
print('init bluetooth.....')
bl = Bluetoothctl()
for i in range(0,2):
bl.start_scan(blscan_time)
devices_dict_updated = bl.get_device_info_dict()
tablenames_updated = table_gen.table_generator(devices_dict_updated)[0]
devices_mydict_updated = table_gen.table_generator(devices_dict_updated)[1]
print(devices_mydict_updated)
bl.remove_devices_VK(devices_mydict_updated)
# Store all 'newly' detected devices
new_detected = self.newly_detected(devices_mydict,devices_mydict_updated)
# Check for disappeared devices: NON-DETECTED case
self.check_disappearance(devices_mydict,devices_mydict_updated)
if devices_mydict_updated == {}:
print('No devices seen. Scan again')
print('-----')
break
else:
pass
# Update
devices_mydict = devices_mydict_updated
init = False
totalscan_time2 -= 1
totalscan_time1 -= 1
except (SystemExit):
print('Scanning Completed. Goodbye!')
except Exception as e:
'''Uncomment below to show what type of error was produced.'''
print(type(e))
print("Exeception occured:{}".format(e))
pass
finally:
connection.close()
if __name__ == '__main__':
bldata = DATA()
bldata.data_gen(blscan_time=5,totalscan_time1=30,totalscan_time2=30)