gpsd.php.in 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081
  1. <?php
  2. # @MASTER@
  3. # @GENERATED@
  4. # Copyright 2006 Chris Kuethe <chris.kuethe@gmail.com>
  5. #
  6. # This file is Copyright 2009 The GPSD project
  7. # SPDX-License-Identifier: BSD-2-clause
  8. # This code validates with https://validator.w3.org/
  9. # Keep it valid.
  10. global $head, $blurb, $title, $showmap, $autorefresh, $footer, $gmap_key;
  11. global $server, $advertise, $port, $open, $swap_ew, $testmode;
  12. global $colors;
  13. $testmode = 1; # leave this set to 1
  14. # Public script parameters:
  15. # host: host name or address where GPSd runs. Default: from config file
  16. # port: port of GPSd. Default: from config file
  17. # op=view: show just the skyview image instead of the whole HTML page
  18. # sz=small: used with op=view, display a small (240x240px) skyview
  19. # op=json: respond with the GPSd POLL JSON structure
  20. # jsonp=prefix: used with op=json, wrap the POLL JSON in parentheses
  21. # and prepend prefix
  22. # To draw the skyview, make sure that GD is installed and configured with PHP
  23. # If you're running PHP with the Suhosin patch (like the Debian PHP5 package),
  24. # it may be necessary to increase the value of the
  25. # suhosin.get.max_value_length parameter to 2048. The imgdata parameter used
  26. # for displaying the skyview is longer than the default 512 allowed by Suhosin.
  27. # Debian has the config file at /etc/php5/conf.d/suhosin.ini.
  28. # If you use the OPenLayers code you will likely want to server
  29. # their JS locally.
  30. # this script shouldn't take more than a few seconds to run
  31. set_time_limit(3);
  32. ini_set('max_execution_time', 3);
  33. if (!file_exists("gpsd_config.inc"))
  34. write_config();
  35. require_once("gpsd_config.inc");
  36. # sample data
  37. $resp = <<<EOF
  38. {"class":"POLL","time":"2010-04-05T21:27:54.84Z","active":1,
  39. "tpv":[{"class":"TPV","tag":"MID41","device":"/dev/ttyUSB0",
  40. "time":1270517264.240,"ept":0.005,"lat":40.035093060,
  41. "lon":-75.519748733,"alt":31.1,"track":99.4319,
  42. "speed":0.123,"mode":3}],
  43. "sky":[{"class":"SKY","tag":"MID41","device":"/dev/ttyUSB0",
  44. "time":"2010-04-05T21:27:44.84Z","hdop":9.20,"vdop":12.1,
  45. "satellites":[{"PRN":16,"el":55,"az":42,"ss":36,"used":true},
  46. {"PRN":19,"el":25,"az":177,"ss":0,"used":false},
  47. {"PRN":7,"el":13,"az":295,"ss":0,"used":false},
  48. {"PRN":6,"el":56,"az":135,"ss":32,"used":true},
  49. {"PRN":13,"el":47,"az":304,"ss":0,"used":false},
  50. {"PRN":23,"el":66,"az":259,"ss":40,"used":true},
  51. {"PRN":20,"el":7,"az":226,"ss":0,"used":false},
  52. {"PRN":3,"el":52,"az":163,"ss":32,"used":true},
  53. {"PRN":31,"el":16,"az":102,"ss":0,"used":false}
  54. ]
  55. }
  56. ]
  57. }
  58. EOF;
  59. # if we're passing in a query, let's unpack and use it
  60. $op = isset($_GET['op']) ? $_GET['op'] : '';
  61. if (isset($_GET['imgdata']) && $op == 'view'){
  62. $resp = base64_decode($_GET['imgdata']);
  63. if ($resp){
  64. gen_image($resp);
  65. exit(0);
  66. }
  67. } else {
  68. if (isset($_GET['host']))
  69. if (!preg_match('/[^a-zA-Z0-9\.-]/', $_GET['host']))
  70. $server = $_GET['host'];
  71. if (isset($_GET['port']))
  72. if (!preg_match('/\D/', $_GET['port']) &&
  73. ($port > 0) && ($port < 65536))
  74. $port = $_GET['port'];
  75. if ($testmode){
  76. $sock = @fsockopen($server, $port, $errno, $errstr, 2);
  77. @fwrite($sock, "?WATCH={\"enable\":true}\n");
  78. usleep(1000);
  79. @fwrite($sock, "?POLL;\n");
  80. usleep(1000);
  81. for($tries = 0; $tries < 10; $tries++){
  82. $resp = @fread($sock, 10000); # SKY can be pretty big
  83. if (preg_match('/{"class":"POLL".+}/i', $resp, $m)){
  84. $resp = $m[0];
  85. break;
  86. }
  87. }
  88. @fclose($sock);
  89. if (!$resp)
  90. $resp = '{"class":"ERROR","message":"no response from GPS daemon"}';
  91. }
  92. }
  93. # ensure all satellites keys exist, for clean logs.
  94. function sat_clean($sat) {
  95. $skeys = array('az', 'el', 'gnssid', 'health', 'PRN', 'ss', 'svid', 'used');
  96. foreach($skeys as $key) {
  97. if (!array_key_exists($key, $sat)) {
  98. $sat[$key] = 'n/a';
  99. }
  100. }
  101. return $sat;
  102. }
  103. # format $arr[$field], adding $unit, optionally add $errkey/$errunit
  104. # otherwise return 'n/a' or 'n/a</td><td>'
  105. function field_str($arr, $key, $unit, $errkey = '', $errunit = '') {
  106. if (!array_key_exists($key, $arr)) {
  107. if ($errkey) {
  108. return 'n/a</td><td>&nbsp;';
  109. } else {
  110. return 'n/a';
  111. }
  112. }
  113. if ('' == $errunit) {
  114. $errunit = $unit;
  115. }
  116. $ret = strval($arr[$key]);
  117. if ('' != $unit) {
  118. $ret .= " " . $unit;
  119. if ($errkey) {
  120. $ret .= "</td><td>&nbsp;" ;
  121. if (array_key_exists($errkey, $arr)) {
  122. $ret .= "&plusmn;" . strval($arr[$errkey]) .
  123. "&nbsp;" . $errunit;
  124. }
  125. }
  126. }
  127. return $ret;
  128. }
  129. if ($op == 'view')
  130. gen_image($resp);
  131. else if ($op == 'json')
  132. write_json($resp);
  133. else
  134. write_html($resp);
  135. exit(0);
  136. function colorsetup($im){
  137. global $colors;
  138. $C['white'] = imageColorAllocate($im, 255, 255, 255);
  139. $C['ltgray'] = imageColorAllocate($im, 191, 191, 191);
  140. $C['mdgray'] = imageColorAllocate($im, 127, 127, 127);
  141. $C['dkgray'] = imageColorAllocate($im, 63, 63, 63);
  142. $C['black'] = imageColorAllocate($im, 0, 0, 0);
  143. $C['red'] = imageColorAllocate($im, 236, 50, 31);
  144. $C['brightgreen'] = imageColorAllocate($im, 0, 255, 0);
  145. $C['darkgreen'] = imageColorAllocate($im, 0, 192, 0);
  146. $C['blue'] = imageColorAllocate($im, 0, 0, 255);
  147. $C['cyan'] = imageColorAllocate($im, 0, 255, 255);
  148. $C['magenta'] = imageColorAllocate($im, 255, 0, 255);
  149. $C['yellow'] = imageColorAllocate($im, 255, 255, 0);
  150. $C['burntyellow'] = imageColorAllocate($im, 199, 163, 23);
  151. $C['orange'] = imageColorAllocate($im, 255, 128, 0);
  152. $colors = $C;
  153. return $C;
  154. }
  155. function legend($im, $sz, $C){
  156. $r = 30;
  157. $fn = 5;
  158. $x = $sz - (4*$r+7) - 2;
  159. $y = $sz - $r - 3;
  160. imageFilledRectangle($im, $x, $y, $x + 4*$r + 7, $y + $r +1,
  161. $C['dkgray']);
  162. imageRectangle($im, $x+0*$r+1, $y+1, $x + 1*$r + 0, $y + $r,
  163. $C['red']);
  164. imageRectangle($im, $x+1*$r+2, $y+1, $x + 2*$r + 2, $y + $r,
  165. $C['burntyellow']);
  166. imageRectangle($im, $x+2*$r+4, $y+1, $x + 3*$r + 4, $y + $r,
  167. $C['darkgreen']);
  168. imageRectangle($im, $x+4*$r+6, $y+1, $x + 3*$r + 6, $y + $r,
  169. $C['brightgreen']);
  170. imageString($im, $fn, $x+3+0*$r, $y+$r/3, "<30", $C['red']);
  171. imageString($im, $fn, $x+5+1*$r, $y+$r/3, "30+", $C['burntyellow']);
  172. imageString($im, $fn, $x+7+2*$r, $y+$r/3, "35+", $C['darkgreen']);
  173. imageString($im, $fn, $x+9+3*$r, $y+$r/3, "40+", $C['brightgreen']);
  174. }
  175. function radial($angle, $sz){
  176. #turn into radians
  177. $angle = deg2rad($angle);
  178. # determine length of radius
  179. $r = $sz * 0.5 * 0.95;
  180. # and convert length/azimuth to cartesian
  181. $x0 = sprintf("%d", (($sz * 0.5) - ($r * cos($angle))));
  182. $y0 = sprintf("%d", (($sz * 0.5) - ($r * sin($angle))));
  183. $x1 = sprintf("%d", (($sz * 0.5) + ($r * cos($angle))));
  184. $y1 = sprintf("%d", (($sz * 0.5) + ($r * sin($angle))));
  185. return array($x0, $y0, $x1, $y1);
  186. }
  187. function azel2xy($az, $el, $sz){
  188. global $swap_ew;
  189. #rotate coords... 90deg E = 180deg trig
  190. $az += 270;
  191. #turn into radians
  192. $az = deg2rad($az);
  193. # determine length of radius
  194. $r = $sz * 0.5 * 0.95;
  195. $r -= ($r * ($el/90));
  196. # and convert length/azimuth to cartesian
  197. $x = sprintf("%d", (($sz * 0.5) + ($r * cos($az))));
  198. $y = sprintf("%d", (($sz * 0.5) + ($r * sin($az))));
  199. if ($swap_ew != 0)
  200. $x = $sz - $x;
  201. return array($x, $y);
  202. }
  203. function imageCircle($im, $x, $y, $r, $color, $filled){
  204. $t = $r / 2;
  205. if ($filled) {
  206. imageFilledArc($im, $x, $y, $r, $r, 0, 360, $color, 0);
  207. } else {
  208. imageArc($im, $x, $y, $r, $r, 0, 360, $color);
  209. }
  210. }
  211. function imageDiamond($im, $x, $y, $r, $color, $filled){
  212. $t = $r / 2;
  213. $vx = array($x + $t, $y, $x, $y + $t, $x - $t, $y, $x, $y - $t,
  214. $x + $t, $y);
  215. if ($filled) {
  216. imageFilledPolygon($im, $vx, 5, $color);
  217. } else {
  218. imagepolygon($im, $vx, 5, $color);
  219. }
  220. }
  221. function imageSquare($im, $x, $y, $r, $color, $filled){
  222. global $colors;
  223. $t = $r / 2;
  224. $vx = array($x + $t, $y + $t,
  225. $x + $t, $y - $t,
  226. $x - $t, $y - $t,
  227. $x - $t, $y + $t,
  228. $x + $t, $y + $t);
  229. if ($filled) {
  230. imageFilledPolygon($im, $vx, 5, $color);
  231. } else {
  232. imagepolygon($im, $vx, 5, $color);
  233. }
  234. }
  235. # Triangle pointing down
  236. function imageTriangleD($im, $x, $y, $r, $color, $filled){
  237. $t = $r / 2;
  238. $vx = array($x, $y + $t,
  239. $x + $t, $y - $t,
  240. $x - $t, $y - $t,
  241. $x, $y + $t);
  242. if ($filled) {
  243. imageFilledPolygon($im, $vx, 4, $color);
  244. } else {
  245. imagepolygon($im, $vx, 4, $color);
  246. }
  247. }
  248. # Triangle pointing up
  249. function imageTriangleU($im, $x, $y, $r, $color, $filled){
  250. $t = $r / 2;
  251. $vx = array($x, $y - $t,
  252. $x - $t, $y + $t,
  253. $x + $t, $y + $t,
  254. $x, $y - $t);
  255. if ($filled) {
  256. imageFilledPolygon($im, $vx, 4, $color);
  257. } else {
  258. imagepolygon($im, $vx, 4, $color);
  259. }
  260. }
  261. function splot($im, $sz, $C, $e){
  262. # ensure all $e keys exist, for clean logs.
  263. $keys = array('PRN', 'az', 'el', 'gnssid', 'ss');
  264. foreach($keys as $key) {
  265. if (!array_key_exists($key, $e)) {
  266. return;
  267. }
  268. if (!is_numeric($e[$key])) {
  269. return;
  270. }
  271. }
  272. if (!array_key_exists('used', $e)) {
  273. return;
  274. }
  275. if (!array_key_exists('health', $e) ||
  276. !is_numeric($e['health'])) {
  277. $e['health'] = 0;
  278. }
  279. # validate ranges
  280. if ((0 >= $e['PRN']) || (0 > $e['az']) || (0 > $e['el']) ||
  281. (0 > $e['ss'])) {
  282. return;
  283. }
  284. $color = $C['brightgreen'];
  285. if ($e['ss'] < 40)
  286. $color = $C['darkgreen'];
  287. if ($e['ss'] < 35)
  288. $color = $C['burntyellow'];
  289. if ($e['ss'] < 30)
  290. $color = $C['red'];
  291. if ($e['el']<10)
  292. $color = $C['blue'];
  293. if ($e['ss'] < 10)
  294. $color = $C['black'];
  295. list($x, $y) = azel2xy($e['az'], $e['el'], $sz);
  296. $r = 12;
  297. if (isset($_GET['sz']) && ($_GET['sz'] == 'small'))
  298. $r = 8;
  299. imageString($im, 3, $x+4, $y+4, $e['PRN'], $C['black']);
  300. imagesetthickness($im, 2);
  301. switch ($e['gnssid']) {
  302. case 0:
  303. # GPS
  304. # FALLTHROUGH
  305. case 5:
  306. # QZSS
  307. imageCircle($im, $x, $y, $r, $color, $e['used']);
  308. break;
  309. case 1:
  310. # SBAS
  311. # FALLTHROUGH
  312. case 4:
  313. # IMES
  314. # FALLTHROUGH
  315. default:
  316. imageDiamond($im, $x, $y, $r, $color, $e['used']);
  317. break;
  318. case 2:
  319. # Galileo
  320. imageTriangleU($im, $x, $y, $r, $color, $e['used']);
  321. break;
  322. case 3:
  323. # BeiDou
  324. imageTriangleD($im, $x, $y, $r, $color, $e['used']);
  325. break;
  326. case 6:
  327. # GLONASS
  328. imageSquare($im, $x, $y, $r, $color, $e['used']);
  329. break;
  330. }
  331. }
  332. function elevation($im, $sz, $C, $a){
  333. $b = 90 - $a;
  334. $a = $sz * 0.95 * ($a/180);
  335. imageArc($im, $sz/2, $sz/2, $a*2, $a*2, 0, 360, $C['ltgray']);
  336. $x = $sz/2 - 16;
  337. $y = $sz/2 - $a;
  338. imageString($im, 2, $x, $y, $b, $C['ltgray']);
  339. }
  340. function skyview($im, $sz, $C){
  341. global $swap_ew;
  342. $a = 90; $a = $sz * 0.95 * ($a/180);
  343. imageFilledArc($im, $sz/2, $sz/2, $a*2, $a*2, 0, 360, $C['mdgray'], 0);
  344. imageArc($im, $sz/2, $sz/2, $a*2, $a*2, 0, 360, $C['black']);
  345. $x = $sz/2 - 16; $y = $sz/2 - $a;
  346. imageString($im, 2, $x, $y, "0", $C['ltgray']);
  347. $a = 85; $a = $sz * 0.95 * ($a/180);
  348. imageFilledArc($im, $sz/2, $sz/2, $a*2, $a*2, 0, 360, $C['white'], 0);
  349. imageArc($im, $sz/2, $sz/2, $a*2, $a*2, 0, 360, $C['ltgray']);
  350. imageString($im, 1, $sz/2 - 6, $sz+$a, '5', $C['black']);
  351. $x = $sz/2 - 16; $y = $sz/2 - $a;
  352. imageString($im, 2, $x, $y, "5", $C['ltgray']);
  353. for($i = 0; $i < 180; $i += 15){
  354. list($x0, $y0, $x1, $y1) = radial($i, $sz);
  355. imageLine($im, $x0, $y0, $x1, $y1, $C['ltgray']);
  356. }
  357. for($i = 15; $i < 90; $i += 15)
  358. elevation($im, $sz, $C, $i);
  359. $x = $sz/2 - 16; $y = $sz/2 - 8;
  360. /* imageString($im, 2, $x, $y, "90", $C['ltgray']); */
  361. imageString($im, 4, $sz/2 + 4, 2 , 'N', $C['black']);
  362. imageString($im, 4, $sz/2 + 4, $sz - 16 , 'S', $C['black']);
  363. if ($swap_ew != 0){
  364. imageString($im, 4, 4 , $sz/2 + 4, 'E', $C['black']);
  365. imageString($im, 4, $sz - 10 , $sz/2 + 4, 'W', $C['black']);
  366. } else {
  367. imageString($im, 4, 4 , $sz/2 + 4, 'W', $C['black']);
  368. imageString($im, 4, $sz - 10 , $sz/2 + 4, 'E', $C['black']);
  369. }
  370. }
  371. function gen_image($resp){
  372. $sz = 600;
  373. if (isset($_GET['sz']) && ($_GET['sz'] == 'small'))
  374. $sz = 240;
  375. $GPS = json_decode($resp, true);
  376. if ($GPS['class'] != "POLL"){
  377. die("json_decode error: $resp");
  378. }
  379. $im = imageCreate($sz, $sz);
  380. $C = colorsetup($im);
  381. skyview($im, $sz, $C);
  382. if (240 < $sz)
  383. legend($im, $sz, $C);
  384. if (array_key_exists('sky', $GPS) &&
  385. array_key_exists(0, $GPS['sky']) &&
  386. array_key_exists('satellites', $GPS['sky'][0])) {
  387. for($i = 0; $i < count($GPS['sky'][0]['satellites']); $i++){
  388. $sat = sat_clean($GPS['sky'][0]['satellites'][$i]);
  389. splot($im, $sz, $C, $sat);
  390. }
  391. }
  392. header("Content-type: image/png");
  393. imagePNG($im);
  394. imageDestroy($im);
  395. }
  396. function dfix($x, $y, $z){
  397. if ($x < 0){
  398. $x = sprintf("%f %s", -1 * $x, $z);
  399. } else {
  400. $x = sprintf("%f %s", $x, $y);
  401. }
  402. return $x;
  403. }
  404. # compare sats for sort. used at top.
  405. function sat_cmp($a, $b) {
  406. if ($b['used'] != $a['used']) {
  407. # used Y before used N
  408. return $b['used'] - $a['used'];
  409. }
  410. return $a['PRN'] - $b['PRN'];
  411. }
  412. function write_html($resp) {
  413. global $sock, $errstr, $errno, $server, $port, $head, $body, $open;
  414. global $blurb, $title, $autorefresh, $showmap, $gmap_key, $footer;
  415. global $testmode, $advertise;
  416. $GPS = json_decode($resp, true);
  417. if ($GPS['class'] != 'POLL'){
  418. die("json_decode error: $resp");
  419. }
  420. header("Content-type: text/html; charset=UTF-8");
  421. global $lat, $lon;
  422. # make sure some things exist
  423. if (!array_key_exists('sky', $GPS)) {
  424. $GPS['sky'] = array();
  425. }
  426. if (!array_key_exists(0, $GPS['sky'])) {
  427. $GPS['sky'][0] = array();
  428. }
  429. if (!array_key_exists('satellites', $GPS['sky'][0])) {
  430. $GPS['sky'][0]['satellites'] = array();
  431. }
  432. if (!array_key_exists('tpv', $GPS)) {
  433. $GPS['tpv'] = array();
  434. }
  435. if (!array_key_exists(0, $GPS['tpv'])) {
  436. $GPS['tpv'][0] = array();
  437. }
  438. if (!array_key_exists('lat', $GPS['tpv'][0]) ||
  439. !array_key_exists('lon', $GPS['tpv'][0])) {
  440. $GPS['tpv'][0]['lat'] = 0.0;
  441. $GPS['tpv'][0]['lon'] = 0.0;
  442. }
  443. if (!array_key_exists('mode', $GPS['tpv'][0])) {
  444. $GPS['tpv'][0]['mode'] = 0;
  445. }
  446. if (!array_key_exists('time', $GPS['tpv'][0])) {
  447. $GPS['tpv'][0]['time'] = 0;
  448. }
  449. $lat = (float)$GPS['tpv'][0]['lat'];
  450. $lon = (float)$GPS['tpv'][0]['lon'];
  451. $x = $server; $y = $port;
  452. $imgdata = base64_encode($resp);
  453. $server = $x; $port = $y;
  454. if ($autorefresh > 0)
  455. $autorefresh = "<meta http-equiv='Refresh' content='$autorefresh'/>";
  456. else
  457. $autorefresh = '';
  458. $map_head = $map_body = $map_code = '';
  459. if ($showmap == 1) {
  460. $map_head = gen_gmap_head();
  461. $map_body = 'onload="Load()" onunload="GUnload()"';
  462. $map_code = gen_map_code();
  463. } else if ($showmap == 2) {
  464. $map_head = gen_osm_head();
  465. $map_body = '';
  466. $map_code = gen_osmmap_code();
  467. }
  468. $part_header = <<<EOF
  469. <!DOCTYPE html>
  470. <html lang="en">
  471. <head>
  472. {$head}
  473. {$map_head}
  474. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
  475. <title>{$title} - GPSD Test Station {$lat}, {$lon}</title>
  476. {$autorefresh}
  477. <style>
  478. blink,
  479. .warning {
  480. color: #dc322f;
  481. }
  482. .fixed {
  483. font-family: mono-space;
  484. }
  485. .caption {
  486. text-align: left;
  487. margin: 1ex 1em 1ex 1em; /* top right bottom left */
  488. }
  489. .administrivia {
  490. font-size: small;
  491. font-family: verdana, sans-serif;
  492. }
  493. body.light {
  494. background-color: #fdf6e3;
  495. color: #657b83;
  496. }
  497. body.dark {
  498. background-color: #002b36;
  499. color: #839496;
  500. }
  501. td:nth-child(1) {
  502. text-align: right;
  503. font-weight: bold;
  504. }
  505. </style>
  506. </head>
  507. <body {$body} {$map_body}>
  508. <div id="main" style="clear:both">
  509. <div>
  510. {$blurb}
  511. <br>
  512. </div>
  513. EOF;
  514. if (!strlen($advertise))
  515. $advertise = $server;
  516. if ($testmode && !$sock)
  517. $part_sky = "";
  518. else
  519. $part_sky = <<<EOF
  520. <!-- -->
  521. <div style="width:600px;float:right;margin:0 0 1ex 1em;">
  522. <img src="?op=view&amp;imgdata={$imgdata}"
  523. width="600" height="600" alt="Skyview"/>
  524. <br style="clear:both">
  525. <p class="caption">A filled shape means the satellite was used in
  526. the last fix.<br>
  527. Green-yellow-red colors indicate signal to noise ratio in dBHz<br>
  528. <span style="color:green;">green=best</span>,
  529. <span style="color:#c7a317;">yellow=fair</span>,
  530. and <span style="color:#dc322f">red=worst</span>.<br>
  531. Circles are GPS and QZSS satellites.<br>
  532. Diamonds indicate augmentation (SBAS, WAAS, etc.) satellites.<br>
  533. Triangles pointing up are Galileo satellites.<br>
  534. Triangles pointing down are BeiDou satellites.<br>
  535. Squares are GLONASS satellites.<br>
  536. </p>
  537. {$map_code}
  538. </div>
  539. EOF;
  540. if ($open)
  541. $part3 = <<<EOF
  542. <!-- -->
  543. <div>To get real-time information, connect to
  544. <span class="fixed">telnet://{$advertise}:{$port}/</span> and type "?POLL;"
  545. or "?WATCH={"enable":true,"raw":true}".<br/>
  546. Use a different server:<br/>
  547. <form method=GET action="${_SERVER['SCRIPT_NAME']}">
  548. <input name="host" value="{$advertise}">:
  549. <input name="port" value="{$port}" size="5" maxlength="5">
  550. <input type=submit value="Get Position"><input type=reset></form>
  551. <br/>
  552. </div>
  553. EOF;
  554. else
  555. $part3 = '';
  556. if ($testmode && !$sock)
  557. $part_tpv_sky = <<<EOF
  558. <div style="clear:both">The gpsd instance that this page monitors is
  559. not running.</div>
  560. EOF;
  561. else {
  562. $fix = $GPS['tpv'][0];
  563. $sky = $GPS['sky'][0];
  564. $sats = $sky['satellites'];
  565. $fixtype = array('Unknown' => 0, 'No Fix' => 1, '2D Fix' => 2,
  566. '3D Fix' => 3);
  567. $type = array_search($fix['mode'], $fixtype);
  568. $nsv = count($sats);
  569. $ts = $fix['time'];
  570. $sat = '';
  571. # gnssid to gnss abbreviation
  572. $gnss = array(0 => 'GP', 1 => 'SB', 2 => 'GA', 3 => 'BD',
  573. 4 => 'IM', 5 => 'QZ', 6 => 'GL');
  574. # sort sats
  575. usort($sats, "sat_cmp");
  576. $sats_used = 0;
  577. foreach($sats as $s) {
  578. $s = sat_clean($s);
  579. if (array_key_exists($s['gnssid'], $gnss)) {
  580. $s['gnssid'] = $gnss[$s['gnssid']];
  581. } else {
  582. $s['gnssid'] = ' ';
  583. }
  584. if ($s['used']) {
  585. $sats_used += 1;
  586. }
  587. if (2 == (int)$s['health']) {
  588. $used = $s['used'] ? 'uY&nbsp;' : 'uN&nbsp; ';
  589. } else {
  590. $used = $s['used'] ? '&nbsp;Y&nbsp;' : '&nbsp;N&nbsp; ';
  591. }
  592. $sat .= sprintf(
  593. "\t<tr style='text-align:right'><td>%s</td>" .
  594. "<td>%d</td><td>%d&nbsp;</td><td>%d&nbsp;</td>" .
  595. "<td>%d</td><td>%s</td></tr>\n",
  596. $s['gnssid'] . $s['svid'],
  597. $s['PRN'], $s['el'], $s['az'], $s['ss'],
  598. $used
  599. );
  600. };
  601. # ensure all $fix keys exist, for clean logs.
  602. $fixkeys = array('lat', 'leapseconds', 'lon');
  603. foreach($fixkeys as $key) {
  604. if (!array_key_exists($key, $fix)) {
  605. $fix[$key] = 'n/a';
  606. }
  607. }
  608. # ensure all $sky keys exist, for clean logs.
  609. $skykeys = array('gdop', 'hdop', 'pdop','tdop', 'vdop',
  610. 'xdop', 'ydop');
  611. foreach($skykeys as $key) {
  612. if (!array_key_exists($key, $sky)) {
  613. $sky[$key] = 'n/a';
  614. }
  615. }
  616. $lat = field_str($fix, 'lat', '&deg;', 'epx', 'm');
  617. $lon = field_str($fix, 'lon', '&deg;', 'epy', 'm');
  618. $altHAE = field_str($fix, 'altHAE', 'm', 'epv');
  619. $altMSL = field_str($fix, 'altMSL', 'm');
  620. $geoidSep = field_str($fix, 'geoidSep', 'm');
  621. $speed = field_str($fix, 'speed', 'm/s', 'eps');
  622. $climb = field_str($fix, 'climb', 'm/s', 'epc');
  623. $velN = field_str($fix, 'velN', 'm/s');
  624. $velE = field_str($fix, 'velE', 'm/s');
  625. $velD = field_str($fix, 'velD', 'm/s');
  626. $track = field_str($fix, 'track', '&deg;', 'epd');
  627. $magtrack = field_str($fix, 'magtrack', '&deg;');
  628. $magvar = field_str($fix, 'magvar', '&deg;');
  629. $ecefx = field_str($fix, 'ecefx', 'm');
  630. $ecefy = field_str($fix, 'ecefy', 'm');
  631. $ecefz = field_str($fix, 'ecefz', 'm', 'ecefpAcc');
  632. $ecefvx = field_str($fix, 'ecefvx', 'm/s');
  633. $ecefvy = field_str($fix, 'ecefvy', 'm/s');
  634. $ecefvz = field_str($fix, 'ecefvz', 'm/s', 'ecefvAcc');
  635. $epc = field_str($fix, 'epc', 'm/s');
  636. $eph = field_str($fix, 'eph', 'm');
  637. $eps = field_str($fix, 'eps', 'm/s');
  638. $ept = field_str($fix, 'ept', 's');
  639. $epx = field_str($fix, 'epx', 'm');
  640. $epy = field_str($fix, 'epy', 'm');
  641. $epv = field_str($fix, 'epv', 'm');
  642. $sep = field_str($fix, 'sep', 'm');
  643. $ecefpAcc = field_str($fix, 'ecefpAcc', 'm');
  644. $ecefvAcc = field_str($fix, 'ecefvAcc', 'm');
  645. $sep = field_str($fix, 'sep', 'm');
  646. if (!array_key_exists('status', $fix)) {
  647. $fix['status'] = 1;
  648. }
  649. switch($fix['status']) {
  650. case 0:
  651. $status = 'No Fix';
  652. break;
  653. case 1:
  654. $status = 'Normal Fix';
  655. break;
  656. case 2:
  657. $status = 'DGPS Fix';
  658. break;
  659. case 3:
  660. $status = 'RTK Fixed Fix';
  661. break;
  662. case 4:
  663. $status = 'RTK Float Fix';
  664. break;
  665. case 5:
  666. $status = 'Dead Reckoning';
  667. break;
  668. case 6:
  669. $status = 'GNSS+DR Fix';
  670. break;
  671. case 7:
  672. $status = 'Surveyed-In Fix';
  673. break;
  674. case 8:
  675. $status = 'Simulated Fix';
  676. break;
  677. default:
  678. $status = 'Unknown Fix Type';
  679. break;
  680. }
  681. $part_tpv_sky = <<<EOF
  682. <!-- -->
  683. <div style="float:left;margin:0 0 1ex 1em;">
  684. <table style="border-width:1px;border-style:solid;text-align:center;">
  685. <tr><th colspan=3 style="text-align:center">Fix Data</th></tr>
  686. <tr><td>Fix Type</td><td>{$type}</td></tr>
  687. <tr><td>Fix Status</td><td>{$status}</td></tr>
  688. <tr><th colspan=3 style="text-align:center">Time</th></tr>
  689. <tr><td>UTC</td><td colspan=2>{$ts}&nbsp;</td></tr>
  690. <tr><td>Leap Seconds</td><td>{$fix['leapseconds']}</td></tr>
  691. <tr><th colspan=3 style="text-align:center">Position</th></tr>
  692. <tr><td>Latitude</td><td>{$lat}</td></tr>
  693. <tr><td>Longitude</td><td>{$lon}</td></tr>
  694. <tr title="Height Above Ellipsoid. Typically WGS84">
  695. <td>Altitude HAE</td><td>{$altHAE}</td></tr>
  696. <tr title="Height Above Mean Sea Level">
  697. <td>Altitude MSL</td><td>{$altMSL}</td></tr>
  698. <tr title="HAE - MSL"><td>Geoid Separation</td><td>{$geoidSep}</td></tr>
  699. <tr><th colspan=3 style="text-align:center">Velocity</th></tr>
  700. <tr title="Horizontal Velocity"><td>Speed</td><td>{$speed}</td></tr>
  701. <tr title="Vertical Velocity"><td>Climb</td><td>{$climb}</td></tr>
  702. <tr title="Velocity North"><td>velN</td><td>{$velN}</td></tr>
  703. <tr title="Velocity East"><td>velE</td><td>{$velE}</td></tr>
  704. <tr title="Velocity Down"><td>velD</td><td>{$velD}</td></tr>
  705. <tr><td>Track True</td><td>{$track}</td></tr>
  706. <tr><td>Track Magnetic</td><td>{$magtrack}</td></tr>
  707. <tr><td>Magnetic Variation</td><td>{$magvar}</td></tr>
  708. </table>
  709. </div>
  710. <div style="float:left;margin:0 0 1ex 1em;">
  711. <table style="border-width:1px;border-style:solid;text-align:center;">
  712. <tr title="Dimentionless Ratios">
  713. <th colspan=2 style="text-align:center">Dilution of Precision
  714. </th></tr>
  715. <tr title="Geometric Dilution Of Precision"><td>GDOP
  716. </td><td>{$sky['gdop']}</td></tr>
  717. <tr title="Horizontal (2D) Dilution Of Precision"><td>HDOP</td>
  718. <td>{$sky['hdop']}</td></tr>
  719. <tr title="Position (3D) Dilution Of Precision"><td>PDOP
  720. </td><td>{$sky['pdop']}</td></tr>
  721. <tr title="Time Dilution Of Precision"><td>TDOP</td>
  722. <td>{$sky['tdop']}</td></tr>
  723. <tr title="X (Longitude) Dilution Of Precision"><td>XDOP
  724. </td><td>{$sky['xdop']}</td></tr>
  725. <tr title="Y (Latitude) Dilution Of Precision"><td>YDOP
  726. </td><td>{$sky['ydop']}</td></tr>
  727. <tr title="Velocity Dilution Of Precision"><td>VDOP</td>
  728. <td>{$sky['vdop']}</td></tr>
  729. </table>
  730. </div>
  731. <div style="float:left;margin:0 0 1ex 1em;">
  732. <table style="border-width:1px;border-style:solid;text-align:center;">
  733. <tr title="Unknown/undefined uncertainty">
  734. <th colspan=2 style="text-align:center">Error Estimates</th></tr>
  735. <tr title="Estimated Precision of Climb"><td>epc</td>
  736. <td>{$epc}</td></tr>
  737. <tr title="Estimated Precision 2D"><td>eph</td>
  738. <td>{$eph}</td></tr>
  739. <tr title="Estimated Precision Speed"><td>eps</td>
  740. <td>{$eps}</td></tr>
  741. <tr title="Estimated Precision Time"><td>ept</td><td>{$ept}</td></tr>
  742. <tr title="Estimated Precision X (Longitude)"><td>epx</td>
  743. <td>{$epx}</td></tr>
  744. <tr title="Estimated Precision Y (Latitude)"><td>epy</td>
  745. <td>{$epy}</td></tr>
  746. <tr title="Estimated Precision Vertical"><td>epv</td>
  747. <td>{$epv}</td></tr>
  748. <tr title="Spherical Error Probability (epe)"><td>sep</td>
  749. <td>{$sep}</td></tr>
  750. <tr title="Estimated ECEF Position Accuracy"><td>ecef pAcc</td>
  751. <td>{$ecefpAcc}</td></tr>
  752. <tr title="Estimated ECEF Velocity Accuracy"><td>ecef vAcc</td>
  753. <td>{$ecefvAcc}</td></tr>
  754. </table>
  755. </div>
  756. <div style="float:right;margin:0 0 1ex 1em;">
  757. <table style="border-width:1px;border-style:solid">
  758. <tr><th colspan=6 style="text-align:center">Satellites</th></tr>
  759. <tr><td colspan=6 style="text-align:center"
  760. >Seen {$nsv}&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Used {$sats_used}
  761. </td></tr>
  762. <tr><th></th><th>PRN</th>
  763. <th>Elv</th><th>Azm</th>
  764. <th>SNR</th><th>Used</th>
  765. </tr>
  766. $sat </table>
  767. </div>
  768. <div style="float:left;margin:0 0 1ex 1em;">
  769. <table style="border-width:1px;border-style:solid;text-align:center;">
  770. <tr><th colspan=5 style="text-align:center"
  771. >Earth Centered Earth Fixed (ECEF)</th></tr>
  772. <tr><th></th><th>X</th><th>Y</th><th>Z</th><th>Acc</th></td>
  773. <tr><td>Position</td>
  774. <td>{$ecefx}</td><td>{$ecefy}</td> <td>{$ecefz}</td></tr>
  775. <tr><td>Velocity</td>
  776. <td>{$ecefvx}</td><td>{$ecefvy}</td> <td>{$ecefvz}</td></tr>
  777. </table>
  778. </div>
  779. <!-- raw response:
  780. {$resp}
  781. -->
  782. EOF;
  783. }
  784. $part_footer = <<<EOF
  785. </div> <!-- end div main -->
  786. <div style="clear:both">
  787. <br>
  788. <hr>
  789. {$footer}
  790. <p class="administrivia">This script is distributed by the
  791. <a href="@WEBSITE@">GPSD project</a>.</p>
  792. </div>
  793. </body>
  794. </html>
  795. EOF;
  796. print $part_header . $part_sky . $part3 . $part_tpv_sky . $part_footer;
  797. }
  798. function write_json($resp){
  799. header('Content-Type: text/javascript');
  800. if (isset($_GET['jsonp']))
  801. print "{$_GET['jsonp']}({$resp})";
  802. else
  803. print $resp;
  804. }
  805. function write_config(){
  806. $f = fopen("gpsd_config.inc", "a");
  807. if (!$f)
  808. die("can't generate prototype config file. try running this script as root in DOCUMENT_ROOT");
  809. $buf = <<<EOB
  810. <?PHP
  811. \$title = 'My GPS Server';
  812. \$server = 'localhost';
  813. #\$advertise = 'localhost';
  814. \$port = 2947;
  815. \$autorefresh = 0; # number of seconds after which to refresh
  816. # set to 1 if you want to have a google map,
  817. # set it to 2 if you want a map based on opemstreetmap/openlayers (osm)
  818. \$showmap = 0;
  819. \$gmap_key = 'GetYourOwnGoogleKey'; # your google API key goes here
  820. \$swap_ew = 0; # set to 1 for upward facing view (nonstandard)
  821. \$open = 0; # set to 1 to show the form to change the GPSd server
  822. ## You can read the header, footer and blurb from a file...
  823. # \$head = file_get_contents('/path/to/header.inc');
  824. # \$body = file_get_contents('/path/to/body.inc');
  825. # \$footer = file_get_contents('/path/to/footer.hinc');
  826. # \$blurb = file_get_contents('/path/to/blurb.inc');
  827. ## ... or you can just define them here
  828. \$head = '';
  829. \$body = '';
  830. \$footer = '';
  831. \$blurb = <<<EOT
  832. This is a
  833. <a href="@WEBSITE@">gpsd</a>
  834. server <blink>located someplace</blink>.
  835. The hardware is a
  836. <blink>hardware description and link</blink>.
  837. This machine is maintained by
  838. <a href="mailto:you@example.com">Your Name Goes Here</a>.<br/>
  839. EOT;
  840. ?>
  841. EOB;
  842. fwrite($f, $buf);
  843. fclose($f);
  844. }
  845. function gen_gmap_head() {
  846. global $gmap_key;
  847. return <<<EOT
  848. <script src="//maps.googleapis.com/maps/api/js?sensor=false"/>
  849. <script>
  850. <!-- note that the google map API is commented out, it requires
  851. an API KEY, and costs money to use! As of March 2020 more info
  852. here: https://developers.google.com/maps/gmp-get-started -->
  853. <!--
  854. function Load() {
  855. var map = new google.maps.Map(
  856. document.getElementById('map'), {
  857. center: new google.maps.LatLng({$GLOBALS['lat']}, {$GLOBALS['lon']}),
  858. zoom: 13,
  859. mapTypeId: google.maps.MapTypeId.ROADMAP
  860. });
  861. var marker = new google.maps.Marker({
  862. position: new google.maps.LatLng({$GLOBALS['lat']}, {$GLOBALS['lon']}),
  863. map: map
  864. });
  865. }
  866. google.maps.event.addDomListener(window, 'load', initialize);
  867. -->
  868. </script>
  869. EOT;
  870. }
  871. # example code from: https://openlayers.org/en/latest/doc/quickstart.html
  872. function gen_osm_head() {
  873. global $GPS;
  874. return <<< EOT
  875. <link rel="stylesheet"
  876. href="https://cdn.jsdelivr.net/gh/openlayers/openlayers.github.io@master/en/v6.2.1/css/ol.css"
  877. type="text/css">
  878. <script
  879. src="https://cdn.jsdelivr.net/gh/openlayers/openlayers.github.io@master/en/v6.2.1/build/ol.js"></script>
  880. EOT;
  881. }
  882. function gen_osmmap_code() {
  883. return <<<EOT
  884. <br>
  885. <div id="map" style="height:400px;"></div>
  886. <noscript>
  887. <span class='warning'>Sorry: you must enable javascript to view our
  888. maps.</span><br/>
  889. </noscript>
  890. <script>
  891. var LonLat = ol.proj.fromLonLat([{$GLOBALS['lon']}, {$GLOBALS['lat']}])
  892. var stroke = new ol.style.Stroke({color: 'red', width: 2});
  893. var feature = new ol.Feature(new ol.geom.Point(LonLat))
  894. var x = new ol.style.Style({
  895. image: new ol.style.RegularShape({
  896. stroke: stroke,
  897. points: 4,
  898. radius: 10,
  899. radius2: 0,
  900. angle: 0.785397 // Pi / 4
  901. })
  902. })
  903. feature.setStyle(x)
  904. var source = new ol.source.Vector({
  905. features: [feature]
  906. });
  907. var vectorLayer = new ol.layer.Vector({
  908. source: source
  909. });
  910. var map = new ol.Map({
  911. target: 'map',
  912. layers: [
  913. new ol.layer.Tile({
  914. source: new ol.source.OSM()
  915. }),
  916. vectorLayer
  917. ],
  918. view: new ol.View({
  919. center: LonLat,
  920. zoom: 6
  921. })
  922. });
  923. </script>
  924. EOT;
  925. }
  926. function gen_map_code() {
  927. return <<<EOT
  928. <br>
  929. <div id="map"
  930. style="width:550px;height:400px;border:1px;border-style:solid;float:left;">
  931. Loading...
  932. <noscript>
  933. <span class='warning'>Sorry: you must enable javascript to view our
  934. maps.</span><br/>
  935. </noscript>
  936. </div>
  937. EOT;
  938. }
  939. ?>