Skip to content

Commit

Permalink
0.2.0 - Fixes buzz/reboot
Browse files Browse the repository at this point in the history
  • Loading branch information
128keaton committed Apr 15, 2022
1 parent 586a26c commit 7ce7786
Show file tree
Hide file tree
Showing 8 changed files with 115 additions and 51 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/code-coverage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
- uses: actions/setup-node@v2
name: Setup Node.JS
with:
node-version: 14
node-version: 16
- run: npm i
name: Install Dependencies
- run: npm run build
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ node_modules/
.DS_Store
dist/
test/coverage/*
.run/
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,12 @@ defaults to `tibbo123`.

<br>

* Buzz - `buzz(ipAddress: string)`
* Buzz - `buzz(ipAddress: string, password: string, key: string)`

Buzz the Tibbo at the IP address passed to the function

<br>

* Reboot - `reboot(ipAddress: string)`
* Reboot - `reboot(ipAddress: string, password: string, key: string)`

Reboot the Tibbo at the IP address passed to the function
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "tibbo-discover",
"version": "0.1.9",
"version": "0.2.0",
"description": "Discover Tibbo devices on the network",
"repository": "https://github.com/128keaton/Tibbo-Discover",
"main": "dist/tibbo-discover.js",
Expand Down Expand Up @@ -33,6 +33,9 @@
"files": [
"dist"
],
"engines" : {
"node" : ">=16.0.0"
},
"dependencies": {
"dgram-as-promised": "^5.0.1"
}
Expand Down
110 changes: 74 additions & 36 deletions src/tibbo-discover.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#! /usr/bin/env node

import {setTimeout} from 'timers/promises';
import DgramAsPromised, {SocketAsPromised} from "dgram-as-promised";
import {Buffer} from "buffer";
import {TibboHelpers} from "./tibbo-helpers";
Expand Down Expand Up @@ -28,7 +28,9 @@ export class TibboDiscover {
public scan(timeout: number = 5000): Promise<TibboDevice[]> {
return this.sendBroadcastMessage(TibboHelpers.discoverMessage).then(() => {
return new Promise(resolve => {
setTimeout(() => this.stop().then(devices => resolve(devices)), timeout);
setTimeout(timeout).then(() => {
this.stop().then(devices => resolve(devices))
})
})
})
}
Expand All @@ -45,32 +47,50 @@ export class TibboDiscover {
resolve(result)
});

setTimeout(() => {
setTimeout(timeout).then(() => {
if (!didResolve) {
resolve(null)
}
}, timeout)
})
})
}

public stop(): Promise<TibboDevice[]> {
const finished = () => {
this.activeSockets.forEach(socket => socket.unref());
this.activeSockets = [];
return Object.values(this.devices)
}

return Promise.all(this.activeSockets.map(socket => socket.close()))
.then(() => Object.values(this.devices))
.then(() => finished())
.catch(() => finished());
}

public login(ipAddress: string, password: string, key: string = this.key): Promise<TibboDeviceLoginResponse> {
const socket = DgramAsPromised.createSocket("udp4");
const message = TibboHelpers.loginMessage(password, key);
const encodedMessage = Buffer.from(message);
const ac = new AbortController();
const signal = ac.signal;

return new Promise<TibboDeviceLoginResponse>((resolve) => {
let didResolve = false;

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);
socket.setMulticastTTL(128);

return socket.send(encodedMessage, 0, encodedMessage.length, BROADCAST_PORT, ipAddress);
}).then(() => socket.recv())
Expand All @@ -86,33 +106,20 @@ export class TibboDiscover {
}))
.then(response => {
didResolve = true;
ac.abort();
resolve(response);
});

setTimeout(() => {
if (!didResolve) {
socket.close().then(() => {
resolve({key, success: false, message: 'ERR_TIMEOUT'});
}).catch(() => {
resolve({key, success: false, message: 'ERR_TIMEOUT'});
})
}
}, 3000);

})
}

public buzz(ipAddress: string) {
const socket = DgramAsPromised.createSocket("udp4");
const encodedMessage = Buffer.from(TibboHelpers.buzzMessage);

return this.handleSendDisconnect(socket, encodedMessage, ipAddress);
public buzz(ipAddress: string, password: string, key: string = this.key) {
return this.sendSingleAuthMessage(ipAddress, password, key, TibboHelpers.buzzMessage(key));
}

public reboot(ipAddress: string) {
const socket = DgramAsPromised.createSocket("udp4");
const encodedMessage = Buffer.from(TibboHelpers.rebootMessage);

return this.handleSendDisconnect(socket, encodedMessage, ipAddress);
public reboot(ipAddress: string, password: string, key: string = this.key) {
return this.sendSingleAuthMessage(ipAddress, password, key, TibboHelpers.rebootMessage(key));
}

public async updateSetting(setting: string,
Expand Down Expand Up @@ -150,7 +157,6 @@ export class TibboDiscover {
this.activeSockets.push(socket);

socket.setBroadcast(true);
socket.setMulticastTTL(128);

return Promise.all(settingMessages.map(setting => TibboHelpers.iterateSend(socket, setting, ipAddress, BROADCAST_PORT)))
}).then((results: boolean[]) => {
Expand All @@ -169,7 +175,6 @@ export class TibboDiscover {
this.activeSockets.push(socket);

socket.setBroadcast(true);
socket.setMulticastTTL(128);

socket.socket.on('message', (msg) => {
const tibboID = TibboHelpers.getMacAddress(msg);
Expand All @@ -186,15 +191,44 @@ export class TibboDiscover {
}).then(() => socket);
}

private handleSendDisconnect(socket: SocketAsPromised, encodedMessage: Buffer, ipAddress: string) {
return socket.bind().then(() => {
this.activeSockets.push(socket);
private sendSingleAuthMessage(ipAddress: string, password: string, key: string, message: string) {
const socket = DgramAsPromised.createSocket("udp4");
const encodedMessage = Buffer.from(message);

socket.setBroadcast(true);
socket.setMulticastTTL(128);
const ac = new AbortController();
const signal = ac.signal;

return new Promise(resolve => {
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);

return socket.send(encodedMessage, 0, encodedMessage.length, BROADCAST_PORT, ipAddress);
}).then(() => socket.close()).then(() => true);
socket.setBroadcast(true);

return socket.send(encodedMessage, 0, encodedMessage.length, BROADCAST_PORT, ipAddress);
}).catch(() => resolve(false))
}
}).then(() => socket.recv()).then(packet => {
const denied = TibboHelpers.checkIfDenied(packet);
ac.abort();

if (denied) {
return {message: 'Access denied'};
}

return {message: 'Success'};

}).then(response => this.stop().then(() => response))
});
}

}
Expand Down Expand Up @@ -238,12 +272,16 @@ if (require.main == module) {
promise = instance.updateSettings(settings, ipAddress, password);
} else if (process.argv[2] === 'buzz') {
const ipAddress: string = process.argv[3];
const password: string = process.argv[4];
const key: string | undefined = process.argv[5];

promise = instance.buzz(ipAddress);
promise = instance.buzz(ipAddress, password, key);
} else if (process.argv[2] === 'reboot') {
const ipAddress: string = process.argv[3];
const password: string = process.argv[4];
const key: string | undefined = process.argv[5];

promise = instance.reboot(ipAddress);
promise = instance.reboot(ipAddress, password, key);
} else if (process.argv[2] === 'query') {
const id: string = process.argv[3];

Expand Down
26 changes: 18 additions & 8 deletions src/tibbo-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,6 @@ export class TibboHelpers {
return `${START_BIT}${DISCOVER_BIT}`;
}

public static get buzzMessage() {
return BUZZ_BIT;
}

public static get rebootMessage() {
return REBOOT_BIT;
}

public static processQueryResponse(id: string, packet?: IncomingPacket): TibboDevice | null {
if (!packet) {
return null;
Expand Down Expand Up @@ -89,6 +81,14 @@ export class TibboHelpers {
return `${START_BIT}${id}${QUERY_BIT}`;
}

public static rebootMessage(key: string): string {
return `${REBOOT_BIT}${DELIMIT_BIT}${key}`;
}

public static buzzMessage(key: string): string {
return `${BUZZ_BIT}${DELIMIT_BIT}${key}`;
}

public static updateSettingMessage(setting: string, value: string, key: string): string {
return `${UPDATE_SETTING_BIT}${setting}@${value}${DELIMIT_BIT}${key}`;
}
Expand Down Expand Up @@ -116,5 +116,15 @@ export class TibboHelpers {
})
}

public static checkIfDenied(packet?: IncomingPacket): boolean {
if (!packet) {
return false
}

const message = packet.msg.toString();

return message === `${DENY_BIT}${DELIMIT_BIT}` || message === DENY_BIT;
}


}
6 changes: 3 additions & 3 deletions test/tibbo-discover.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ test('#testInstances', () => {
expect(discover.sendBroadcastMessage).toBeInstanceOf(Function);

// @ts-ignore
expect(discover.handleSendDisconnect).toBeInstanceOf(Function);
expect(discover.sendSingleAuthMessage).toBeInstanceOf(Function);
});

test('#testKey', () => {
Expand Down Expand Up @@ -99,15 +99,15 @@ test('#testUpdateSettings', () => {
test('#testReboot', () => {
const discover = new TibboDiscover();

return discover.reboot('0.0.0.0').then(result => {
return discover.reboot('0.0.0.0', 'password').then(result => {
expect(result).toBeTruthy();
})
});

test('#testBuzz', () => {
const discover = new TibboDiscover();

return discover.buzz('0.0.0.0').then(result => {
return discover.buzz('0.0.0.0', 'password').then(result => {
expect(result).toBeTruthy();
})
});
12 changes: 12 additions & 0 deletions test/tibbo-helpers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,18 @@ test('#testLoginMessage', () => {
expect(TibboHelpers.loginMessage(password, key)).toEqual(valid);
});

test('#testRebootMessage', () => {
const key = '123'

expect(TibboHelpers.rebootMessage(key)).toEqual(`E|${key}`);
});

test('#testBuzzMessage', () => {
const key = '123'

expect(TibboHelpers.buzzMessage(key)).toEqual(`B|${key}`);
});

jest.setTimeout(10000);
test('#testIterateSend', () => {
const socket = DgramAsPromised.createSocket("udp4");
Expand Down

0 comments on commit 7ce7786

Please sign in to comment.