Skip to content

Commit

Permalink
rewrite, discover returns array
Browse files Browse the repository at this point in the history
  • Loading branch information
jessedp committed Apr 19, 2020
1 parent 014855d commit 9e2e274
Show file tree
Hide file tree
Showing 4 changed files with 195 additions and 133 deletions.
6 changes: 2 additions & 4 deletions src/IDevice.ts → src/Device.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
export default interface IDevice {
ip: string;
export default interface Device {
board: string;
private_ip: string;
server_id: string;
Expand All @@ -9,7 +8,7 @@ export default interface IDevice {
dev_type: string;
host?: string;
public_ip?: string;
slip?: number; // a port?
slip?: number; // a port
http?: number; // a port
ssl?: number; // a port or bool
inserted?: string; // this is a date
Expand All @@ -18,5 +17,4 @@ export default interface IDevice {
server_version?: string;
name?: string;
roku?: number; // probably a bool

}
21 changes: 21 additions & 0 deletions src/ServerInfo.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
export default interface ServerInfo {
server_id: string;
name: string;
timezone: string;
version: string;
local_address: string;
setup_completed: boolean;
build_number: number;
model: Model;
availability: string;
cache_key: string;
checked: string;
}

export interface Model {
wifi: boolean;
tuners: number;
type: string;
name: string;
device: string;
}
55 changes: 33 additions & 22 deletions src/discovery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,18 @@ import dgram = require('dgram');

const debug = Debug('discovery');

import IDevice from "./IDevice";
import Device from "./Device";

class Discover {
export default class Discover {
private readonly sendPort: number = 8881;
private readonly recvPort: number = 8882;
private readonly discoveryUrl: string = 'https://api.tablotv.com/assocserver/getipinfo/';
private watcher: Timeout;

public async broadcast(): Promise<[IDevice]> {
/**
* Attempt discovery via UDP broadcast. Will only return a single device.
*/
public async broadcast(): Promise<[Device]> {
const server = dgram.createSocket('udp4');

server.on('error', (error) => {
Expand Down Expand Up @@ -43,7 +46,7 @@ class Discover {
});
});

let outerDevice;
let outerDevice:Device;

server.on('message', (msg, info) => {
if (msg.length !== 140) {
Expand All @@ -55,14 +58,15 @@ class Discover {
// this is the proper format string let s = struct('>4s64s32s20s10s10s')
// s = struct.format('b').unpack()

const trunc = (txt) => txt.split('\0', 1)[0];
const device = {
const trunc = (txt: string) => txt.split('\0', 1)[0];
const device:Device = {
host: trunc(bytes.unpackString(msg, 4, 68)),
private_ip: trunc(bytes.unpackString(msg, 68, 100)),
resp_code: trunc(bytes.unpackString(msg, 0, 4)),
// resp_code: trunc(bytes.unpackString(msg, 0, 4)),
server_id: trunc(bytes.unpackString(msg, 100, 120)),
dev_type: trunc(bytes.unpackString(msg, 120, 130)),
board: trunc(bytes.unpackString(msg, 130, 140)),
via: 'broadcast'
};
clearTimeout(this.watcher);
server.close();
Expand Down Expand Up @@ -91,22 +95,29 @@ class Discover {
});
}

public async http(): Promise<[IDevice]> {
let data;
try {
const response = await axios.get(this.discoveryUrl);
data = response.data.cpes;
} catch (error) {
debug('Http Error:', error);
}
return new Promise((resolve) => {
resolve(data);
/**
* Attempt discovery via HTTP broadcast using Tablo discovery service
*/
public async http(): Promise<Device[]>{
return new Promise(async (resolve, reject) => {

let data: Device[];
try {
type Response = { data: { cpes: Device[]}};
const response:Response = await axios.get(this.discoveryUrl);
data = response.data.cpes;
data.forEach((part, index, arr) => {
if (arr[index]) {
arr[index].via = 'http';
}
}, data); // use arr as this
} catch (error) {
debug('Http Error:', error);
reject( new Error(`Http Error: ${error}`) );
}
resolve(data);
});

}
}

export const discovery = new Discover();

exports.default = discovery;
exports.discovery = discovery;
export const discovery = new Discover();
Loading

0 comments on commit 9e2e274

Please sign in to comment.