summaryrefslogtreecommitdiff
path: root/altoslib/AltosIdleMonitor.java
blob: ae3b7b06b94748aa7c3862d3688baecb8c13d2a0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/*
 * Copyright © 2010 Keith Packard <keithp@keithp.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 2 of the License.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
 */

package org.altusmetrum.AltosLib;

import java.io.*;
import java.util.concurrent.*;


public class AltosIdleMonitor extends Thread {
	AltosLink		link;
	AltosIdleMonitorListener	listener;
	AltosState		state;
	boolean			remote;
	double			frequency;
	AltosState		previous_state;
	AltosConfigData		config_data;
	AltosGPS		gps;

	int AltosRSSI() throws TimeoutException, InterruptedException {
		link.printf("s\n");
		String line = link.get_reply_no_dialog(5000);
		if (line == null)
			throw new TimeoutException();
		String[] items = line.split("\\s+");
		if (items.length < 2)
			return 0;
		if (!items[0].equals("RSSI:"))
			return 0;
		int rssi = Integer.parseInt(items[1]);
		return rssi;
	}

	void update_state() throws InterruptedException, TimeoutException {
		AltosRecord	record = null;

		try {
			if (remote) {
				link.set_radio_frequency(frequency);
				link.start_remote();
			} else
				link.flush_input();
			config_data = new AltosConfigData(link);
			if (config_data.product.startsWith("TeleMetrum")) {
				record = new AltosSensorTM(link, config_data);
			} else if (config_data.product.startsWith("MegaMetrum")) {
				AltosRecordMM record_mm = new AltosRecordMM();
				AltosSensorMM sensor = new AltosSensorMM(link);
				AltosMs5607 ms5607 = new AltosMs5607Query(link);
				AltosIMU imu = new AltosIMUQuery(link);

				record_mm.accel_plus_g = config_data.accel_cal_plus;
				record_mm.accel_minus_g = config_data.accel_cal_minus;

				record_mm.ground_accel = sensor.accel;
				record_mm.accel = sensor.accel;
				record_mm.ground_pres = ms5607.pa;
				record_mm.pres = ms5607.pa;
				record_mm.temp = ms5607.cc;

				record_mm.v_batt = sensor.v_batt;
				record_mm.v_pyro = sensor.v_pyro;
				record_mm.sense = sensor.sense;

				record_mm.imu = imu;

				record = record_mm;
			} else
				record = new AltosRecord();

			gps = new AltosGPSQuery(link, config_data);

			record.version = 0;
			record.callsign = config_data.callsign;
			record.serial = config_data.serial;
			record.flight = config_data.log_available() > 0 ? 255 : 0;
			record.status = 0;
			record.state = AltosLib.ao_flight_idle;
			record.gps = gps;
			record.new_gps = true;
			state = new AltosState (record, state);
		} finally {
			if (remote) {
				link.stop_remote();
				if (record != null)
					record.rssi = AltosRSSI();
			} else {
				if (record != null)
					record.rssi = 0;
			}
		}

	}

	public void set_frequency(double in_frequency) {
		frequency = in_frequency;
	}

	public void post_state() {
		listener.update(state);
	}

	public void run() {
		try {
			for (;;) {
				try {
					update_state();
					post_state();
				} catch (TimeoutException te) {
				}
				Thread.sleep(1000);
			}
		} catch (InterruptedException ie) {
			link.close();
		}
	}

	public AltosIdleMonitor(AltosIdleMonitorListener in_listener, AltosLink in_link, boolean in_remote)
		throws FileNotFoundException, InterruptedException, TimeoutException {
		listener = in_listener;
		link = in_link;
		remote = in_remote;
		state = null;
	}
}