source: src/webapp/webclient/js/main.js@ 24d4504

0.5 app-cordova debian/bullseye debian/jessie
Last change on this file since 24d4504 was 4972eea, checked in by Joachim Langenbach <joachim.langenbach@…>, 9 years ago

Improved GNUBoat webclient to be repsonsive

  • Property mode set to 100644
File size: 4.3 KB
Line 
1/*
2
3 Copyright (C) 2016 - 2017 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(!Array.isArray(data) && !(data instanceof Object)){
31 console.log("No data array returned!");
32 console.log(data);
33 return;
34 }
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)){
45 var text = value.toFixed(1);
46 if(key == "watertemperature"){
47 text = value.toFixed(0);
48 }
49 $("#display #"+ keys[i] +" #value").text(text);
50 }
51 else if(key == "gpsfix"){
52 $("#display #"+ keys[i] +" #value").text(value);
53 }
54 }
55 }
56
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 }
66 if(points.length > 0){
67 if(!polyline){
68 polyline = L.polyline(points, {color: 'red'}).addTo(map);
69 }
70 else{
71 polyline.setLatLngs(points);
72 }
73 map.fitBounds(polyline.getBounds());
74 }
75 }
76}
77
78function displayDataGet() {
79 var gnuboatApi = "../webservice/";
80 $.getJSON( gnuboatApi, displayDataSet);
81}
82
83function mapSetup(data){
84 var overlay;
85 var latitudeTop = data["bounds"][3];
86 var latitudeBottom = data["bounds"][1];
87 var longitudeLeft = data["bounds"][0];
88 var longitudeRight = data["bounds"][2];
89
90 var mapZoomMin = data["minzoom"];
91 var mapZoomMax = data["maxzoom"];
92
93 var latitudeCenter = (latitudeTop - latitudeBottom)/2 + latitudeBottom;
94 var longitudeCenter = (longitudeLeft - longitudeRight)/2 + longitudeRight;
95 var mapZoomStart = (mapZoomMax - mapZoomMin)/2 + mapZoomMin;
96
97 var mapBounds = new L.LatLngBounds(
98 new L.LatLng(latitudeBottom, longitudeLeft),
99 new L.LatLng(latitudeTop, longitudeRight));
100
101 map = L.map('map').setView([latitudeCenter, longitudeCenter], mapZoomStart);
102
103 overlay = L.tileLayer('../webservice/tileserver.php?/index.json?/map/{z}/{x}/{y}.png', {
104 minZoom: mapZoomMin, maxZoom: mapZoomMax,
105 bounds: mapBounds,
106 attribution: 'undefined',opacity: 0.85
107 }).addTo(map);
108}
109
110function shutdown(){
111 $.getJSON("../webservice/?request=shutdown", function(data){
112 var shutdownTime = data["shutdownstart"];
113 var shutdownDate = Date.parse(shutdownTime);
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
123function uploadMap(form)
124{
125 $.ajax({
126 type: "POST",
127 url: "../webservice/mapupload.php",
128 data: $(form).serialize(),
129 success: function(data){
130 if(("error" in data) && data["error"] == 1){
131 alert("Failed to upload map: "+ data["errormessage"]);
132 return;
133 }
134 alert("Map upload successfully");
135 },
136 error: function(jqXHR, exception, errorThrown){ alert("Failed to upload map: "+ exception); }
137 });
138}
139
140
141$(document).ready(function() {
142 displayDataGet();
143 setInterval(displayDataGet, 10000);
144
145 $.getJSON("../webservice/tileserver.php?/map.json", mapSetup);
146
147 $("shutdown").click(shutdown);
148
149 $("#mapTileUpload").validate({
150 rules: {
151 mapFile: {
152 required: true
153 }
154 },
155 submitHandler: function(form){
156 uploadMap(form);
157 }
158 });
159});
Note: See TracBrowser for help on using the repository browser.