|
- package net.hoo2.auth.vmodem;
-
- import java.util.Arrays;
-
- class GPS {
- static final int GPS_BUFFER_SIZE = 256;
- static final String GPS_BEGIN = "START ITHAKI GPS TRACKING";
- static final String GPS_END = "STOP ITHAKI GPS TRACKING";
- static final int GPS_USE_TRACK = 1;
- static final String GPS_TRACK_PREFIX = "R=";
- static final String GPS_IMAGE_PREFIX = "T=";
- static final int GPS_MAX_POINTS = 9;
- static final int GPS_COORDINATES_SIZE = 6;
-
- static final int GPS_LATITUDE_BEGIN = 17;
- static final int GPS_LATITUDE_END = 28;
- static final int GPS_LONGITUDE_BEGIN= 29;
- static final int GPS_LONGITUDE_END = 41;
-
- private Com com_;
- private Log log_;
- private Transaction transaction_;
- private byte[] code_;
- private int start_;
- private int duration_;
- private int points_;
- private byte[] coordinates_;
-
- GPS (Com com, Log log, byte[] code, int start, int duration, int points) {
- com_ = com;
- log_ = log;
- transaction_= new Transaction(null, new byte[GPS_BUFFER_SIZE]);
- code_ = code;
- start_ = start;
- duration_ = duration;
- points_ = (points <= GPS_MAX_POINTS) ? points : GPS_MAX_POINTS;
- coordinates_= new byte[GPS_COORDINATES_SIZE];
- }
-
- void caption () {
- String line;
-
- line = "Running GPS with: " + new String(code_)
- + " time [" + start_ + " - " +(start_+duration_) + ") sec" + " [" + points_ + " points]";
- log_.write(line, true);
-
- transaction_ = com_.request (transaction_, null, null, null, false);
- line = new String(transaction_.getResponse());
- log_.out(line);
- }
-
- String run () {
- String code, image_code;
- String line;
-
- log_.out("Get traces");
- image_code = new String(code_);
- for (int trace =start_ ; trace < start_+duration_ ; trace += duration_/points_) {
- code = new String(code_) + GPS_TRACK_PREFIX + GPS_USE_TRACK + String.format("%04d", trace) + "01";
- transaction_ = com_.request(transaction_, code.getBytes(), GPS_BEGIN.getBytes(), GPS_END.getBytes(), false);
- line = new String(transaction_.code) + ": "
- + new String(transaction_.getResponse())
- + " Tr= " + (transaction_.arrival - transaction_.departure) + " [msec]";
- log_.write(line);
-
- _get_coordinates(transaction_.getResponse());
- image_code += GPS_IMAGE_PREFIX +
- String.format("%02d%02d%02d%02d%02d%02d", coordinates_[0], coordinates_[1],
- coordinates_[2], coordinates_[3],
- coordinates_[4], coordinates_[5]);
-
- }
- return image_code;
- }
-
- private byte[] _get_coordinates (byte[] stream) {
- int start = Com.detect(stream, "GPGGA".getBytes(), stream.length);
- double latitude = _nmea2dec (Double.valueOf(
- new String (Arrays.copyOfRange(stream,
- start+GPS_LATITUDE_BEGIN,
- start+GPS_LATITUDE_END-2))));
- double longitude = _nmea2dec (Double.valueOf(
- new String(Arrays.copyOfRange(stream,
- start+GPS_LONGITUDE_BEGIN,
- start+GPS_LONGITUDE_END-2))));
-
- coordinates_[0] = (byte)(longitude); // longitude deg
- coordinates_[1] = (byte)((longitude - coordinates_[0]) * 60.0); // longitude '
- coordinates_[2] = (byte)((longitude - coordinates_[0]
- - coordinates_[1]/60.0)*3600); // longitude "
-
- coordinates_[3] = (byte)(latitude); // latitude deg
- coordinates_[4] = (byte)((latitude - coordinates_[3]) * 60.0); // latitude '
- coordinates_[5] = (byte)((latitude - coordinates_[3]
- - coordinates_[4]/60.0)*3600); // latitude "
- return coordinates_;
- }
-
- double _nmea2dec (double c) {
- int d = (int)c/100;
- c -= d*100;
- return d + (c/60);
- }
- }
|