Network programming assignment for University
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

105 line
4.2 KiB

  1. package net.hoo2.auth.vmodem;
  2. import java.util.Arrays;
  3. class GPS {
  4. static final int GPS_BUFFER_SIZE = 256;
  5. static final String GPS_BEGIN = "START ITHAKI GPS TRACKING";
  6. static final String GPS_END = "STOP ITHAKI GPS TRACKING";
  7. static final int GPS_USE_TRACK = 1;
  8. static final String GPS_TRACK_PREFIX = "R=";
  9. static final String GPS_IMAGE_PREFIX = "T=";
  10. static final int GPS_MAX_POINTS = 9;
  11. static final int GPS_COORDINATES_SIZE = 6;
  12. static final int GPS_LATITUDE_BEGIN = 17;
  13. static final int GPS_LATITUDE_END = 28;
  14. static final int GPS_LONGITUDE_BEGIN= 29;
  15. static final int GPS_LONGITUDE_END = 41;
  16. private Com com_;
  17. private Log log_;
  18. private Transaction transaction_;
  19. private byte[] code_;
  20. private int start_;
  21. private int duration_;
  22. private int points_;
  23. private byte[] coordinates_;
  24. GPS (Com com, Log log, byte[] code, int start, int duration, int points) {
  25. com_ = com;
  26. log_ = log;
  27. transaction_= new Transaction(null, new byte[GPS_BUFFER_SIZE]);
  28. code_ = code;
  29. start_ = start;
  30. duration_ = duration;
  31. points_ = (points <= GPS_MAX_POINTS) ? points : GPS_MAX_POINTS;
  32. coordinates_= new byte[GPS_COORDINATES_SIZE];
  33. }
  34. void caption () {
  35. String line;
  36. line = "Running GPS with: " + new String(code_)
  37. + " time [" + start_ + " - " +(start_+duration_) + ") sec" + " [" + points_ + " points]";
  38. log_.write(line, true);
  39. transaction_ = com_.request (transaction_, null, null, null, false);
  40. line = new String(transaction_.getResponse());
  41. log_.out(line);
  42. }
  43. String run () {
  44. String code, image_code;
  45. String line;
  46. log_.out("Get traces");
  47. image_code = new String(code_);
  48. for (int trace =start_ ; trace < start_+duration_ ; trace += duration_/points_) {
  49. code = new String(code_) + GPS_TRACK_PREFIX + GPS_USE_TRACK + String.format("%04d", trace) + "01";
  50. transaction_ = com_.request(transaction_, code.getBytes(), GPS_BEGIN.getBytes(), GPS_END.getBytes(), false);
  51. line = new String(transaction_.code) + ": "
  52. + new String(transaction_.getResponse())
  53. + " Tr= " + (transaction_.arrival - transaction_.departure) + " [msec]";
  54. log_.write(line);
  55. _get_coordinates(transaction_.getResponse());
  56. image_code += GPS_IMAGE_PREFIX +
  57. String.format("%02d%02d%02d%02d%02d%02d", coordinates_[0], coordinates_[1],
  58. coordinates_[2], coordinates_[3],
  59. coordinates_[4], coordinates_[5]);
  60. }
  61. return image_code;
  62. }
  63. private byte[] _get_coordinates (byte[] stream) {
  64. int start = Com.detect(stream, "GPGGA".getBytes(), stream.length);
  65. double latitude = _nmea2dec (Double.valueOf(
  66. new String (Arrays.copyOfRange(stream,
  67. start+GPS_LATITUDE_BEGIN,
  68. start+GPS_LATITUDE_END-2))));
  69. double longitude = _nmea2dec (Double.valueOf(
  70. new String(Arrays.copyOfRange(stream,
  71. start+GPS_LONGITUDE_BEGIN,
  72. start+GPS_LONGITUDE_END-2))));
  73. coordinates_[0] = (byte)(longitude); // longitude deg
  74. coordinates_[1] = (byte)((longitude - coordinates_[0]) * 60.0); // longitude '
  75. coordinates_[2] = (byte)((longitude - coordinates_[0]
  76. - coordinates_[1]/60.0)*3600); // longitude "
  77. coordinates_[3] = (byte)(latitude); // latitude deg
  78. coordinates_[4] = (byte)((latitude - coordinates_[3]) * 60.0); // latitude '
  79. coordinates_[5] = (byte)((latitude - coordinates_[3]
  80. - coordinates_[4]/60.0)*3600); // latitude "
  81. return coordinates_;
  82. }
  83. double _nmea2dec (double c) {
  84. int d = (int)c/100;
  85. c -= d*100;
  86. return d + (c/60);
  87. }
  88. }