Skip to content

Commit f91ca3b

Browse files
committed
try to add d.ts file
1 parent 872ea26 commit f91ca3b

4 files changed

Lines changed: 61 additions & 17 deletions

File tree

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,9 @@ Of course I'm also very glad about issues and pull requests if you find a bug /
173173
Placeholder for next versions (this needs to be indented):
174174
### __WORK IN PROGRESS__
175175
-->
176+
### __WORK IN PROGRESS__
177+
* add d.ts for typescript support.
178+
176179
### 0.5.1 (2022-08-30)
177180
* fix support for ws 8.*
178181

getToken.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ if (!process.argv[2]) {
66
process.exit(-2);
77
}
88
const s = net.createConnection(23, process.argv[2], () => {
9-
console.log("Connected.");
9+
console.log('Connected.');
1010
});
1111

1212
s.on('data', buffer => {

index.d.ts

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import {EventEmitter} from 'events';
2+
3+
declare module 'dlink_websocketclient' {
4+
export class WebSocketClient extends EventEmitter.EventEmitter {
5+
constructor(options: {
6+
/**
7+
* ip of device as string
8+
*/
9+
ip: string
10+
/**
11+
* either Pin on the back or device token (if paired with App! Needs to be extracted, see readme).
12+
*/
13+
pin: string;
14+
15+
/**
16+
* defaults to 8080
17+
*/
18+
port?: number;
19+
20+
/**
21+
* either w115 or w245.
22+
*/
23+
model?: string;
24+
25+
/**
26+
* function for debug logging, defaults to noop.
27+
*/
28+
log?: (string) => void;
29+
30+
/**
31+
* seconds to ping, defaults to 30. 0 to turn off.
32+
*/
33+
keepAlive?: number;
34+
35+
/**
36+
* library should get the device token from telnet (which needs to be active).
37+
*/
38+
useTelnetForToken?: boolean;
39+
});
40+
41+
connect(): Promise<boolean>;
42+
disconnect(): void;
43+
getDeviceId(): string;
44+
getDeviceInfoFromTelnet(): Promise<Record<string, string>>;
45+
getTokenFromTelnet(): Promise<boolean>;
46+
login(): Promise<boolean>;
47+
setPin(newPin: string): void;
48+
isDeviceReady(): boolean;
49+
switch(on: boolean, socket: number): Promise<boolean>;
50+
switchLED(on: boolean, socket: number): Promise<boolean>;
51+
state(socket: number): Promise<boolean>;
52+
}
53+
}

index.js

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ const WebSocket = require('ws');
44
const EventEmitter = require('events');
55
const util = require('util');
66

7+
/* globals Buffer */
8+
79
//type constants:
810
const TYPE_SOCKET = 16;
911
const TYPE_LED = 41;
@@ -18,19 +20,6 @@ const TYPE_LED = 41;
1820
// 22, 400: "model", "nickname", "photo_index" read from event.
1921
// 1, 4, 0: code from message and if fitting (?) channel_url and some more stuff.
2022

21-
/**
22-
* @typedef WebSocketClean
23-
* @type {Object}
24-
* @property {function} send
25-
* @property {number} readyState
26-
* @property {function} ping
27-
* @property {function} close
28-
* @property {function} terminate
29-
*
30-
* @typedef {import("net").Socket} Socket
31-
* @typedef {WebSocketClean & Socket} WebSocket
32-
*/
33-
3423
function noop() {}
3524

3625
class WebSocketClient extends EventEmitter.EventEmitter {
@@ -60,7 +49,7 @@ class WebSocketClient extends EventEmitter.EventEmitter {
6049
deviceToken: '',
6150
deviceId: '',
6251
salt: '',
63-
socket: /** @type {WebSocket} */ ({}),
52+
socket: {},
6453
pingHandler: /** @type {NodeJS.Timeout|undefined} */ (undefined),
6554
sequence: 1000,
6655
state: [false],
@@ -107,8 +96,7 @@ class WebSocketClient extends EventEmitter.EventEmitter {
10796
connect() {
10897
let resolved = false;
10998
return new Promise((resolve, reject) => {
110-
this._device.socket = new WebSocket('wss://' + this._device.ip + ':' + this._device.port + '/SwitchCamera', {
111-
protocolVersion: 13,
99+
this._device.socket = new WebSocket('wss://' + this._device.ip + ':' + this._device.port + '/SwitchCamera', ['13'], {
112100
rejectUnauthorized: false,
113101
timeout: 5000
114102
});

0 commit comments

Comments
 (0)