Change: separating core functions from server
[CellLocator.git] / gsmpos / gsmpos_sever.py
blob8344362d80e911cc6ac10065a021cd57558d4a29
1 #!/usr/bin/env python
2 """
3 GSM Positioning
5 (C) 2008 Gergely Imreh <imrehg@gmail.com>
6 --- using code from:
7 cell_locator: Baruch Even <baruch@ev-en.org>
9 GPLv3 or later
10 """
11 """
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
18 """
21 import sys, os, serial, time, dbus, csv
22 from time import strftime
23 from math import *
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
27 from gsmpos import *
29 __version__ = '0.2'
32 ###############
34 # Debugging output
35 debug = False
37 ################
40 def nmealocform(pos,padd=2):
41 """ Convert location into NMEA format """
42 if pos == '':
43 nmeapos = ''
44 else :
45 a = int(pos)
46 b = (pos-a)*60.0
47 nmeapos = str(a).zfill(padd)+'%02.5f'%b
48 return nmeapos
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")
58 lon = gsm.lon
59 lat = gsm.lat
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"
68 else:
69 if (lon >= 0):
70 ew = "E"
71 printlon = nmealocform(lon,3)
72 else:
73 ew = "W"
74 printlon = nmealocform(-1*lon,3)
75 if (lat >= 0):
76 ns = "N"
77 printlat = nmealocform(lat,2)
78 else:
79 ns = "S"
80 printlat = nmealocform(-1*lat,2)
81 sentence = ''
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"
86 return sentence
90 if __name__ == "__main__":
92 cells = loadcellinfo('cellinfo.dat')
94 from dbus.mainloop.glib import DBusGMainLoop
95 DBusGMainLoop(set_as_default=True)
97 import gobject
98 loop = gobject.MainLoop()
100 # Start GPS
101 gps = GPS()
102 gps.init()
104 t = Terminal()
105 t.open()
107 # Init GSM position storage
108 gsm = GSMpos()
111 ##################
112 # Quick and dirty server section
113 #################
114 import socket
116 ##########
117 HOST = 'localhost'
118 PORT = 2940
119 ##########
121 s = None
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
124 try:
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:
128 s = None
129 continue
130 try:
131 s.bind(sa)
132 s.listen(1)
133 except socket.error, msg:
134 s.close()
135 s = None
136 continue
137 break
139 if s is None:
140 print 'could not open socket'
141 sys.exit(1)
142 while 1:
143 print "Awaiting connection..."
144 run = 1
145 try:
146 conn, addr = s.accept()
147 except:
148 break
150 print 'Connected by', addr
151 while 1:
153 try :
154 # If manual connection, have to press a button to start, tangoGPS automatic
155 data = conn.recv(1024)
156 except:
157 conn.close()
158 run = 0
160 if data:
161 while 1:
162 try:
163 looping(t,gsm,cells)
164 nmeaout = nmeasentence(gsm)
165 print nmeaout
166 conn.send(nmeaout)
167 time.sleep(2.0)
168 except (KeyboardInterrupt, SystemExit):
169 conn.close()
170 print "KeyboardInterrupt - Clean Exit"
171 run = 0
172 break
173 except :
174 conn.close()
175 print "Exception:", sys.exc_type, ":", sys.exc_value
176 run = 0
177 break
178 else:
179 break
180 print "Level 2"
181 if run ==0 : break
182 print "Level 1"
183 if run ==0 : break
184 print "Level 0"
187 # Closing down
188 print "Closing GSM"
189 t.close()
190 print "Closing GPS - wait for it!!"
191 gps.uninit()