170 lines
4.6 KiB
JavaScript
170 lines
4.6 KiB
JavaScript
|
|
exports.id = 'tcpserver';
|
|||
|
|
exports.title = 'TCP/IP Server';
|
|||
|
|
exports.version = '1.0.4';
|
|||
|
|
exports.group = 'TCP/IP';
|
|||
|
|
exports.color = '#888600';
|
|||
|
|
exports.output = ["red", "white"];
|
|||
|
|
exports.click = false;
|
|||
|
|
exports.author = 'Jakub Klena';
|
|||
|
|
exports.icon = 'server';
|
|||
|
|
exports.options = { ip: '0.0.0.0', port: 8421, edge: "M6ogKQW09bOXewAYvZyvkn5JrV1aRnPGE37p42Nx" };
|
|||
|
|
|
|||
|
|
exports.html = `<div class="padding">
|
|||
|
|
<div class="row">
|
|||
|
|
<div class="col-md-6">
|
|||
|
|
<div data-jc="textbox" data-jc-path="ip" data-jc-config="placeholder:0.0.0.0;required:true" class="m">IP</div>
|
|||
|
|
</div>
|
|||
|
|
<div class="col-md-6">
|
|||
|
|
<div data-jc="textbox" data-jc-path="port" data-jc-config="placeholder:8421;required:true" class="m">Port</div>
|
|||
|
|
</div>
|
|||
|
|
</div>
|
|||
|
|
<div class="row">
|
|||
|
|
<div class="col-md-6">
|
|||
|
|
<div data-jc="textbox" data-jc-path="edge" data-jc-config="placeholder:M6ogKQW09bOXewAYvZyvkn5JrV1aRnPGE37p42Nx;required:true" class="m">Edge TB Name</div>
|
|||
|
|
</div>
|
|||
|
|
</div>
|
|||
|
|
</div>`;
|
|||
|
|
|
|||
|
|
exports.readme = `# TCP Server
|
|||
|
|
## Outputs
|
|||
|
|
|
|||
|
|
- *Red* - ERROR output
|
|||
|
|
- *White* - Incomming message output
|
|||
|
|
`;
|
|||
|
|
|
|||
|
|
|
|||
|
|
|
|||
|
|
|
|||
|
|
exports.install = function(instance) {
|
|||
|
|
|
|||
|
|
let net = require('net');
|
|||
|
|
let server = null;
|
|||
|
|
let myip = "0.0.0.0";
|
|||
|
|
let myport = 8421;
|
|||
|
|
let myedge = "M6ogKQW09bOXewAYvZyvkn5JrV1aRnPGE37p42Nx";
|
|||
|
|
let interv = null;
|
|||
|
|
let ERRWEIGHT = {
|
|||
|
|
EMERGENCY: "emergency", // System unusable
|
|||
|
|
ALERT: "alert", // Action must be taken immidiately
|
|||
|
|
CRITICAL: "critical", // Component unable to function
|
|||
|
|
ERROR: "error", // Error, but component able to recover from it
|
|||
|
|
WARNING: "warning", // Possibility of error, system running futher
|
|||
|
|
NOTICE: "notice", // Significant message but not an error, things user might want to know about
|
|||
|
|
INFO: "informational", // Info
|
|||
|
|
DEBUG: "debug" // Debug - only if CONFIG.debug is enabled
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
setTimeout(function(){
|
|||
|
|
if (server !== null){
|
|||
|
|
if (server.listening){
|
|||
|
|
instance.status("Listening", "green");
|
|||
|
|
} else {
|
|||
|
|
instance.status("Not listening", "red");
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}, 5000);
|
|||
|
|
|
|||
|
|
function resetServer(){
|
|||
|
|
sendError(myedge, "resetServer", ERRWEIGHT.DEBUG, "resetServer called !", {});
|
|||
|
|
if (server !== null){
|
|||
|
|
sendError(myedge, "resetServer", ERRWEIGHT.DEBUG, "Server already exists", {});
|
|||
|
|
server.close(function(){
|
|||
|
|
sendError(myedge, "resetServer", ERRWEIGHT.DEBUG, "Server closed intentionally", {});
|
|||
|
|
server = null;
|
|||
|
|
resetServer();
|
|||
|
|
});
|
|||
|
|
} else {
|
|||
|
|
sendError(myedge, "resetServer", ERRWEIGHT.DEBUG, "Server doesn’t exist", {});
|
|||
|
|
server = net.createServer((c) => {
|
|||
|
|
sendError(myedge, "resetServer", ERRWEIGHT.INFO, "New client connected !", {"ip":c.localAddress, "port":c.localPort});
|
|||
|
|
c.on("data", (d) => {
|
|||
|
|
sendOutputMsg({
|
|||
|
|
"ip":c.localAddress,
|
|||
|
|
"port":c.localPort,
|
|||
|
|
"data":d.toString()
|
|||
|
|
});
|
|||
|
|
});
|
|||
|
|
c.on('end', () => {
|
|||
|
|
sendError(myedge, "resetServer", ERRWEIGHT.INFO, "Client disconnected !", {"ip":c.localAddress, "port":c.localPort});
|
|||
|
|
});
|
|||
|
|
});
|
|||
|
|
server.listen(myport, myip);
|
|||
|
|
}
|
|||
|
|
};
|
|||
|
|
|
|||
|
|
function isJson(str) {
|
|||
|
|
try {
|
|||
|
|
JSON.parse(str);
|
|||
|
|
} catch (e) {
|
|||
|
|
return false;
|
|||
|
|
}
|
|||
|
|
return true;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
instance.reconfigure = function() {
|
|||
|
|
|
|||
|
|
//code
|
|||
|
|
myip = instance.options.ip;
|
|||
|
|
myport = instance.options.port;
|
|||
|
|
myedge = instance.options.edge;
|
|||
|
|
|
|||
|
|
setTimeout(resetServer, 5000);
|
|||
|
|
|
|||
|
|
};
|
|||
|
|
|
|||
|
|
instance.close = function() {
|
|||
|
|
// close sockets and such
|
|||
|
|
if (server !== null){
|
|||
|
|
server.close(function(){});
|
|||
|
|
}
|
|||
|
|
};
|
|||
|
|
|
|||
|
|
function sendError(device, func, weight, str, extra){
|
|||
|
|
|
|||
|
|
let content = {
|
|||
|
|
"type": weight,
|
|||
|
|
"status": "new",
|
|||
|
|
"source": {
|
|||
|
|
"function":func,
|
|||
|
|
"component":instance.id,
|
|||
|
|
"component_name":instance.name
|
|||
|
|
},
|
|||
|
|
"message":str,
|
|||
|
|
"message_data": extra
|
|||
|
|
};
|
|||
|
|
|
|||
|
|
let error = {};
|
|||
|
|
error[device] = [
|
|||
|
|
{
|
|||
|
|
"ts": Date.now(),
|
|||
|
|
"values": {
|
|||
|
|
"_event":content
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
];
|
|||
|
|
|
|||
|
|
instance.send(0, error);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
function sendOutputMsg(str){
|
|||
|
|
instance.send(1, str);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
instance.on('options', instance.reconfigure);
|
|||
|
|
instance.reconfigure();
|
|||
|
|
|
|||
|
|
function humanReadableTimeAndDate(){
|
|||
|
|
let date_ob = new Date();
|
|||
|
|
|
|||
|
|
let date = ("0" + date_ob.getDate()).slice(-2);
|
|||
|
|
let month = ("0" + (date_ob.getMonth() + 1)).slice(-2);
|
|||
|
|
let year = date_ob.getFullYear();
|
|||
|
|
|
|||
|
|
let hours = ("0" + date_ob.getHours()).slice(-2);
|
|||
|
|
let minutes = ("0" + date_ob.getMinutes()).slice(-2);
|
|||
|
|
let seconds = ("0" + date_ob.getSeconds()).slice(-2);
|
|||
|
|
|
|||
|
|
return date+"."+month+"."+year+" "+hours+":"+minutes+":"+seconds;
|
|||
|
|
}
|
|||
|
|
};
|