1 | #!/usr/bin/env python
|
---|
2 | # -*- coding: utf-8 -*-
|
---|
3 | #
|
---|
4 | # Script for importing DroidStumbler .csv files, which takes the best value of
|
---|
5 | # each measurement point.
|
---|
6 | #
|
---|
7 | # Original by Dennis Wagenaar <d.wagenaar@gmail.com>
|
---|
8 | #
|
---|
9 | # Rick van der Zwet <info@rickvanderzwet.nl>
|
---|
10 | #
|
---|
11 | from django.core.management import setup_environ
|
---|
12 | from django.core.management.base import BaseCommand, CommandError
|
---|
13 | from django.db.utils import IntegrityError
|
---|
14 | from optparse import OptionParser, make_option
|
---|
15 | from gheat.models import *
|
---|
16 | import csv
|
---|
17 | import datetime
|
---|
18 | import gzip
|
---|
19 | import logging
|
---|
20 | import os
|
---|
21 | import sys
|
---|
22 |
|
---|
23 | def user_feedback(count, dot_step, summary_report):
|
---|
24 | if (count % summary_report) == 0:
|
---|
25 | sys.stdout.write(str(count))
|
---|
26 | elif (count % dot_step) == 0:
|
---|
27 | sys.stdout.write(".")
|
---|
28 | sys.stdout.flush()
|
---|
29 | return count + 1
|
---|
30 |
|
---|
31 |
|
---|
32 | def import_droidstumbler(location, meetrondje, gebruiker, email, datum,show_progres=False):
|
---|
33 |
|
---|
34 | g, created = Gebruiker.objects.get_or_create(naam=gebruiker , email=email)
|
---|
35 | a, created = Apparatuur.objects.get_or_create(antenne='buildin' , kaart='mobilePhone')
|
---|
36 | mr, created = MeetRondje.objects.get_or_create(datum=datum , naam=meetrondje , gebruiker=g , apparatuur=a)
|
---|
37 |
|
---|
38 | ap_cache = {}
|
---|
39 | meting_pool = {}
|
---|
40 |
|
---|
41 | # Process file
|
---|
42 | if location.endswith('.gz'):
|
---|
43 | fh = gzip.open(location,'rb')
|
---|
44 | else:
|
---|
45 | fh = open(location,'rb')
|
---|
46 | csvfile = csv.reader(fh, delimiter=',')
|
---|
47 | for row in csvfile:
|
---|
48 | try:
|
---|
49 | epoch, msg_type, lat, lon, accuracy, ssid, bssid, level, frequency, capabilities = row
|
---|
50 | except ValueError:
|
---|
51 | logging.error("Unable to parse line:%i '%s'" % (csvfile.line_num, row))
|
---|
52 | continue
|
---|
53 | if msg_type == "data" and lat and lon:
|
---|
54 | if not ap_cache.has_key(bssid):
|
---|
55 | ap_cache[bssid], created = Accespoint.objects.get_or_create(mac=bssid, ssid=ssid, encryptie=capabilities)
|
---|
56 |
|
---|
57 | # We store the best value found
|
---|
58 | key = (ap_cache[bssid], lat, lon)
|
---|
59 | signaal=(100 + int(level))
|
---|
60 | if meting_pool.has_key(key):
|
---|
61 | meting_pool[key] = max(meting_pool[key], signaal)
|
---|
62 | else:
|
---|
63 | meting_pool[key] = signaal
|
---|
64 |
|
---|
65 |
|
---|
66 | # Import the data into the database
|
---|
67 | count = 0
|
---|
68 | for (ap,lat,lon),signal in meting_pool.iteritems():
|
---|
69 | try:
|
---|
70 | m = Meting.objects.create(meetrondje=mr, accespoint=ap, latitude=lat, longitude=lon, signaal=signaal)
|
---|
71 | except IntegrityError, e:
|
---|
72 | logging.error("Unable to import - %s" % e)
|
---|
73 | continue
|
---|
74 |
|
---|
75 | # Give some feedback to the user
|
---|
76 | if show_progres:
|
---|
77 | count = user_feedback(count, 100, 1000)
|
---|
78 |
|
---|
79 | if show_progres:
|
---|
80 | sys.stdout.write("%s\n" % count)
|
---|
81 |
|
---|
82 |
|
---|
83 | class Command(BaseCommand):
|
---|
84 | args = '<csvfile>[.gz]'
|
---|
85 | option_list = BaseCommand.option_list + (
|
---|
86 | make_option('-m', '--meetrondje', dest='meetrondje', default=None),
|
---|
87 | make_option('-g', '--gebruiker', dest='gebruiker', default=os.environ['USER']),
|
---|
88 | make_option('-e', '--email', dest='email', default=os.environ['USER'] + '@example.org'),
|
---|
89 | make_option('-d', '--datum', dest='datum', default=None, help="Provide date \
|
---|
90 | in following format: %Y-%m-%d-%H%M%S, by default it will be generated from \
|
---|
91 | the filename"),)
|
---|
92 |
|
---|
93 | def handle(self, *args, **options):
|
---|
94 | try:
|
---|
95 | (csv_file,) = args
|
---|
96 | except ValueError:
|
---|
97 | self.print_help(sys.argv[0],sys.argv[1])
|
---|
98 | raise CommandError("Not all arguments are provided")
|
---|
99 | if not os.path.isfile(csv_file):
|
---|
100 | raise CommandError("csv file '%s' does not exists" % csv_file)
|
---|
101 |
|
---|
102 | # Meetrondje from filename if needed
|
---|
103 | if options['meetrondje'] == None:
|
---|
104 | meetrondje = os.path.basename(csv_file).rstrip('.gz').rstrip('.csv')
|
---|
105 | else:
|
---|
106 | meetrondje = options['meetrondje']
|
---|
107 | # Date from filename if needed
|
---|
108 | if options['datum'] == None:
|
---|
109 | datum = os.path.basename(csv_file).lstrip('ScanResult-').rstrip('.csv.gz')
|
---|
110 | else:
|
---|
111 | datum = options['datum']
|
---|
112 | try:
|
---|
113 | datum = datetime.datetime.strptime(datum,'%Y-%m-%d-%H%M%S')
|
---|
114 | except ValueError:
|
---|
115 | logging.error("Invalid date '%s'" % options['datum'])
|
---|
116 | sys.exit(1)
|
---|
117 |
|
---|
118 | self.stdout.write('Meetrondje: %s @ %s\n' % (meetrondje, datum))
|
---|
119 | self.stdout.write("Going to import '%s' for gebruiker '%s <%s>'\n" % (os.path.basename(csv_file), options['gebruiker'], options['email']))
|
---|
120 | import_droidstumbler(csv_file,meetrondje,options['gebruiker'],options['email'], datum, True)
|
---|
121 | self.stdout.write("All done, goodbye")
|
---|