2020-12-08 01:37:40 +08:00
|
|
|
/* eslint-disable no-inner-declarations */
|
2016-04-01 18:31:07 +08:00
|
|
|
/**
|
2019-01-27 00:25:25 +08:00
|
|
|
* Copyright 2015, 2019 IBM Corp.
|
2016-04-01 18:31:07 +08:00
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
**/
|
|
|
|
|
|
|
|
module.exports = function(RED) {
|
|
|
|
"use strict";
|
2017-05-03 02:46:48 +08:00
|
|
|
var path = require("path");
|
2016-04-01 18:31:07 +08:00
|
|
|
var express = require("express");
|
2019-06-23 23:50:56 +08:00
|
|
|
var compression = require("compression");
|
2017-08-14 01:31:53 +08:00
|
|
|
var sockjs = require('sockjs');
|
2018-10-13 06:58:20 +08:00
|
|
|
var sockets = {};
|
2019-02-07 18:58:49 +08:00
|
|
|
RED.log.info("Worldmap version " + require('./package.json').version );
|
2018-10-13 06:58:20 +08:00
|
|
|
// add the cgi module for serving local maps....
|
|
|
|
RED.httpNode.use("/cgi-bin/mapserv", require('cgi')(__dirname + '/mapserv'));
|
2016-04-01 18:31:07 +08:00
|
|
|
|
2019-09-02 20:22:38 +08:00
|
|
|
function worldMap(node, n) {
|
|
|
|
RED.nodes.createNode(node,n);
|
|
|
|
node.lat = n.lat || "";
|
|
|
|
node.lon = n.lon || "";
|
|
|
|
node.zoom = n.zoom || "";
|
|
|
|
node.layer = n.layer || "";
|
|
|
|
node.cluster = n.cluster || "";
|
|
|
|
node.maxage = n.maxage || "";
|
|
|
|
if (n.maxage == 0) { node.maxage = "0"; }
|
|
|
|
node.showmenu = n.usermenu || "show";
|
|
|
|
node.layers = n.layers || "show";
|
|
|
|
node.panlock = n.panlock || "false";
|
|
|
|
node.zoomlock = n.zoomlock || "false";
|
|
|
|
node.panit = n.panit || "false";
|
|
|
|
node.hiderightclick = n.hiderightclick || "false";
|
|
|
|
node.coords = n.coords || "none";
|
|
|
|
node.showgrid = n.showgrid || "false";
|
2020-12-07 07:53:47 +08:00
|
|
|
node.allowFileDrop = n.allowFileDrop || "false";
|
2019-09-02 20:22:38 +08:00
|
|
|
node.path = n.path || "/worldmap";
|
2019-09-27 19:21:59 +08:00
|
|
|
if (node.path.charAt(0) != "/") { node.path = "/" + node.path; }
|
2019-09-02 20:22:38 +08:00
|
|
|
if (!sockets[node.path]) {
|
|
|
|
var libPath = path.posix.join(RED.settings.httpNodeRoot, node.path, 'leaflet', 'sockjs.min.js');
|
|
|
|
var sockPath = path.posix.join(RED.settings.httpNodeRoot,node.path,'socket');
|
|
|
|
sockets[node.path] = sockjs.createServer({prefix:sockPath, sockjs_url:libPath, log:function() { return; }});
|
|
|
|
sockets[node.path].installHandlers(RED.server);
|
2018-10-13 06:58:20 +08:00
|
|
|
}
|
2019-09-02 20:22:38 +08:00
|
|
|
//node.log("Serving "+__dirname+" as "+node.path);
|
|
|
|
node.log("started at "+node.path);
|
2017-08-14 01:31:53 +08:00
|
|
|
var clients = {};
|
2019-06-23 23:50:56 +08:00
|
|
|
RED.httpNode.use(compression());
|
2018-10-13 06:58:20 +08:00
|
|
|
RED.httpNode.use(node.path, express.static(__dirname + '/worldmap'));
|
2020-04-14 20:46:45 +08:00
|
|
|
// RED.httpNode.use(node.path, express.static(__dirname + '/worldmap', {maxage:3600000}));
|
2018-10-13 06:58:20 +08:00
|
|
|
|
2016-06-27 18:55:30 +08:00
|
|
|
var callback = function(client) {
|
2020-06-04 23:34:00 +08:00
|
|
|
if (!client.headers.hasOwnProperty("user-agent")) { client.close(); }
|
2017-08-14 01:31:53 +08:00
|
|
|
//client.setMaxListeners(0);
|
|
|
|
clients[client.id] = client;
|
|
|
|
client.on('data', function(message) {
|
|
|
|
message = JSON.parse(message);
|
|
|
|
if (message.action === "connected") {
|
2016-06-12 19:40:29 +08:00
|
|
|
var c = {init:true};
|
|
|
|
if (node.lat && node.lat.length > 0) { c.lat = node.lat; }
|
|
|
|
if (node.lon && node.lon.length > 0) { c.lon = node.lon; }
|
|
|
|
if (node.zoom && node.zoom.length > 0) { c.zoom = node.zoom; }
|
|
|
|
if (node.layer && node.layer.length > 0) { c.layer = node.layer; }
|
2016-06-15 19:57:31 +08:00
|
|
|
if (node.cluster && node.cluster.length > 0) { c.cluster = node.cluster; }
|
|
|
|
if (node.maxage && node.maxage.length > 0) { c.maxage = node.maxage; }
|
2016-11-22 16:21:56 +08:00
|
|
|
c.showmenu = node.showmenu;
|
|
|
|
c.panit = node.panit;
|
2018-12-23 22:30:26 +08:00
|
|
|
c.panlock = node.panlock;
|
|
|
|
c.zoomlock = node.zoomlock;
|
2017-10-11 06:51:18 +08:00
|
|
|
c.showlayers = node.layers;
|
2019-04-22 05:32:01 +08:00
|
|
|
c.grid = {showgrid:node.showgrid};
|
2019-02-28 06:05:02 +08:00
|
|
|
c.hiderightclick = node.hiderightclick;
|
2020-12-07 07:53:47 +08:00
|
|
|
c.allowFileDrop = node.allowFileDrop;
|
2019-03-11 02:29:44 +08:00
|
|
|
c.coords = node.coords;
|
2020-11-17 22:45:52 +08:00
|
|
|
c.toptitle = node.name;
|
2017-08-14 01:31:53 +08:00
|
|
|
client.write(JSON.stringify({command:c}));
|
2016-06-12 19:40:29 +08:00
|
|
|
}
|
|
|
|
});
|
2017-08-14 01:31:53 +08:00
|
|
|
client.on('close', function() {
|
|
|
|
delete clients[client.id];
|
2019-03-18 17:03:04 +08:00
|
|
|
node.status({fill:"green",shape:"ring",text:"connected "+Object.keys(clients).length,_sessionid:client.id});
|
2016-06-02 06:40:38 +08:00
|
|
|
});
|
2019-03-18 17:03:04 +08:00
|
|
|
node.status({fill:"green",shape:"dot",text:"connected "+Object.keys(clients).length,_sessionid:client.id});
|
2016-06-02 06:40:38 +08:00
|
|
|
}
|
2017-08-14 01:31:53 +08:00
|
|
|
node.on('input', function(msg) {
|
2018-09-13 19:58:18 +08:00
|
|
|
if (msg.hasOwnProperty("_sessionid")) {
|
|
|
|
if (clients.hasOwnProperty(msg._sessionid)) {
|
|
|
|
clients[msg._sessionid].write(JSON.stringify(msg.payload));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
for (var c in clients) {
|
|
|
|
if (clients.hasOwnProperty(c)) {
|
|
|
|
clients[c].write(JSON.stringify(msg.payload));
|
|
|
|
}
|
2017-08-14 01:31:53 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2017-06-28 15:56:38 +08:00
|
|
|
node.on("close", function() {
|
2017-08-14 01:31:53 +08:00
|
|
|
for (var c in clients) {
|
|
|
|
if (clients.hasOwnProperty(c)) {
|
|
|
|
clients[c].end();
|
|
|
|
}
|
|
|
|
}
|
2019-03-21 00:11:20 +08:00
|
|
|
clients = {};
|
2019-09-02 20:22:38 +08:00
|
|
|
sockets[node.path].removeListener('connection', callback);
|
2018-10-13 06:58:20 +08:00
|
|
|
for (var i=0; i < RED.httpNode._router.stack.length; i++) {
|
|
|
|
var r = RED.httpNode._router.stack[i];
|
|
|
|
if ((r.name === "serveStatic") && (r.regexp.test(node.path))) {
|
|
|
|
RED.httpNode._router.stack.splice(i, 1)
|
|
|
|
}
|
|
|
|
}
|
2017-08-14 01:31:53 +08:00
|
|
|
node.status({});
|
2017-06-28 15:56:38 +08:00
|
|
|
});
|
2019-09-02 20:22:38 +08:00
|
|
|
sockets[node.path].on('connection', callback);
|
|
|
|
}
|
|
|
|
|
|
|
|
var WorldMap = function(n) {
|
|
|
|
worldMap(this, n);
|
2016-04-01 18:31:07 +08:00
|
|
|
}
|
|
|
|
RED.nodes.registerType("worldmap",WorldMap);
|
2016-05-19 06:26:08 +08:00
|
|
|
|
2019-09-02 20:22:38 +08:00
|
|
|
function HTML(ui, config) {
|
|
|
|
var width = config.width;
|
|
|
|
if (width == 0) {
|
|
|
|
var group = RED.nodes.getNode(config.group);
|
2019-09-02 20:30:35 +08:00
|
|
|
if (group) { width = group.config.width; }
|
2019-09-02 20:22:38 +08:00
|
|
|
}
|
|
|
|
var height = config.height;
|
2019-09-02 20:30:35 +08:00
|
|
|
if (height == 0) { height = 10; }
|
2019-09-02 20:22:38 +08:00
|
|
|
var size = ui.getSizes();
|
2019-09-02 20:30:35 +08:00
|
|
|
var frameWidth = (size.sx +size.cx) *width - size.cx - 1;
|
|
|
|
var frameHeight = (size.sy +size.cy) *height - size.cy - 2;
|
2019-09-02 20:22:38 +08:00
|
|
|
var url = encodeURI(config.path);
|
2020-12-07 07:53:47 +08:00
|
|
|
var html = `<style>.nr-dashboard-ui_worldmap{padding:0;}</style><div style="overflow:hidden;">
|
2019-09-02 20:30:35 +08:00
|
|
|
<iframe src="${url}" width="${frameWidth}px" height="${frameHeight}px" style="border:none;"></iframe>
|
2019-09-02 20:22:38 +08:00
|
|
|
</div>
|
|
|
|
`;
|
|
|
|
return html;
|
|
|
|
}
|
|
|
|
|
|
|
|
function checkConfig(node, conf) {
|
|
|
|
if (!conf || !conf.hasOwnProperty("group")) {
|
|
|
|
node.error("no group");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
var ui = undefined;
|
2019-09-05 05:13:01 +08:00
|
|
|
try {
|
|
|
|
ui = RED.require("node-red-dashboard")(RED);
|
2019-09-05 05:29:53 +08:00
|
|
|
if (ui) {
|
2019-09-05 05:13:01 +08:00
|
|
|
function UIWorldMap(config) {
|
|
|
|
try {
|
|
|
|
var node = this;
|
|
|
|
worldMap(node, config);
|
|
|
|
var done = null;
|
|
|
|
if (checkConfig(node, config)) {
|
|
|
|
var html = HTML(ui, config);
|
|
|
|
done = ui.addWidget({
|
|
|
|
node: node,
|
|
|
|
order: config.order,
|
|
|
|
group: config.group,
|
|
|
|
width: config.width,
|
|
|
|
height: config.height,
|
|
|
|
format: html,
|
|
|
|
templateScope: "local",
|
|
|
|
emitOnlyNewValues: false,
|
|
|
|
forwardInputMessages: false,
|
|
|
|
storeFrontEndInputAsState: false,
|
|
|
|
convertBack: function (value) {
|
|
|
|
return value;
|
|
|
|
},
|
|
|
|
beforeEmit: function(msg, value) {
|
|
|
|
return { msg: { items: value } };
|
|
|
|
},
|
|
|
|
beforeSend: function (msg, orig) {
|
|
|
|
if (orig) { return orig.msg; }
|
|
|
|
},
|
|
|
|
initController: function($scope, events) {
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (e) {
|
|
|
|
console.log(e);
|
|
|
|
}
|
|
|
|
node.on("close", function() {
|
2019-09-05 05:29:53 +08:00
|
|
|
if (done) { done(); }
|
2019-09-02 20:22:38 +08:00
|
|
|
});
|
|
|
|
}
|
2020-08-24 18:39:59 +08:00
|
|
|
setImmediate(function() { RED.nodes.registerType("ui_worldmap", UIWorldMap) });
|
2019-09-02 20:22:38 +08:00
|
|
|
}
|
|
|
|
}
|
2019-09-05 05:13:01 +08:00
|
|
|
catch(e) {
|
2019-09-05 05:29:53 +08:00
|
|
|
RED.log.info("Node-RED Dashboard not found - ui_worldmap not installed.");
|
2019-09-05 05:13:01 +08:00
|
|
|
}
|
2019-09-02 20:22:38 +08:00
|
|
|
|
2016-05-19 06:26:08 +08:00
|
|
|
var WorldMapIn = function(n) {
|
|
|
|
RED.nodes.createNode(this,n);
|
2018-10-13 06:58:20 +08:00
|
|
|
this.path = n.path || "/worldmap";
|
2019-09-02 22:08:34 +08:00
|
|
|
this.events = n.events || "all";
|
2018-10-16 00:11:48 +08:00
|
|
|
if (this.path.charAt(0) != "/") { this.path = "/" + this.path; }
|
2018-10-13 06:58:20 +08:00
|
|
|
if (!sockets[this.path]) {
|
2019-03-21 00:11:20 +08:00
|
|
|
var libPath = path.posix.join(RED.settings.httpNodeRoot, this.path, 'leaflet', 'sockjs.min.js');
|
|
|
|
var sockPath = path.posix.join(RED.settings.httpNodeRoot,this.path,'socket');
|
|
|
|
sockets[this.path] = sockjs.createServer({prefix:sockPath, sockjs_url:libPath, log:function() { return; }});
|
2018-10-13 06:58:20 +08:00
|
|
|
sockets[this.path].installHandlers(RED.server);
|
2017-07-13 00:59:56 +08:00
|
|
|
}
|
2016-05-19 06:26:08 +08:00
|
|
|
var node = this;
|
2017-08-14 01:31:53 +08:00
|
|
|
var clients = {};
|
2016-06-27 18:55:30 +08:00
|
|
|
|
|
|
|
var callback = function(client) {
|
2017-08-14 01:31:53 +08:00
|
|
|
//client.setMaxListeners(0);
|
|
|
|
clients[client.id] = client;
|
2019-03-18 17:03:04 +08:00
|
|
|
node.status({fill:"green",shape:"dot",text:"connected "+Object.keys(clients).length,_sessionid:client.id});
|
2017-08-14 01:31:53 +08:00
|
|
|
client.on('data', function(message) {
|
|
|
|
message = JSON.parse(message);
|
2020-12-07 07:53:47 +08:00
|
|
|
if (message.hasOwnProperty("action")) {
|
|
|
|
if ((node.events === "files") && (message.action === "file")) {
|
2020-12-08 01:37:40 +08:00
|
|
|
message.content = Buffer.from(message.content.split('base64,')[1], 'base64');
|
2020-12-07 07:53:47 +08:00
|
|
|
setImmediate(function() {node.send({payload:message, topic:node.path.substr(1), _sessionid:client.id})});
|
|
|
|
}
|
|
|
|
else if ((node.events === "connect") && (message.action === "connected")) {
|
|
|
|
setImmediate(function() {node.send({payload:message, topic:node.path.substr(1), _sessionid:client.id})});
|
|
|
|
}
|
|
|
|
else if (node.events === "all") {
|
2019-09-02 22:19:09 +08:00
|
|
|
setImmediate(function() {node.send({payload:message, topic:node.path.substr(1), _sessionid:client.id})});
|
|
|
|
}
|
|
|
|
}
|
2016-05-19 06:26:08 +08:00
|
|
|
});
|
2017-08-14 01:31:53 +08:00
|
|
|
client.on('close', function() {
|
|
|
|
delete clients[client.id];
|
2019-03-18 17:03:04 +08:00
|
|
|
node.status({fill:"green",shape:"ring",text:"connected "+Object.keys(clients).length,_sessionid:client.id});
|
2020-12-07 07:53:47 +08:00
|
|
|
if (node.events !== "files") {
|
2019-09-02 22:08:34 +08:00
|
|
|
node.send({payload:{action:"disconnect", clients:Object.keys(clients).length}, topic:node.path.substr(1), _sessionid:client.id});
|
|
|
|
}
|
2016-06-02 06:40:38 +08:00
|
|
|
});
|
|
|
|
}
|
2016-06-27 18:55:30 +08:00
|
|
|
|
2016-06-02 19:35:12 +08:00
|
|
|
node.on("close", function() {
|
2017-08-14 01:31:53 +08:00
|
|
|
for (var c in clients) {
|
|
|
|
if (clients.hasOwnProperty(c)) {
|
|
|
|
clients[c].end();
|
|
|
|
}
|
|
|
|
}
|
2019-03-21 00:11:20 +08:00
|
|
|
clients = {};
|
2018-10-13 06:58:20 +08:00
|
|
|
sockets[this.path].removeListener('connection', callback);
|
2016-06-02 19:35:12 +08:00
|
|
|
node.status({});
|
|
|
|
});
|
2018-10-13 06:58:20 +08:00
|
|
|
sockets[this.path].on('connection', callback);
|
2016-05-19 06:26:08 +08:00
|
|
|
}
|
|
|
|
RED.nodes.registerType("worldmap in",WorldMapIn);
|
2017-05-15 20:55:22 +08:00
|
|
|
|
|
|
|
var WorldMapTracks = function(n) {
|
|
|
|
RED.nodes.createNode(this,n);
|
2019-04-01 19:17:20 +08:00
|
|
|
this.depth = parseInt(Number(n.depth) || 20);
|
2017-08-14 01:31:53 +08:00
|
|
|
this.pointsarray = {};
|
2019-04-01 19:17:20 +08:00
|
|
|
this.layer = n.layer || "combined"; // separate, single
|
2017-05-15 20:55:22 +08:00
|
|
|
var node = this;
|
|
|
|
|
2020-04-09 05:06:48 +08:00
|
|
|
var doTrack = function(msg) {
|
2017-05-15 20:55:22 +08:00
|
|
|
if (msg.hasOwnProperty("payload") && msg.payload.hasOwnProperty("name")) {
|
|
|
|
var newmsg = RED.util.cloneMessage(msg);
|
|
|
|
if (msg.payload.deleted) {
|
2020-03-17 03:36:40 +08:00
|
|
|
if (msg.payload.name.substr(-1) === '_') {
|
|
|
|
var a = node.pointsarray[msg.payload.name.substr(0,msg.payload.name.length-1)].pop();
|
|
|
|
node.pointsarray[msg.payload.name.substr(0,msg.payload.name.length-1)] = [ a ];
|
2020-10-29 18:07:19 +08:00
|
|
|
node.send(newmsg);
|
2020-03-17 03:36:40 +08:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
delete node.pointsarray[msg.payload.name];
|
|
|
|
}
|
2018-06-07 01:13:50 +08:00
|
|
|
//newmsg.payload.name = msg.payload.name + "_";
|
2020-10-29 18:07:19 +08:00
|
|
|
node.send(newmsg);
|
2017-05-15 20:55:22 +08:00
|
|
|
return;
|
|
|
|
}
|
2019-04-02 20:04:06 +08:00
|
|
|
if (!msg.payload.hasOwnProperty("lat") || !msg.payload.hasOwnProperty("lon")) { return; }
|
2017-08-14 01:31:53 +08:00
|
|
|
if (!node.pointsarray.hasOwnProperty(msg.payload.name)) {
|
|
|
|
node.pointsarray[msg.payload.name] = [];
|
2017-05-15 20:55:22 +08:00
|
|
|
}
|
2019-04-01 19:17:20 +08:00
|
|
|
if (msg.payload.hasOwnProperty("trackpoints") && !isNaN(parseInt(msg.payload.trackpoints)) ) {
|
|
|
|
var tl = parseInt(msg.payload.trackpoints);
|
|
|
|
if (tl < 0) { tl = 0; }
|
|
|
|
if (node.pointsarray[msg.payload.name].length > tl) {
|
|
|
|
node.pointsarray[msg.payload.name] = node.pointsarray[msg.payload.name].slice(-tl);
|
|
|
|
}
|
|
|
|
node.depth = tl;
|
|
|
|
}
|
|
|
|
if (node.depth < 2) { return; } // if set less than 2 then don't bother.
|
|
|
|
|
2019-04-02 20:04:06 +08:00
|
|
|
var still = false;
|
|
|
|
if (node.pointsarray[msg.payload.name].length > 0) {
|
|
|
|
var oldlat = node.pointsarray[msg.payload.name][node.pointsarray[msg.payload.name].length-1].lat;
|
|
|
|
var oldlon = node.pointsarray[msg.payload.name][node.pointsarray[msg.payload.name].length-1].lon;
|
|
|
|
if (msg.payload.lat === oldlat && msg.payload.lon === oldlon) { still = true; }
|
|
|
|
}
|
|
|
|
if (!still) { node.pointsarray[msg.payload.name].push(msg.payload);
|
|
|
|
if (node.pointsarray[msg.payload.name].length > node.depth) {
|
|
|
|
node.pointsarray[msg.payload.name].shift();
|
|
|
|
}
|
2017-05-15 20:55:22 +08:00
|
|
|
}
|
|
|
|
var line = [];
|
2017-08-14 01:31:53 +08:00
|
|
|
for (var i=0; i<node.pointsarray[msg.payload.name].length; i++) {
|
|
|
|
var m = node.pointsarray[msg.payload.name][i];
|
2017-05-15 20:55:22 +08:00
|
|
|
if (m.hasOwnProperty("lat") && m.hasOwnProperty("lon")) {
|
|
|
|
line.push( [m.lat*1, m.lon*1] );
|
|
|
|
delete newmsg.payload.lat;
|
|
|
|
delete newmsg.payload.lon;
|
|
|
|
}
|
2017-06-21 06:27:18 +08:00
|
|
|
if (m.hasOwnProperty("latitude") && m.hasOwnProperty("longitude")) {
|
|
|
|
line.push( [m.latitude*1, m.longitude*1] );
|
|
|
|
delete newmsg.payload.latitude;
|
|
|
|
delete newmsg.payload.longitude;
|
|
|
|
}
|
2017-05-15 20:55:22 +08:00
|
|
|
if (m.hasOwnProperty("position") && m.position.hasOwnProperty("lat") && m.position.hasOwnProperty("lon")) {
|
|
|
|
line.push( [m.position.lat*1, m.position.lon*1] );
|
|
|
|
delete newmsg.payload.position;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (line.length > 1) { // only send track if two points or more
|
|
|
|
newmsg.payload.line = line;
|
|
|
|
newmsg.payload.name = msg.payload.name + "_";
|
2019-04-01 19:17:20 +08:00
|
|
|
if (node.layer === "separate") {
|
|
|
|
newmsg.payload.layer = msg.payload.layer + " tracks";
|
|
|
|
if (newmsg.payload.layer.indexOf('_') === 0) {
|
|
|
|
newmsg.payload.layer = newmsg.payload.layer.substr(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (node.layer === "single") {
|
|
|
|
newmsg.payload.layer = "Tracks";
|
|
|
|
}
|
2017-05-15 20:55:22 +08:00
|
|
|
node.send(newmsg); // send the track
|
|
|
|
}
|
|
|
|
}
|
2019-06-21 06:13:24 +08:00
|
|
|
if (msg.hasOwnProperty("payload") && msg.payload.hasOwnProperty("command") && msg.payload.command.hasOwnProperty("clear")) {
|
|
|
|
for (var p in node.pointsarray) {
|
|
|
|
if (node.pointsarray.hasOwnProperty(p)) {
|
|
|
|
if (node.pointsarray[p][0].layer === msg.payload.command.clear) {
|
|
|
|
delete node.pointsarray[p];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-04-09 05:06:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
node.on("input", function(m) {
|
2020-04-14 20:46:45 +08:00
|
|
|
if (Array.isArray(m.payload)) {
|
|
|
|
m.payload.forEach(function (pay) {
|
|
|
|
var n = RED.util.cloneMessage(m)
|
|
|
|
n.payload = pay;
|
|
|
|
doTrack(n);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
else {
|
2020-10-29 18:07:19 +08:00
|
|
|
doTrack(m);
|
2020-04-09 05:06:48 +08:00
|
|
|
}
|
2017-05-15 20:55:22 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
node.on("close", function() {
|
2017-08-14 01:31:53 +08:00
|
|
|
node.pointsarray = {};
|
2017-05-15 20:55:22 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
RED.nodes.registerType("worldmap-tracks",WorldMapTracks);
|
2019-09-05 05:13:01 +08:00
|
|
|
|
2020-07-10 05:09:57 +08:00
|
|
|
var WorldMapHull = function(n) {
|
|
|
|
RED.nodes.createNode(this,n);
|
|
|
|
this.prop = n.prop || "layer";
|
|
|
|
var node = this;
|
2020-11-18 03:50:38 +08:00
|
|
|
node.oldlayercount = {};
|
2020-07-10 05:09:57 +08:00
|
|
|
node.hulls = {};
|
|
|
|
|
|
|
|
var convexHull = function(points) {
|
|
|
|
var arr = [];
|
|
|
|
for (const val of Object.values(points)) {
|
|
|
|
arr.push(val);
|
|
|
|
}
|
2020-10-29 18:07:19 +08:00
|
|
|
|
2020-07-10 05:09:57 +08:00
|
|
|
arr.sort(function (a, b) {
|
|
|
|
return a.lat != b.lat ? a.lat - b.lat : a.lon - b.lon;
|
|
|
|
});
|
2020-10-29 18:07:19 +08:00
|
|
|
|
2020-07-10 05:09:57 +08:00
|
|
|
var n = arr.length;
|
|
|
|
var hull = [];
|
2020-10-29 18:07:19 +08:00
|
|
|
|
2020-07-10 05:09:57 +08:00
|
|
|
for (var i = 0; i < 2 * n; i++) {
|
|
|
|
var j = i < n ? i : 2 * n - 1 - i;
|
|
|
|
while (hull.length >= 2 && removeMiddle(hull[hull.length - 2], hull[hull.length - 1], arr[j]))
|
|
|
|
hull.pop();
|
|
|
|
hull.push(arr[j]);
|
|
|
|
}
|
2020-10-29 18:07:19 +08:00
|
|
|
|
2020-07-10 05:09:57 +08:00
|
|
|
hull.pop();
|
|
|
|
return hull;
|
|
|
|
}
|
2020-10-29 18:07:19 +08:00
|
|
|
|
2020-07-10 05:09:57 +08:00
|
|
|
var removeMiddle = function(a, b, c) {
|
|
|
|
var cross = (a.lat- b.lat) * (c.lon - b.lon) - (a.lon - b.lon) * (c.lat- b.lat);
|
|
|
|
var dot = (a.lat- b.lat) * (c.lat- b.lat) + (a.lon - b.lon) * (c.lon - b.lon);
|
|
|
|
return cross < 0 || cross == 0 && dot <= 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
var doHull = function(msg) {
|
|
|
|
if (msg.hasOwnProperty("payload") && msg.payload.hasOwnProperty("name")) {
|
|
|
|
var newmsg = RED.util.cloneMessage(msg);
|
|
|
|
newmsg.payload = {};
|
|
|
|
newmsg.payload[node.prop] = msg.payload[node.prop] || "unknown";
|
|
|
|
if (msg.payload.deleted === true) {
|
|
|
|
if (node.hulls.hasOwnProperty(newmsg.payload[node.prop])) {
|
|
|
|
delete node.hulls[newmsg.payload[node.prop]][msg.payload.name];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (!msg.payload.hasOwnProperty("lat") || !msg.payload.hasOwnProperty("lon")) { return; }
|
|
|
|
if (!node.hulls.hasOwnProperty(newmsg.payload[node.prop])) {
|
|
|
|
node.hulls[newmsg.payload[node.prop]] = {};
|
|
|
|
}
|
|
|
|
node.hulls[newmsg.payload[node.prop]][msg.payload.name] = {lat:msg.payload.lat,lon:msg.payload.lon};
|
|
|
|
}
|
|
|
|
var convexHullPoints = convexHull(node.hulls[newmsg.payload[node.prop]]);
|
|
|
|
var leafletHull = convexHullPoints.map(function (element) {return ([element.lat,element.lon])})
|
|
|
|
|
2020-09-02 23:38:01 +08:00
|
|
|
newmsg.payload.name = newmsg.payload[node.prop];
|
|
|
|
newmsg.payload.clickable = true;
|
|
|
|
|
2020-11-16 17:13:30 +08:00
|
|
|
if (msg.payload.fillColor) {
|
|
|
|
newmsg.payload.color = msg.payload.fillColor;
|
|
|
|
newmsg.payload.fillColor = msg.payload.fillColor;
|
|
|
|
}
|
|
|
|
|
2020-11-18 03:50:38 +08:00
|
|
|
if (node.oldlayercount[newmsg.payload[node.prop]] === undefined) {
|
|
|
|
node.oldlayercount[newmsg.payload[node.prop]] = 0;
|
|
|
|
}
|
|
|
|
var oldl = node.oldlayercount[newmsg.payload[node.prop]];
|
|
|
|
|
2020-10-29 18:07:19 +08:00
|
|
|
if (leafletHull.length === 1 && oldl === 2) {
|
2020-09-02 23:38:01 +08:00
|
|
|
newmsg.payload.deleted = true;
|
|
|
|
node.send(newmsg);
|
|
|
|
}
|
2020-11-18 03:50:38 +08:00
|
|
|
if (leafletHull.length === 2 && (oldl === 1 || oldl === 3)) {
|
2020-09-02 23:38:01 +08:00
|
|
|
newmsg.payload.deleted = true;
|
|
|
|
node.send(newmsg);
|
|
|
|
delete newmsg.payload.deleted;
|
2020-10-29 18:07:19 +08:00
|
|
|
newmsg.payload.line = leafletHull;
|
2020-09-02 23:38:01 +08:00
|
|
|
node.send(newmsg);
|
|
|
|
}
|
2020-10-29 18:07:19 +08:00
|
|
|
if (leafletHull.length === 3 && oldl === 2) {
|
2020-09-02 23:38:01 +08:00
|
|
|
newmsg.payload.deleted = true;
|
|
|
|
node.send(newmsg);
|
|
|
|
delete newmsg.payload.deleted;
|
|
|
|
}
|
|
|
|
if (leafletHull.length >= 3) {
|
2020-10-29 18:07:19 +08:00
|
|
|
newmsg.payload.area = leafletHull;
|
2020-07-10 05:09:57 +08:00
|
|
|
node.send(newmsg);
|
|
|
|
}
|
2020-10-29 18:07:19 +08:00
|
|
|
|
2020-11-18 03:50:38 +08:00
|
|
|
node.oldlayercount[newmsg.payload[node.prop]] = leafletHull.length;
|
2020-07-10 05:09:57 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
node.on("input", function(m) {
|
|
|
|
if (Array.isArray(m.payload)) {
|
|
|
|
m.payload.forEach(function (pay) {
|
|
|
|
var n = RED.util.cloneMessage(m)
|
|
|
|
n.payload = pay;
|
|
|
|
doHull(n);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
else {
|
2020-10-29 18:07:19 +08:00
|
|
|
doHull(m);
|
2020-07-10 05:09:57 +08:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
node.on("close", function() {
|
|
|
|
node.hulls = {};
|
|
|
|
});
|
|
|
|
}
|
|
|
|
RED.nodes.registerType("worldmap-hull",WorldMapHull);
|
|
|
|
|
2020-10-29 18:07:19 +08:00
|
|
|
|
2019-09-05 05:13:01 +08:00
|
|
|
RED.httpNode.get("/.ui-worldmap", function(req, res) {
|
|
|
|
res.send(ui ? "true": "false");
|
|
|
|
});
|
2016-04-01 18:31:07 +08:00
|
|
|
}
|