### 1.0.0 (2020-12-13)

* (bluefox) added the support of compact mode
This commit is contained in:
GermanBluefox
2020-12-13 17:50:27 +01:00
parent 09436c0929
commit d7adbddcc0
14 changed files with 658 additions and 407 deletions
+37 -50
View File
@@ -1,30 +1,32 @@
var util = require('util');
var EventEmitter = require('events').EventEmitter;
var WebSocket = require('ws');
const util = require('util');
const EventEmitter = require('events').EventEmitter;
const WebSocket = require('ws');
function HASS(options, log) {
if (!(this instanceof HASS)) return new HASS(options);
if (!(this instanceof HASS)) {
return new HASS(options);
}
options = options || {};
options.host = options.host || '127.0.0.1';
options.port = parseInt(options.port, 10) || 8123;
var ERRORS = {
const ERRORS = {
1: 'ERR_CANNOT_CONNECT',
2: 'ERR_INVALID_AUTH',
3: 'ERR_CONNECTION_LOST'
};
this.socket = null;
var that = this;
var currentId = 1;
var requests = {};
var connected;
var connectTimeout = null;
const that = this;
let currentId = 1;
const requests = {};
let connected;
let connectTimeout = null;
function subscribeEvents(socket, callback) {
if (socket && typeof socket.send === 'function') {
var id = currentId++;
const id = currentId++;
requests[id] = {type: 'subscribe_events', ts: Date.now(), cb: callback};
socket.send(JSON.stringify({
@@ -39,7 +41,7 @@ function HASS(options, log) {
function getConfig(socket, callback) {
if (socket && typeof socket.send === 'function') {
var id = currentId++;
const id = currentId++;
requests[id] = {type: 'get_config', cb: callback, ts: Date.now()};
socket.send(JSON.stringify({
id: id,
@@ -52,7 +54,7 @@ function HASS(options, log) {
function getStates(socket, callback) {
if (socket && typeof socket.send === 'function') {
var id = currentId++;
const id = currentId++;
requests[id] = {type: 'get_states', cb: callback, ts: Date.now()};
socket.send(JSON.stringify({
id: id,
@@ -65,7 +67,7 @@ function HASS(options, log) {
function getPanels(socket, callback) {
if (socket && typeof socket.send === 'function') {
var id = currentId++;
const id = currentId++;
requests[id] = {type: 'get_panels', cb: callback, ts: Date.now()};
socket.send(JSON.stringify({
id: id,
@@ -78,7 +80,7 @@ function HASS(options, log) {
function getServices(socket, callback) {
if (socket && typeof socket.send === 'function') {
var id = currentId++;
const id = currentId++;
requests[id] = {type: 'get_services', cb: callback, ts: Date.now()};
socket.send(JSON.stringify({
id: id,
@@ -91,7 +93,7 @@ function HASS(options, log) {
function callService(socket, service, domain, serviceData, callback) {
if (socket && typeof socket.send === 'function') {
var id = currentId++;
const id = currentId++;
requests[id] = {type: 'call_service', cb: callback, ts: Date.now()};
socket.send(JSON.stringify({
id: id,
@@ -115,10 +117,10 @@ function HASS(options, log) {
}
function initSocket(socket) {
socket.on('message', function (msg) {
socket.on('message', msg => {
log.silly(msg);
var response = JSON.parse(msg);
const response = JSON.parse(msg);
if (response.type === 'event') {
if (response.event.data && response.event.event_type === 'system_log_event') {
if (response.event.data.level === 'WARNING') {
@@ -138,25 +140,22 @@ function HASS(options, log) {
that.emit('error', 'Password required. Connection closed');
socket.terminate();
} else {
setTimeout(function () {
sendAuth(socket, options.password);
}, 50);
setTimeout(() => sendAuth(socket, options.password), 50);
}
} else
if (response.type === 'auth_ok') {
setImmediate(function () {
subscribeEvents(socket, function (err) {
setImmediate(() =>
subscribeEvents(socket, err => {
if (!err) {
connected = true;
that.emit('connected');
}
});
});
}));
} else if (response.id === undefined) {
log.error('Invalid answer: ' + msg);
log.error(`Invalid answer: ${msg}`);
} else {
if (response.type === 'result' && requests[response.id]) {
log.debug('got answer for ' + requests[response.id].type);
log.debug(`got answer for ${requests[response.id].type}`);
if (typeof requests[response.id].cb === 'function') {
requests[response.id].cb(!response.success, response.result);
delete requests[response.id];
@@ -165,7 +164,7 @@ function HASS(options, log) {
}
});
socket.on('error', function (err) {
socket.on('error', err => {
socket = null;
if (err && err.message.indexOf('RSV2 and RSV3 must be clear') !== -1) {
// ignore deflate error
@@ -173,12 +172,12 @@ function HASS(options, log) {
log.error(err);
}
});
socket.on('open', function () {
socket.on('open', () => {
if (!connected) {
}
});
socket.on('close', function () {
socket.on('close', () => {
that.socket = null;
if (connected) {
connected = false;
@@ -186,7 +185,7 @@ function HASS(options, log) {
}
if (!connectTimeout) {
setTimeout(function () {
setTimeout(() => {
connectTimeout = null;
that.connect();
}, 3000);
@@ -194,15 +193,11 @@ function HASS(options, log) {
});
}
this.isConnected = function () {
return connected;
};
this.isConnected = () => connected;
this.getConfig = function (callback) {
if (!connected) {
if (typeof callback === 'function') {
callback('not connected');
}
typeof callback === 'function' && callback('not connected');
} else {
getConfig(this.socket, callback);
}
@@ -210,9 +205,7 @@ function HASS(options, log) {
this.getStates = function (callback) {
if (!connected) {
if (typeof callback === 'function') {
callback('not connected');
}
typeof callback === 'function' && callback('not connected');
} else {
getStates(this.socket, callback);
}
@@ -220,9 +213,7 @@ function HASS(options, log) {
this.getServices = function (callback) {
if (!connected) {
if (typeof callback === 'function') {
callback('not connected');
}
typeof callback === 'function' && callback('not connected');
} else {
getServices(this.socket, callback);
}
@@ -230,9 +221,7 @@ function HASS(options, log) {
this.getPanels = function (callback) {
if (!connected) {
if (typeof callback === 'function') {
callback('not connected');
}
typeof callback === 'function' && callback('not connected');
} else {
getPanels(this.socket, callback);
}
@@ -240,9 +229,7 @@ function HASS(options, log) {
this.callService = function (service, domain, serviceData, callback) {
if (!connected) {
if (typeof callback === 'function') {
callback('not connected');
}
typeof callback === 'function' && callback('not connected');
} else {
callService(this.socket, service, domain, serviceData, callback);
}
@@ -254,7 +241,7 @@ function HASS(options, log) {
connectTimeout = null;
}
this.socket = new WebSocket('ws' + (options.secure ? 's' : '') + '://' + options.host + ':' + options.port + '/api/websocket', {
this.socket = new WebSocket(`ws${options.secure ? 's' : ''}://${options.host}:${options.port}/api/websocket`, {
perMessageDeflate: false
});