Convert node.js snippet to PHP
New here? Learn about Bountify and follow @bountify to get notified of new bounties! x

Hi there, I need the following node.js converted to PHP so that I can control my Daikin aircon controller using a homebrewed automation system...

var http = require("http");

process.argv.shift(); //shift out node
process.argv.shift(); //shift out file
var host = process.argv.shift();
var port = process.argv.shift();
var address01 = process.argv.shift();
var address02 = process.argv.shift();
var address03 = process.argv.shift();
var address04 = process.argv.shift();
var address05 = process.argv.shift();
var address06 = process.argv.shift();
var address07 = process.argv.shift();
var address08 = process.argv.shift();
var address09 = process.argv.shift();
var address10 = process.argv.shift();
var address11 = process.argv.shift();
var address12 = process.argv.shift();
var address13 = process.argv.shift();
var address14 = process.argv.shift();
var address15 = process.argv.shift();
var address16 = process.argv.shift();

var buffer = new Buffer(32);
buffer.fill();                          //fills the buffer with 0
buffer.writeInt32LE(32, 0);             //write Command Length 32 Byte
buffer.writeInt32LE(70004, 4);          //write Command ID 700004
buffer.writeInt32LE(0, 8);              //Reserved 1
buffer.writeInt32LE(0, 12);             //Reserved 2
buffer.writeInt8(address01, 16);
buffer.writeInt8(address02, 17);
buffer.writeInt8(address03, 18);
buffer.writeInt8(address04, 19);
buffer.writeInt8(address05, 20);
buffer.writeInt8(address06, 21);
buffer.writeInt8(address07, 22);
buffer.writeInt8(address08, 23);
buffer.writeInt8(address09, 24);
buffer.writeInt8(address10, 25);
buffer.writeInt8(address11, 26);
buffer.writeInt8(address12, 27);
buffer.writeInt8(address13, 28);
buffer.writeInt8(address14, 29);
buffer.writeInt8(address15, 30);
buffer.writeInt8(address16, 31);

var opt = {
    host: host,
    port: port,
    path: '/cmd/',
    method: 'POST',
    headers: {
        'Content-Type':'application/octet-stream',
        'Content-Length':32
    }
}
var req = http.request(opt, function(res) {
    res.setEncoding('hex');
    res.on('data', function(chunk) {
        console.log("CommandSize    : " + chunk.substring(0, 8));
        console.log("CommandID      : " + chunk.substring(8, 16));
        console.log("Number Of AC   : " + chunk.substring(16, 24));
        console.log("AC Address     : " + chunk.substring(64, 72));
        console.log("Status         : " + chunk.substring(72, 76));
        console.log("Operation Mode : " + chunk.substring(84, 88));
        console.log("On/Off         : " + chunk.substring(80, 84));
        console.log("Set Temp       : " + chunk.substring(112, 120));
        console.log("Room Temp      : " + chunk.substring(104, 112));
        console.log("Fan Speed      : " + chunk.substring(120, 122));

    });

});
req.on('error', function(e) {
    console.log('problem with request: ' + e.message);
});
req.write(buffer);
req.end();
Can you give example address? Add in problem.
deepak-manwal over 3 years ago
awarded to thelink2012
Tags
PHP
node.js

Crowdsource coding tasks.

1 Solution

Winning solution

Here is my conversion:
https://gist.github.com/thelink2012/f660ec1e46308e5913e7

I've put the implementation on a function so that you can easily reuse it, mainly if you aren't going to use this as a php script itself but on the backend of something.

I tested it on a dummy implementation of what seems to be host/cmd/ and seems to be fine, but if you have any problem with the actual thing, let me know.