Skip to content

Commit

Permalink
0.2.1 - Breaks the DS functions out into a separate class
Browse files Browse the repository at this point in the history
  • Loading branch information
128keaton committed Apr 25, 2022
1 parent 5cf7ad0 commit 2f249d7
Show file tree
Hide file tree
Showing 20 changed files with 489 additions and 361 deletions.
1 change: 1 addition & 0 deletions dist/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './tibbo-discover';
export * from './tibbo-helpers';
export * from './tibbo-types';
export * from './tibbo-device-server';
2 changes: 2 additions & 0 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,5 @@ __exportStar(require("./tibbo-discover"), exports);
__exportStar(require("./tibbo-helpers"), exports);
/* istanbul ignore next */
__exportStar(require("./tibbo-types"), exports);
/* istanbul ignore next */
__exportStar(require("./tibbo-device-server"), exports);
14 changes: 14 additions & 0 deletions dist/tibbo-device-server.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { TibboDeviceLoginResponse, TibboDeviceSetting, TibboDeviceUpdateSettingResponse } from "./tibbo-types";
export declare class TibboDeviceServer {
private activeSockets;
private readonly key;
constructor(key?: string);
login(ipAddress: string, password: string, key?: string): Promise<TibboDeviceLoginResponse>;
buzz(ipAddress: string, password: string, key?: string): Promise<unknown>;
reboot(ipAddress: string, password: string, key?: string): Promise<unknown>;
initializeSettings(ipAddress: string, password: string, key?: string): Promise<unknown>;
updateSetting(setting: string, value: string, ipAddress: string, password: string): Promise<TibboDeviceUpdateSettingResponse[] | TibboDeviceLoginResponse>;
updateSettings(settings: TibboDeviceSetting[], ipAddress: string, password: string): Promise<TibboDeviceUpdateSettingResponse[] | TibboDeviceLoginResponse>;
stop(): Promise<void>;
private sendSingleAuthMessage;
}
139 changes: 139 additions & 0 deletions dist/tibbo-device-server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.TibboDeviceServer = void 0;
const dgram_as_promised_1 = __importDefault(require("dgram-as-promised"));
const tibbo_helpers_1 = require("./tibbo-helpers");
const buffer_1 = require("buffer");
const promises_1 = require("timers/promises");
const tibbo_shared_1 = require("./tibbo-shared");
class TibboDeviceServer {
constructor(key) {
this.activeSockets = [];
this.key = 'tibbo123';
if (!!key && key.length) {
this.key = key;
}
}
login(ipAddress, password, key = this.key) {
const socket = dgram_as_promised_1.default.createSocket("udp4");
const message = tibbo_helpers_1.TibboHelpers.loginMessage(password, key);
const encodedMessage = buffer_1.Buffer.from(message);
const ac = new AbortController();
const signal = ac.signal;
return new Promise((resolve) => {
let didResolve = false;
(0, promises_1.setTimeout)(3000, 'timeout', { signal }).then(() => {
if (!didResolve) {
this.stop().then(() => {
resolve({ key, success: false, message: 'ERR_TIMEOUT' });
}).catch(() => {
resolve({ key, success: false, message: 'ERR_TIMEOUT' });
});
}
}).catch(() => resolve({ key, success: false, message: 'ERR_TIMEOUT' }));
socket.bind().then(() => {
this.activeSockets.push(socket);
socket.setBroadcast(true);
return socket.send(encodedMessage, 0, encodedMessage.length, tibbo_shared_1.TIBBO_BROADCAST_PORT, ipAddress);
}).then(() => socket.recv())
.then(packet => tibbo_helpers_1.TibboHelpers.processLoginResponse(packet))
.then(response => {
return socket.close()
.catch(() => response)
.then(() => response);
})
.then(response => ({
success: (response || false),
key,
}))
.then(response => {
didResolve = true;
ac.abort();
resolve(response);
});
});
}
buzz(ipAddress, password, key = this.key) {
return this.sendSingleAuthMessage(ipAddress, password, key, tibbo_helpers_1.TibboHelpers.buzzMessage(key));
}
reboot(ipAddress, password, key = this.key) {
return this.sendSingleAuthMessage(ipAddress, password, key, tibbo_helpers_1.TibboHelpers.rebootMessage(key));
}
initializeSettings(ipAddress, password, key = this.key) {
return this.sendSingleAuthMessage(ipAddress, password, key, tibbo_helpers_1.TibboHelpers.initializeSettingsMessage(key));
}
async updateSetting(setting, value, ipAddress, password) {
const settings = [
{
settingValue: value,
settingName: setting
}
];
return this.updateSettings(settings, ipAddress, password);
}
async updateSettings(settings, ipAddress, password) {
const didAuth = await this.login(ipAddress, password);
if (!didAuth.success) {
return didAuth;
}
const socket = dgram_as_promised_1.default.createSocket("udp4");
const settingMessages = settings.map(setting => tibbo_helpers_1.TibboHelpers.updateSettingMessage(setting.settingName, setting.settingValue, didAuth.key))
.map(string => buffer_1.Buffer.from(string));
return socket.bind().then(() => {
this.activeSockets.push(socket);
socket.setBroadcast(true);
return Promise.all(settingMessages.map(setting => tibbo_helpers_1.TibboHelpers.iterateSend(socket, setting, ipAddress, tibbo_shared_1.TIBBO_BROADCAST_PORT)));
}).then((results) => {
return results.map((result, index) => ({
success: result,
setting: settings[index]
}));
}).then(results => socket.close().then(() => results));
}
stop() {
const finished = () => {
this.activeSockets.forEach(socket => socket.unref());
this.activeSockets = [];
return;
};
return Promise.all(this.activeSockets.map(socket => socket.close()))
.then(() => finished())
.catch(() => finished());
}
sendSingleAuthMessage(ipAddress, password, key, message) {
const socket = dgram_as_promised_1.default.createSocket("udp4");
const encodedMessage = buffer_1.Buffer.from(message);
const ac = new AbortController();
const signal = ac.signal;
return new Promise(resolve => {
(0, promises_1.setTimeout)(1000, 'timeout', { signal }).then(() => {
Promise.all([this.stop(), socket.close()])
.catch(() => resolve({ message: 'Success' }))
.then(() => resolve({ message: 'Success' }));
}).catch(() => resolve({ message: 'Success' }));
this.login(ipAddress, password, key).then(result => {
if (!result.success) {
resolve({ message: 'Access denied' });
}
else {
return socket.bind().then(() => {
this.activeSockets.push(socket);
socket.setBroadcast(true);
return socket.send(encodedMessage, 0, encodedMessage.length, tibbo_shared_1.TIBBO_BROADCAST_PORT, ipAddress);
}).catch(() => resolve(false));
}
}).then(() => socket.recv()).then(packet => {
const denied = tibbo_helpers_1.TibboHelpers.checkIfDenied(packet);
ac.abort();
if (denied) {
return { message: 'Access denied' };
}
return { message: 'Success' };
}).then(response => this.stop().then(() => response));
});
}
}
exports.TibboDeviceServer = TibboDeviceServer;
10 changes: 1 addition & 9 deletions dist/tibbo-discover.d.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,10 @@
#! /usr/bin/env node
import { TibboDevice, TibboDeviceLoginResponse, TibboDeviceSetting, TibboDeviceUpdateSettingResponse } from "./tibbo-types";
import { TibboDevice } from "./tibbo-types";
export declare class TibboDiscover {
private devices;
private activeSockets;
private readonly key;
constructor(key?: string);
scan(timeout?: number): Promise<TibboDevice[]>;
query(id: string, timeout?: number): Promise<TibboDevice | null>;
stop(): Promise<TibboDevice[]>;
login(ipAddress: string, password: string, key?: string): Promise<TibboDeviceLoginResponse>;
buzz(ipAddress: string, password: string, key?: string): Promise<unknown>;
reboot(ipAddress: string, password: string, key?: string): Promise<unknown>;
updateSetting(setting: string, value: string, ipAddress: string, password: string): Promise<TibboDeviceUpdateSettingResponse[] | TibboDeviceLoginResponse>;
updateSettings(settings: TibboDeviceSetting[], ipAddress: string, password: string): Promise<TibboDeviceUpdateSettingResponse[] | TibboDeviceLoginResponse>;
private sendBroadcastMessage;
private sendSingleAuthMessage;
}
134 changes: 13 additions & 121 deletions dist/tibbo-discover.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,12 @@ const promises_1 = require("timers/promises");
const dgram_as_promised_1 = __importDefault(require("dgram-as-promised"));
const buffer_1 = require("buffer");
const tibbo_helpers_1 = require("./tibbo-helpers");
const BROADCAST_PORT = 65535;
const BROADCAST_ADDR = '255.255.255.255';
const tibbo_shared_1 = require("./tibbo-shared");
const tibbo_device_server_1 = require("./tibbo-device-server");
class TibboDiscover {
constructor(key) {
constructor() {
this.devices = {};
this.activeSockets = [];
this.key = 'tibbo123';
if (!!key && key.length) {
this.key = key;
}
}
scan(timeout = 5000) {
return this.sendBroadcastMessage(tibbo_helpers_1.TibboHelpers.discoverMessage).then(() => {
Expand Down Expand Up @@ -56,79 +52,6 @@ class TibboDiscover {
.then(() => finished())
.catch(() => finished());
}
login(ipAddress, password, key = this.key) {
const socket = dgram_as_promised_1.default.createSocket("udp4");
const message = tibbo_helpers_1.TibboHelpers.loginMessage(password, key);
const encodedMessage = buffer_1.Buffer.from(message);
const ac = new AbortController();
const signal = ac.signal;
return new Promise((resolve) => {
let didResolve = false;
(0, promises_1.setTimeout)(3000, 'timeout', { signal }).then(() => {
if (!didResolve) {
this.stop().then(() => {
resolve({ key, success: false, message: 'ERR_TIMEOUT' });
}).catch(() => {
resolve({ key, success: false, message: 'ERR_TIMEOUT' });
});
}
}).catch(() => resolve({ key, success: false, message: 'ERR_TIMEOUT' }));
socket.bind().then(() => {
this.activeSockets.push(socket);
socket.setBroadcast(true);
return socket.send(encodedMessage, 0, encodedMessage.length, BROADCAST_PORT, ipAddress);
}).then(() => socket.recv())
.then(packet => tibbo_helpers_1.TibboHelpers.processLoginResponse(packet))
.then(response => {
return socket.close()
.catch(() => response)
.then(() => response);
})
.then(response => ({
success: (response || false),
key,
}))
.then(response => {
didResolve = true;
ac.abort();
resolve(response);
});
});
}
buzz(ipAddress, password, key = this.key) {
return this.sendSingleAuthMessage(ipAddress, password, key, tibbo_helpers_1.TibboHelpers.buzzMessage(key));
}
reboot(ipAddress, password, key = this.key) {
return this.sendSingleAuthMessage(ipAddress, password, key, tibbo_helpers_1.TibboHelpers.rebootMessage(key));
}
async updateSetting(setting, value, ipAddress, password) {
const settings = [
{
settingValue: value,
settingName: setting
}
];
return this.updateSettings(settings, ipAddress, password);
}
async updateSettings(settings, ipAddress, password) {
const didAuth = await this.login(ipAddress, password);
if (!didAuth.success) {
return didAuth;
}
const socket = dgram_as_promised_1.default.createSocket("udp4");
const settingMessages = settings.map(setting => tibbo_helpers_1.TibboHelpers.updateSettingMessage(setting.settingName, setting.settingValue, didAuth.key))
.map(string => buffer_1.Buffer.from(string));
return socket.bind().then(() => {
this.activeSockets.push(socket);
socket.setBroadcast(true);
return Promise.all(settingMessages.map(setting => tibbo_helpers_1.TibboHelpers.iterateSend(socket, setting, ipAddress, BROADCAST_PORT)));
}).then((results) => {
return results.map((result, index) => ({
success: result,
setting: settings[index]
}));
}).then(results => socket.close().then(() => results));
}
sendBroadcastMessage(message) {
const socket = dgram_as_promised_1.default.createSocket("udp4");
const encodedMessage = buffer_1.Buffer.from(message);
Expand All @@ -144,58 +67,27 @@ class TibboDiscover {
});
}
});
return socket.send(encodedMessage, 0, encodedMessage.length, BROADCAST_PORT, BROADCAST_ADDR);
return socket.send(encodedMessage, 0, encodedMessage.length, tibbo_shared_1.TIBBO_BROADCAST_PORT, tibbo_shared_1.TIBBO_BROADCAST_ADDR);
}).then(() => socket);
}
sendSingleAuthMessage(ipAddress, password, key, message) {
const socket = dgram_as_promised_1.default.createSocket("udp4");
const encodedMessage = buffer_1.Buffer.from(message);
const ac = new AbortController();
const signal = ac.signal;
return new Promise(resolve => {
(0, promises_1.setTimeout)(1000, 'timeout', { signal }).then(() => {
Promise.all([this.stop(), socket.close()])
.catch(() => resolve({ message: 'Success' }))
.then(() => resolve({ message: 'Success' }));
}).catch(() => resolve({ message: 'Success' }));
this.login(ipAddress, password, key).then(result => {
if (!result.success) {
resolve({ message: 'Access denied' });
}
else {
return socket.bind().then(() => {
this.activeSockets.push(socket);
socket.setBroadcast(true);
return socket.send(encodedMessage, 0, encodedMessage.length, BROADCAST_PORT, ipAddress);
}).catch(() => resolve(false));
}
}).then(() => socket.recv()).then(packet => {
const denied = tibbo_helpers_1.TibboHelpers.checkIfDenied(packet);
ac.abort();
if (denied) {
return { message: 'Access denied' };
}
return { message: 'Success' };
}).then(response => this.stop().then(() => response));
});
}
}
exports.TibboDiscover = TibboDiscover;
/* istanbul ignore if */
if (require.main == module) {
const instance = new TibboDiscover();
const tibboDiscover = new TibboDiscover();
const tibboDeviceServer = new tibbo_device_server_1.TibboDeviceServer();
let promise;
if (process.argv[2] === 'login') {
const ipAddress = process.argv[3];
const password = process.argv[4];
promise = instance.login(ipAddress, password);
promise = tibboDeviceServer.login(ipAddress, password);
}
else if (process.argv[2] === 'setting') {
const ipAddress = process.argv[3];
const password = process.argv[4];
const setting = process.argv[5];
const value = process.argv[6];
promise = instance.updateSetting(setting, value, ipAddress, password);
promise = tibboDeviceServer.updateSetting(setting, value, ipAddress, password);
}
else if (process.argv[2] === 'settings') {
const ipAddress = process.argv[3];
Expand All @@ -214,34 +106,34 @@ if (require.main == module) {
});
}
});
promise = instance.updateSettings(settings, ipAddress, password);
promise = tibboDeviceServer.updateSettings(settings, ipAddress, password);
}
else if (process.argv[2] === 'buzz') {
const ipAddress = process.argv[3];
const password = process.argv[4];
const key = process.argv[5];
promise = instance.buzz(ipAddress, password, key);
promise = tibboDeviceServer.buzz(ipAddress, password, key);
}
else if (process.argv[2] === 'reboot') {
const ipAddress = process.argv[3];
const password = process.argv[4];
const key = process.argv[5];
promise = instance.reboot(ipAddress, password, key);
promise = tibboDeviceServer.reboot(ipAddress, password, key);
}
else if (process.argv[2] === 'query') {
const id = process.argv[3];
let timeout = Number(process.argv[4]);
if (isNaN(timeout)) {
timeout = undefined;
}
promise = instance.query(id, timeout).then(result => instance.stop().then(() => result));
promise = tibboDiscover.query(id, timeout).then(result => tibboDiscover.stop().then(() => result));
}
else {
let timeout = Number(process.argv[2]);
if (isNaN(timeout)) {
timeout = undefined;
}
promise = instance.scan(timeout);
promise = tibboDiscover.scan(timeout);
}
promise.then(result => console.log(JSON.stringify(result, null, 2)));
}
1 change: 1 addition & 0 deletions dist/tibbo-helpers.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export declare class TibboHelpers {
static processSettingResponse(packet?: IncomingPacket): boolean;
static queryMessage(id: string): string;
static rebootMessage(key: string): string;
static initializeSettingsMessage(key: string): string;
static buzzMessage(key: string): string;
static updateSettingMessage(setting: string, value: string, key: string): string;
static loginMessage(password: string, key: string): string;
Expand Down
Loading

0 comments on commit 2f249d7

Please sign in to comment.