nanoleaf-cove
Version:
Simple NanoLeaf Api for Node.js
146 lines (145 loc) • 6.04 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var tslib_1 = require("tslib");
var dgram_1 = tslib_1.__importDefault(require("dgram"));
var byte_morph_1 = require("byte-morph");
var utils_1 = tslib_1.__importDefault(require("./utils"));
var dgram;
if (typeof window !== "undefined" && typeof window.dgram !== undefined)
dgram = window.dgram;
else
dgram = dgram_1.default;
var NanoleafStream = (function () {
function NanoleafStream(config) {
this._config = {
host: "",
streamAddress: "",
streamPort: "",
username: null,
type: null,
timeout: 3000,
};
this._keepAlive = null;
this._keepAliveInterval = null;
this._connectionTimeout = null;
this.setupStream = function (keepAlive) {
return new Promise(function (resolve, reject) {
var _this = this;
this._keepAlive = keepAlive;
this._connectionTimeout = setTimeout(function () {
reject("Connection Timed Out");
}, this._config.timeout);
utils_1.default
.setStreamingMode(this._config.host, this._config.username)
.then(function (res) {
clearTimeout(this._connectionTimeout);
if (!res || res.status >= 400) {
reject(res.data);
}
var splitHost = this._config.host
.replace("http://", "")
.split(":");
this._config.streamAddress = splitHost[0];
this._config.streamPort = 60222;
this._socket = dgram.createSocket("udp4");
if (this._keepAlive) {
this.keepAlive();
}
resolve(true);
}.bind(this))
.catch(function (err) {
clearTimeout(_this._connectionTimeout);
reject(err);
});
}.bind(this));
};
this.keepAlive = function () {
if (this._keepAliveInterval) {
clearInterval(this._keepAliveInterval);
}
this._keepAliveInterval = setInterval(function () {
}, 8000);
};
this.stopStream = function () {
var _this = this;
return new Promise(function (resolve, reject) {
if (_this._socket) {
try {
_this._socket.close(function () {
this._socket = null;
if (this._keepAliveInterval) {
clearInterval(this._keepAliveInterval);
this._keepAliveInterval = null;
}
resolve(true);
});
}
catch (e) {
reject(e);
}
}
});
};
this.sendStream = function (lights) {
return new Promise(function (resolve, reject) {
if (!this._socket) {
reject("Setup stream must be called before sending");
}
var humanReadable = [0, lights.length];
lights.map(function (light) {
if (Array.isArray(light.color)) {
light.color = {
r: light.color[0],
g: light.color[1],
b: light.color[2],
};
}
var white = 0;
var color = [0, 0, 0];
color = utils_1.default.brightnessConvert([light.color.r, light.color.g, light.color.b], light.brightness);
var frames = 1;
var transitiontime = light.transition || 0;
var lightBytes = [
Math.floor(light.id / 256),
light.id % 256,
color[0],
color[1],
color[2],
white,
Math.floor(transitiontime / 256),
transitiontime % 256,
];
humanReadable = [].concat(humanReadable, lightBytes);
}.bind(this));
var bytesArray = humanReadable.map(function (digit) {
return byte_morph_1.convertToBytes(digit, 16);
});
var message = Buffer.concat([Buffer.from(bytesArray)]);
this._socket.send(message, 0, message.length, this._config.streamPort, this._config.streamAddress);
resolve();
}.bind(this));
};
this.sendAnim = function (animation) {
return new Promise(function (resolve, reject) {
if (!this._socket) {
reject("Setup stream must be called before sending");
}
var humanReadable = animation.split(" ");
var bytesArray = humanReadable.map(function (digit) {
console.log("digit: ", digit);
return Buffer.from(digit);
});
console.log("bytesArray: ", bytesArray);
var message = Buffer.concat([Buffer.from(bytesArray)]);
this._socket.send(message, 0, message.length, this._config.streamPort, this._config.streamAddress);
resolve();
}.bind(this));
};
if (config.host) {
config.host = utils_1.default.checkHost(config.host);
}
this._config = tslib_1.__assign(tslib_1.__assign({}, this._config), config);
}
return NanoleafStream;
}());
exports.default = NanoleafStream;