forked from verovaleros/bluedriving
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bluedriving.py
executable file
·1100 lines (1004 loc) · 42.7 KB
/
bluedriving.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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/python
# Copyright (C) 2009 Veronica Valeros, Juan Manuel Abrigo, Sebastian Garcia
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
#
# Author:
# Veronica Valeros vero.valeros@gmail.com
#
# Changelog
# - Cache the requested coordinates and addresses to save bandwith
# - Cache the device information to avoid extra queries to the database
# - Updated README on the github wiki
#
# TODO
# When there is a communication error with the bluetooth device do not kill the thread and continue working.
# Check this: Fix crashing when multiple threads try to write in the database
# Redesign the whole program.
#
# KNOWN BUGS
#
# Description
#
# standar imports
import sys
import re
import getopt
import copy
import os
import time
try:
import sqlite3
except:
print 'Library needed. apt-get install python-sqlite'
exit(-1)
try:
import bluetooth
except:
print 'Library needed. apt-get install python-bluez'
exit(-1)
import time
try:
from gps import *;
except:
print 'Library needed. apt-get install python-gps . Includes gpsd and gpsd-clients'
exit(-1)
import threading
#import getCoordinatesFromAddress
from getCoordinatesFromAddress import getCoordinates
from bluedrivingWebServer import createWebServer
try:
import lightblue
except:
print 'Library needed. apt-get install python-lightblue'
exit(-1)
import Queue
try:
import pygame
except:
print 'Library needed. apt-get install python-pygame'
exit(-1)
import getpass
import smtplib
# Global variables
vernum = '0.1'
debug = False
verbose = False
threadbreak = False
global_location = ""
flag_sound = True
flag_internet = True
flag_gps = True
flag_lookup_services = True
flag_alarm = True
list_devices = {}
queue_devices = ""
mail_username = ""
mail_password = ""
address_cache = {}
deviceservices = {}
GRE='\033[92m'
END='\033[0m'
RED='\033[91m'
CYA='\033[96m'
# End global variables
def version():
"""
This function prints information about this utility
"""
global RED
global END
print RED
print " "+ sys.argv[0] + " Version "+ vernum +" @COPYLEFT"
print " Authors: Veronica Valeros (vero.valeros@gmail.com), Seba Garcia (eldraco@gmail.com)"
print " Contributors: nanojaus"
print " Bluedriving is a bluetooth wardriving utility."
print
print END
def usage():
"""
This function prints the posible options of this program.
"""
global RED
global END
print RED
print
print " "+ sys.argv[0] + " Version "+ vernum +" @COPYLEFT"
print " Authors: Veronica Valeros (vero.valeros@gmail.com), Seba Garcia (eldraco@gmail.com)"
print " Contributors: nanojaus"
print " Bluedriving is a bluetooth wardriving utility."
print
print "\n Usage: %s <options>" % sys.argv[0]
print " Options:"
print " \t-h, --help Show this help message and exit."
print " \t-D, --debug Debug mode ON. Prints debug information on the screen."
print " \t-d, --database-name Name of the database to store the data."
print " \t-w, --webserver It runs a local webserver to visualize and interact with "
print " the collected information. Defaults to port 8000."
print " \t-p, --webserver-port Port where the webserver is going to listen. Defaults to 8000."
print " \t-I, --webserver-ip IP address where the webserver binds. Defaults to 127.0.0.1."
print " \t-s, --not-sound Do not play the beautiful discovering sounds. Are you sure you wanna miss this?"
print " \t-i, --not-internet If you dont have internet use this option to save time while getting "
print " coordinates and addresses from the web."
print " \t-l, --not-lookup-services Use this option to not lookup for services for each device. "
print " This option makes the discovering a little faster."
print " \t-g, --not-gps Use this option when you want to run the bluedriving withouth a gpsd connection."
print " \t-f, --fake-gps Use a fake gps position. Useful when you don't have a gps but know your location from google maps."
print " Example: -f '38.897388,-77.036543'"
print " \t-m, --mail-user Gmail user to send mails from and to when a mail alarm is found. The password is entered later."
print " Alarms can be set up only from the web interface at the moment."
print
print END
def getGPS():
"""
This functions gets the gps data from the gpsd session already started.
"""
global debug
global global_location
global threadbreak
counter = 0
gps_session = ""
gps_flag = False
try:
gps_session = gps(mode=WATCH_ENABLE)
while not threadbreak:
if counter > 10:
global_location = ""
try:
location = gps_session.next()
location['lon']
location['lat']
if global_location == "":
pygame.mixer.music.load('gps.ogg')
pygame.mixer.music.play()
global_location = location
counter = 0
time.sleep(0.5)
except:
counter = counter + 1
pass
except KeyboardInterrupt:
print 'Exiting received in getGPS() function. It may take a few seconds.'
threadbreak = True
except Exception as inst:
print 'Exception getGPS() function.'
threadbreak = True
print 'Ending threads, exiting when finished'
print type(inst) # the exception instance
print inst.args # arguments stored in .args
print inst # _str_ allows args to printed directly
x, y = inst # _getitem_ allows args to be unpacked directly
print 'x =', x
print 'y =', y
sys.exit(1)
def get_address_from_gps(location_gps):
"""
This function gets an address from gps coordinates.
"""
global debug
global verbose
global address_cache
global flag_internet
global threadbreak
coordinates = ""
address = ""
try:
if location_gps:
if debug:
print 'Coordinates: {}'.format(location_gps)
try:
# If the location is already stored, we get it.
address = address_cache[location_gps]
except:
if flag_internet:
# print location_gps
#[coordinates,address] = getCoordinatesFromAddress.getCoordinates(location_gps)
[coordinates,address] = getCoordinates(location_gps)
address = address.encode('utf-8')
if debug:
print 'Coordinates: {} Address: {}'.format(coordinates,address)
address_cache[location_gps] = address
else:
address = "Internet option deactivated"
return address
except KeyboardInterrupt:
print 'Exiting received in get_address_from_gps(location_gps) function. It may take a few seconds.'
threadbreak = True
except Exception as inst:
print 'Exception in get_address_from_gps(location_gps)'
print 'Received coordinates: {}'.format(location_gps)
print 'Retrieved coordinates: {}'.format(coordinates)
print 'Retrieved Address: {}'.format(address)
threadbreak = True
print 'Ending threads, exiting when finished'
print type(inst) # the exception instance
print inst.args # arguments stored in .args
print inst # _str_ allows args to printed directly
x, y = inst # _getitem_ allows args to be unpacked directly
print 'x =', x
print 'y =', y
sys.exit(1)
# Discovering function
def bluetooth_discovering():
"""
This function performs a continue discovering of the nearby bluetooth devices.
It sends the list of devices to the lookupdevices function.
"""
global debug
global verbose
global threadbreak
global flag_sound
global global_location
try:
if debug:
print '# In bluetooth_discovering() function'
print '# debug={0}'.format(debug)
print '# verbose={0}'.format(verbose)
print '# threadbreak={0}'.format(threadbreak)
print '# flag_sound={0}'.format(flag_sound)
print '# global_location={0}'.format(global_location)
print
counter=0
while not threadbreak:
data = ""
try:
if debug:
print '# In bluetooth_discovering() function'
print '# Discovering devices...'
print
# Discovering devices
data = bluetooth.bluez.discover_devices(duration=3,lookup_names=True)
#data = bluetooth.discover_devices(duration=3,lookup_names=True)
if debug:
print '# In bluetooth_discovering() function'
print '# Data retrieved: {}'.format(data)
print
if data:
# If there is some data:
# We start a new thread that process the information retrieved
loc = global_location
if debug:
print '# We start a new thread that process the information retrieved'
print '# loc={}'.format(loc)
print '# threading.Thread(None,target = process_devices,args=(data,loc))'
print '# process_device_information_thread.setDaemon(True)'
print
if verbose:
print 'Found: {} devices'.format(len(data))
process_device_information_thread = threading.Thread(None,target = process_devices,args=(data,loc))
process_device_information_thread.setDaemon(True)
process_device_information_thread.start()
else:
# If there is NO data:
# we print a dash and play a sound
print ' -'
if flag_sound:
if global_location:
# If we have gps, play a sound
pygame.mixer.music.load('nodevice-withgps.ogg')
pygame.mixer.music.play()
else:
if debug:
print 'No global location on discover_devices'
print global_location
# If we do not have gps, play a sound
pygame.mixer.music.load('nodevice-withoutgps.ogg')
pygame.mixer.music.play()
counter=0
except KeyboardInterrupt:
print 'Exiting received in bluetooth_discovering() function, inside the while loop. It may take a few seconds.'
threadbreak = True
except:
counter=counter+1
if debug:
print 'An exception occured on the bluetooth_discovering() function. Trying to continue the scanning'
if counter > 9000:
print 'Too many exceptions in bluetooth_discovering() function'
threadbreak = True
print 'Ending threads, exiting when finished'
print type(inst) # the exception instance
print inst.args # arguments stored in .args
print inst # _str_ allows args to printed directly
x, y = inst # _getitem_ allows args to be unpacked directly
print 'x =', x
print 'y =', y
sys.exit(1)
threadbreak = True
return True
except KeyboardInterrupt:
print 'Exiting received in bluetooth_discovering function. It may take a few seconds.'
threadbreak = True
except Exception as inst:
print 'Exception in bluetooth_discovering() function'
threadbreak = True
print 'Ending threads, exiting when finished'
print type(inst) # the exception instance
print inst.args # arguments stored in .args
print inst # _str_ allows args to printed directly
x, y = inst # _getitem_ allows args to be unpacked directly
print 'x =', x
print 'y =', y
sys.exit(1)
def process_devices(device_list,loc):
"""
This function gets a list of discovered devices and gets it ready for storing on the database. The services discovering happens here.
"""
global debug
global verbose
global threadbreak
global flag_gps
global flag_internet
global flag_sound
global list_devices
global queue_devices
location_gps = ""
location_address = ""
ftime = ""
#this flag will help us identify if we found a new device or we already seen the device before.
#with this we can play different sounds.
flag_new_device = False
try:
if device_list:
if debug:
print '# In process_devices(device_list,loc) function'
print '# device_list len={}'.format(len(device_list))
print '# loc={}'.format(loc)
# We process all devices retrieved in one run of the discovery function
for d in device_list:
flag_new_device = False
try:
#if the device is on list devices, then we already see it.
list_devices[d[0]]
flag_new_device = False
except:
#if the device is not in the list is a new device
list_devices[d[0]]=d[1]
flag_new_device = True
if debug:
print 'New device found'
# We setup the timestamp
ftime = time.strftime("%Y-%m-%d %H:%M:%S",time.localtime())
# We get location's related information
if flag_gps:
if debug:
print "# flag_gps={}".format(flag_gps)
try:
location_gps = str(loc.get('lat'))+","+str(loc.get('lon'))
if debug:
print "# location_gps={}".format(location_gps)
if flag_internet:
location_address = get_address_from_gps(location_gps)
if debug:
print "# location_address={}".format(location_gps)
except:
location_address=""
if debug:
print "# location_address={}".format(location_gps)
else:
if loc:
location_gps=loc
if debug:
print "# flag_gps={}".format(flag_gps)
print "# location_gps={}".format(location_gps)
if flag_internet:
location_address = get_address_from_gps(location_gps)
if debug:
print "# location_address={}".format(location_gps)
# We try to lookup more information about the device
device_services = []
if flag_lookup_services:
if debug:
print '# flag_lookup_services={}'.format(flag_lookup_services)
try:
services_data = lightblue.findservices(d[0])
except:
print 'Exception in process_devices, lightblue.findservices(d[0])'
services_data=[]
if services_data:
for i in services_data:
device_services.append(i[2])
if len(device_services) > 1:
print ' {:<24} {:<17} {:<30} {:<27} {:<30} {:<20}'.format(ftime,d[0],d[1],location_gps,location_address.split(',')[0],device_services[0])
for service in device_services[1:]:
print ' {:<24} {:<17} {:<30} {:<27} {:<30} {:<20}'.format('','','','','',service)
#print '\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t{:<30}'.format(service)
else:
print ' {:<24} {:<17} {:<30} {:<27} {:<30} {:<20}'.format(ftime,d[0],d[1],location_gps,location_address.split(',')[0],device_services)
if flag_sound:
if flag_new_device:
pygame.mixer.music.load('new.ogg')
pygame.mixer.music.play()
else:
pygame.mixer.music.load('old.ogg')
pygame.mixer.music.play()
queue_devices.put([ftime,d[0],d[1],location_gps,location_address,device_services])
if debug:
print 'Data loaded to queue'
# no devices?
except KeyboardInterrupt:
print 'Exiting. It may take a few seconds.'
threadbreak = True
except Exception as inst:
print 'Exception in process_devices() function'
threadbreak = True
print 'Ending threads, exiting when finished'
print type(inst) # the exception instance
print inst.args # arguments stored in .args
print inst # _str_ allows args to printed directly
x, y = inst # _getitem_ allows args to be unpacked directly
print 'x =', x
print 'y =', y
sys.exit(1)
def db_create_database(database_name):
"""
This function creates a database for storing the data collected
"""
global debug
global verbose
global threadbreak
try:
# We check if the database exists
if not os.path.exists(database_name):
if debug:
print 'Creating database'
# Creating database
connection = sqlite3.connect(database_name)
# Creating tables
#connection.execute("CREATE TABLE Devices(Id INTEGER PRIMARY KEY AUTOINCREMENT, Mac TEXT , Info TEXT)")
connection.execute("CREATE TABLE Devices(Id INTEGER PRIMARY KEY AUTOINCREMENT, Mac TEXT , Info TEXT, Vendor TEXT)")
connection.execute("CREATE TABLE Locations(Id INTEGER PRIMARY KEY AUTOINCREMENT, MacId INTEGER, GPS TEXT, FirstSeen TEXT, LastSeen TEXT, Address TEXT, Name TEXT, UNIQUE(MacId,GPS))")
connection.execute("CREATE TABLE Notes(Id INTEGER, Note TEXT)")
connection.execute("CREATE TABLE Alarms(Id INTEGER, Alarm TEXT)")
if debug:
print 'Database created'
else:
if debug:
print 'Database already exist'
except KeyboardInterrupt:
print 'Exiting. It may take a few seconds.'
threadbreak = True
except Exception as inst:
print 'Exception in db_create_database(database_name) function'
threadbreak = True
print 'Ending threads, exiting when finished'
print type(inst) # the exception instance
print inst.args # arguments stored in .args
print inst # _str_ allows args to printed directly
x, y = inst # _getitem_ allows args to be unpacked directly
print 'x =', x
print 'y =', y
sys.exit(1)
def db_get_database_connection(database_name):
"""
This function creates a database connection and returns it
"""
global debug
global verbose
global threadbreak
try:
if not os.path.exists(database_name):
db_create_database(database_name)
connection = sqlite3.connect(database_name)
if debug:
print 'Database connection retrieved'
return connection
except KeyboardInterrupt:
print 'Exiting. It may take a few seconds.'
threadbreak = True
except Exception as inst:
print 'Exception in get_database_connection(database_name) function'
threadbreak = True
print 'Ending threads, exiting when finished'
print type(inst) # the exception instance
print inst.args # arguments stored in .args
print inst # _str_ allows args to printed directly
x, y = inst # _getitem_ allows args to be unpacked directly
print 'x =', x
print 'y =', y
sys.exit(1)
def db_get_device_id(connection,bdaddr,device_information):
"""
Receives a device address and returns a device id from the database. If device does not exists in database, it adds it.
"""
global debug
global verbose
global threadbreak
try:
mac_id = ""
try:
mac_id = connection.execute("SELECT Id FROM Devices WHERE Mac = \""+bdaddr+"\" limit 1")
mac_id = mac_id.fetchall()
if not mac_id:
db_add_device(connection,bdaddr,device_information)
mac_id = connection.execute("SELECT Id FROM Devices WHERE Mac = \""+bdaddr+"\" limit 1")
mac_id = mac_id.fetchall()
if debug:
print 'Macid in db_get_device_id() function: {}'.format(mac_id)
return mac_id[0][0]
except:
print 'Device Id could not be retrieved. BDADDR: {}'.format(bdaddr)
return False
except KeyboardInterrupt:
print 'Exiting. It may take a few seconds.'
threadbreak = True
except Exception as inst:
print 'Exception in db_get_device_id() function'
threadbreak = True
print 'Ending threads, exiting when finished'
print type(inst) # the exception instance
print inst.args # arguments stored in .args
print inst # _str_ allows args to printed directly
x, y = inst # _getitem_ allows args to be unpacked directly
print 'x =', x
print 'y =', y
sys.exit(1)
def db_add_device(connection,bdaddr,device_information):
"""
This function receives a db connection ,mac address and device information. It insert this information in the table Devices. If the mac is already there it will ignore it.
"""
global debug
global verbose
global threadbreak
try:
try:
connection.execute("INSERT OR IGNORE INTO Devices (Mac,Info) VALUES (?,?)",(bdaddr,repr(device_information)))
connection.commit()
if debug:
print 'New device added'
return True
except:
if debug:
print 'Device already exists'
return False
except KeyboardInterrupt:
print 'Exiting. It may take a few seconds.'
threadbreak = True
except Exception as inst:
print 'Exception in db_add_device() function'
threadbreak = True
print 'Ending threads, exiting when finished'
print type(inst) # the exception instance
print inst.args # arguments stored in .args
print inst # _str_ allows args to printed directly
x, y = inst # _getitem_ allows args to be unpacked directly
print 'x =', x
print 'y =', y
sys.exit(1)
def db_update_device(connection,device_id,device_information):
"""
This function updates the Info field of the table devices with the new device information.
"""
global debug
global verbose
global threadbreak
try:
try:
connection.execute("UPDATE Devices SET Info=? WHERE Id=?", (repr(device_information), repr(device_id)))
connection.commit()
if debug:
print 'Device information updated'
return True
except:
if debug:
print 'Device information not updated'
print 'Device ID: {}'.format(device_id)
print 'Device Information: {}'.format(device_information)
return False
except KeyboardInterrupt:
print 'Exiting. It may take a few seconds.'
threadbreak = True
except Exception as inst:
print 'Exception in db_update_device() function'
threadbreak = True
print 'Ending threads, exiting when finished'
print type(inst) # the exception instance
print inst.args # arguments stored in .args
print inst # _str_ allows args to printed directly
x, y = inst # _getitem_ allows args to be unpacked directly
print 'x =', x
print 'y =', y
sys.exit(1)
def db_add_location(connection,device_id,location_gps,first_seen,location_address,device_name):
"""
This function adds a new location to the Locations database
"""
global debug
global verbose
global threadbreak
try:
try:
connection.execute("INSERT INTO Locations(MacId, GPS, FirstSeen, LastSeen, Address, Name) VALUES (?, ?, ?, ?, ?, ?)",(int(device_id), repr(location_gps),repr(first_seen),repr(first_seen),repr(location_address),repr(device_name.replace("'","''"))))
connection.commit()
if debug:
print 'Location added'
except:
if debug:
print 'Location not added'
print 'Device ID: {}'.format(device_id)
print 'Device Name: {}'.format(device_name)
print 'GPS Location: {}'.format(location_gps)
print 'First seen: {}'.format(first_seen)
print 'Last seen: {}'.format(first_seen)
return False
except KeyboardInterrupt:
print 'Exiting. It may take a few seconds.'
threadbreak = True
except Exception as inst:
print 'Exception in db_add_location() function'
threadbreak = True
print 'Ending threads, exiting when finished'
print type(inst) # the exception instance
print inst.args # arguments stored in .args
print inst # _str_ allows args to printed directly
x, y = inst # _getitem_ allows args to be unpacked directly
print 'x =', x
print 'y =', y
sys.exit(1)
def db_update_location(connection,device_id,location_gps,first_seen):
"""
This function updates the lastSeen time of the table Locations for a given macId.
"""
global debug
global verbose
global threadbreak
try:
try:
connection.execute("UPDATE Locations SET LastSeen=? WHERE MacId=? AND GPS=?",(repr(first_seen), int(device_id), repr(location_gps)))
connection.commit()
if debug:
print 'Location updated'
print 'Device ID: {}'.format(device_id)
print 'GPS Location: {}'.format(location_gps)
print 'Last seen: {}'.format(first_seen)
except:
if debug:
print 'Location not updated'
print 'Device ID: {}'.format(device_id)
print 'GPS Location: {}'.format(location_gps)
print 'Last seen: {}'.format(first_seen)
return False
except KeyboardInterrupt:
print 'Exiting. It may take a few seconds.'
threadbreak = True
except Exception as inst:
print 'Exception in db_update_location() function'
threadbreak = True
print 'Ending threads, exiting when finished'
print type(inst) # the exception instance
print inst.args # arguments stored in .args
print inst # _str_ allows args to printed directly
x, y = inst # _getitem_ allows args to be unpacked directly
print 'x =', x
print 'y =', y
sys.exit(1)
def device_alert(device_id,device_name,database_name,location_gps,location_address,last_seen):
"""
This function handles the alerts for devices already seen.
"""
global debug
global verbose
global mail_username
global mail_password
global flag_internet
try:
connection = db_get_database_connection(database_name)
result = connection.execute('select * from alarms where id = ?',(device_id,))
data = result.fetchall()
for alarm in data:
if 'Sound' in alarm:
pygame.mixer.music.load('alarm.ogg')
pygame.mixer.music.play()
break
if 'Festival' in alarm:
os.system("echo "+device_name+"|festival --tts")
break
if 'Mail' in alarm:
if flag_internet:
fromaddr = mail_username+'@gmail.com'
toaddrs = mail_username+'@gmail.com'
msg = 'Device '+device_name+'\nLocation '+location_gps+'\nAddress '+location_address+'\nLast seen '+last_seen
server = smtplib.SMTP('smtp.gmail.com:587')
server.starttls()
server.login(mail_username,mail_password)
server.sendmail(fromaddr, toaddrs, msg)
server.quit()
break
connection.commit()
connection.close()
except KeyboardInterrupt:
print 'Exiting. It may take a few seconds.'
threadbreak = True
except Exception as inst:
print 'Exception in device_alert() function'
threadbreak = True
print 'Ending threads, exiting when finished'
print type(inst) # the exception instance
print inst.args # arguments stored in .args
print inst # _str_ allows args to printed directly
x, y = inst # _getitem_ allows args to be unpacked directly
print 'x =', x
print 'y =', y
sys.exit(1)
def store_device_information(database_name):
"""
This function handles the storage of the information collected by the bluetooth_discovering. The information is stored in the database.
"""
global debug
global verbose
global queue_devices
global threadbreak
global flag_alarm
connection = ""
try:
# We create a database connection
connection = db_get_database_connection(database_name)
while not threadbreak:
while not queue_devices.empty():
# We clear the variables to use
device_id = ""
device_bdaddr = ""
device_name = ""
device_information = ""
location_gps = ""
location_address = ""
first_seen = ""
last_seen = ""
if not queue_devices.empty():
# We extract the device from the queue
temp = queue_devices.get()
# We load the information
# From the text to time structure
#temp2 = time.strptime(temp[0],"%a %b %d %H:%M:%S %Y")
# From time structure to supported text.
#temp_date = time.strftime("%Y-%m-%d %H:%M:%S",temp2)
last_seen = temp[0]
first_seen = temp[0]
device_bdaddr = temp[1]
device_name = temp[2]
location_gps = temp[3]
location_address = temp[4]
device_information = temp[5]
device_id = db_get_device_id(connection,device_bdaddr,device_information)
if flag_alarm:
# Here we start the discovering devices threads
device_alert_thread = threading.Thread(None,target = device_alert, args=(device_id,device_name,database_name,location_gps,location_address,last_seen))
device_alert_thread.setDaemon(True)
device_alert_thread.start()
if device_id:
# If we have a device information, then we update the information for the device
result = db_update_device(connection,device_id,device_information)
if not result:
print 'Device information could not be updated'
# We try to store a new location
result = db_add_location(connection,device_id,location_gps,first_seen,location_address,device_name)
# If the location has not changed, result will be False. We update the last seen field into locations.
if not result:
result = db_update_location(connection,device_id,location_gps,last_seen)
#print ' {:<24} {:<17} {:<30} {:<27} {:<30} {:<20}'.format(temp[0],temp[1],temp[2],temp[3],temp[4],temp[5])
time.sleep(2)
except KeyboardInterrupt:
print 'Exiting. It may take a few seconds.'
threadbreak = True
except Exception as inst:
print 'Exception in store_device_information() function'
threadbreak = True
print 'Ending threads, exiting when finished'
print type(inst) # the exception instance
print inst.args # arguments stored in .args
print inst # _str_ allows args to printed directly
x, y = inst # _getitem_ allows args to be unpacked directly
print 'x =', x
print 'y =', y
sys.exit(1)
##########
# MAIN
##########
def main():
global debug
global threadbreak
global flag_sound
global flag_internet
global flag_gps
global flag_lookup_services
global flag_alarm
global queue_devices
global GRE
global CYA
global END
global global_location
global mail_username
global mail_password
database_name = "bluedriving.db"
flag_run_webserver = False
fake_gps = ''
mail_username = ""
mail_password = ""
webserver_port = 8000
webserver_ip = "127.0.0.1"
flag_fake_gps=False
try:
opts, args = getopt.getopt(sys.argv[1:], "hDd:wsilgf:m:I:p:", ["help","debug","database-name=","webserver","disable-sound","not-internet","not-lookup-services","not-gps","fake-gps=","mail-user=","webserver-port=","webserver-ip="])
except:
usage()
exit(-1)
for opt, arg in opts:
if opt in ("-h", "--help"): usage(); sys.exit()
if opt in ("-D", "--debug"): debug = True
if opt in ("-d", "--database-name"): database_name = arg
if opt in ("-w", "--webserver"): flag_run_webserver = True
if opt in ("-s", "--disable-sound"): flag_sound = False
if opt in ("-i", "--not-internet"): flag_internet = False
if opt in ("-l", "--not-lookup-services"): flag_lookup_services = False
if opt in ("-g", "--not-gps"): flag_gps = False; flag_internet = False
if opt in ("-f", "--fake-gps"): fake_gps = arg; flag_gps = False;
if opt in ("-m", "--mail-user"): mail_username = arg; print 'Provide your gmail password for given user: ',; mail_password = getpass.getpass()
if opt in ("-p", "--webserver-port"): webserver_port = int(arg)
if opt in ("-I", "--webserver-ip"): webserver_ip = str(arg)
try:
version()
queue_devices = Queue.Queue()
startTime = time.time()
if flag_lookup_services:
# We print the header for printing results on console
print ' {:<24} {:<17} {:<30} {:<27} {:<30} {:<20}'.format("Date","MAC address","Device name","Global Position","Aproximate address","Info")
print ' {:<24} {:<17} {:<30} {:<27} {:<30} {:<20}'.format("----","-----------","-----------","---------------","------------------","----")
else:
# We print the header for printing results on console
print ' {:<24} {:<17} {:<30} {:<27} {:<30}'.format("Date","MAC address","Device name","Global Position","Aproximate address")
print ' {:<24} {:<17} {:<30} {:<27} {:<30}'.format("----","-----------","-----------","---------------","------------------")
# Here we start the thread to get gps location
if flag_gps and not flag_fake_gps:
if debug:
print '# Here we start the thread to get gps location'
print '# flag_gps={0}'.format(flag_gps)
print '# fake_gps={0}'.format(fake_gps)
print
#gps_thread = threading.Thread(None,target=get_coordinates_from_gps)
gps_thread = threading.Thread(None,target=getGPS)
gps_thread.setDaemon(True)
gps_thread.start()
elif fake_gps:
# Here we are setting up the global location to use the fake gps
if debug:
print '# Here we are setting up the global location to use the fake gps'
print '# flag_gps={0}'.format(flag_gps)
print '# fake_gps={0}'.format(fake_gps)
global_location = fake_gps
if debug:
print '# global_location={0}'.format(global_location)
print
# Here we start the web server
if flag_run_webserver:
if debug:
print '# Here we start the thread to get the web server running'
print '# flag_run_webserver={}'.format(flag_run_webserver)
print
#webserver_thread = threading.Thread(None,createWebServer,"web_server",args=(webserver_port,webserver_ip))
webserver_thread = threading.Thread(None,createWebServer,"web_server",args=(webserver_port,webserver_ip,database_name))
webserver_thread.setDaemon(True)
webserver_thread.start()
else:
if debug:
print '# The webserver flag is not set. Not running webserver.'
print '# flag_run_webserver={}'.format(flag_run_webserver)
print
# Here we start the discovering devices threads
if debug:
print '# Here we start the discovering devices thread'
print '# threading.Thread(None,target = bluetooth_discovering)'
print '# bluetooth_discovering_thread.setDaemon(True)'
print
bluetooth_discovering_thread = threading.Thread(None,target = bluetooth_discovering)
bluetooth_discovering_thread.setDaemon(True)
bluetooth_discovering_thread.start()
# Here we start the thread that will continuosly store data to the database
if debug:
print '# Here we start the thread that will continuosly store data to the database'
print '# threading.Thread(None,target = store_device_information,args=(database_name,))'
print '# store_device_information_thread.setDaemon(True)'
print
store_device_information_thread = threading.Thread(None,target = store_device_information,args=(database_name,))