source: src/webapp/webclient/javascript/main.js@ 8310f73

debian/bullseye debian/jessie
Last change on this file since 8310f73 was eb1ccd6, checked in by Joachim Langenbach <joachim.langenbach@…>, 9 years ago
  • Fixed typo in webservice
  • Fixed wrong datatype detection in main.js from webclient
  • Property mode set to 100644
File size: 3.8 KB
RevLine 
[d7e6e9c]1/*
2
[33c6557]3 Copyright (C) 2016 - 2017 EngSaS - Engineering Solutions and Services Langenbach. All rights reserved.
[d7e6e9c]4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
18 MA 02110-1301 USA.
19
20*/
21
[f8dfbe7]22var map;
23var polyline;
24
[d7e6e9c]25function openLink(target){
26 window.location=target;
27}
28
29function displayDataSet(data){
[eb1ccd6]30 if(!Array.isArray(data) && !(data instanceof Object)){
[c61b79b]31 console.log("No data array returned!");
[eb1ccd6]32 console.log(data);
[c61b79b]33 return;
[eb1ccd6]34 }
[4ab1c0a]35 if(!("lastvalid" in data)){
36 console.log("No last valid values given!");
37 }
38 else{
39 var lastValid = data["lastvalid"];
40 var keys = ["speedthroughwater", "speedoverground", "waterdepth", "watertemperature", "gpsfix"];
41 for(var i = 0; i < keys.length; i++){
42 var key = keys[i];
43 var value = lastValid[key];
44 if($.isNumeric(value)){
[5149714]45 var text = value.toFixed(1);
[4ab1c0a]46 if(key == "watertemperature"){
[5149714]47 text = value.toFixed(0);
[4ab1c0a]48 }
[5149714]49 $("#display #"+ keys[i] +" #value").text(text);
[4ab1c0a]50 }
51 else if(key == "gpsfix"){
52 $("#display #"+ keys[i] +" #value").text(value);
53 }
54 }
55 }
[f8dfbe7]56
[4ab1c0a]57 if(!("track" in data)){
58 console.log("No track given!");
59 }
60 else{
61 var track = data["track"];
62 var points = [];
63 for(var time in track){
64 points.push(new L.latLng(track[time]["latitude"], track[time]["longitude"]));
65 }
[f8dfbe7]66 if(points.length > 0){
[4ab1c0a]67 if(!polyline){
68 polyline = L.polyline(points, {color: 'red'}).addTo(map);
69 }
70 else{
[f8dfbe7]71 polyline.setLatLngs(points);
72 }
[4ab1c0a]73 map.fitBounds(polyline.getBounds());
[f8dfbe7]74 }
75 }
[d7e6e9c]76}
77
78function displayDataGet() {
[6f38643]79 var gnuboatApi = "../webservice/";
[d7e6e9c]80 $.getJSON( gnuboatApi, displayDataSet);
81}
82
[6f38643]83function mapSetup(data){
84 var overlay;
85 var latitudeTop = data["bounds"][3];
[f8dfbe7]86 var latitudeBottom = data["bounds"][1];
[6f38643]87 var longitudeLeft = data["bounds"][0];
[f8dfbe7]88 var longitudeRight = data["bounds"][2];
89
90 var mapZoomMin = data["minzoom"];
91 var mapZoomMax = data["maxzoom"];
[6f38643]92
93 var latitudeCenter = (latitudeTop - latitudeBottom)/2 + latitudeBottom;
94 var longitudeCenter = (longitudeLeft - longitudeRight)/2 + longitudeRight;
[f8dfbe7]95 var mapZoomStart = (mapZoomMax - mapZoomMin)/2 + mapZoomMin;
96
[6f38643]97 var mapBounds = new L.LatLngBounds(
[f8dfbe7]98 new L.LatLng(latitudeBottom, longitudeLeft),
99 new L.LatLng(latitudeTop, longitudeRight));
100
101 map = L.map('map').setView([latitudeCenter, longitudeCenter], mapZoomStart);
[6f38643]102
103 overlay = L.tileLayer('../webservice/tileserver.php?/index.json?/map/{z}/{x}/{y}.png', {
[f8dfbe7]104 minZoom: mapZoomMin, maxZoom: mapZoomMax,
[6f38643]105 bounds: mapBounds,
106 attribution: 'undefined',opacity: 0.85
[f8dfbe7]107 }).addTo(map);
[6f38643]108}
109
[5519ced]110function shutdown(){
111 $.getJSON("../webservice/?request=shutdown", function(data){
112 var shutdownTime = data["shutdownstart"];
[d7ee6ae]113 var shutdownDate = Date.parse(shutdownTime);
[5519ced]114 var currentDate = new Date();
115 var shutdownHint = "Shutting down in "+ Math.floor((shutdownDate - currentDate)/1000) +"s at "+ shutdownTime.toLocaleString();
116 if(shutdownTime == -1){
117 shutdownHint = "Failed to initiate shutdown!";
118 }
119 $("#shutdownMessage").text(shutdownHint);
120 });
121}
122
[d7e6e9c]123$(document).ready(function() {
124 displayDataGet();
[33c6557]125 setInterval(displayDataGet, 10000);
[c84f87e]126
[6f38643]127 $.getJSON("../webservice/tileserver.php?/map.json", mapSetup);
[5519ced]128
129 $("#shutdownButton").click(shutdown);
[d7e6e9c]130});
Note: See TracBrowser for help on using the repository browser.