# PyNetMony

import appuifw
import e32
import location
import sysinfo
import time
from graphics import *

light=True
running=1
tab=0

font=u"normal"
color=(255,255,255)
bg=(0,0,60)

rxl_log = []

img=Image.new((240,235))
img.rectangle((0,0,240,235),0xff0000, bg)
img.text((30,80), u'Welcome to PyNetMony!', color, font)
img.text((30,100), u'(c) by Carsten Knütter', color, font)
img.text((65,120), u'Version 0.1.5', color, font)
img.text((35,230), u'Press Options to start... ', color, font)

def main_menu_setup():
	appuifw.app.menu = [(u"Netmonitor", netmonitor),
			    (u"Toggle Light", toggle_light),
			    (u"Settings", settings),
			    (u"Quit", exit_key_handler)]
	
def tab_setup():
	appuifw.app.set_tabs([u"All", u"Graph"], set_tab)
	appuifw.app.activate_tab(tab)

def set_tab(newtab):
	global tab
	tab = newtab
	handle_redraw(())

def toggle_light():
	global light
	light = not light
	
def settings():
	handle_redraw(())

def log_rxl(rxl):
	global rxl_log
	rxl_log += [rxl]

def draw_rxlgraph():
	global rxl_log
	sublog = rxl_log[-240:]
	line = zip(range(len(sublog)), sublog)
	img.line(line, outline=0xff8080)
	img.text((160, 20), u"-" + unicode(rxl_log[-1:][0]) + u"dBm", color)
	
def netmonitor():
	global running, light, tab
	imei=sysinfo.imei()
	ver = sysinfo.os_version()
	size,offset = appuifw.app.layout(appuifw.EMainPane)
	while running:
		#clear(color=(0,0,60))
		#img.line((20,20,20,120),0xff00ee)
		img.rectangle((0,0)+size,0xff0000, bg)
		#img.point((50.,150.),0xff0000,width=40)
		#img.ellipse((100,150,150,180),0x0000ff)
		#img.text((100,80), u'hello')
		rnc="n/a"
		gsmloc = location.gsm_location()
		if gsmloc is None:
			mcc = mnc = lac = cid = rnc = cid = "n/a"
		else:
			(mcc, mnc, lac, cid) = gsmloc
			if (mnc == 7) and (mcc == 262) and (cid > 65535):
				cidhex=hex(cid)
				rnc=int(cidhex[2:4],16)
				cid=int(cidhex[len(cidhex)-4:len(cidhex)],16)
		bars = sysinfo.signal_bars()
		rxl = sysinfo.signal_dbm()
		log_rxl(rxl)
		batty = sysinfo.battery()
		if light:
			e32.reset_inactivity()
		ram = sysinfo.free_ram()/1024
		totram = sysinfo.total_ram()/1024
		x = appuifw.app.layout(appuifw.EScreen)
		img.text((40,20), u'Time: '+unicode(time.strftime("%H:%M:%S")), color)
		if tab == 0:
			img.text((40,40), u'CID: '+unicode(cid), color, font)
			img.text((40,60), u'LAC: '+unicode(lac), color, font)
			img.text((40,100), u'NET: '+unicode(mcc)+"   "+unicode(mnc), color, font)
			img.text((40,80), u'RNC: '+unicode(rnc), color, font)
			img.text((40,120), u'RXL: -'+unicode(rxl)+u" dBm ("+unicode(bars)+u")", color, font)
			img.text((40,140), u'BAT: '+unicode(batty)+u" %", color, font)
			img.text((40,160), u'RAM: '+unicode(ram)+u" / "+unicode(totram)+u" KB", color, font)
			img.text((40,180), u'VER: '+unicode(ver), color, font)
			img.text((40,200), u'IMEI: '+imei, color, font)
			img.text((40,220), u'RES: '+unicode(x), color, font)
		elif tab == 1:
			draw_rxlgraph()
		handle_redraw(())
		e32.ao_sleep(0.5)
		#e32.ao_yield()
		
def handle_redraw(rect):
	canvas.blit(img)
	
canvas=appuifw.Canvas(event_callback=None, redraw_callback=handle_redraw)

def exit_key_handler():
	global script_lock, running
	running=0
	script_lock.signal()
	#appuifw.app.set_exit()
	
appuifw.app.screen='normal'

script_lock = e32.Ao_lock()

appuifw.app.title = u"PyNetMony"
appuifw.app.body=canvas

#appuifw.app.body = appuifw.Text(u"Press Options button below ...")

main_menu_setup()
tab_setup()
appuifw.app.exit_key_handler = exit_key_handler
netmonitor()
script_lock.wait()
