5 (C) 2008 Gergely Imreh <imrehg@gmail.com>
7 cell_locator: Baruch Even <baruch@ev-en.org>
13 Uses input file: cellinfo.dat
14 Contain : Locations of known cells
15 Format : comma-separated values
16 Fields : mmc,mnc,cell_id,lattitude,longitude
21 import sys
, os
, serial
, time
, dbus
, csv
22 from time
import strftime
25 ### GPS import functions from stripped down version of cell_locator.py
26 from cell_locator_bare
import Terminal
27 from cell_locator_bare
import GPS
41 def nmealocform(pos
,padd
=2):
42 """ Convert location into NMEA format """
48 nmeapos
= str(a
).zfill(padd
)+'%02.5f'%b
51 def nmeasentence(gsmpos
,hdop
=99):
52 """ Prepare minimal information needed for tangoGPS location display """
53 """ Shows: location, date, time, number of seen/known towers (at seen/fixed satellite fields """
54 """ for info on sentences: http://gpsd.berlios.de/NMEA.txt and check gpsd output """
56 nmeatime
= strftime("%H%M%S.00")
57 nmeadate
= strftime("%d%m%y")
61 numfixtower
= gsm
.numtower
62 numtottower
= gsm
.tottower
64 if (lon
== None) or (lat
== None):
65 ## When no GSM position: send 'Navigation device warning' and 'no fix' sentences
66 sentence
= "$GPRMC,"+nmeatime
+",V,,,,,0.0,0.0,"+nmeadate
+",,\n"
67 sentence
+= '$GPGGA,'+strftime("%H%M%S.00")+',,,,,0,,,0,M,0,M,,\n'
68 sentence
+= "$GPGSV,1,0,,,,,,,,,,,,,,,,,\n"
72 printlon
= nmealocform(lon
,3)
75 printlon
= nmealocform(-1*lon
,3)
78 printlat
= nmealocform(lat
,2)
81 printlat
= nmealocform(-1*lat
,2)
83 sentence
+= "$GPRMC,"+nmeatime
+",A,"+printlat
+","+ns
+","+printlon
+","+ew
+",0.0,0.0,"+nmeadate
+",,\n"
84 sentence
+= '$GPGGA,'+strftime("%H%M%S.00")+','+printlat
+','+ns
+','+printlon
+','+ew
+',1,'+str(numfixtower
)+','+str(hdop
)+',0,M,0,M,,\n'
85 sentence
+= "$GPGSV,1,1,"+str(numtottower
)+",,,,,,,,,,,,,,,,\n"
89 def addtangogpsdatabase():
90 """ Adding known cell towers to tangoGPS POI database """
91 """ Tagged as Services/Other, with a common keywords field for easy fitering """
94 cells
= loadcellinfo('cellinfo.dat')
96 conn
= sqlite3
.connect('/.tangogps/poi.db')
98 # First delete everything, clean start every time
99 c
.execute('delete from poi where keywords like "gsmpos%"')
103 comment
= 'MCC: '+str(cline
['mcc'])+', MNC:'+str(cline
['mnc'])
104 key
= 'gsmpos CellID '+str(cline
['cell_id'])
105 indat
= (lat
,lon
,key
,comment
,1,10,6,3,0)
106 c
.execute('insert into poi (lat,lon,keywords,desc,visibility,cat,subcat,price_range,extended_open) values (?,?,?,?,?,?,?,?,?)', indat
)
108 etype
= sys
.exc_info()[0]
109 evalue
= sys
.exc_info()[1]
110 etb
= sys
.exc_info()[2]
111 print "tangoGPS POI Add Error: ", str(etype
), " : ", str(evalue
) , ' : ', str(etb
)
117 if __name__
== "__main__":
119 cells
= loadcellinfo('cellinfo.dat')
121 from dbus
.mainloop
.glib
import DBusGMainLoop
122 DBusGMainLoop(set_as_default
=True)
125 loop
= gobject
.MainLoop()
134 # Init GSM position storage
137 # Adding known cells to tangoGPS POI database
138 addtangogpsdatabase()
141 # Quick and dirty server section
151 for res
in socket
.getaddrinfo(HOST
, PORT
, socket
.AF_UNSPEC
, socket
.SOCK_STREAM
, 0, socket
.AI_PASSIVE
):
152 af
, socktype
, proto
, canonname
, sa
= res
154 s
= socket
.socket(af
, socktype
, proto
)
155 s
.setsockopt(socket
.SOL_SOCKET
, socket
.SO_REUSEADDR
, 1) # Important for using again after unexpected crash!
156 except socket
.error
, msg
:
162 except socket
.error
, msg
:
169 print 'could not open socket'
172 print "Awaiting connection..."
175 conn
, addr
= s
.accept()
179 print 'Connected by', addr
183 # If manual connection, have to press a button to start, tangoGPS automatic
184 data
= conn
.recv(1024)
193 nmeaout
= nmeasentence(gsm
)
197 except (KeyboardInterrupt, SystemExit):
199 print "KeyboardInterrupt - Clean Exit"
204 print "Exception:", sys
.exc_type
, ":", sys
.exc_value
219 print "Closing GPS - wait for it!!"