/* Copyright (C) 2016 - 2017 EngSaS - Engineering Solutions and Services Langenbach. All rights reserved. This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ var map = null; var mapLayerCurrent = null; // var mapControl = null; var mapsMetadata = null; var polyline = null; function openLink(target) { window.location=target; } function displayDataSet(data) { if(!Array.isArray(data) && !(data instanceof Object)){ console.log("No data array returned!"); console.log(data); return; } if(!("lastvalid" in data)){ console.log("No last valid values given!"); } else{ var lastValid = data["lastvalid"]; var keys = ["speedthroughwater", "speedoverground", "waterdepth", "watertemperature", "gpsfix"]; for(var i = 0; i < keys.length; i++){ var key = keys[i]; var value = lastValid[key]; if($.isNumeric(value)){ var text = value.toFixed(1); if(key == "watertemperature"){ text = value.toFixed(0); } $("#display #"+ keys[i] +" #value").text(text); } else if(key == "gpsfix"){ $("#display #"+ keys[i] +" #value").text(value); } } } if(!("track" in data)){ console.log("No track given!"); } else{ if(map == null){ console.log("No map in use"); } else{ var track = data["track"]; var points = []; for(var time in track){ points.push(new L.latLng(track[time]["latitude"], track[time]["longitude"])); } if(points.length > 0){ if(!polyline){ polyline = L.polyline(points, {color: 'red'}).addTo(map); } else{ polyline.setLatLngs(points); } map.fitBounds(polyline.getBounds()); } } } } function displayDataGet() { var gnuboatApi = "../webservice/"; $.getJSON( gnuboatApi, displayDataSet); } function mapSetup() { var mapMetadataIndex = $("#mapSelect").val(); if(mapsMetadata == null){ console.log("No mapsMetadata set"); } if(mapMetadataIndex < 0 || mapMetadataIndex >= mapsMetadata.length){ console.log("Map metadata is outside of range :"+ mapMetadataIndex.toString()); console.log(mapsMetadata); return; } var mapMetadata = mapsMetadata[mapMetadataIndex]; var latitudeTop = mapMetadata["bounds"][3]; var latitudeBottom = mapMetadata["bounds"][1]; var longitudeLeft = mapMetadata["bounds"][0]; var longitudeRight = mapMetadata["bounds"][2]; var mapZoomMin = mapMetadata["minzoom"]; var mapZoomMax = mapMetadata["maxzoom"]; var latitudeCenter = (latitudeTop - latitudeBottom)/2 + latitudeBottom; var longitudeCenter = (longitudeLeft - longitudeRight)/2 + longitudeRight; var mapZoomStart = (mapZoomMax - mapZoomMin)/2 + mapZoomMin; var mapBounds = new L.LatLngBounds( new L.LatLng(latitudeBottom, longitudeLeft), new L.LatLng(latitudeTop, longitudeRight)); if(map == null){ // only initialize the map (viewport) once map = L.map('mapViewport'); } map.setView([latitudeCenter, longitudeCenter], mapZoomStart); if(mapLayerCurrent != null){ map.removeLayer(mapLayerCurrent); mapLayerCurrent = null; } mapLayerCurrent = L.tileLayer(mapMetadata["tiles"][0], { minZoom: mapZoomMin, maxZoom: mapZoomMax, bounds: mapBounds, attribution: mapMetadata["name"], opacity: 0.85 }).addTo(map); } function getMapInfos() { $.getJSON("../webservice/tileserver.php?/index.json", mapInfos); } function mapInfos(data) { mapsMetadata = data; $("#mapSelect").empty(); var selected = "selected"; data.forEach(function(value, index){ $("").appendTo("#mapSelect"); selected = ""; }); mapSetup(); $("#mapSelect").change(function(){ mapSetup(); }); } function shutdown() { $.getJSON("../webservice/?request=shutdown", function(data){ var shutdownTime = data["shutdownstart"]; var shutdownDate = Date.parse(shutdownTime); var currentDate = new Date(); var shutdownHint = "Shutting down in "+ Math.floor((shutdownDate - currentDate)/1000) +"s at "+ shutdownTime.toLocaleString(); if(shutdownTime == -1){ shutdownHint = "Failed to initiate shutdown: "+ data["errormessage"]; } $("#shutdownMessage").text(shutdownHint); }); location.hash = "#shutdown"; } function uploadMap(form) { $("#mapFileInput").hide(); $("#mapTileUploadProgress").show(); $.ajax({ type: 'POST', url: '../webservice/?request=map', data: new FormData($("#mapTileUpload")[0]), contentType: false, processData: false, success: function(data){ if(("error" in data) && data["error"] == 1){ alert("Failed to upload map: "+ data["errormessage"]); $("#mapTileUploadProgress").hide(); $("#mapFileInput").show(); return; } alert("Map upload successfully"); $("#mapTileUploadProgress").hide(); $("#mapFileInput").show(); getMapInfos(); }, error: function(jqXHR, exception, errorThrown){ alert("Failed to upload map (XHR): "+ exception); $("#mapTileUploadProgress").hide(); $("#mapFileInput").show(); } }).progress(function(e){ $("#mapTileUploadProgress").attr({value: e.oloaded, max: e.total}); }); } function logfilesLoad() { $("#logfilesForm").empty(); $.getJSON("../webservice/?request=logfiles", function(data){ if(!Array.isArray(data) || data.length < 1){ $("

No Logfiles found!

").appendTo("#logfilesForm"); console.log(data); return; } if(("error" in data) && data["error"] == 1){ alert("Failed to upload map: "+ data["errormessage"]); $("#mapTileUploadProgress").hide(); $("#mapFileInput").show(); return; } $("").appendTo("#logfilesForm"); $("").appendTo("#logfilesForm"); data.forEach(function(value, index){ $("").appendTo("#logfilesSelect"); }); }); } $(document).ready(function() { $("#mapTileUploadProgress").hide(); $("#logfilesReload").click(logfilesLoad); $("#shutdownButton").click(shutdown); $("#mapTileUpload").validate({ rules: { mapFile: { required: true }, mapName: { required: true } }, submitHandler: function(form){ uploadMap(form); } }); $("#logfilesForm").validate({ rules: { logfilesSelect: { required: true }, } }); getMapInfos(); displayDataGet(); setInterval(displayDataGet, 10000); logfilesLoad(); });