Friday, December 21, 2007

Continuous Cell Tracking And Logging With Series 60 Python

I have now a modified version of mycellinfo script that I posted earlier. In this version, you can:
  • track and log your cell sites continuously,
  • periodically update the location name when you change over to a major location, and
  • store complete URLs that you can use later to register the cell sites with www.cellspotting.com
I tried registering the cell sites automatically into cellspotting, but unfortunately it appears that I need an internet APN with access to DNS. I keep getting name resolution errors with whatever mechanism I tried. So you'll find some commented code intended to hit cellspotting while on the go. If you have an internet APN and can try these out do let me know!

Here's the new script.

# Simple app to display and store current cell info and signal strength.

import e32
import appuifw, socket, urllib, httplib
import location, sysinfo, time

class my_cell_id:
def __init__(self):
self.text = u""
self.autolog = False
self.prev_mcc = self.mcc = 0
self.prev_mnc = self.mnc = 0
self.prev_lac = self.lac = 0
self.prev_lacstr = self.lacstr = u""
self.prev_cellid = self.cellid = 0
self.prev_cellidstr = self.cellidstr = u""
self.apid = None
self.apo = None

def remove_auto_log(self):
self.autolog = False

def set_curr_location(self, curr_location_new):
self.curr_location = curr_location_new

def set_auto_log(self, file_name, curr_location):
self.autolog = True
self.the_file = file_name
self.curr_location = curr_location
#self.apid = socket.select_access_point()
#self.apo = socket.access_point(self.apid)
#self.apo.start()
#socket.set_default_access_point(self.apo)

def _check_and_log(self):
if self.autolog == True:
if not self.prev_mcc == self.mcc or not self.prev_mnc == self.mnc or not self.prev_lac == self.lac or not self.prev_cellid == self.cellid:
self._write_to_db()
self._register_cell()

def _write_to_db(self):
w_file = open(self.the_file,'a') # w = open for reading and writing
w_file.write(u"" + `self.mcc` + u", " + `self.mnc` + u", " + self.lacstr + u", " + self.cellidstr + u", " + self.curr_location + u"\n")
w_file.close()

def _register_cell(self):
#url = u"GET "
url = u"http://www.cellspotting.com"
url += u"/thin/registercell.php?network="
url += `self.mcc`
url += `self.mnc`
url += u"&area="
url += self.lacstr
url += u"&cell="
url += self.cellidstr
url += u"&imei="
url += sysinfo.imei()
url += u"&lat=&lon=&cellinfo=&cellname="
url += urllib.quote_plus(self.curr_location)
url += u"\r\n\r\n"

#s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
#s.connect(('www.cellspotting.com',80))
#s.send(’GET /\r\n\r\n’)
#s.send(url)
#recvdata = s.recv(100)
#s.close()

#u = urllib.urlopen(url)
#recvdata = u.readline()

#urllib.urlretrieve(url)
#recvdata = ""
#urllib.urlcleanup()

#proxies = {'http': 'http://100.1.200.99:8080/'}
#opener = urllib.FancyURLopener(proxies)
#f = opener.open(url)
#recvdata = f.read()

#conn = httplib.HTTPConnection("www.cellspotting.com")
#conn.request("GET", url)
#r1 = conn.getresponse()
#print r1.status, r1.reason
#recvdata = r1.read()

w_file = open(self.the_file+u"_url",'a') # w = open for reading and writing
w_file.write(url)
w_file.close()

def get_new_data(self):
self.prev_mcc = self.mcc
self.prev_mnc = self.mnc
self.prev_lac = self.lac
self.prev_lacstr = self.lacstr
self.prev_cellid = self.cellid
self.prev_cellidstr = self.cellidstr
self.mcc, self.mnc, self.lac, self.cellid = location.gsm_location()

self.lacstr = hex(self.lac)
self.lacstr = self.lacstr[2:]
while len(self.lacstr) != 4:
self.lacstr = u"0" + self.lacstr

self.cellidstr = hex(self.cellid)
self.cellidstr = self.cellidstr[2:]
while len(self.cellidstr) != 4:
self.cellidstr = u"0" + self.cellidstr

self._check_and_log()
self.bars = sysinfo.signal_bars()
self.dbm = sysinfo.signal_dbm()
self.sigpct = self.bars*100/7

def _lookup_mcc(self, mcc):
if 404 == mcc or 405 == mcc:
return u"India"
else:
return u"Unregistered Country"

