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 int res = libgps.poll(ref data);
57 zavai.log.error(libgps.errstr(res));
59 if (data.status != old_fix_status)
61 fix_status_changed(data.status);
62 old_fix_status = data.status;
65 uint cur_time = (uint)data.fix.time;
66 if (data.status != libgps.STATUS_NO_FIX && old_time != cur_time)
68 time_changed(cur_time);
72 double lat = (data.status == libgps.STATUS_NO_FIX ? 1000 : data.fix.latitude);
73 double lon = (data.status == libgps.STATUS_NO_FIX ? 1000 : data.fix.longitude);
74 if (lat != old_lat || lon != old_lon)
78 stderr.printf("GPSMSG %d %d\n", (int)data.set, data.status);
79 stderr.printf("SATUSED %d\n", data.satellites_used);
80 stderr.printf("SWT %f\n", data.skyview_time);
81 stderr.printf("SATVIS %d\n", data.satellites_visible);
82 for (int i = 0; i < data.satellites_visible; ++i)
84 stderr.printf("PRN %d ELE %d AZI %d SS %f\n",
85 data.PRN[i], data.elevation[i], data.azimuth[i], data.ss[i]);
91 /// Request GPS resource
92 public override void start()
96 if (!script_start()) return;
98 zavai.log.info("Connecting to gpsd at " + config.gpsd_host + ":" + config.gpsd_port);
99 int res = libgps.open_r(config.gpsd_host, config.gpsd_port, ref data);
102 zavai.log.error(libgps.errstr(res));
106 res = libgps.stream(ref data, libgps.WATCH_ENABLE, null);
109 zavai.log.error(libgps.errstr(res));
113 //res = libgps.send(ref data, "?SKY;");
114 //res = libgps.send(ref data, "?WATCH;");
115 //if (res != 0) zavai.log.error(libgps.errstr(res));
117 gpsfd = new IOChannel.unix_new(data.gps_fd);
119 gpsfd.set_encoding(null);
121 zavai.log.error("Setting encoding to null on gpsd io channel: " + e.message);
123 //gpsfd.set_buffered(false);
124 gpsfd_watch = gpsfd.add_watch(IOCondition.IN, on_input_data);
126 zavai.log.info("GPS turned on");
130 // Release usage of GPS
131 public override void stop()
133 if (!started) return;
135 Source.remove(gpsfd_watch);
137 int res = libgps.close(ref data);
139 zavai.log.error(libgps.errstr(res));
143 if (old_fix_status != libgps.STATUS_NO_FIX)
145 old_fix_status = libgps.STATUS_NO_FIX;
146 fix_status_changed(old_fix_status);
152 time_changed(old_time);
159 // # def start_recording(self):
160 // # if self.gps_monitor:
161 // # self.gps_monitor.stop()
162 // # self.gps_monitor = None
164 // # if not self.audio:
167 // # # Sync system time
168 // # gpstime = self.gps.gps_time.GetTime()
169 // # subprocess.call(["date", "-s", "@%d" % gpstime])
170 // # subprocess.call(["hwclock", "--systohc"])
172 // # # Compute basename for output files
173 // # self.basename = time.strftime("%Y-%m-%d-%H-%M-%S", time.localtime(gpstime))
174 // # self.basename = os.path.join(AUDIODIR, self.basename)
176 // # # Start recording the GPX track
177 // # self.gpx = GPX(self.basename)
178 // # self.gps.track_position(self.on_position_changed)
180 // # # Start recording in background forking arecord
181 // # self.audio.set_basename(self.basename)
182 // # self.audio.start_recording()
185 // Write GPX track and waypoint files
186 public class GPX : Service
188 protected uint wpt_seq = 0;
189 protected zavai.log.Log log = null;
193 Object(name: "gps.gpx");
196 public override void start()
200 log = zavai.log.log.start("track", "GPS track");
205 public override void stop()
209 zavai.log.log.end(log);
216 public void waypoint(string? name = null)
218 if (log == null) return;
223 wptname = "wpt_%u".printf(wpt_seq);
233 public zavai.gps.GPS gps = null;
234 public zavai.gps.GPX gpx = null;