source: src/webapp/webclient/js/main.js@ 84bd893

0.5 app-cordova debian/bullseye debian/jessie
Last change on this file since 84bd893 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
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
[f03591a]22var map = null;
23var mapLayerCurrent = null;
24// var mapControl = null;
25var mapsMetadata = null;
26var polyline = null;
[f8dfbe7]27
[f03591a]28function openLink(target)
29{
[d7e6e9c]30 window.location=target;
31}
32
[f03591a]33function displayDataSet(data)
34{
[eb1ccd6]35 if(!Array.isArray(data) && !(data instanceof Object)){
[c61b79b]36 console.log("No data array returned!");
[eb1ccd6]37 console.log(data);
[c61b79b]38 return;
[eb1ccd6]39 }
[4ab1c0a]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)){
[5149714]50 var text = value.toFixed(1);
[4ab1c0a]51 if(key == "watertemperature"){
[5149714]52 text = value.toFixed(0);
[4ab1c0a]53 }
[5149714]54 $("#display #"+ keys[i] +" #value").text(text);
[4ab1c0a]55 }
56 else if(key == "gpsfix"){
57 $("#display #"+ keys[i] +" #value").text(value);
58 }
59 }
60 }
[f8dfbe7]61
[4ab1c0a]62 if(!("track" in data)){
63 console.log("No track given!");
64 }
65 else{
[f03591a]66 if(map == null){
67 console.log("No map in use");
[4ab1c0a]68 }
[f03591a]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"]));
[4ab1c0a]74 }
[f03591a]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());
[f8dfbe7]83 }
84 }
85 }
[d7e6e9c]86}
87
[f03591a]88function displayDataGet()
89{
[6f38643]90 var gnuboatApi = "../webservice/";
[d7e6e9c]91 $.getJSON( gnuboatApi, displayDataSet);
92}
93
[f03591a]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];
[f8dfbe7]106
[f03591a]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"];
[6f38643]114
115 var latitudeCenter = (latitudeTop - latitudeBottom)/2 + latitudeBottom;
116 var longitudeCenter = (longitudeLeft - longitudeRight)/2 + longitudeRight;
[f8dfbe7]117 var mapZoomStart = (mapZoomMax - mapZoomMin)/2 + mapZoomMin;
118
[6f38643]119 var mapBounds = new L.LatLngBounds(
[f8dfbe7]120 new L.LatLng(latitudeBottom, longitudeLeft),
121 new L.LatLng(latitudeTop, longitudeRight));
122
[f03591a]123 if(map == null){
124 // only initialize the map (viewport) once
125 map = L.map('mapViewport');
126 }
127 map.setView([latitudeCenter, longitudeCenter], mapZoomStart);
[6f38643]128
[f03591a]129 if(mapLayerCurrent != null){
130 map.removeLayer(mapLayerCurrent);
131 mapLayerCurrent = null;
132 }
133 mapLayerCurrent = L.tileLayer(mapMetadata["tiles"][0], {
[f8dfbe7]134 minZoom: mapZoomMin, maxZoom: mapZoomMax,
[6f38643]135 bounds: mapBounds,
[f03591a]136 attribution: mapMetadata["name"],
137 opacity: 0.85
[f8dfbe7]138 }).addTo(map);
[6f38643]139}
140
[f03591a]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{
[5519ced]163 $.getJSON("../webservice/?request=shutdown", function(data){
164 var shutdownTime = data["shutdownstart"];
[d7ee6ae]165 var shutdownDate = Date.parse(shutdownTime);
[5519ced]166 var currentDate = new Date();
167 var shutdownHint = "Shutting down in "+ Math.floor((shutdownDate - currentDate)/1000) +"s at "+ shutdownTime.toLocaleString();
168 if(shutdownTime == -1){
[c455d7b]169 shutdownHint = "Failed to initiate shutdown: "+ data["errormessage"];
[5519ced]170 }
171 $("#shutdownMessage").text(shutdownHint);
172 });
[c455d7b]173 location.hash = "#shutdown";
[5519ced]174}
175
[4972eea]176function uploadMap(form)
177{
[f03591a]178 $("#mapFileInput").hide();
179 $("#mapTileUploadProgress").show();
[4972eea]180 $.ajax({
[f03591a]181 type: 'POST',
[c455d7b]182 url: '../webservice/?request=map',
183 data: new FormData($("#mapTileUpload")[0]),
[f03591a]184 contentType: false,
185 processData: false,
[4972eea]186 success: function(data){
187 if(("error" in data) && data["error"] == 1){
188 alert("Failed to upload map: "+ data["errormessage"]);
[f03591a]189 $("#mapTileUploadProgress").hide();
190 $("#mapFileInput").show();
[4972eea]191 return;
192 }
193 alert("Map upload successfully");
[f03591a]194 $("#mapTileUploadProgress").hide();
195 $("#mapFileInput").show();
196 getMapInfos();
[4972eea]197 },
[f03591a]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});
[4972eea]205 });
206}
207
[c455d7b]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}
[4972eea]230
[d7e6e9c]231$(document).ready(function() {
[f03591a]232 $("#mapTileUploadProgress").hide();
[c455d7b]233 $("#logfilesReload").click(logfilesLoad);
234 $("#shutdownButton").click(shutdown);
[f03591a]235
[c455d7b]236 $("#mapTileUpload").validate({
[4972eea]237 rules: {
238 mapFile: {
239 required: true
[82b4b49]240 },
241 mapName: {
242 required: true
[4972eea]243 }
244 },
245 submitHandler: function(form){
246 uploadMap(form);
247 }
248 });
[c455d7b]249
250 $("#logfilesForm").validate({
251 rules: {
252 logfilesSelect: {
253 required: true
254 },
255 }
256 });
[f03591a]257
258 getMapInfos();
259 displayDataGet();
260 setInterval(displayDataGet, 10000);
[c455d7b]261 logfilesLoad();
[d7e6e9c]262});
Note: See TracBrowser for help on using the repository browser.