2016-04-01 18:31:07 +08:00
|
|
|
/**
|
2018-10-03 21:57:14 +08:00
|
|
|
* Copyright 2015, 2018 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");
|
2017-08-14 01:31:53 +08:00
|
|
|
var sockjs = require('sockjs');
|
2018-10-13 06:58:20 +08:00
|
|
|
var sockets = {};
|
|
|
|
// 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
|
|
|
|
|
|
|
var WorldMap = function(n) {
|
|
|
|
RED.nodes.createNode(this,n);
|
2016-06-12 19:40:29 +08:00
|
|
|
this.lat = n.lat || "";
|
|
|
|
this.lon = n.lon || "";
|
|
|
|
this.zoom = n.zoom || "";
|
|
|
|
this.layer = n.layer || "";
|
2016-06-15 19:57:31 +08:00
|
|
|
this.cluster = n.cluster || "";
|
|
|
|
this.maxage = n.maxage || "";
|
2016-11-22 16:21:56 +08:00
|
|
|
this.showmenu = n.usermenu || "show";
|
|
|
|
this.panit = n.panit || "false";
|
2017-10-11 06:51:18 +08:00
|
|
|
this.layers = n.layers || "show";
|
2018-10-13 06:58:20 +08:00
|
|
|
this.path = n.path || "/worldmap";
|
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]) {
|
|
|
|
var fullPath = path.posix.join(RED.settings.httpNodeRoot, this.path, 'leaflet', 'sockjs.min.js');
|
|
|
|
sockets[this.path] = sockjs.createServer({sockjs_url:fullPath, log:function() {}, transports:"xhr-polling"});
|
2018-10-15 23:18:22 +08:00
|
|
|
var sockPath = path.posix.join(RED.settings.httpNodeRoot,this.path,'socket');
|
2018-10-16 00:11:48 +08:00
|
|
|
this.log("Serving "+__dirname+" as "+this.path); // +" and socket "+sockPath);
|
2018-10-15 23:18:22 +08:00
|
|
|
sockets[this.path].installHandlers(RED.server, {prefix:sockPath});
|
2018-10-13 06:58:20 +08:00
|
|
|
}
|
2016-05-19 06:26:08 +08:00
|
|
|
var node = this;
|
2017-08-14 01:31:53 +08:00
|
|
|
var clients = {};
|
2018-10-13 06:58:20 +08:00
|
|
|
RED.httpNode.use(node.path, express.static(__dirname + '/worldmap'));
|
|
|
|
|
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;
|
|
|
|
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;
|
2017-10-11 06:51:18 +08:00
|
|
|
c.showlayers = node.layers;
|
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];
|
|
|
|
node.status({fill:"green",shape:"ring",text:"connected "+Object.keys(clients).length});
|
2016-06-02 06:40:38 +08:00
|
|
|
});
|
2018-06-07 06:45:06 +08:00
|
|
|
node.status({fill:"green",shape:"dot",text:"connected "+Object.keys(clients).length});
|
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();
|
|
|
|
}
|
|
|
|
}
|
2018-10-13 06:58:20 +08:00
|
|
|
sockets[this.path].removeListener('connection', callback);
|
|
|
|
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
|
|
|
});
|
2018-10-13 06:58:20 +08:00
|
|
|
sockets[this.path].on('connection', callback);
|
2016-04-01 18:31:07 +08:00
|
|
|
}
|
|
|
|
RED.nodes.registerType("worldmap",WorldMap);
|
2016-05-19 06:26:08 +08:00
|
|
|
|
2017-05-03 02:46:48 +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";
|
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]) {
|
|
|
|
var fullPath = path.posix.join(RED.settings.httpNodeRoot, this.path, 'leaflet', 'sockjs.min.js');
|
|
|
|
sockets[this.path] = sockjs.createServer({sockjs_url:fullPath, prefix:path.posix.join(RED.settings.httpNodeRoot,this.path,'socket')});
|
|
|
|
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;
|
|
|
|
node.status({fill:"green",shape:"dot",text:"connected "+Object.keys(clients).length});
|
|
|
|
client.on('data', function(message) {
|
|
|
|
message = JSON.parse(message);
|
2018-10-13 06:58:20 +08:00
|
|
|
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];
|
|
|
|
node.status({fill:"green",shape:"ring",text:"connected "+Object.keys(clients).length});
|
|
|
|
node.send({payload:{action:"disconnect", clients:Object.keys(clients).length}, topic:"worldmap"});
|
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();
|
|
|
|
}
|
|
|
|
}
|
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
|
|
|
|
2017-06-21 06:27:18 +08:00
|
|
|
|
2017-05-15 20:55:22 +08:00
|
|
|
var WorldMapTracks = function(n) {
|
|
|
|
RED.nodes.createNode(this,n);
|
|
|
|
this.depth = Number(n.depth) || 20;
|
2017-08-14 01:31:53 +08:00
|
|
|
this.pointsarray = {};
|
2017-05-15 20:55:22 +08:00
|
|
|
var node = this;
|
|
|
|
|
|
|
|
node.on("input", function(msg) {
|
|
|
|
if (msg.hasOwnProperty("payload") && msg.payload.hasOwnProperty("name")) {
|
|
|
|
var newmsg = RED.util.cloneMessage(msg);
|
|
|
|
if (msg.payload.deleted) {
|
2017-08-14 01:31:53 +08:00
|
|
|
delete node.pointsarray[msg.payload.name];
|
2018-06-07 01:13:50 +08:00
|
|
|
//newmsg.payload.name = msg.payload.name + "_";
|
2017-05-15 20:55:22 +08:00
|
|
|
node.send(newmsg); // send the track to be deleted
|
|
|
|
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
|
|
|
}
|
2017-08-14 01:31:53 +08:00
|
|
|
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 + "_";
|
|
|
|
node.send(newmsg); // send the track
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
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);
|
2016-04-01 18:31:07 +08:00
|
|
|
}
|