source: src/webapp/webclient/js/main.js@ 2c3c458

0.5 debian/bullseye debian/jessie
Last change on this file since 2c3c458 was c455d7b, checked in by Joachim Langenbach <joachim.langenbach@…>, 9 years ago
  • Webservice now leverages PHP classes and autoloader functionality. All webservice tasks are combined in index.php and served by Webservice.php
  • Webclient lists available log files and webservice provides them to download
  • Property mode set to 100644
File size: 7.0 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 = null;
23var mapLayerCurrent = null;
24// var mapControl = null;
25var mapsMetadata = null;
26var polyline = null;
27
28function openLink(target)
29{
30 window.location=target;
31}
32
33function displayDataSet(data)
34{
35 if(!Array.isArray(data) && !(data instanceof Object)){
36 console.log("No data array returned!");
37 console.log(data);
38 return;
39 }
40 if(!("lastvalid" in data)){
41 console.log("No last valid values given!");
42 }
43 else{
44 var lastValid = data["lastvalid"];
45 var keys = ["speedthroughwater", "speedoverground", "waterdepth", "watertemperature", "gpsfix"];
46 for(var i = 0; i < keys.length; i++){
47 var key = keys[i];
48 var value = lastValid[key];
49 if($.isNumeric(value)){
50 var text = value.toFixed(1);
51 if(key == "watertemperature"){
52 text = value.toFixed(0);
53 }
54 $("#display #"+ keys[i] +" #value").text(text);
55 }
56 else if(key == "gpsfix"){
57 $("#display #"+ keys[i] +" #value").text(value);
58 }
59 }
60 }
61
62 if(!("track" in data)){
63 console.log("No track given!");
64 }
65 else{
66 if(map == null){
67 console.log("No map in use");
68 }
69 else{
70 var track = data["track"];
71 var points = [];
72 for(var time in track){
73 points.push(new L.latLng(track[time]["latitude"], track[time]["longitude"]));
74 }
75 if(points.length > 0){
76 if(!polyline){
77 polyline = L.polyline(points, {color: 'red'}).addTo(map);
78 }
79 else{
80 polyline.setLatLngs(points);
81 }
82 map.fitBounds(polyline.getBounds());
83 }
84 }
85 }
86}
87
88function displayDataGet()
89{
90 var gnuboatApi = "../webservice/";
91 $.getJSON( gnuboatApi, displayDataSet);
92}
93
94function mapSetup()
95{
96 var mapMetadataIndex = $("#mapSelect").val();
97 if(mapsMetadata == null){
98 console.log("No mapsMetadata set");
99 }
100 if(mapMetadataIndex < 0 || mapMetadataIndex >= mapsMetadata.length){
101 console.log("Map metadata is outside of range :"+ mapMetadataIndex.toString());
102 console.log(mapsMetadata);
103 return;
104 }
105 var mapMetadata = mapsMetadata[mapMetadataIndex];
106
107 var latitudeTop = mapMetadata["bounds"][3];
108 var latitudeBottom = mapMetadata["bounds"][1];
109 var longitudeLeft = mapMetadata["bounds"][0];
110 var longitudeRight = mapMetadata["bounds"][2];
111
112 var mapZoomMin = mapMetadata["minzoom"];
113 var mapZoomMax = mapMetadata["maxzoom"];
114
115 var latitudeCenter = (latitudeTop - latitudeBottom)/2 + latitudeBottom;
116 var longitudeCenter = (longitudeLeft - longitudeRight)/2 + longitudeRight;
117 var mapZoomStart = (mapZoomMax - mapZoomMin)/2 + mapZoomMin;
118
119 var mapBounds = new L.LatLngBounds(
120 new L.LatLng(latitudeBottom, longitudeLeft),
121 new L.LatLng(latitudeTop, longitudeRight));
122
123 if(map == null){
124 // only initialize the map (viewport) once
125 map = L.map('mapViewport');
126 }
127 map.setView([latitudeCenter, longitudeCenter], mapZoomStart);
128
129 if(mapLayerCurrent != null){
130 map.removeLayer(mapLayerCurrent);
131 mapLayerCurrent = null;
132 }
133 mapLayerCurrent = L.tileLayer(mapMetadata["tiles"][0], {
134 minZoom: mapZoomMin, maxZoom: mapZoomMax,
135 bounds: mapBounds,
136 attribution: mapMetadata["name"],
137 opacity: 0.85
138 }).addTo(map);
139}
140
141function getMapInfos()
142{
143 $.getJSON("../webservice/tileserver.php?/index.json", mapInfos);
144}
145
146function mapInfos(data)
147{
148 mapsMetadata = data;
149
150 $("#mapSelect").empty();
151 var selected = "selected";
152 data.forEach(function(value, index){
153 $("<option value=\""+ index.toString() +"\" "+ selected +">"+ value["name"] +"</option>").appendTo("#mapSelect");
154 selected = "";
155 });
156
157 mapSetup();
158 $("#mapSelect").change(function(){ mapSetup(); });
159}
160
161function shutdown()
162{
163 $.getJSON("../webservice/?request=shutdown", function(data){
164 var shutdownTime = data["shutdownstart"];
165 var shutdownDate = Date.parse(shutdownTime);
166 var currentDate = new Date();
167 var shutdownHint = "Shutting down in "+ Math.floor((shutdownDate - currentDate)/1000) +"s at "+ shutdownTime.toLocaleString();
168 if(shutdownTime == -1){
169 shutdownHint = "Failed to initiate shutdown: "+ data["errormessage"];
170 }
171 $("#shutdownMessage").text(shutdownHint);
172 });
173 location.hash = "#shutdown";
174}
175
176function uploadMap(form)
177{
178 $("#mapFileInput").hide();
179 $("#mapTileUploadProgress").show();
180 $.ajax({
181 type: 'POST',
182 url: '../webservice/?request=map',
183 data: new FormData($("#mapTileUpload")[0]),
184 contentType: false,
185 processData: false,
186 success: function(data){
187 if(("error" in data) && data["error"] == 1){
188 alert("Failed to upload map: "+ data["errormessage"]);
189 $("#mapTileUploadProgress").hide();
190 $("#mapFileInput").show();
191 return;
192 }
193 alert("Map upload successfully");
194 $("#mapTileUploadProgress").hide();
195 $("#mapFileInput").show();
196 getMapInfos();
197 },
198 error: function(jqXHR, exception, errorThrown){
199 alert("Failed to upload map (XHR): "+ exception);
200 $("#mapTileUploadProgress").hide();
201 $("#mapFileInput").show();
202 }
203 }).progress(function(e){
204 $("#mapTileUploadProgress").attr({value: e.oloaded, max: e.total});
205 });
206}
207
208function logfilesLoad()
209{
210 $("#logfilesForm").empty();
211 $.getJSON("../webservice/?request=logfiles", function(data){
212 if(!Array.isArray(data) || data.length < 1){
213 $("<p>No Logfiles found!</p>").appendTo("#logfilesForm");
214 console.log(data);
215 return;
216 }
217 if(("error" in data) && data["error"] == 1){
218 alert("Failed to upload map: "+ data["errormessage"]);
219 $("#mapTileUploadProgress").hide();
220 $("#mapFileInput").show();
221 return;
222 }
223 $("<select id=\"logfilesSelect\" name=\"logfilesSelected[]\" class=\"pure-input-1\" multiple></select>").appendTo("#logfilesForm");
224 $("<input type=\"submit\" class=\"pure-button pure-button-primary\" name=\"logfilesDownload\" value=\"Download\"/>").appendTo("#logfilesForm");
225 data.forEach(function(value, index){
226 $("<option value=\""+ value +"\">"+ value +"</option>").appendTo("#logfilesSelect");
227 });
228 });
229}
230
231$(document).ready(function() {
232 $("#mapTileUploadProgress").hide();
233 $("#logfilesReload").click(logfilesLoad);
234 $("#shutdownButton").click(shutdown);
235
236 $("#mapTileUpload").validate({
237 rules: {
238 mapFile: {
239 required: true
240 },
241 mapName: {
242 required: true
243 }
244 },
245 submitHandler: function(form){
246 uploadMap(form);
247 }
248 });
249
250 $("#logfilesForm").validate({
251 rules: {
252 logfilesSelect: {
253 required: true
254 },
255 }
256 });
257
258 getMapInfos();
259 displayDataGet();
260 setInterval(displayDataGet, 10000);
261 logfilesLoad();
262});
Note: See TracBrowser for help on using the repository browser.