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
24 ### GPS import functions from stripped down version of cell_locator.py
25 from cell_locator_bare
import Terminal
26 from cell_locator_bare
import GPS
40 def nmealocform(pos
,padd
=2):
41 """ Convert location into NMEA format """
47 nmeapos
= str(a
).zfill(padd
)+'%02.5f'%b
50 def nmeasentence(gsmpos
,hdop
=99):
51 """ Prepare minimal information needed for tangoGPS location display """
52 """ Shows: location, date, time, number of seen/known towers (at seen/fixed satellite fields """
53 """ for info on sentences: http://gpsd.berlios.de/NMEA.txt and check gpsd output """
55 nmeatime
= strftime("%H%M%S.00")
56 nmeadate
= strftime("%d%m%y")
60 numfixtower
= gsm
.numtower
61 numtottower
= gsm
.tottower
63 if (lon
== None) or (lat
== None):
64 ## When no GSM position: send 'Navigation device warning' and 'no fix' sentences
65 sentence
= "$GPRMC,"+nmeatime
+",V,,,,,0.0,0.0,"+nmeadate
+",,\n"
66 sentence
+= '$GPGGA,'+strftime("%H%M%S.00")+',,,,,0,,,0,M,0,M,,\n'
67 sentence
+= "$GPGSV,1,0,,,,,,,,,,,,,,,,,\n"
71 printlon
= nmealocform(lon
,3)
74 printlon
= nmealocform(-1*lon
,3)
77 printlat
= nmealocform(lat
,2)
80 printlat
= nmealocform(-1*lat
,2)
82 sentence
+= "$GPRMC,"+nmeatime
+",A,"+printlat
+","+ns
+","+printlon
+","+ew
+",0.0,0.0,"+nmeadate
+",,\n"
83 sentence
+= '$GPGGA,'+strftime("%H%M%S.00")+','+printlat
+','+ns
+','+printlon
+','+ew
+',1,'+str(numfixtower
)+','+str(hdop
)+',0,M,0,M,,\n'
84 sentence
+= "$GPGSV,1,1,"+str(numtottower
)+",,,,,,,,,,,,,,,,\n"
90 if __name__
== "__main__":
92 cells
= loadcellinfo('cellinfo.dat')
94 from dbus
.mainloop
.glib
import DBusGMainLoop
95 DBusGMainLoop(set_as_default
=True)
98 loop
= gobject
.MainLoop()
107 # Init GSM position storage
112 # Quick and dirty server section
122 for res
in socket
.getaddrinfo(HOST
, PORT
, socket
.AF_UNSPEC
, socket
.SOCK_STREAM
, 0, socket
.AI_PASSIVE
):
123 af
, socktype
, proto
, canonname
, sa
= res
125 s
= socket
.socket(af
, socktype
, proto
)
126 s
.setsockopt(socket
.SOL_SOCKET
, socket
.SO_REUSEADDR
, 1) # Important for using again after unexpected crash!
127 except socket
.error
, msg
:
133 except socket
.error
, msg
:
140 print 'could not open socket'
143 print "Awaiting connection..."
146 conn
, addr
= s
.accept()
150 print 'Connected by', addr
154 # If manual connection, have to press a button to start, tangoGPS automatic
155 data
= conn
.recv(1024)
164 nmeaout
= nmeasentence(gsm
)
168 except (KeyboardInterrupt, SystemExit):
170 print "KeyboardInterrupt - Clean Exit"
175 print "Exception:", sys
.exc_type
, ":", sys
.exc_value
190 print "Closing GPS - wait for it!!"