2016-04-01 18:31:07 +08:00
|
|
|
/**
|
2016-05-19 06:26:08 +08:00
|
|
|
* Copyright 2015, 2016 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";
|
|
|
|
var express = require("express");
|
2016-06-27 18:55:30 +08:00
|
|
|
var io = require('socket.io');
|
|
|
|
var socket;
|
2016-04-01 18:31:07 +08:00
|
|
|
|
|
|
|
var WorldMap = function(n) {
|
|
|
|
RED.nodes.createNode(this,n);
|
2016-06-27 18:55:30 +08:00
|
|
|
if (!socket) { socket = io.listen(RED.server); }
|
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";
|
2016-05-19 06:26:08 +08:00
|
|
|
var node = this;
|
|
|
|
//node.log("Serving map from "+__dirname+" as "+RED.settings.httpNodeRoot.slice(0,-1)+"/worldmap");
|
2016-04-01 18:31:07 +08:00
|
|
|
RED.httpNode.use("/worldmap", express.static(__dirname + '/worldmap'));
|
2016-06-27 18:55:30 +08:00
|
|
|
|
|
|
|
var callback = function(client) {
|
2016-08-17 16:24:12 +08:00
|
|
|
client.setMaxListeners(0);
|
2016-06-27 18:55:30 +08:00
|
|
|
node.status({fill:"green",shape:"dot",text:"connected "+socket.engine.clientsCount});
|
|
|
|
function handler(msg) {
|
|
|
|
client.emit("worldmapdata",msg.payload);
|
|
|
|
}
|
|
|
|
node.on('input', handler);
|
|
|
|
client.on('worldmap', function(data) {
|
2016-06-12 19:40:29 +08:00
|
|
|
if (data.action === "connected") {
|
|
|
|
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;
|
2016-06-27 18:55:30 +08:00
|
|
|
client.emit("worldmapdata",{command:c});
|
2016-06-12 19:40:29 +08:00
|
|
|
}
|
|
|
|
});
|
2016-06-27 18:55:30 +08:00
|
|
|
client.on('disconnect', function() {
|
|
|
|
node.removeListener("input", handler);
|
|
|
|
node.status({fill:"green",shape:"ring",text:"connected "+socket.engine.clientsCount});
|
2016-05-25 04:34:18 +08:00
|
|
|
});
|
2016-06-02 06:40:38 +08:00
|
|
|
node.on("close", function() {
|
2016-11-22 16:21:56 +08:00
|
|
|
node.status({});
|
2016-06-27 18:55:30 +08:00
|
|
|
client.disconnect(true);
|
2016-06-02 06:40:38 +08:00
|
|
|
});
|
|
|
|
}
|
2016-11-22 16:21:56 +08:00
|
|
|
node.status({});
|
2016-06-27 18:55:30 +08:00
|
|
|
socket.on('connection', callback);
|
2016-04-01 18:31:07 +08:00
|
|
|
}
|
|
|
|
RED.nodes.registerType("worldmap",WorldMap);
|
2016-05-19 06:26:08 +08:00
|
|
|
|
|
|
|
var WorldMapIn = function(n) {
|
|
|
|
RED.nodes.createNode(this,n);
|
2016-06-27 18:55:30 +08:00
|
|
|
if (!socket) { socket = io.listen(RED.server); }
|
2016-05-19 06:26:08 +08:00
|
|
|
var node = this;
|
2016-06-27 18:55:30 +08:00
|
|
|
|
|
|
|
var callback = function(client) {
|
2016-08-17 16:24:12 +08:00
|
|
|
client.setMaxListeners(0);
|
2016-06-27 18:55:30 +08:00
|
|
|
node.status({fill:"green",shape:"dot",text:"connected "+socket.engine.clientsCount});
|
|
|
|
client.on('worldmap', function(data) {
|
2016-05-19 06:26:08 +08:00
|
|
|
node.send({payload:data, topic:"worldmap"});
|
|
|
|
});
|
2016-06-27 18:55:30 +08:00
|
|
|
client.on('disconnect', function() {
|
|
|
|
node.status({fill:"green",shape:"ring",text:"connected "+socket.engine.clientsCount});
|
|
|
|
node.send({payload:{action:"disconnect", clients:socket.engine.clientsCount}, topic:"worldmap"});
|
2016-05-19 06:26:08 +08:00
|
|
|
});
|
2016-06-02 06:40:38 +08:00
|
|
|
node.on("close", function() {
|
2016-06-27 18:55:30 +08:00
|
|
|
client.disconnect(true);
|
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() {
|
|
|
|
node.status({});
|
2016-06-27 18:55:30 +08:00
|
|
|
//socket.close();
|
2016-06-02 19:35:12 +08:00
|
|
|
});
|
2016-06-27 18:55:30 +08:00
|
|
|
socket.on('connection', callback);
|
2016-05-19 06:26:08 +08:00
|
|
|
}
|
|
|
|
RED.nodes.registerType("worldmap in",WorldMapIn);
|
2016-04-01 18:31:07 +08:00
|
|
|
}
|