source: src/webapp/webclient/javascript/main.js@ 747b9ea

0.5 app-cordova debian/bullseye debian/jessie
Last change on this file since 747b9ea was d7ee6ae, checked in by Joachim Langenbach <joachim.langenbach@…>, 10 years ago

Fixed shutdown function in webclient and improved design

Signed-off-by: Joachim Langenbach <joachim.langenbach@…>

  • Property mode set to 100644
File size: 3.6 KB
Line 
1/*
2
3 Copyright (C) 2016 EngSaS - Engineering Solutions and Services Langenbach. All rights reserved.
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
22var map;
23var polyline;
24
25function openLink(target){
26 window.location=target;
27}
28
29function displayDataSet(data){
30 if(!("lastvalid" in data)){
31 console.log("No last valid values given!");
32 }
33 else{
34 var lastValid = data["lastvalid"];
35 var keys = ["speedthroughwater", "speedoverground", "waterdepth", "watertemperature", "gpsfix"];
36 for(var i = 0; i < keys.length; i++){
37 var key = keys[i];
38 var value = lastValid[key];
39 if($.isNumeric(value)){
40 if(key == "watertemperature"){
41 value = Math.round(value);
42 }
43 $("#display #"+ keys[i] +" #value").text(value.toString());
44 }
45 else if(key == "gpsfix"){
46 $("#display #"+ keys[i] +" #value").text(value);
47 }
48 }
49 }
50
51 if(!("track" in data)){
52 console.log("No track given!");
53 }
54 else{
55 var track = data["track"];
56 var points = [];
57 for(var time in track){
58 points.push(new L.latLng(track[time]["latitude"], track[time]["longitude"]));
59 }
60 if(points.length > 0){
61 if(!polyline){
62 polyline = L.polyline(points, {color: 'red'}).addTo(map);
63 }
64 else{
65 polyline.setLatLngs(points);
66 }
67 map.fitBounds(polyline.getBounds());
68 }
69 }
70}
71
72function displayDataGet() {
73 var gnuboatApi = "../webservice/";
74 $.getJSON( gnuboatApi, displayDataSet);
75}
76
77function mapSetup(data){
78 var overlay;
79 var latitudeTop = data["bounds"][3];
80 var latitudeBottom = data["bounds"][1];
81 var longitudeLeft = data["bounds"][0];
82 var longitudeRight = data["bounds"][2];
83
84 var mapZoomMin = data["minzoom"];
85 var mapZoomMax = data["maxzoom"];
86
87 var latitudeCenter = (latitudeTop - latitudeBottom)/2 + latitudeBottom;
88 var longitudeCenter = (longitudeLeft - longitudeRight)/2 + longitudeRight;
89 var mapZoomStart = (mapZoomMax - mapZoomMin)/2 + mapZoomMin;
90
91 var mapBounds = new L.LatLngBounds(
92 new L.LatLng(latitudeBottom, longitudeLeft),
93 new L.LatLng(latitudeTop, longitudeRight));
94
95 map = L.map('map').setView([latitudeCenter, longitudeCenter], mapZoomStart);
96
97 overlay = L.tileLayer('../webservice/tileserver.php?/index.json?/map/{z}/{x}/{y}.png', {
98 minZoom: mapZoomMin, maxZoom: mapZoomMax,
99 bounds: mapBounds,
100 attribution: 'undefined',opacity: 0.85
101 }).addTo(map);
102}
103
104function shutdown(){
105 $.getJSON("../webservice/?request=shutdown", function(data){
106 var shutdownTime = data["shutdownstart"];
107 var shutdownDate = Date.parse(shutdownTime);
108 var currentDate = new Date();
109 var shutdownHint = "Shutting down in "+ Math.floor((shutdownDate - currentDate)/1000) +"s at "+ shutdownTime.toLocaleString();
110 if(shutdownTime == -1){
111 shutdownHint = "Failed to initiate shutdown!";
112 }
113 $("#shutdownMessage").text(shutdownHint);
114 });
115}
116
117$(document).ready(function() {
118 displayDataGet();
119 setInterval(displayDataGet, 5000);
120
121 $.getJSON("../webservice/tileserver.php?/map.json", mapSetup);
122
123 $("#shutdownButton").click(shutdown);
124});
Note: See TracBrowser for help on using the repository browser.