Skip to content

Commit

Permalink
Merge pull request #2 from T-REX-XP/merge-from-orig
Browse files Browse the repository at this point in the history
Merge from orig
  • Loading branch information
T-REX-XP authored Apr 19, 2019
2 parents 324c7ee + 8938db1 commit cf5f3e5
Show file tree
Hide file tree
Showing 9 changed files with 415 additions and 244 deletions.
9 changes: 3 additions & 6 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
FROM homeassistant/amd64-base:latest
ARG BUILD_FROM
FROM $BUILD_FROM

ENV LANG C.UTF-8

RUN apk add --no-cache jq nodejs nodejs-npm && \
npm set unsafe-perm true

# Copy data for add-on
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app
COPY . .
RUN chmod +x run.sh

RUN npm install

#CMD [ "./run.sh" ]
ENTRYPOINT ./run.sh
CMD [ "./run.sh" ]
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,8 @@ climate:
## Changelog
[1.1.2.1] - Updated sources from orig
[1.0.3] - Fixed power off command
[1.0.2] - Fixed modes and WindModes
Expand Down
237 changes: 175 additions & 62 deletions app/deviceFactory.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,8 @@
'use strict';
const net = require('net');
const dgram = require('dgram');
const socket = dgram.createSocket('udp4');
const encryptionService = require('./encryptionService')();
//const encryptionService = require('./encryptionService')();
const cmd = require('./commandEnums');
const _ = require('lodash');

const utils = require("./utils");

const client = new net.Socket();



/**
* Class representing a single connected device
Expand All @@ -30,16 +22,11 @@ class Device {
// Set defaults
this.options = {
host: options.host || '192.168.111.255',
onStatus: options.onStatus || function() {},
onUpdate: options.onUpdate || function() {},
onConnected: options.onConnected || function() {}
};
client.on('data', (msg, rinfo) => this._handleResponse(msg, rinfo));

client.on('listening', () => {
const address = client.address();
console.log(`server listening ${address.address}:${address.port}`);
});
onStatus: options.onStatus || function () {},
onUpdate: options.onUpdate || function () {},
onConnected: options.onConnected || function () {}
}

/**
* Device object
* @typedef {object} Device
Expand All @@ -51,11 +38,11 @@ class Device {
* @property {object} props - Properties
*/
this.device = {};
//

this.defaultPort = 12414;
this.defaultDiscoveryPort = 2415;
this.deviceStatusPort = 12416
// Initialize connection and bind with device
this.deviceStatusPort = 12416;
// Initialize connection and bind with device
this._connectToDevice(this.options.host);

