#    PyNetMony Netmonitor for S60 3rd Edition phones
#
#    Copyright (C) 2007  Carsten Knuetter, Georg Lukas
#
#    email: netmonitor.s80@o2online.de
#
#    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 3 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, see <http://www.gnu.org/licenses/>.


import appuifw
import e32
import location
import sysinfo
import time
from graphics import *


index=0
running=1
light=True
tab=0


font=u"normal"
color=(255,255,255)
linecol=(255,128,128)
border=(255,0,0)
bg=(0,0,60)

rxl_log = []
rxl_last = 0

loc_log = []

img=Image.new((800,352))

def screen_setup():
	global img
	size,offset = appuifw.app.layout(appuifw.EMainPane)

	img.rectangle((0,0)+size,outline=border,fill=bg)
	img.text((50,20), u'PyNetMony 0.2.2',fill=color)
	img.text((75,40), u'(c) 2007 by',fill=color)
	img.text((10,60), u'Carsten Knütter, Georg Lukas',fill=color)
	img.text((10,90), u'netmoitor.s80@o2online.de',fill=color)
	img.text((5,110), u'This program comes with',fill=color)
	img.text((5,130), u'ABSOLUTELY NO WARRANTY',fill=color)
	img.text((5,160), u'This is free software, and you',fill=color)
	img.text((5,180), u'are welcome to redistribute',fill=color)
	img.text((5,200), u'it under certain conditions',fill=color)
	#img.text((2,210), u'certain conditions',fill=color)
	img.text((35,230), u'Press Options to start... ',fill=color)

def menus_setup():
	global tab
	appuifw.app.menu = [(u"Netmonitor", netmonitor),(u"Orientation", rotate),(u"Settings", settings)]
	appuifw.app.set_tabs([u"NetMon", u"Graph", u"History"], set_tab)
	appuifw.app.activate_tab(tab)
	
def rotate():
	if appuifw.app.orientation == 'landscape':
		appuifw.app.orientation = 'portrait'
	else:
		appuifw.app.orientation = 'landscape'

def set_tab(newtab):
	global tab
	tab = newtab
	handle_redraw(())

def settings():
	global index
	L = [u'Light on']
	index = appuifw.multi_selection_list(L , style='checkmark', search_field=0)
	#light = (index == (0,))
	handle_redraw(())
	

def decode_cid(mcc, mnc, cid):
	if (mnc == 7) and (mcc == 262) and (cid > 65535):
		cidhex=hex(cid)
		rnc=int(cidhex[2:4],16)
		cid=int(cidhex[-4:],16)
		return (cid, rnc)
	else:
		return (cid, 'n/a')


def log_rxl(t, rxl):
	global rxl_log, rxl_last
	if rxl_last == 0:
		rxl_last = t - 1
	while t > rxl_last:
		# rescue last 1000 entries
		rxl_log = rxl_log[-999:] + [rxl]
		rxl_last += 1

def log_loc(t, gsmloc):
	global loc_log
	if len(loc_log) > 0 and gsmloc == loc_log[-1:][0][1]:
		return
	loc_log += [[t, gsmloc]]

def draw_netmon(lac, mcc, mnc, rnc, rxl, ver, imei):
	bars = sysinfo.signal_bars()
	batty = sysinfo.battery()
	ram = sysinfo.free_ram()/1024
	totram = sysinfo.total_ram()/1024
	x = appuifw.app.layout(appuifw.EScreen)
	img.text((20,60), u'LAC: '+unicode(lac),fill=color)
	img.text((20,100), u'NET: '+unicode(mcc)+u" - "+unicode(mnc),fill=color)
	img.text((20,80), u'RNC: '+unicode(rnc),fill=color)
	img.text((20,120), u'RXL: -'+unicode(rxl)+u" dBm ("+unicode(bars)+u")",fill=color)
	img.text((20,140), u'BAT: '+unicode(batty)+u" %",fill=color)
	#img.text((20,160), u'RAM: '+unicode(ram)+u" KB",fill=color)
	img.text((20,160), u'RAM: '+unicode(ram)+u" / "+unicode(totram)+u" KB",fill=color)
	img.text((20,180), u'VER: '+unicode(ver),fill=color)
	img.text((20,200), u'IMEI: '+imei,fill=color)
	img.text((20,220), u'RES: '+unicode(x),fill=color)
	#img.text((20,220), unicode(index),fill=color)