def get_display_text(self):
self.text = u"Mobile Country Code: %s (%s)\nMobile Network Code: %s\nLocation Area Code: %s (%s)\nCell ID: %s (%s)\nSignal Strength: %s(%s%%) - %sdbm\nTime: %s\n" % (self.mcc, self._lookup_mcc(self.mcc), self.mnc, self.lac, self.lacstr, self.cellid, self.cellidstr, self.bars, self.sigpct, self.dbm, time.asctime())
return self.text

def close(self):
self.text = u""
if not self.apo == None:
self.apo.stop()
pass

e32.ao_yield()

class my_cell_info:
def __init__(self):
self.lock = e32.Ao_lock()
self.timerstarted = 0

self.old_title = appuifw.app.title
appuifw.app.title = u"My Cell Info"

self.exit_flag = False
appuifw.app.exit_key_handler = self.abort

appuifw.app.body = appuifw.Text()
appuifw.app.body.set(u"Choose \"Fetch\" from options to fetch or refresh cell info anytime")
self.dataobj = my_cell_id()

self.timerobj = e32.Ao_timer()

self.menu_fetch = (u"Fetch", self.handle_fetch)
self.menu_auto_refresh_start = (u"Start Auto Fetch", self.handle_start_timer)
self.menu_auto_refresh_end = (u"Stop Auto Fetch", self.handle_stop_timer)
self.menu_start_auto_tag = (u"Start Auto Tag", self.handle_start_auto_tag)
self.menu_stop_auto_tag = (u"Stop Auto Tag", self.handle_stop_auto_tag)
self.menu_set_auto_tag_location = (u"Set Auto Tag Location", self.handle_auto_tag_location)
appuifw.app.menu = [self.menu_fetch, self.menu_auto_refresh_start, self.menu_start_auto_tag]

def loop(self):
try:
self.lock.wait()
while not self.exit_flag:
self.refresh()
self.lock.wait()
finally:
self.dataobj.close()

def close(self):
appuifw.app.menu = []
appuifw.app.body = None
appuifw.app.exit_key_handler = None
appuifw.app.title = self.old_title
self.timerstarted = 0
self.timerobj.cancel()

def abort(self):
# Exit-key handler.
self.exit_flag = True
self.lock.signal()

def refresh(self):
if 1 == self.timerstarted:
self.dataobj.get_new_data()
appuifw.app.body.set(self.dataobj.get_display_text())
if 1 == self.timerstarted:
self.timerobj.after(5, self.refresh)

def handle_fetch(self):
self.dataobj.get_new_data()
self.refresh()

def handle_start_timer(self):
self.timerstarted = 1
appuifw.app.menu = [self.menu_auto_refresh_end, self.menu_start_auto_tag]
self.refresh()

def handle_stop_timer(self):
self.timerstarted = 0
appuifw.app.menu = [self.menu_fetch, self.menu_auto_refresh_start, self.menu_start_auto_tag]
self.timerobj.cancel()

def handle_auto_tag_location(self):
newloc = None
while None == newloc:
newloc = appuifw.query(u"Enter Location", 'text', u"")
self.dataobj.set_curr_location(newloc)

def handle_start_auto_tag(self):
newloc = None
while None == newloc:
newloc = appuifw.query(u"Enter Location", 'text', u"")
newfile = None
tempfile = u"c:\\Data\\" + newloc + u".cellinfo"
while None == newfile:
newfile = appuifw.query(u"File to save as", 'text', tempfile)
self.dataobj.set_auto_log(newfile, newloc)
self.timerstarted = 1
self.refresh()
appuifw.app.menu = [self.menu_stop_auto_tag, self.menu_set_auto_tag_location]

def handle_stop_auto_tag(self):
self.dataobj.remove_auto_log()
self.timerstarted = 0
self.timerobj.cancel()
appuifw.app.menu = [self.menu_fetch, self.menu_auto_refresh_start, self.menu_start_auto_tag]

def main():
app = my_cell_info()
try:
app.loop()
finally:
app.close()

if __name__ == "__main__":
main()

I have started using it when I am traveling to create a log of cell sites and register them into www.cellspotting.com offline.

2 comments:

Mkononi said...

Hi There,

Did you get the sysinfo.signal_dbm() to work properly as on my N95 it doesn't update the value.

Cheers

Toby

tan said...

Hi Toby,

signal_dbm() has been working fine for me on a Nokia E61. Haven't tried on a N95 yet, but will check once I get a chance. Do you mean to say that it has an initial value but the value does not get updated even if signal strength changes?

- tan