2 * gps - gps resource for zavai
4 * Copyright (C) 2009--2010 Enrico Zini <enrico@enricozini.org>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 // For a list of dbus services, look in /etc/dbus-1/system.d/
27 public class GPS: zavai.ScriptService
29 protected libgps.data_t data;
30 protected IOChannel gpsfd = null;
31 protected uint gpsfd_watch = 0;
33 protected int old_fix_status = libgps.STATUS_NO_FIX;
34 protected uint old_time = 0;
35 protected double old_lat = 1000;
36 protected double old_lon = 1000;
38 public signal void fix_status_changed(int status);
39 public signal void time_changed(uint time);
40 public signal void pos_changed();
45 data = libgps.data_t();
46 started = script_status();
49 public int fix_status() { return old_fix_status; }
50 public double time() { return old_time; }
51 public weak libgps.data_t info() { return data; }
53 protected bool on_input_data(IOChannel source, IOCondition condition)
55 while (libgps.waiting(ref data))
57 int res = libgps.poll(ref data);
59 zavai.log.error(libgps.errstr(res));
61 if (data.status != old_fix_status)
63 fix_status_changed(data.status);
64 old_fix_status = data.status;
67 uint cur_time = (uint)data.fix.time;
68 if (data.status != libgps.STATUS_NO_FIX && old_time != cur_time)
70 time_changed(cur_time);
74 double lat = (data.status == libgps.STATUS_NO_FIX ? 1000 : data.fix.latitude);
75 double lon = (data.status == libgps.STATUS_NO_FIX ? 1000 : data.fix.longitude);
76 if (lat != old_lat || lon != old_lon)
79 stderr.printf("GPSMSG %d %d\n", (int)data.set, data.status);
80 stderr.printf("SATUSED %d\n", data.satellites_used);
81 stderr.printf("SWT %f\n", data.skyview_time);
82 stderr.printf("SATVIS %d\n", data.satellites_visible);
83 for (int i = 0; i < data.satellites_visible; ++i)
85 stderr.printf("PRN %d ELE %d AZI %d SS %f\n",
86 data.PRN[i], data.elevation[i], data.azimuth[i], data.ss[i]);
94 /// Request GPS resource
95 public override void start()
99 if (!script_start()) return;
101 zavai.log.info("Connecting to gpsd at " + config.gpsd_host + ":" + config.gpsd_port);
102 int res = libgps.open_r(config.gpsd_host, config.gpsd_port, ref data);
105 zavai.log.error(libgps.errstr(res));
109 res = libgps.stream(ref data, libgps.WATCH_ENABLE, null);
112 zavai.log.error(libgps.errstr(res));
116 //res = libgps.send(ref data, "?SKY;");
117 //res = libgps.send(ref data, "?WATCH;");
118 //if (res != 0) zavai.log.error(libgps.errstr(res));
120 gpsfd = new IOChannel.unix_new(data.gps_fd);
122 gpsfd.set_encoding(null);
124 zavai.log.error("Setting encoding to null on gpsd io channel: " + e.message);
126 //gpsfd.set_buffered(false);
127 gpsfd_watch = gpsfd.add_watch(IOCondition.IN, on_input_data);
129 zavai.log.info("GPS turned on");
133 // Release usage of GPS
134 public override void stop()
136 if (!started) return;
138 Source.remove(gpsfd_watch);
140 int res = libgps.close(ref data);
142 zavai.log.error(libgps.errstr(res));
146 if (old_fix_status != libgps.STATUS_NO_FIX)
148 old_fix_status = libgps.STATUS_NO_FIX;
149 fix_status_changed(old_fix_status);
155 time_changed(old_time);
162 // # def start_recording(self):
163 // # if self.gps_monitor:
164 // # self.gps_monitor.stop()
165 // # self.gps_monitor = None
167 // # if not self.audio:
170 // # # Sync system time
171 // # gpstime = self.gps.gps_time.GetTime()
172 // # subprocess.call(["date", "-s", "@%d" % gpstime])
173 // # subprocess.call(["hwclock", "--systohc"])
175 // # # Compute basename for output files
176 // # self.basename = time.strftime("%Y-%m-%d-%H-%M-%S", time.localtime(gpstime))
177 // # self.basename = os.path.join(AUDIODIR, self.basename)
179 // # # Start recording the GPX track
180 // # self.gpx = GPX(self.basename)
181 // # self.gps.track_position(self.on_position_changed)
183 // # # Start recording in background forking arecord
184 // # self.audio.set_basename(self.basename)
185 // # self.audio.start_recording()
188 // Write GPX track and waypoint files
189 public class GPX : Service
191 protected uint wpt_seq = 0;
192 protected uint log_id = 0;
196 Object(name: "gps.gpx");
199 public override void start()
203 log_id = log.log.start("track", "GPS track");
208 public override void stop()
219 public void waypoint(string? name = null)
221 if (log_id == 0) return;
226 wptname = "wpt_%u".printf(wpt_seq);
232 log.log.add(log_id, wptname);
236 public zavai.gps.GPS gps = null;
237 public zavai.gps.GPX gpx = null;