HELP python developer for Dash Button server
Posted: Saturday 17 March 2018 17:37
Hi all, i arranged a basic script in node.js by copy/merge/paste some found around and finally obtain a semi-working one (need to be optimized) which i post here.
It could be awesome if someone with python skill can translate it and optimize.
It run an http server on port 8443, defined dash_X with the ip of each dash button that my mikrotik lease to them and listen for 200 handleresponse. Based on the client ip i run json actions. It works but it's all off than optimized...
It could be awesome if someone with python skill can translate it and optimize.
It run an http server on port 8443, defined dash_X with the ip of each dash button that my mikrotik lease to them and listen for 200 handleresponse. Based on the client ip i run json actions. It works but it's all off than optimized...
Code: Select all
var http = require('http');
var fs = require('fs');
var server = http.createServer(handleResponse);
var requestify = require('requestify');
server.on('connection', handleConnection);
function handleResponse(req, res) {
res.writeHead(200);
res.end();
}
var queue = {};
var dash_1 = '::ffff:192.168.99.57';
var dash_2 = '::ffff:192.168.99.58';
var dash_3 = '::ffff:192.168.99.59';
var dash_4 = '::ffff:192.168.99.60';
function handleConnection(conn) {
var remoteAddress = conn.remoteAddress;
// Only respond to our dash button
if ((remoteAddress !== dash_1) || (remoteAddress != dash_2) || (remoteAddress != dash_3) || (remoteAddress != dash_4));
// Don't process the second petition
var now = new Date().getTime() /1000;
if (queue[remoteAddress] && now - queue[remoteAddress] < 1) {
queue[remoteAddress] = new Date().getTime() /1000;
return
}
queue[remoteAddress] = now;
// Here your action!
if (remoteAddress == dash_1) {
requestify.get('http://127.0.0.1:8080/json.htm?type=command¶m=switchlight&idx=169&switchcmd=Toggle');
} else if (remoteAddress == dash_2) {
requestify.get('http://127.0.0.1:8080/json.htm?type=command¶m=switchlight&idx=181&switchcmd=Toggle');
} else if(remoteAddress == dash_3) {
requestify.get('http://127.0.0.1:8080/json.htm?type=command¶m=switchlight&idx=167&switchcmd=Toggle');
} else if (remoteAddress == dash_4) {
requestify.get('http://127.0.0.1:8080/json.htm?type=command¶m=switchlight&idx=4&switchcmd=Toggle');
};
};
server.listen(8443);