#    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
oldcid=0
cid18="n/a"
cid17="n/a"
cid16="n/a"
cid15="n/a"
cid14="n/a"
cid13="n/a"
cid12="n/a"
cid11="n/a"
cid10="n/a"
cid9="n/a"
cid8="n/a"
cid7="n/a"
cid6="n/a"
cid5="n/a"
cid4="n/a"
cid3="n/a"
cid2="n/a"
cid1="n/a"

size,offset = appuifw.app.layout(appuifw.EMainPane)

img=Image.new((800,352))
img.rectangle((0,0)+size,outline=border,fill=bg)
img.text((50,20), u'PyNetMony 0.2.1',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():
	appuifw.app.menu = [(u"Netmonitor", netmonitor),(u"Settings", settings)]
	appuifw.app.set_tabs([u"Stats", u"Graph", u"History"], set_tab)
	appuifw.app.activate_tab(tab)

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 log_rxl(rxl):
	global rxl_log, rxl_last
	# TODO: only update every second
	# rescue last 1000 entries
	t = time.time()
	if t != rxl_last:
		rxl_log = rxl_log[-999:] + [rxl]
		rxl_last = t

def draw_rxlgraph(size):
	global rxl_log
	sublog = rxl_log[-size[0]:]
	line = zip(range(len(sublog)), sublog)
	img.line(line, outline=linecol)
	img.text((size[0]-80, 20), u"-" + unicode(rxl_log[-1:][0]) + u"dBm", color)
	
	
def netmonitor():
	global index, oldcid, cid1,cid2,cid3,cid4,cid5,cid6,cid7,cid8,cid9,cid10,cid11,cid12,cid13,cid14,cid15,cid16,cid17,cid18
	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),0xff0000,fill=(0,0,60))
		#img.rectangle((0,0)+size,0xff0000,fill=(0,0,60))
		#img.point((50.,150.),0xff0000,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"
		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()
		ram = sysinfo.free_ram()/1024
		totram = sysinfo.total_ram()/1024
		x = appuifw.app.layout(appuifw.EScreen)
		img.text((20,20), u'CLK: '+unicode(time.strftime("%H:%M:%S")),fill=color)
		img.text((20,40), u'CID: '+unicode(cid),fill=color)
		if (cid != oldcid) and (oldcid!="n/a") and (oldcid!=cid1):
				cid18=cid17
				cid17=cid16
				cid16=cid15
				cid15=cid14
				cid14=cid13
				cid13=cid12
				cid12=cid11
				cid11=cid10
				cid10=cid9
				cid9=cid8
				cid8=cid7
				cid7=cid6
				cid6=cid5
				cid5=cid4
				cid4=cid3
				cid3=cid2
				cid2=cid1
				cid1=oldcid
		if tab==0:
			img.text((20,60), u'LAC: '+unicode(lac),fill=color)
			img.text((20,100), u'NET: '+unicode(mcc)+"   "+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)
		elif tab==1:
			draw_rxlgraph(size)
		elif tab==2:
			img.text((5,60), u'1: '+unicode(cid1),fill=color)
			img.text((5,80), u'2: '+unicode(cid2),fill=color)
			img.text((5,100), u'3: '+unicode(cid3),fill=color)
			img.text((5,120), u'4: '+unicode(cid4),fill=color)
			img.text((5,140), u'5: '+unicode(cid5),fill=color)
			img.text((5,160), u'6: '+unicode(cid6),fill=color)
			img.text((5,180), u'7: '+unicode(cid7),fill=color)
			img.text((5,200), u'8: '+unicode(cid8),fill=color)
			img.text((5,220), u'9: '+unicode(cid9),fill=color)
			img.text((110,60), u'10: '+unicode(cid10),fill=color)
			img.text((110,80), u'11: '+unicode(cid11),fill=color)
			img.text((110,100), u'12: '+unicode(cid12),fill=color)
			img.text((110,120), u'13: '+unicode(cid13),fill=color)
			img.text((110,140), u'14: '+unicode(cid14),fill=color)
			img.text((110,160), u'15: '+unicode(cid15),fill=color)
			img.text((110,180), u'16: '+unicode(cid16),fill=color)
			img.text((110,200), u'17: '+unicode(cid17),fill=color)
			img.text((110,220), u'18: '+unicode(cid18),fill=color)
		if index==(0,):
			e32.reset_inactivity()
		handle_redraw(())
		oldcid=cid
		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
	script_lock.signal()
	#appuifw.app.set_exit()
	running=0
	
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 ...")

menus_setup()
appuifw.app.exit_key_handler = exit_key_handler
script_lock.wait()