| 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 |
|
|---|
| 22 | var map = null;
|
|---|
| 23 | var mapLayerCurrent = null;
|
|---|
| 24 | // var mapControl = null;
|
|---|
| 25 | var mapsMetadata = null;
|
|---|
| 26 | var polyline = null;
|
|---|
| 27 |
|
|---|
| 28 | function openLink(target)
|
|---|
| 29 | {
|
|---|
| 30 | window.location=target;
|
|---|
| 31 | }
|
|---|
| 32 |
|
|---|
| 33 | function 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 |
|
|---|
| 88 | function displayDataGet()
|
|---|
| 89 | {
|
|---|
| 90 | var gnuboatApi = "../webservice/";
|
|---|
| 91 | $.getJSON( gnuboatApi, displayDataSet);
|
|---|
| 92 | }
|
|---|
| 93 |
|
|---|
| 94 | function 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 |
|
|---|
| 141 | function getMapInfos()
|
|---|
| 142 | {
|
|---|
| 143 | $.getJSON("../webservice/tileserver.php?/index.json", mapInfos);
|
|---|
| 144 | }
|
|---|
| 145 |
|
|---|
| 146 | function 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 |
|
|---|
| 161 | function 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!";
|
|---|
| 170 | }
|
|---|
| 171 | $("#shutdownMessage").text(shutdownHint);
|
|---|
| 172 | });
|
|---|
| 173 | }
|
|---|
| 174 |
|
|---|
| 175 | function uploadMap(form)
|
|---|
| 176 | {
|
|---|
| 177 | var formData = new FormData($("#mapTileUpload")[0]);
|
|---|
| 178 | // formData.append("mapFile", $("#mapFileInput")[0].files[0]);
|
|---|
| 179 |
|
|---|
| 180 | $("#mapFileInput").hide();
|
|---|
| 181 | $("#mapTileUploadProgress").show();
|
|---|
| 182 | $.ajax({
|
|---|
| 183 | type: 'POST',
|
|---|
| 184 | url: '../webservice/mapupload.php',
|
|---|
| 185 | data: formData,
|
|---|
| 186 | contentType: false,
|
|---|
| 187 | processData: false,
|
|---|
| 188 | success: function(data){
|
|---|
| 189 | if(("error" in data) && data["error"] == 1){
|
|---|
| 190 | alert("Failed to upload map: "+ data["errormessage"]);
|
|---|
| 191 | $("#mapTileUploadProgress").hide();
|
|---|
| 192 | $("#mapFileInput").show();
|
|---|
| 193 | return;
|
|---|
| 194 | }
|
|---|
| 195 | alert("Map upload successfully");
|
|---|
| 196 | $("#mapTileUploadProgress").hide();
|
|---|
| 197 | $("#mapFileInput").show();
|
|---|
| 198 | getMapInfos();
|
|---|
| 199 | },
|
|---|
| 200 | error: function(jqXHR, exception, errorThrown){
|
|---|
| 201 | alert("Failed to upload map (XHR): "+ exception);
|
|---|
| 202 | $("#mapTileUploadProgress").hide();
|
|---|
| 203 | $("#mapFileInput").show();
|
|---|
| 204 | }
|
|---|
| 205 | }).progress(function(e){
|
|---|
| 206 | $("#mapTileUploadProgress").attr({value: e.oloaded, max: e.total});
|
|---|
| 207 | });
|
|---|
| 208 | }
|
|---|
| 209 |
|
|---|
| 210 |
|
|---|
| 211 | $(document).ready(function() {
|
|---|
| 212 | $("#mapTileUploadProgress").hide();
|
|---|
| 213 | $("shutdown").click(shutdown);
|
|---|
| 214 |
|
|---|
| 215 | $("#mapTileUpload").validate({
|
|---|
| 216 | rules: {
|
|---|
| 217 | mapFile: {
|
|---|
| 218 | required: true
|
|---|
| 219 | }
|
|---|
| 220 | },
|
|---|
| 221 | submitHandler: function(form){
|
|---|
| 222 | uploadMap(form);
|
|---|
| 223 | }
|
|---|
| 224 | });
|
|---|
| 225 |
|
|---|
| 226 | getMapInfos();
|
|---|
| 227 | displayDataGet();
|
|---|
| 228 | setInterval(displayDataGet, 10000);
|
|---|
| 229 | });
|
|---|