-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
0.2.1 - Breaks the DS functions out into a separate class
- Loading branch information
Showing
20 changed files
with
489 additions
and
361 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.