// Handle incoming messages
Expand Down Expand Up @@ -86,7 +73,7 @@ class Device {
console.log('[UDP] Connected to device at %s', address);
});
} catch (err) {
const timeout = 60
const timeout = 60;

console.log('[UDP] Unable to connect (' + err.message + '). Retrying in ' + timeout + 's...');
setTimeout(() => {
Expand Down Expand Up @@ -118,21 +105,22 @@ class Device {
* @param {Device} device Device object
*/
_sendBindRequest(device) {
/*const message = {
mac: this.device.id,
t: 'bind',
uid: 0
};
const encryptedBoundMessage = encryptionService.encrypt(message);
const request = {
cid: 'app',
i: 1,
t: 'pack',
uid: 0,
pack: encryptedBoundMessage
};
const toSend = new Buffer(JSON.stringify(request));
socket.send(toSend, 0, toSend.length, device.port, device.address);*/
/* const message = {
mac: this.device.id,
t: 'bind',
uid: 0
};
const encryptedBoundMessage = encryptionService.encrypt(message);
const request = {
cid: 'app',
i: 1,
t: 'pack',
uid: 0,
pack: encryptedBoundMessage
};
const toSend = Buffer.from(JSON.stringify(request));
socket.send(toSend, 0, toSend.length, device.port, device.address);
*/
}

/**
Expand All @@ -141,21 +129,21 @@ class Device {
* @param {String} key - Encryption key
*/
_confirmBinding(id, key) {
/* this.device.bound = true;
this.device.key = key;
console.log('[UDP] Device %s is bound!', this.device.name);*/
/*this.device.bound = true;
this.device.key = key;
console.log('[UDP] Device %s is bound!', this.device.name);*/
}

/**
* Confirm device is bound and update device status on list
* @param {Device} device - Device
*/
_requestDeviceStatus(device, that) {
_requestDeviceStatus(device) {
console.log("--in _requestDeviceStatus");
let serializedRequest = new Buffer([0xAA, 0xAA, 0x12, 0xA0, 0x0A, 0x0A, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1A]);

if (!this.isConnected) {
client.connect(this.deviceStatusPort, device.address, function(data) {
client.connect(this.deviceStatusPort, device.address, function (data) {
console.log('Connected to tcp port');
this.isConnected = true;
client.write(serializedRequest);
Expand All @@ -164,8 +152,6 @@ class Device {
console.log("-- already connected");
client.write(serializedRequest);
}

// socket.send(serializedRequest, 0, serializedRequest.length, device.port, device.ip);
}

/**
Expand All @@ -184,14 +170,18 @@ class Device {
this._requestDeviceStatus(this.device, this);
this.options.onConnected(this.device);
// this._sendBindRequest(this.device);
return;
} else {
console.log("received status msg.");
let statusMessage = utils.parseMessage(msg);
console.log(statusMessage);
this.device.lastCmd = msg;
this.device.props = statusMessage;
this.options.onStatus(this.device);
return;
}

console.log('[UDP] Unknown message of type %s: %s, %s', pack.t, message, pack);
}

/**
Expand All @@ -200,12 +190,12 @@ class Device {
* @param {number[]} values List of values
*/
_sendCommand(commands = [], values = []) {
/* const message = {
opt: commands,
p: values,
t: 'cmd'
};
this._sendRequest(message);*/
/* const message = {
opt: commands,
p: values,
t: 'cmd'
};
this._sendRequest(message);*/
};

/**
Expand All @@ -217,7 +207,18 @@ class Device {
* @param {string} [address] IP/host address
* @param {number} [port] Port number
*/
_sendRequest(message, address = this.device.address, port = this.device.port) {};
_sendRequest(message, address = this.device.address, port = this.device.port) {
/* const encryptedMessage = encryptionService.encrypt(message, this.device.key);
const request = {
cid: 'app',
i: 0,
t: 'pack',
uid: 0,
pack: encryptedMessage
};
const serializedRequest = Buffer.from(JSON.stringify(request));
socket.send(serializedRequest, 0, serializedRequest.length, port, address);*/
};

/**
* Turn on/off
Expand All @@ -227,7 +228,7 @@ class Device {
console.log('--In setPower: ' + value);
if (this.device.lastCmd)
client.write(utils.cmd01(this.device.lastCmd, value));
};
}

/**
* Set temperature
Expand All @@ -238,40 +239,152 @@ class Device {
console.log('--In setTemp: ' + value);
if (this.device.lastCmd)
client.write(utils.cmd07(this.device.lastCmd, value, false));

};
}

/**
* Set mode
* @param {number} value Mode value (0-4)
*/
setMode(value) {
console.log('--In setMode: ' + value);
this._sendCommand(
[cmd.mode.code], [value]
);
};
}

/**
* Set fan speed
* @param {number} value Fan speed value (0-5)
*/
setFanSpeed(value) {
console.log('--In setFanSpeed: ' + value);
this._sendCommand(
[cmd.fanSpeed.code], [value]
[cmd.fanSpeed.code],
[value]
);
};
}

/**
* Set horizontal swing
* @param {number} value Horizontal swing value (0-7)
*/
setSwingHor(value) {
console.log('--In setSwingHor: ' + value);
this._sendCommand(
[cmd.swingHor.code],
[value]
);
}

/**
* Set vertical swing
* @param {number} value Vertical swing value (0-11)
*/
setSwingVert(value) {
console.log('--In setSwingVert: ' + value);
this._sendCommand(
[cmd.swingVert.code], [value]
[cmd.swingVert.code],
[value]
);
};
};
}

/**
* Set power save mode
* @param {boolean} value on/off
*/
setPowerSave(value) {
console.log('--In setPowerSave: ' + value);
this._sendCommand(
[cmd.energySave.code],
[value ? 1 : 0]
);
}

/**
* Set lights on/off
* @param {boolean} value on/off
*/
setLights(value) {
console.log('--In setLights: ' + value);
this._sendCommand(
[cmd.lights.code],
[value ? 1 : 0]
);
}

/**
* Set health mode
* @param {boolean} value on/off
*/
setHealthMode(value) {
console.log('--In setLights: ' + value);
this._sendCommand(
[cmd.health.code],
[value ? 1 : 0]
);
}

/**
* Set quiet mode
* @param {boolean} value on/off
*/
setQuietMode(value) {
console.log('--In setQuietMode: ' + value);
this._sendCommand(
[cmd.quiet.code],
[value]
);
}

/**
* Set blow mode
* @param {boolean} value on/off
*/
setBlow(value) {
console.log('--In setBlow: ' + value);
this._sendCommand(
[cmd.blow.code],
[value ? 1 : 0]
);
}

/**
* Set air valve mode
* @param {boolean} value on/off
*/
setAir(value) {
console.log('--In setAir: ' + value);
this._sendCommand(
[cmd.air.code],
[value]
);
}

/**
* Set sleep mode
* @param {boolean} value on/off
*/
setSleepMode(value) {
console.log('--In setSleepMode: ' + value);
this._sendCommand(
[cmd.sleep.code],
[value ? 1 : 0]
);
}

/**
* Set turbo mode
* @param {boolean} value on/off
*/
setTurbo(value) {
console.log('--In setTurbo: ' + value);
this._sendCommand(
[cmd.turbo.code],
[value ? 1 : 0]
);
}
}

module.exports.connect = function(options) {
module.exports.connect = function (options) {
return new Device(options);
};
Loading

0 comments on commit cf5f3e5

Please sign in to comment.