From 7c2974bb74e4db4470559d4784a423c3c6c97638 Mon Sep 17 00:00:00 2001 From: Tom Date: Thu, 12 Apr 2012 17:38:40 +0200 Subject: implemented databases save for guis --- Src/PyCatcher/src/CellIDDatabase.py | 96 ++++++++++++++++++++++++++++++++++ Src/PyCatcher/src/LocalAreaDatabse.py | 97 +++++++++++++++++++++++++++++++++++ Src/PyCatcher/src/parstertest.py | 47 ----------------- Src/PyCatcher/src/pyCatcherModel.py | 24 ++++++++- Src/PyCatcher/src/pyCatcherView.py | 6 ++- Src/PyCatcher/src/settings.py | 7 +-- 6 files changed, 224 insertions(+), 53 deletions(-) create mode 100644 Src/PyCatcher/src/CellIDDatabase.py create mode 100644 Src/PyCatcher/src/LocalAreaDatabse.py delete mode 100644 Src/PyCatcher/src/parstertest.py (limited to 'Src/PyCatcher/src') diff --git a/Src/PyCatcher/src/CellIDDatabase.py b/Src/PyCatcher/src/CellIDDatabase.py new file mode 100644 index 0000000..2471a5a --- /dev/null +++ b/Src/PyCatcher/src/CellIDDatabase.py @@ -0,0 +1,96 @@ +import re +import urllib2 +from settings import Open_Cell_ID_Key +from struct import pack, unpack +from httplib import HTTP + +class Translator: + Country = { + 'Germany':'262' + } + + Provider = { + 'T-Mobile':'01', + 'Vodafone':'02', + 'E-Plus':'03', + 'O2':'07' + } + + MCC = { + 262:'de' + } + +class CellIDDBStatus: + CONFIRMED = 0 + APPROXIMATED = 1 + ERROR = 2 + NOT_LOOKED_UP = 3 + NOT_IN_DB = 4 + +class CellIDDatabaseFetcher: + + + def fetch(self, cid, lac, mcc, mnc): + print CID.fetch_id_from_Google(cid,lac,mcc) + print CID.fetch_id_from_OpenCellID(cid,lac,mcc,mnc) + + + def fetch_id_from_OpenCellID(self,cid, lac, mcc, mnc): + key_ocid = Open_Cell_ID_Key + + url = 'http://www.opencellid.org/cell/get?key=%s&mnc=%d&mcc=%d&lac=%d&cellid=%d'%(key_ocid, mnc, mcc, lac, cid) + response = urllib2.urlopen(url).read() + + status = (re.search(r'stat="(.+)"',response)).group(1) + + if status != 'ok': + status = CellIDDBStatus.ERROR + return + + match = re.search(r'lat="(\d+\.\d+)".*lon="(\d+\.\d+).*range="(\d+)"',response) + latitude,longitude,range = match.group(1),match.group(2),match.group(3) + + if int(range) > 10000: + status = CellIDDBStatus.APPROXIMATED + else: + status = CellIDDBStatus.CONFIRMED + + latitude = float(latitude) + longitude = float(longitude) + + if latitude == 0 or longitude == 0: + status = CellIDDBStatus.NOT_IN_DB + + return status, latitude, longitude + + + def fetch_id_from_Google(self, cid, lac, country): + device = "Motorola C123" + country = Translator.MCC[country] + b_string = pack('>hqh2sh13sh5sh3sBiiihiiiiii', + 21, 0, + len(country), country, + len(device), device, + len('1.3.1'), "1.3.1", + len('Web'), "Web", + 27, 0, 0, + 3, 0, cid, lac, + 0, 0, 0, 0) + + http = HTTP('www.google.com', 80) + http.putrequest('POST', '/glm/mmap') + http.putheader('Content-Type', 'application/binary') + http.putheader('Content-Length', str(len(b_string))) + http.endheaders() + http.send(b_string) + code, msg, headers = http.getreply() + try: + bytes = http.file.read() + (a, b,errorCode, latitude, longitude, c, d, e) = unpack(">hBiiiiih",bytes) + latitude = latitude / 1000000.0 + longitude = longitude / 1000000.0 + status = CellIDDBStatus.CONFIRMED + except: + status = CellIDDBStatus.NOT_IN_DB + + return status, latitude, longitude \ No newline at end of file diff --git a/Src/PyCatcher/src/LocalAreaDatabse.py b/Src/PyCatcher/src/LocalAreaDatabse.py new file mode 100644 index 0000000..102408a --- /dev/null +++ b/Src/PyCatcher/src/LocalAreaDatabse.py @@ -0,0 +1,97 @@ +import sqlite3 +import os +from pyCatcherModel import BaseStationInformation + +class dummystation: + + def __init__(self): + self.arfcn = 0 + self.bsic = '2,3' + self.cell = 2 + self.country = 'Nirvana' + self.lac = 4 + self.provider = 'T-Schrott' + self.rxlev = -70 + + +class LocalAreaDatabase: + + def __init__(self): + self._connection = None + self._cursor = None + + def load_or_create_database(self, path): + if self._connection: + self._connection.close() + self._connection = None + + database_exists = os.path.exists(path) + self._connection = sqlite3.connect(path) + self._cursor = self._connection.cursor() + if not database_exists: + print 'file has been created' + self._create_base_table() + else: + print 'connected to existing' + + def _create_base_table(self): + sql = '''CREATE TABLE basestations( + cellid INTEGER, country TEXT, provider TEXT, arfcn INTEGER, bsic TEXT, lac INTEGER, + rxmin INTEGER, rxmax INTEGER, sightings INTEGER + ) + ''' + self._cursor.execute(sql) + self._connection.commit() + + def _insert_station(self, base_station): + values = ( base_station.cell, + base_station.country, + base_station.provider, + base_station.arfcn, + base_station.bsic, + base_station.lac, + base_station.rxlev, + base_station.rxlev, + 1 + ) + sql = 'INSERT INTO basestations VALUES (?,?,?,?,?,?,?,?,?)' + self._cursor.execute(sql, values) + self._connection.commit() + + def _alter_station(self, base_station, old_rmin, old_rmax, old_sightings): + if base_station.rxlev < old_rmin: + rmin = base_station.rxlev + else: + rmin = old_rmin + if base_station.rxlev > old_rmax: + rmax = base_station.rxlev + else: + rmax = old_rmax + sightings = old_sightings + 1 + values = (rmin,rmax,sightings,base_station.cell) + sql = 'UPDATE basestations SET rxmin=?, rxmax=?, sightings=? WHERE cellid=?' + self._cursor.execute(sql, values) + self._connection.commit() + + def get_station(self, cellID): + sql = 'SELECT * FROM basestations WHERE cellid =%d'%cellID + self._cursor.execute(sql) + try: + result = (self._cursor.fetchall())[0] + except: + result = None + return result + + def insert_or_alter_base_stations(self, base_station_list): + for station in base_station_list: + self.insert_or_alter_base_station(station) + + def insert_or_alter_base_station(self, base_station): + lookupresult = self.get_station(base_station.cell) + if lookupresult: + self._alter_station(station,lookupresult[6],lookupresult[7],lookupresult[8]) + else: + self._insert_station(base_station) + + def __del__(self): + self._connection.close() \ No newline at end of file diff --git a/Src/PyCatcher/src/parstertest.py b/Src/PyCatcher/src/parstertest.py deleted file mode 100644 index c824b36..0000000 --- a/Src/PyCatcher/src/parstertest.py +++ /dev/null @@ -1,47 +0,0 @@ -def si_to_bin(si): - system_information = si.split(' ') - neighbours = system_information[3:19] - bin_representation = '' - for value in neighbours: - bin_representation += str(bin(int(value, 16))[2:].zfill(8)) - return bin_representation - -def parse_bit_mask(si, offset): - bin_representation = si_to_bin(si) - neighbours = [] - for x in xrange(1,125): - index = 0-x - bit = bin_representation[index] - if bit == '1': - neighbours.append(abs(index) + offset) - return neighbours - - -def parse_900(si): - neighbours = parse_bit_mask(si, 0) - print neighbours - -def parse_1800(si,siter,sibis): - pass - -def parse_900_ext(si): - pass - - - -system_information_1 = '59 06 1A 0B B9 3B 30 00 00 00 00 00 00 00 00 00 00 00 00 80 9D 00 00' -system_information_2 = '59 06 1a 00 00 00 00 02 10 00 00 00 00 00 48 20 95 00 00 08 a5 00 00' -system_information_3 = '59 06 1a 00 00 00 0b 90 08 00 00 00 01 14 08 00 11 00 00 88 a5 00 00' -system_information_4 = '59 06 1a 00 00 00 0b 90 08 00 00 00 01 14 08 00 11 00 00 88 a5 00 00' - -parse_900(system_information_2) - - - - - - - - - - diff --git a/Src/PyCatcher/src/pyCatcherModel.py b/Src/PyCatcher/src/pyCatcherModel.py index bf4be14..112646b 100644 --- a/Src/PyCatcher/src/pyCatcherModel.py +++ b/Src/PyCatcher/src/pyCatcherModel.py @@ -1,6 +1,12 @@ import datetime import gtk import math +from CellIDDatabase import CellIDDBStatus + +class LocationProvider: + GOOGLE = 0 + OPENCELLID = 1 + NONE = 2 class BaseStationInformation: @@ -21,6 +27,9 @@ class BaseStationInformation: self.evaluation_report = {} self.evaluation = 'NYE' self.evaluation_by = 'NYE' + self.latitude = 0 + self.longitude = 0 + self.locationprovider = LocationProvider.NONE def get_list_model(self): return self.provider, str(self.arfcn), str(self.rxlev), self.evaluation, self.discovery_time @@ -59,6 +68,11 @@ class BaseStationInformation: pass def create_report(self): + #TODO: remove this after scans with new data model are available + self.locationprovider = 'NONE' + self.longitude = 0 + self.latitude = 0 + report_params = '''------- Base Station Parameters ----------- Country: %s Provider: %s @@ -68,8 +82,11 @@ BSIC: %s LAC: %s Cell ID: %s Neighbours: %s +Latitude: %s +Longitude: %s +Location Provider: %s Evaluation: %s\n -'''%(self.country,self.provider, self.arfcn, self.rxlev, self.bsic, self.lac, self.cell, ', '.join(map(str,self.get_neighbour_arfcn())),self.evaluation) +'''%(self.country,self.provider, self.arfcn, self.rxlev, self.bsic, self.lac, self.cell, ', '.join(map(str,self.get_neighbour_arfcn())),self.latitude,self.longitude,self.locationprovider,self.evaluation) report_rules ='------- Rule Results -----------\n' for key in self.rules_report.keys(): @@ -125,6 +142,8 @@ class BaseStationInformationList: for item in filtered_list: store.append(item.get_list_model()) + def _get_unfiltered_list(self): + return self._base_station_list def _get_filtered_list(self, band_filter, filters): filtered_list = [] @@ -160,4 +179,5 @@ class BaseStationInformationList: station.rules_report = rule_results.copy() station.evaluation, station.evaluation_report = evaluator.evaluate(rule_results) station.evaluation_by = evaluator.identifier - \ No newline at end of file + + diff --git a/Src/PyCatcher/src/pyCatcherView.py b/Src/PyCatcher/src/pyCatcherView.py index 6cb33a4..e39a247 100644 --- a/Src/PyCatcher/src/pyCatcherView.py +++ b/Src/PyCatcher/src/pyCatcherView.py @@ -161,7 +161,11 @@ class PyCatcherGUI: def _on_evaluators_clicked(self, widget): self._evaluators_window.show() - + + def _on_evaluators_window_close_clicked(self, window, event): + window.hide() + return True + def _on_rules_clicked(self, widget): self._rules_window.show() diff --git a/Src/PyCatcher/src/settings.py b/Src/PyCatcher/src/settings.py index 9d94054..8f5c002 100644 --- a/Src/PyCatcher/src/settings.py +++ b/Src/PyCatcher/src/settings.py @@ -44,11 +44,12 @@ ARFCN_mapping = { LAC_threshold = 0.5 -BSIC_database = '' +#Database Configuration ---------------------------------------------------------------------------------------- + +Open_Cell_ID_Key = 'd7a5bc3f21b44d4bf93d1ec2b3f83dc4' +Open_Cell_ID_Local = '' -Cell_ID_database = '' -Home_database = '' #Evaluator Configuration --------------------------------------------------------------------------------------- -- cgit v1.2.3-55-g7522