| [4ce069e] | 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 | /** @namespace GnuBoat JavaScript namespace */
|
|---|
| 23 | var GnuBoat = GnuBoat || {};
|
|---|
| 24 |
|
|---|
| 25 | /**
|
|---|
| 26 | * @brief Sets the webservice base url, used by any other functions
|
|---|
| 27 | */
|
|---|
| 28 | GnuBoat.setServer = function(baseUrl)
|
|---|
| 29 | {
|
|---|
| 30 | if(baseUrl.slice(-1) != "/")
|
|---|
| 31 | baseUrl += "/";
|
|---|
| 32 | GnuBoat.private.serverBaseUrl = baseUrl;
|
|---|
| 33 |
|
|---|
| 34 | }
|
|---|
| 35 |
|
|---|
| 36 | /**
|
|---|
| 37 | * @brief Used to display user messages
|
|---|
| 38 | */
|
|---|
| 39 | GnuBoat.displayMessage = function(text)
|
|---|
| 40 | {
|
|---|
| 41 | $("#message").clear();
|
|---|
| 42 | $(shutdownHint).appendTo("#message");
|
|---|
| 43 | location.hash = "#messages";
|
|---|
| 44 | }
|
|---|
| 45 |
|
|---|
| 46 | /**
|
|---|
| 47 | * @brief Fetches the data shown in the display area from the webservice
|
|---|
| 48 | */
|
|---|
| 49 | GnuBoat.displayDataGet = function()
|
|---|
| 50 | {
|
|---|
| 51 | if(GnuBoat.private.serverBaseUrl == null){
|
|---|
| 52 | GnuBoat.displayMessage("No server set/found");
|
|---|
| 53 | return;
|
|---|
| 54 | }
|
|---|
| 55 | var gnuboatApi = GnuBoat.private.serverBaseUrl;
|
|---|
| 56 | $.getJSON(gnuboatApi, GnuBoat.private.displayDataSet);
|
|---|
| 57 | };
|
|---|
| 58 |
|
|---|
| 59 | /**
|
|---|
| 60 | * @brief Gets the metadata of all maps, provided by the tileserver
|
|---|
| 61 | */
|
|---|
| 62 | GnuBoat.getMapInfos = function()
|
|---|
| 63 | {
|
|---|
| 64 | if(GnuBoat.private.serverBaseUrl == null){
|
|---|
| 65 | GnuBoat.displayMessage("No server set/found");
|
|---|
| 66 | return;
|
|---|
| 67 | }
|
|---|
| 68 | $.getJSON(GnuBoat.private.serverBaseUrl +"tileserver.php?/index.json", GnuBoat.private.mapInfos);
|
|---|
| 69 | };
|
|---|
| 70 |
|
|---|
| 71 | /**
|
|---|
| 72 | * @brief Fetches the list of available logfiles and adds them to the log file download form
|
|---|
| 73 | */
|
|---|
| 74 | GnuBoat.logfilesLoad = function()
|
|---|
| 75 | {
|
|---|
| 76 | if(GnuBoat.private.serverBaseUrl == null){
|
|---|
| 77 | GnuBoat.displayMessage("No server set/found");
|
|---|
| 78 | return;
|
|---|
| 79 | }
|
|---|
| 80 | $("#logfilesForm").empty();
|
|---|
| 81 | $.getJSON(GnuBoat.private.serverBaseUrl +"?request=logfiles", function(data){
|
|---|
| 82 | if(!Array.isArray(data) || data.length < 1){
|
|---|
| 83 | $("<p>No Logfiles found!</p>").appendTo("#logfilesForm");
|
|---|
| 84 | console.log(data);
|
|---|
| 85 | return;
|
|---|
| 86 | }
|
|---|
| 87 | if(("error" in data) && data["error"] == 1){
|
|---|
| 88 | alert("Failed to upload map: "+ data["errormessage"]);
|
|---|
| 89 | $("#mapTileUploadProgress").hide();
|
|---|
| 90 | $("#mapFileInput").show();
|
|---|
| 91 | return;
|
|---|
| 92 | }
|
|---|
| 93 | $("<select id=\"logfilesSelect\" name=\"logfilesSelected[]\" class=\"pure-input-1\" multiple></select>").appendTo("#logfilesForm");
|
|---|
| 94 | $("<input type=\"submit\" class=\"pure-button pure-button-primary\" name=\"logfilesDownload\" value=\"Download\"/>").appendTo("#logfilesForm");
|
|---|
| 95 | data.forEach(function(value, index){
|
|---|
| 96 | $("<option value=\""+ value +"\">"+ value +"</option>").appendTo("#logfilesSelect");
|
|---|
| 97 | });
|
|---|
| 98 | });
|
|---|
| 99 | };
|
|---|
| 100 | /**
|
|---|
| 101 | * @brief Sends a shutdown request to the webservice
|
|---|
| 102 | */
|
|---|
| 103 | GnuBoat.shutdown = function()
|
|---|
| 104 | {
|
|---|
| 105 | if(GnuBoat.private.serverBaseUrl == null){
|
|---|
| 106 | GnuBoat.displayMessage("No server set/found");
|
|---|
| 107 | return;
|
|---|
| 108 | }
|
|---|
| 109 | $.getJSON(GnuBoat.private.serverBaseUrl +"?request=shutdown", function(data){
|
|---|
| 110 | var shutdownTime = data["shutdownstart"];
|
|---|
| 111 | var shutdownDate = Date.parse(shutdownTime);
|
|---|
| 112 | var currentDate = new Date();
|
|---|
| 113 | var shutdownHint = "Shutting down in "+ Math.floor((shutdownDate - currentDate)/1000) +"s at "+ shutdownTime.toLocaleString();
|
|---|
| 114 | if(shutdownTime == -1){
|
|---|
| 115 | shutdownHint = "Failed to initiate shutdown: "+ data["errormessage"];
|
|---|
| 116 | }
|
|---|
| 117 | GnuBoat.displayMessage(shutdownHint);
|
|---|
| 118 | });
|
|---|
| 119 | };
|
|---|
| 120 |
|
|---|
| 121 | /**
|
|---|
| 122 | * @brief Uploads a map and refreshes the maps metadata on success
|
|---|
| 123 | */
|
|---|
| 124 | GnuBoat.uploadMap = function(form)
|
|---|
| 125 | {
|
|---|
| 126 | if(GnuBoat.private.serverBaseUrl == null){
|
|---|
| 127 | displayMessage("No server set/found");
|
|---|
| 128 | return;
|
|---|
| 129 | }
|
|---|
| 130 | $("#mapFileInput").hide();
|
|---|
| 131 | $("#mapTileUploadProgress").show();
|
|---|
| 132 | $.ajax({
|
|---|
| 133 | type: 'POST',
|
|---|
| 134 | url: GnuBoat.private.serverBaseUrl +'?request=map',
|
|---|
| 135 | data: new FormData($("#mapTileUpload")[0]),
|
|---|
| 136 | contentType: false,
|
|---|
| 137 | processData: false,
|
|---|
| 138 | success: function(data){
|
|---|
| 139 | if(("error" in data) && data["error"] == 1){
|
|---|
| 140 | alert("Failed to upload map: "+ data["errormessage"]);
|
|---|
| 141 | $("#mapTileUploadProgress").hide();
|
|---|
| 142 | $("#mapFileInput").show();
|
|---|
| 143 | return;
|
|---|
| 144 | }
|
|---|
| 145 | alert("Map upload successfully");
|
|---|
| 146 | $("#mapTileUploadProgress").hide();
|
|---|
| 147 | $("#mapFileInput").show();
|
|---|
| 148 | GnuBoat.getMapInfos();
|
|---|
| 149 | },
|
|---|
| 150 | error: function(jqXHR, exception, errorThrown){
|
|---|
| 151 | alert("Failed to upload map (XHR): "+ exception);
|
|---|
| 152 | $("#mapTileUploadProgress").hide();
|
|---|
| 153 | $("#mapFileInput").show();
|
|---|
| 154 | }
|
|---|
| 155 | }).progress(function(e){
|
|---|
| 156 | $("#mapTileUploadProgress").attr({value: e.oloaded, max: e.total});
|
|---|
| 157 | });
|
|---|
| 158 | };
|
|---|
| 159 |
|
|---|
| 160 | /**
|
|---|
| 161 | * @brief Setups up GnuBoat to be able to start() after configured a server with setServer().
|
|---|
| 162 | */
|
|---|
| 163 | GnuBoat.initialize = function()
|
|---|
| 164 | {
|
|---|
| 165 | $("#mapTileUploadProgress").hide();
|
|---|
| 166 | $("#logfilesReload").click(GnuBoat.logfilesLoad);
|
|---|
| 167 | $("#shutdownButton").click(GnuBoat.shutdown);
|
|---|
| 168 |
|
|---|
| 169 | $("#mapTileUpload").validate({
|
|---|
| 170 | rules: {
|
|---|
| 171 | mapFile: {
|
|---|
| 172 | required: true
|
|---|
| 173 | },
|
|---|
| 174 | mapName: {
|
|---|
| 175 | required: true
|
|---|
| 176 | }
|
|---|
| 177 | },
|
|---|
| 178 | submitHandler: function(form){
|
|---|
| 179 | GnuBoat.uploadMap(form);
|
|---|
| 180 | }
|
|---|
| 181 | });
|
|---|
| 182 |
|
|---|
| 183 | $("#logfilesForm").validate({
|
|---|
| 184 | rules: {
|
|---|
| 185 | logfilesSelect: {
|
|---|
| 186 | required: true
|
|---|
| 187 | },
|
|---|
| 188 | }
|
|---|
| 189 | });
|
|---|
| 190 | }
|
|---|
| 191 |
|
|---|
| 192 | /**
|
|---|
| 193 | * @brief Starts querying the webservice
|
|---|
| 194 | *
|
|---|
| 195 | * Call initialize() and setServer() before start()
|
|---|
| 196 | */
|
|---|
| 197 | GnuBoat.start = function()
|
|---|
| 198 | {
|
|---|
| 199 | if(GnuBoat.private.serverBaseUrl == null){
|
|---|
| 200 | GnuBoat.displayMessage("No server set/found");
|
|---|
| 201 | return;
|
|---|
| 202 | }
|
|---|
| 203 |
|
|---|
| 204 | GnuBoat.getMapInfos();
|
|---|
| 205 | GnuBoat.displayDataGet();
|
|---|
| 206 | if(GnuBoat.private.timerDisplay == null)
|
|---|
| 207 | GnuBoat.private.timerDisplay = setInterval(GnuBoat.displayDataGet, 10000);
|
|---|
| 208 | GnuBoat.logfilesLoad();
|
|---|
| 209 | }
|
|---|
| 210 |
|
|---|
| 211 | /**
|
|---|
| 212 | * @brief Stops querying the webservice
|
|---|
| 213 | */
|
|---|
| 214 | GnuBoat.stop = function()
|
|---|
| 215 | {
|
|---|
| 216 | if(GnuBoat.private.timerDisplay != null){
|
|---|
| 217 | clearInterval(GnuBoat.private.timerDisplay);
|
|---|
| 218 | GnuBoat.private.timerDisplay = null;
|
|---|
| 219 | }
|
|---|
| 220 | }
|
|---|
| 221 |
|
|---|
| 222 | /**
|
|---|
| 223 | * @brief Private methods and properties of GnuBoat
|
|---|
| 224 | */
|
|---|
| 225 | GnuBoat.private = {
|
|---|
| 226 | map: null,
|
|---|
| 227 | mapLayerCurrent: null,
|
|---|
| 228 | // var mapControl = null;
|
|---|
| 229 | mapsMetadata: null,
|
|---|
| 230 | polyline: null,
|
|---|
| 231 | serverBaseUrl: null,
|
|---|
| 232 | timerDisplay: null,
|
|---|
| 233 |
|
|---|
| 234 | openLink: function(target)
|
|---|
| 235 | {
|
|---|
| 236 | window.location=target;
|
|---|
| 237 | },
|
|---|
| 238 |
|
|---|
| 239 | displayDataSet: function(data)
|
|---|
| 240 | {
|
|---|
| 241 | if(!Array.isArray(data) && !(data instanceof Object)){
|
|---|
| 242 | console.log("No data array returned!");
|
|---|
| 243 | console.log(data);
|
|---|
| 244 | return;
|
|---|
| 245 | }
|
|---|
| 246 | if(!("lastvalid" in data)){
|
|---|
| 247 | console.log("No last valid values given!");
|
|---|
| 248 | }
|
|---|
| 249 | else{
|
|---|
| 250 | var lastValid = data["lastvalid"];
|
|---|
| 251 | var keys = ["speedthroughwater", "speedoverground", "waterdepth", "watertemperature", "gpsfix"];
|
|---|
| 252 | for(var i = 0; i < keys.length; i++){
|
|---|
| 253 | var key = keys[i];
|
|---|
| 254 | var value = lastValid[key];
|
|---|
| 255 | if($.isNumeric(value)){
|
|---|
| 256 | var text = value.toFixed(1);
|
|---|
| 257 | if(key == "watertemperature"){
|
|---|
| 258 | text = value.toFixed(0);
|
|---|
| 259 | }
|
|---|
| 260 | $("#display #"+ keys[i] +" #value").text(text);
|
|---|
| 261 | }
|
|---|
| 262 | else if(key == "gpsfix"){
|
|---|
| 263 | $("#display #"+ keys[i] +" #value").text(value);
|
|---|
| 264 | }
|
|---|
| 265 | }
|
|---|
| 266 | }
|
|---|
| 267 |
|
|---|
| 268 | if(!("track" in data)){
|
|---|
| 269 | console.log("No track given!");
|
|---|
| 270 | }
|
|---|
| 271 | else{
|
|---|
| 272 | if(GnuBoat.private.map == null){
|
|---|
| 273 | console.log("No map in use");
|
|---|
| 274 | }
|
|---|
| 275 | else{
|
|---|
| 276 | var track = data["track"];
|
|---|
| 277 | var points = [];
|
|---|
| 278 | for(var time in track){
|
|---|
| 279 | points.push(new L.latLng(track[time]["latitude"], track[time]["longitude"]));
|
|---|
| 280 | }
|
|---|
| 281 | if(points.length > 0){
|
|---|
| 282 | if(!GnuBoat.private.polyline){
|
|---|
| 283 | GnuBoat.private.polyline = L.polyline(points, {color: 'red'}).addTo(GnuBoat.private.map);
|
|---|
| 284 | }
|
|---|
| 285 | else{
|
|---|
| 286 | GnuBoat.private.polyline.setLatLngs(points);
|
|---|
| 287 | }
|
|---|
| 288 | GnuBoat.private.map.fitBounds(GnuBoat.private.polyline.getBounds());
|
|---|
| 289 | }
|
|---|
| 290 | }
|
|---|
| 291 | }
|
|---|
| 292 | },
|
|---|
| 293 |
|
|---|
| 294 | mapSetup: function ()
|
|---|
| 295 | {
|
|---|
| 296 | var mapMetadataIndex = $("#mapSelect").val();
|
|---|
| 297 | if(GnuBoat.private.mapsMetadata == null){
|
|---|
| 298 | console.log("No mapsMetadata set");
|
|---|
| 299 | return;
|
|---|
| 300 | }
|
|---|
| 301 | if(mapMetadataIndex < 0 || mapMetadataIndex >= GnuBoat.private.mapsMetadata.length){
|
|---|
| 302 | console.log("Map metadata is outside of range :"+ mapMetadataIndex.toString());
|
|---|
| 303 | console.log(mapsMetadata);
|
|---|
| 304 | return;
|
|---|
| 305 | }
|
|---|
| 306 | var mapMetadata = GnuBoat.private.mapsMetadata[mapMetadataIndex];
|
|---|
| 307 |
|
|---|
| 308 | var latitudeTop = mapMetadata["bounds"][3];
|
|---|
| 309 | var latitudeBottom = mapMetadata["bounds"][1];
|
|---|
| 310 | var longitudeLeft = mapMetadata["bounds"][0];
|
|---|
| 311 | var longitudeRight = mapMetadata["bounds"][2];
|
|---|
| 312 |
|
|---|
| 313 | var mapZoomMin = mapMetadata["minzoom"];
|
|---|
| 314 | var mapZoomMax = mapMetadata["maxzoom"];
|
|---|
| 315 |
|
|---|
| 316 | var latitudeCenter = (latitudeTop - latitudeBottom)/2 + latitudeBottom;
|
|---|
| 317 | var longitudeCenter = (longitudeLeft - longitudeRight)/2 + longitudeRight;
|
|---|
| 318 | var mapZoomStart = (mapZoomMax - mapZoomMin)/2 + mapZoomMin;
|
|---|
| 319 |
|
|---|
| 320 | var mapBounds = new L.LatLngBounds(
|
|---|
| 321 | new L.LatLng(latitudeBottom, longitudeLeft),
|
|---|
| 322 | new L.LatLng(latitudeTop, longitudeRight));
|
|---|
| 323 |
|
|---|
| 324 | if(GnuBoat.private.map == null){
|
|---|
| 325 | // only initialize the map (viewport) once
|
|---|
| 326 | GnuBoat.private.map = L.map('mapViewport');
|
|---|
| 327 | }
|
|---|
| 328 | GnuBoat.private.map.setView([latitudeCenter, longitudeCenter], mapZoomStart);
|
|---|
| 329 |
|
|---|
| 330 | if(GnuBoat.private.mapLayerCurrent != null){
|
|---|
| 331 | GnuBoat.private.map.removeLayer(GnuBoat.private.mapLayerCurrent);
|
|---|
| 332 | GnuBoat.private.mapLayerCurrent = null;
|
|---|
| 333 | }
|
|---|
| 334 | GnuBoat.private.mapLayerCurrent = L.tileLayer(mapMetadata["tiles"][0], {
|
|---|
| 335 | minZoom: mapZoomMin, maxZoom: mapZoomMax,
|
|---|
| 336 | bounds: mapBounds,
|
|---|
| 337 | attribution: mapMetadata["name"],
|
|---|
| 338 | opacity: 0.85
|
|---|
| 339 | }).addTo(GnuBoat.private.map);
|
|---|
| 340 | },
|
|---|
| 341 |
|
|---|
| 342 | mapInfos: function(data)
|
|---|
| 343 | {
|
|---|
| 344 | GnuBoat.private.mapsMetadata = data;
|
|---|
| 345 |
|
|---|
| 346 | $("#mapSelect").empty();
|
|---|
| 347 | var selected = "selected";
|
|---|
| 348 | data.forEach(function(value, index){
|
|---|
| 349 | $("<option value=\""+ index.toString() +"\" "+ selected +">"+ value["name"] +"</option>").appendTo("#mapSelect");
|
|---|
| 350 | selected = "";
|
|---|
| 351 | });
|
|---|
| 352 |
|
|---|
| 353 | GnuBoat.private.mapSetup();
|
|---|
| 354 | $("#mapSelect").change(function(){ mapSetup(); });
|
|---|
| 355 | },
|
|---|
| 356 | };
|
|---|