def draw_rxl_line(size, pos, text):
	sz = (size[1]-2, 14)
	i = Image.new(sz, '1')
	i.rectangle((0,0)+sz, fill=0x000000)
	i.text((2,11), text, 0xffffff)
	irot = i.transpose(ROTATE_90)
	img.blit(irot, target=pos, mask=irot)
	#img.text((0,200), unicode(sz)+unicode(pos)+text, color)

def draw_rxlgraph(size):
	global rxl_log, loc_log
	t = time.time()
	w = size[0]
	h = size[1]
	sublog = rxl_log[-w:]
	xcoord = range(w-len(sublog)+1, w+1)
	line = zip(xcoord, sublog)
	img.line(line, outline=linecol)
	x = w
	l = len(loc_log)-1
	#img.text((0,h-36),unicode(loc_log),linecol)
	while x > -60 and l >= 0:
		offset = t - loc_log[l][0]
		(mcc, mnc, lac, cid) = loc_log[l][1]
		x -= offset
		img.line((x,0,x,h),outline=linecol)
		draw_rxl_line(size, (x+1, 1), unicode(mcc)+"-"+unicode(mnc)+u"  CID: "+unicode(cid)+u"  LAC: "+unicode(lac))
		l-=1
	img.text((w-80, 20), u"-" + unicode(rxl_log[-1:][0]) + u"dBm", color)
		
def draw_history_line(y, tstamp, net, lac, cid, rnc):
	img.text((20, y), unicode(tstamp), color)
	img.text((80, y), unicode(net), color)
	img.text((120, y), unicode(lac), color)
	img.text((160, y), unicode(cid), color)
	#if rnc != 'n/a':
	img.text((200, y), unicode(rnc), color)

def draw_history(size):
	global loc_log
	y = 80
	count = (size[1]-y)/16
	#img.text((0, 10), unicode(count), color)
	draw_history_line(y-16, "Time", "Net", "LAC", "CID", "RNC")
	for v in loc_log[-count:]:
		t = time.strftime("%H:%M:%S ", time.localtime(v[0]))
		(mcc, mnc, lac, cid) = v[1]
		(cid, rnc) = decode_cid(mcc, mnc, cid)
		draw_history_line(y, t, unicode(mcc)+"-"+unicode(mnc), \
			lac, cid, rnc)
		y += 16
		




	
def netmonitor():
	global index
	imei=sysinfo.imei()
	ver = sysinfo.os_version()
	while running:
		size,offset = appuifw.app.layout(appuifw.EMainPane)
		#clear(color=(0,0,60))
		#img.line((20,20,20,120),0xff00ee)
		#img.rectangle((0,0,240,235),border,fill=bg)
		#img.rectangle((0,0)+size,border,fill=bg)
		#img.point((50.,150.),border,width=40)
		#img.ellipse((100,150,150,180),0x0000ff)
		#img.text((100,80), u'hello')
		img.rectangle((0,0)+size,outline=border,fill=bg)
		rnc="n/a"
		t = time.time()
		gsmloc = location.gsm_location()
		rxl = sysinfo.signal_dbm()
		if gsmloc is None:
			mcc = mnc = lac = cid = rnc = cid = "n/a"
		else:
			log_loc(t, gsmloc)
			(mcc, mnc, lac, cid) = gsmloc
			(cid, rnc) = decode_cid(mnc, mcc, cid)
		log_rxl(t, rxl)
		if tab==0:
			draw_netmon(lac, mcc, mnc, rnc, rxl, ver, imei)
		elif tab==1:
			draw_rxlgraph(size)
		elif tab==2:
			draw_history(size)
		# CLK and CID always on top
		img.text((20,20), u'CLK: '+unicode(time.strftime("%H:%M:%S")),fill=color)
		img.text((20,40), u'CID: '+unicode(cid),fill=color)
		handle_redraw(())
		#if light:
		if index==(0,):
			e32.reset_inactivity()
		e32.ao_sleep(0.5)
		#e32.ao_yield()
		
def handle_redraw(rect):
	canvas.blit(img)
	
def exit_key_handler():
	global script_lock, running
	script_lock.signal()
	#appuifw.app.set_exit()
	running=0
	appuifw.app.set_tabs(None)
	
canvas=appuifw.Canvas(event_callback=None, redraw_callback=handle_redraw)
appuifw.app.screen='normal'
appuifw.app.title = u"PyNetMony"
appuifw.app.body=canvas
#appuifw.app.body = appuifw.Text(u"Press Options button below ...")

script_lock = e32.Ao_lock()

screen_setup()
menus_setup()
appuifw.app.exit_key_handler = exit_key_handler
netmonitor()
script_lock.wait()
