Support for manual protocol 3.3

Add support to explisitly set protocol version.
This commit is contained in:
tsightler
2019-06-22 23:31:38 -04:00
parent 3b72a000c8
commit 279590eb71
2 changed files with 20 additions and 72 deletions

View File

@@ -10,72 +10,10 @@ const debugColor = require('debug')('TuyAPI:device:color');
id: '03200240600194781244',
key: 'b8bdebab418f5b55',
ip: '192.168.178.45',
type: "socket"
type: "ver33"
});
*/
// Helpers
const MessageParser = require('tuyapi/lib/message-parser').MessageParser;
const Parser = new MessageParser()
/**
* Extends default TuyAPI-Class to add some more error handlers
*/
class CustomTuyAPI extends TuyAPI {
get(options) {
// Set empty object as default
options = options ? options : {};
const payload = {
gwId: this.device.gwID,
devId: this.device.id
};
debug('GET Payload:');
debug(payload);
// Create byte buffer
const buffer = Parser.encode({
data: payload,
commandByte: 10 // 0x0a
});
// Send request and parse response
return new Promise((resolve, reject) => {
try {
// Send request
this._send(buffer).then(() => {
// Runs when data event is emitted
const resolveGet = data => {
// Remove self listener
this.removeListener('data', resolveGet);
try {
if (options.schema === true) {
// Return whole response
resolve(data);
} else if (options.dps) {
// Return specific property
resolve(data.dps[options.dps]);
} else {
// Return first property by default
resolve(data.dps['1']);
}
} catch (error) {
reject(error);
}
};
// Add listener
this.on('data', resolveGet);
});
} catch (error) {
reject(error);
}
});
}
}
var TuyaDevice = (function () {
var devices = [];
var events = {};
@@ -126,7 +64,7 @@ var TuyaDevice = (function () {
this.options = options;
Object.defineProperty(this, 'device', {
value: new CustomTuyAPI(JSON.parse(JSON.stringify(this.options)))
value: new TuyAPI(JSON.parse(JSON.stringify(this.options)))
});
this.device.on('data', data => {
@@ -183,7 +121,11 @@ var TuyaDevice = (function () {
}
TuyaDevice.prototype.toString = function () {
return this.type + " (" + this.options.ip + ", " + this.options.id + ", " + this.options.key + ")";
if (typeof this.type != "undefined") {
return this.type + " (" + this.options.ip + ", " + this.options.id + ", " + this.options.key + ")";
} else {
return " (" + this.options.ip + ", " + this.options.id + ", " + this.options.key + ")";
}
}
TuyaDevice.prototype.triggerAll = function (name, argument) {

View File

@@ -79,10 +79,10 @@ function IsJsonString(text) {
* check mqtt-topic string for old notation with included device type
* @param {String} topic
*/
function checkTopicForOldNotation(_topic) {
function checkTopicNotation(_topic) {
var topic = _topic.split("/");
var type = topic[1];
var result = (type == "socket" || type == "lightbulb");
var result = (type == "socket" || type == "lightbulb" || type == "ver3.1" || type == "ver3.3");
return result;
}
@@ -94,7 +94,7 @@ function checkTopicForOldNotation(_topic) {
function getActionFromTopic(_topic) {
var topic = _topic.split("/");
if (checkTopicForOldNotation(_topic)) {
if (checkTopicNotation(_topic)) {
return topic[5];
} else {
return topic[4];
@@ -111,7 +111,7 @@ function getActionFromTopic(_topic) {
function getDeviceFromTopic(_topic) {
var topic = _topic.split("/");
if (checkTopicForOldNotation(_topic)) {
if (checkTopicNotation(_topic)) {
return {
id: topic[2],
key: topic[3],
@@ -137,7 +137,7 @@ function getCommandFromTopic(_topic, _message) {
var topic = _topic.split("/");
var command = null;
if (checkTopicForOldNotation(_topic)) {
if (checkTopicNotation(_topic)) {
command = topic[6];
} else {
command = topic[5];
@@ -178,7 +178,13 @@ mqtt_client.on('message', function (topic, message) {
options: options
}));
if (options.ip == "discover") {
delete options.ip
delete options.ip;
} else if (options.type == "ver3.3") {
delete options.type;
options.version = "3.3";
} else if (options.type == "ver3.1") {
delete options.type;
options.version = "3.1";
}
var device = new TuyaDevice(options);
device.then(function (params) {
@@ -314,7 +320,7 @@ function publishDPS(device, dps) {
TuyaDevice.onAll('data', function (data) {
try {
if (typeof data.dps != "undefined") {
debugTuya('Data from device ' + this.type + ' :', data);
debugTuya('Data from device ' + this.tuyID + ' :', data);
var status = data.dps['1'];
if (typeof status != "undefined") {
publishStatus(this, bmap(status));