@lumiastream/tapo-cove 0.0.1-alpha.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/module/discovery.d.ts +8 -0
- package/dist/module/discovery.js +118 -0
- package/dist/module/index.d.ts +5 -0
- package/dist/module/index.js +12 -0
- package/dist/module/lightstate.d.ts +39 -0
- package/dist/module/lightstate.js +123 -0
- package/dist/module/shared/cipher.d.ts +11 -0
- package/dist/module/shared/cipher.js +66 -0
- package/dist/module/shared/color-transformer.d.ts +8 -0
- package/dist/module/shared/color-transformer.js +42 -0
- package/dist/module/shared/helpers.d.ts +5 -0
- package/dist/module/shared/helpers.js +105 -0
- package/dist/module/shared/tapo-ca-cert.d.ts +2 -0
- package/dist/module/shared/tapo-ca-cert.js +3 -0
- package/dist/module/shared/tapo.constants.d.ts +21 -0
- package/dist/module/shared/tapo.constants.js +25 -0
- package/dist/module/shared/utils.d.ts +23 -0
- package/dist/module/shared/utils.js +152 -0
- package/dist/module/tapo-api.d.ts +48 -0
- package/dist/module/tapo-api.js +142 -0
- package/dist/module/types/interfaces.d.ts +3 -0
- package/dist/module/types/interfaces.js +2 -0
- package/dist/module/types/raw-types.d.ts +84 -0
- package/dist/module/types/raw-types.js +2 -0
- package/lib/cjs/discovery.d.ts +8 -0
- package/lib/cjs/discovery.js +118 -0
- package/lib/cjs/index.d.ts +5 -0
- package/lib/cjs/index.js +12 -0
- package/lib/cjs/lightstate.d.ts +39 -0
- package/lib/cjs/lightstate.js +123 -0
- package/lib/cjs/shared/cipher.d.ts +11 -0
- package/lib/cjs/shared/cipher.js +66 -0
- package/lib/cjs/shared/color-transformer.d.ts +8 -0
- package/lib/cjs/shared/color-transformer.js +42 -0
- package/lib/cjs/shared/helpers.d.ts +5 -0
- package/lib/cjs/shared/helpers.js +105 -0
- package/lib/cjs/shared/tapo-ca-cert.d.ts +2 -0
- package/lib/cjs/shared/tapo-ca-cert.js +3 -0
- package/lib/cjs/shared/tapo.constants.d.ts +21 -0
- package/lib/cjs/shared/tapo.constants.js +25 -0
- package/lib/cjs/shared/utils.d.ts +23 -0
- package/lib/cjs/shared/utils.js +152 -0
- package/lib/cjs/tapo-api.d.ts +48 -0
- package/lib/cjs/tapo-api.js +142 -0
- package/lib/cjs/types/interfaces.d.ts +3 -0
- package/lib/cjs/types/interfaces.js +2 -0
- package/lib/cjs/types/raw-types.d.ts +84 -0
- package/lib/cjs/types/raw-types.js +2 -0
- package/lib/esm/discovery.d.ts +8 -0
- package/lib/esm/discovery.js +116 -0
- package/lib/esm/index.d.ts +5 -0
- package/lib/esm/index.js +5 -0
- package/lib/esm/lightstate.d.ts +39 -0
- package/lib/esm/lightstate.js +120 -0
- package/lib/esm/shared/cipher.d.ts +11 -0
- package/lib/esm/shared/cipher.js +56 -0
- package/lib/esm/shared/color-transformer.d.ts +8 -0
- package/lib/esm/shared/color-transformer.js +40 -0
- package/lib/esm/shared/helpers.d.ts +5 -0
- package/lib/esm/shared/helpers.js +98 -0
- package/lib/esm/shared/tapo-ca-cert.d.ts +2 -0
- package/lib/esm/shared/tapo-ca-cert.js +1 -0
- package/lib/esm/shared/tapo.constants.d.ts +21 -0
- package/lib/esm/shared/tapo.constants.js +22 -0
- package/lib/esm/shared/utils.d.ts +23 -0
- package/lib/esm/shared/utils.js +146 -0
- package/lib/esm/tapo-api.d.ts +48 -0
- package/lib/esm/tapo-api.js +140 -0
- package/lib/esm/types/interfaces.d.ts +3 -0
- package/lib/esm/types/interfaces.js +1 -0
- package/lib/esm/types/raw-types.d.ts +84 -0
- package/lib/esm/types/raw-types.js +1 -0
- package/package.json +38 -0
- package/test/auth.test.ts +22 -0
- package/test/discover.test.ts +9 -0
- package/test/light.test.ts +17 -0
- package/test/plug.test.ts +16 -0
- package/tslint.json +53 -0
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
var tslib_1 = require("tslib");
|
|
4
|
+
var lumia_rgb_types_1 = require("@lumiastream/lumia-rgb-types");
|
|
5
|
+
var axios_1 = tslib_1.__importDefault(require("axios"));
|
|
6
|
+
var local_devices_1 = tslib_1.__importDefault(require("local-devices"));
|
|
7
|
+
var cipher_1 = require("./shared/cipher");
|
|
8
|
+
var helpers_1 = require("./shared/helpers");
|
|
9
|
+
var tapo_api_1 = tslib_1.__importDefault(require("./tapo-api"));
|
|
10
|
+
var discover = function (config) { return tslib_1.__awaiter(void 0, void 0, void 0, function () {
|
|
11
|
+
var api, token, getDeviceRequest, response, devices, localDevices;
|
|
12
|
+
var _a;
|
|
13
|
+
return tslib_1.__generator(this, function (_b) {
|
|
14
|
+
switch (_b.label) {
|
|
15
|
+
case 0:
|
|
16
|
+
api = new tapo_api_1.default(config);
|
|
17
|
+
token = config.token;
|
|
18
|
+
if (!!token) return [3, 2];
|
|
19
|
+
return [4, api.auth({ email: config.email, password: config.password })];
|
|
20
|
+
case 1:
|
|
21
|
+
token = _b.sent();
|
|
22
|
+
_b.label = 2;
|
|
23
|
+
case 2:
|
|
24
|
+
getDeviceRequest = {
|
|
25
|
+
method: 'getDeviceList',
|
|
26
|
+
};
|
|
27
|
+
return [4, (0, axios_1.default)({
|
|
28
|
+
method: 'post',
|
|
29
|
+
url: "".concat(api._baseUrl, "?token=").concat(token),
|
|
30
|
+
data: getDeviceRequest,
|
|
31
|
+
})];
|
|
32
|
+
case 3:
|
|
33
|
+
response = _b.sent();
|
|
34
|
+
(0, helpers_1.throwErrorIfFound)(response.data);
|
|
35
|
+
devices = [];
|
|
36
|
+
return [4, (0, local_devices_1.default)({ skipNameResolution: true })];
|
|
37
|
+
case 4:
|
|
38
|
+
localDevices = _b.sent();
|
|
39
|
+
(_a = response.data.result) === null || _a === void 0 ? void 0 : _a.deviceList.map(function (deviceInfo) { return tslib_1.__awaiter(void 0, void 0, void 0, function () {
|
|
40
|
+
var findableMac_1, found, isTapo, alias, isTapo, alias;
|
|
41
|
+
var _a, _b;
|
|
42
|
+
return tslib_1.__generator(this, function (_c) {
|
|
43
|
+
if (!deviceInfo.ip) {
|
|
44
|
+
findableMac_1 = deviceInfo.deviceMac.replace(/:/g, '').toUpperCase();
|
|
45
|
+
found = localDevices.find(function (device) { return findableMac_1 == device.mac.replace(/:/g, '').toUpperCase(); });
|
|
46
|
+
if (found) {
|
|
47
|
+
deviceInfo.ip = found.ip;
|
|
48
|
+
}
|
|
49
|
+
else {
|
|
50
|
+
return [2];
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
switch (deviceInfo.deviceType) {
|
|
54
|
+
case 'IOT.SMARTBULB':
|
|
55
|
+
case 'SMART.TAPOBULB': {
|
|
56
|
+
if (config.types && config.types !== lumia_rgb_types_1.ILumiaDeviceType.LIGHT) {
|
|
57
|
+
return [2];
|
|
58
|
+
}
|
|
59
|
+
isTapo = deviceInfo.deviceType === 'SMART.TAPOBULB';
|
|
60
|
+
alias = (_a = (isTapo ? (0, cipher_1.base64Decode)(deviceInfo.alias) : deviceInfo.alias)) !== null && _a !== void 0 ? _a : deviceInfo.deviceName;
|
|
61
|
+
devices.push({
|
|
62
|
+
name: alias,
|
|
63
|
+
id: deviceInfo.deviceMac,
|
|
64
|
+
address: "http://".concat(deviceInfo.ip),
|
|
65
|
+
host: deviceInfo.ip,
|
|
66
|
+
lumiaInfo: {
|
|
67
|
+
alias: alias,
|
|
68
|
+
identifier: deviceInfo.deviceMac,
|
|
69
|
+
serial: deviceInfo.hwId,
|
|
70
|
+
lumiaType: lumia_rgb_types_1.ILumiaDeviceType.LIGHT,
|
|
71
|
+
zonable: false,
|
|
72
|
+
maxZones: 1,
|
|
73
|
+
zones: null,
|
|
74
|
+
rgb: true,
|
|
75
|
+
white: true,
|
|
76
|
+
connectionType: lumia_rgb_types_1.ILumiaDeviceConnectionType.WIFI,
|
|
77
|
+
brand: isTapo ? lumia_rgb_types_1.ILumiaDeviceBrands.TPLINK : lumia_rgb_types_1.ILumiaDeviceBrands.TPLINK,
|
|
78
|
+
product: deviceInfo === null || deviceInfo === void 0 ? void 0 : deviceInfo.deviceModel,
|
|
79
|
+
},
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
case 'IOT.SMARTPLUGSWITCH':
|
|
83
|
+
case 'SMART.TAPOPLUG': {
|
|
84
|
+
if (config.types && config.types !== lumia_rgb_types_1.ILumiaDeviceType.PLUG) {
|
|
85
|
+
return [2];
|
|
86
|
+
}
|
|
87
|
+
isTapo = deviceInfo.deviceType === 'SMART.TAPOPLUG';
|
|
88
|
+
alias = (_b = (isTapo ? (0, cipher_1.base64Decode)(deviceInfo.alias) : deviceInfo.alias)) !== null && _b !== void 0 ? _b : deviceInfo.deviceName;
|
|
89
|
+
devices.push({
|
|
90
|
+
name: alias,
|
|
91
|
+
id: deviceInfo.deviceMac,
|
|
92
|
+
address: "http://".concat(deviceInfo.ip),
|
|
93
|
+
host: deviceInfo.ip,
|
|
94
|
+
lumiaInfo: {
|
|
95
|
+
alias: alias,
|
|
96
|
+
identifier: deviceInfo.deviceMac,
|
|
97
|
+
serial: deviceInfo.hwId,
|
|
98
|
+
lumiaType: lumia_rgb_types_1.ILumiaDeviceType.PLUG,
|
|
99
|
+
zonable: false,
|
|
100
|
+
maxZones: null,
|
|
101
|
+
zones: null,
|
|
102
|
+
rgb: false,
|
|
103
|
+
white: false,
|
|
104
|
+
connectionType: lumia_rgb_types_1.ILumiaDeviceConnectionType.WIFI,
|
|
105
|
+
brand: isTapo ? lumia_rgb_types_1.ILumiaDeviceBrands.TPLINK : lumia_rgb_types_1.ILumiaDeviceBrands.TPLINK,
|
|
106
|
+
product: deviceInfo === null || deviceInfo === void 0 ? void 0 : deviceInfo.deviceModel,
|
|
107
|
+
},
|
|
108
|
+
});
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
return [2];
|
|
112
|
+
});
|
|
113
|
+
}); });
|
|
114
|
+
return [2, devices];
|
|
115
|
+
}
|
|
116
|
+
});
|
|
117
|
+
}); };
|
|
118
|
+
exports.default = discover;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.TapoConstants = exports.TapoApi = exports.discover = exports.LightState = void 0;
|
|
4
|
+
var tslib_1 = require("tslib");
|
|
5
|
+
var discovery_1 = tslib_1.__importDefault(require("./discovery"));
|
|
6
|
+
exports.discover = discovery_1.default;
|
|
7
|
+
var lightstate_1 = require("./lightstate");
|
|
8
|
+
Object.defineProperty(exports, "LightState", { enumerable: true, get: function () { return lightstate_1.LightState; } });
|
|
9
|
+
var tapo_api_1 = tslib_1.__importDefault(require("./tapo-api"));
|
|
10
|
+
exports.TapoApi = tapo_api_1.default;
|
|
11
|
+
var TapoConstants = tslib_1.__importStar(require("./shared/tapo.constants"));
|
|
12
|
+
exports.TapoConstants = TapoConstants;
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
declare class SuperState {
|
|
2
|
+
protected _values: {};
|
|
3
|
+
transition: (value: number) => this;
|
|
4
|
+
duration: (value: number) => this;
|
|
5
|
+
getValues: () => {};
|
|
6
|
+
}
|
|
7
|
+
export declare class LightState extends SuperState {
|
|
8
|
+
constructor(values?: any);
|
|
9
|
+
create: (values?: any) => this;
|
|
10
|
+
on: (value?: boolean) => this;
|
|
11
|
+
turnOn: (value?: boolean) => this;
|
|
12
|
+
off: () => this;
|
|
13
|
+
turnOff: () => this;
|
|
14
|
+
mode: (value?: string) => this;
|
|
15
|
+
hue: (value: number) => this;
|
|
16
|
+
bri: (value: number) => this;
|
|
17
|
+
brightness: (value: number) => this;
|
|
18
|
+
sat: (value: number) => this;
|
|
19
|
+
saturation: (value: number) => this;
|
|
20
|
+
temp: (value: number) => this;
|
|
21
|
+
colorTemperature: (value: number) => this;
|
|
22
|
+
hsv: (value: [number, number, number] | {
|
|
23
|
+
h: number;
|
|
24
|
+
s: number;
|
|
25
|
+
v: number;
|
|
26
|
+
}) => this;
|
|
27
|
+
rgb: (value: [number, number, number] | {
|
|
28
|
+
r: number;
|
|
29
|
+
g: number;
|
|
30
|
+
b: number;
|
|
31
|
+
}) => this;
|
|
32
|
+
color: (value: {
|
|
33
|
+
ct?: number;
|
|
34
|
+
r?: number;
|
|
35
|
+
g?: number;
|
|
36
|
+
b?: number;
|
|
37
|
+
}) => this;
|
|
38
|
+
}
|
|
39
|
+
export {};
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.LightState = void 0;
|
|
4
|
+
var tslib_1 = require("tslib");
|
|
5
|
+
var color_transformer_1 = tslib_1.__importDefault(require("./shared/color-transformer"));
|
|
6
|
+
var utils_1 = require("./shared/utils");
|
|
7
|
+
var isFunction = function (object) {
|
|
8
|
+
var getClass = {}.toString;
|
|
9
|
+
return object && getClass.call(object) === '[object Function]';
|
|
10
|
+
};
|
|
11
|
+
var SuperState = (function () {
|
|
12
|
+
function SuperState() {
|
|
13
|
+
var _this = this;
|
|
14
|
+
this._values = {};
|
|
15
|
+
this.transition = function (value) {
|
|
16
|
+
_this._values['transition_period'] = value;
|
|
17
|
+
return _this;
|
|
18
|
+
};
|
|
19
|
+
this.duration = this.transition;
|
|
20
|
+
this.getValues = function () {
|
|
21
|
+
return _this._values;
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
return SuperState;
|
|
25
|
+
}());
|
|
26
|
+
var LightState = (function (_super) {
|
|
27
|
+
tslib_1.__extends(LightState, _super);
|
|
28
|
+
function LightState(values) {
|
|
29
|
+
var _this = _super.call(this) || this;
|
|
30
|
+
_this.create = function (values) {
|
|
31
|
+
if (values) {
|
|
32
|
+
Object.keys(values).forEach(function (value) {
|
|
33
|
+
var fn;
|
|
34
|
+
if (_this.hasOwnProperty(value)) {
|
|
35
|
+
fn = _this[value];
|
|
36
|
+
if (isFunction(fn) && (0, utils_1.isTruly)(values[value])) {
|
|
37
|
+
fn.apply(_this, [values[value]]);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
return _this;
|
|
43
|
+
};
|
|
44
|
+
_this.on = function (value) {
|
|
45
|
+
if (value === void 0) { value = true; }
|
|
46
|
+
_this._values['device_on'] = value;
|
|
47
|
+
return _this;
|
|
48
|
+
};
|
|
49
|
+
_this.turnOn = _this.on;
|
|
50
|
+
_this.off = function () {
|
|
51
|
+
_this.on(false);
|
|
52
|
+
return _this;
|
|
53
|
+
};
|
|
54
|
+
_this.turnOff = _this.off;
|
|
55
|
+
_this.mode = function (value) {
|
|
56
|
+
if (value === void 0) { value = 'normal'; }
|
|
57
|
+
if (value === 'normal') {
|
|
58
|
+
_this._values['color_temp'] = 0;
|
|
59
|
+
}
|
|
60
|
+
_this._values['mode'] = value;
|
|
61
|
+
return _this;
|
|
62
|
+
};
|
|
63
|
+
_this.hue = function (value) {
|
|
64
|
+
_this._values['hue'] = value;
|
|
65
|
+
_this._values['color_temp'] = 0;
|
|
66
|
+
return _this;
|
|
67
|
+
};
|
|
68
|
+
_this.bri = function (value) {
|
|
69
|
+
_this._values['brightness'] = value;
|
|
70
|
+
return _this;
|
|
71
|
+
};
|
|
72
|
+
_this.brightness = _this.bri;
|
|
73
|
+
_this.sat = function (value) {
|
|
74
|
+
_this._values['saturation'] = value;
|
|
75
|
+
return _this;
|
|
76
|
+
};
|
|
77
|
+
_this.saturation = _this.sat;
|
|
78
|
+
_this.temp = function (value) {
|
|
79
|
+
_this._values['color_temp'] = value;
|
|
80
|
+
return _this;
|
|
81
|
+
};
|
|
82
|
+
_this.colorTemperature = _this.temp;
|
|
83
|
+
_this.hsv = function (value) {
|
|
84
|
+
var convertedHsv = [];
|
|
85
|
+
if (Array.isArray(value)) {
|
|
86
|
+
convertedHsv = value;
|
|
87
|
+
}
|
|
88
|
+
else {
|
|
89
|
+
convertedHsv = [value.h, value.s, value.v];
|
|
90
|
+
}
|
|
91
|
+
_this.hue(convertedHsv[0]);
|
|
92
|
+
_this.sat(convertedHsv[1]);
|
|
93
|
+
_this.bri(convertedHsv[2]);
|
|
94
|
+
return _this;
|
|
95
|
+
};
|
|
96
|
+
_this.rgb = function (value) {
|
|
97
|
+
var hsv;
|
|
98
|
+
if (Array.isArray(value)) {
|
|
99
|
+
hsv = color_transformer_1.default.rgb2hsv(value);
|
|
100
|
+
}
|
|
101
|
+
else {
|
|
102
|
+
hsv = color_transformer_1.default.rgb2hsv([value.r, value.g, value.b]);
|
|
103
|
+
}
|
|
104
|
+
return _this.hsv(hsv);
|
|
105
|
+
};
|
|
106
|
+
_this.color = function (value) {
|
|
107
|
+
if (Array.isArray(value)) {
|
|
108
|
+
value = { r: value[0], g: value[1], b: value[2] };
|
|
109
|
+
}
|
|
110
|
+
if (value.ct) {
|
|
111
|
+
return _this.temp(value.ct);
|
|
112
|
+
}
|
|
113
|
+
else {
|
|
114
|
+
return _this.rgb({ r: value.r, g: value.g, b: value.b });
|
|
115
|
+
}
|
|
116
|
+
};
|
|
117
|
+
if (values)
|
|
118
|
+
_this.create(values);
|
|
119
|
+
return _this;
|
|
120
|
+
}
|
|
121
|
+
return LightState;
|
|
122
|
+
}(SuperState));
|
|
123
|
+
exports.LightState = LightState;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
/// <reference types="node" />
|
|
3
|
+
import crypto, { KeyObject } from 'crypto';
|
|
4
|
+
import type { TapoDeviceKey } from '../types/raw-types';
|
|
5
|
+
export declare const generateKeyPair: () => Promise<crypto.KeyPairKeyObjectResult>;
|
|
6
|
+
export declare const encrypt: (data: any, deviceKey: TapoDeviceKey) => string;
|
|
7
|
+
export declare const decrypt: (data: string, deviceKey: TapoDeviceKey) => any;
|
|
8
|
+
export declare const readDeviceKey: (pemKey: string, privateKey: KeyObject) => Buffer;
|
|
9
|
+
export declare const base64Encode: (data: string) => string;
|
|
10
|
+
export declare const base64Decode: (data: string) => string;
|
|
11
|
+
export declare const shaDigest: (data: string) => string;
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.shaDigest = exports.base64Decode = exports.base64Encode = exports.readDeviceKey = exports.decrypt = exports.encrypt = exports.generateKeyPair = void 0;
|
|
4
|
+
var tslib_1 = require("tslib");
|
|
5
|
+
var crypto_1 = tslib_1.__importDefault(require("crypto"));
|
|
6
|
+
var util_1 = tslib_1.__importDefault(require("util"));
|
|
7
|
+
var RSA_CIPHER_ALGORITHM = 'rsa';
|
|
8
|
+
var AES_CIPHER_ALGORITHM = 'aes-128-cbc';
|
|
9
|
+
var PASSPHRASE = 'top secret';
|
|
10
|
+
var generateKeyPair = function () { return tslib_1.__awaiter(void 0, void 0, void 0, function () {
|
|
11
|
+
var RSA_OPTIONS, generateKeyPair;
|
|
12
|
+
return tslib_1.__generator(this, function (_a) {
|
|
13
|
+
RSA_OPTIONS = {
|
|
14
|
+
modulusLength: 1024,
|
|
15
|
+
publicKeyEncoding: {
|
|
16
|
+
type: 'spki',
|
|
17
|
+
format: 'pem',
|
|
18
|
+
},
|
|
19
|
+
privateKeyEncoding: {
|
|
20
|
+
type: 'pkcs1',
|
|
21
|
+
format: 'pem',
|
|
22
|
+
cipher: 'aes-256-cbc',
|
|
23
|
+
passphrase: PASSPHRASE,
|
|
24
|
+
},
|
|
25
|
+
};
|
|
26
|
+
generateKeyPair = util_1.default.promisify(crypto_1.default.generateKeyPair);
|
|
27
|
+
return [2, generateKeyPair(RSA_CIPHER_ALGORITHM, RSA_OPTIONS)];
|
|
28
|
+
});
|
|
29
|
+
}); };
|
|
30
|
+
exports.generateKeyPair = generateKeyPair;
|
|
31
|
+
var encrypt = function (data, deviceKey) {
|
|
32
|
+
var cipher = crypto_1.default.createCipheriv(AES_CIPHER_ALGORITHM, deviceKey.key, deviceKey.iv);
|
|
33
|
+
var ciphertext = cipher.update(Buffer.from(JSON.stringify(data)));
|
|
34
|
+
return Buffer.concat([ciphertext, cipher.final()]).toString('base64');
|
|
35
|
+
};
|
|
36
|
+
exports.encrypt = encrypt;
|
|
37
|
+
var decrypt = function (data, deviceKey) {
|
|
38
|
+
var cipher = crypto_1.default.createDecipheriv(AES_CIPHER_ALGORITHM, deviceKey.key, deviceKey.iv);
|
|
39
|
+
var ciphertext = cipher.update(Buffer.from(data, 'base64'));
|
|
40
|
+
return JSON.parse(Buffer.concat([ciphertext, cipher.final()]).toString());
|
|
41
|
+
};
|
|
42
|
+
exports.decrypt = decrypt;
|
|
43
|
+
var readDeviceKey = function (pemKey, privateKey) {
|
|
44
|
+
var keyBytes = Buffer.from(pemKey, 'base64');
|
|
45
|
+
var deviceKey = crypto_1.default.privateDecrypt({
|
|
46
|
+
key: privateKey,
|
|
47
|
+
padding: crypto_1.default.constants.RSA_PKCS1_PADDING,
|
|
48
|
+
passphrase: PASSPHRASE,
|
|
49
|
+
}, keyBytes);
|
|
50
|
+
return deviceKey;
|
|
51
|
+
};
|
|
52
|
+
exports.readDeviceKey = readDeviceKey;
|
|
53
|
+
var base64Encode = function (data) {
|
|
54
|
+
return Buffer.from(data).toString('base64');
|
|
55
|
+
};
|
|
56
|
+
exports.base64Encode = base64Encode;
|
|
57
|
+
var base64Decode = function (data) {
|
|
58
|
+
return Buffer.from(data, 'base64').toString();
|
|
59
|
+
};
|
|
60
|
+
exports.base64Decode = base64Decode;
|
|
61
|
+
var shaDigest = function (data) {
|
|
62
|
+
var shasum = crypto_1.default.createHash('sha1');
|
|
63
|
+
shasum.update(data);
|
|
64
|
+
return shasum.digest('hex');
|
|
65
|
+
};
|
|
66
|
+
exports.shaDigest = shaDigest;
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
var rgb2hsv = function (_a) {
|
|
4
|
+
var r = _a[0], g = _a[1], b = _a[2];
|
|
5
|
+
var rr, gg, bb;
|
|
6
|
+
r = r / 255;
|
|
7
|
+
g = g / 255;
|
|
8
|
+
b = b / 255;
|
|
9
|
+
var h, s, v = Math.max(r, g, b), diff = v - Math.min(r, g, b), diffc = function (c) {
|
|
10
|
+
return (v - c) / 6 / diff + 1 / 2;
|
|
11
|
+
};
|
|
12
|
+
if (diff == 0) {
|
|
13
|
+
h = s = 0;
|
|
14
|
+
}
|
|
15
|
+
else {
|
|
16
|
+
s = diff / v;
|
|
17
|
+
rr = diffc(r);
|
|
18
|
+
gg = diffc(g);
|
|
19
|
+
bb = diffc(b);
|
|
20
|
+
if (r === v) {
|
|
21
|
+
h = bb - gg;
|
|
22
|
+
}
|
|
23
|
+
else if (g === v) {
|
|
24
|
+
h = (1 / 3) + rr - bb;
|
|
25
|
+
}
|
|
26
|
+
else if (b === v) {
|
|
27
|
+
h = (2 / 3) + gg - rr;
|
|
28
|
+
}
|
|
29
|
+
if (h < 0) {
|
|
30
|
+
h += 1;
|
|
31
|
+
}
|
|
32
|
+
else if (h > 1) {
|
|
33
|
+
h -= 1;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
return {
|
|
37
|
+
h: Math.round(h * 360),
|
|
38
|
+
s: Math.round(s * 100),
|
|
39
|
+
v: Math.round(v * 100)
|
|
40
|
+
};
|
|
41
|
+
};
|
|
42
|
+
exports.default = { rgb2hsv: rgb2hsv };
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { TapoDeviceKey } from '../types/raw-types';
|
|
2
|
+
export declare const throwErrorIfFound: (responseData: any) => void;
|
|
3
|
+
export declare const throwTapoCareErrorIfFound: (responseData: any) => void;
|
|
4
|
+
export declare const handshake: (deviceIp: string) => Promise<TapoDeviceKey>;
|
|
5
|
+
export declare const securePassthrough: (deviceRequest: any, deviceKey: TapoDeviceKey) => Promise<any>;
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.securePassthrough = exports.handshake = exports.throwTapoCareErrorIfFound = exports.throwErrorIfFound = void 0;
|
|
4
|
+
var tslib_1 = require("tslib");
|
|
5
|
+
var axios_1 = tslib_1.__importDefault(require("axios"));
|
|
6
|
+
var cipher_1 = require("./cipher");
|
|
7
|
+
var throwErrorIfFound = function (responseData) {
|
|
8
|
+
var errorCode = responseData['error_code'];
|
|
9
|
+
if (errorCode) {
|
|
10
|
+
switch (errorCode) {
|
|
11
|
+
case 0:
|
|
12
|
+
return;
|
|
13
|
+
case -1010:
|
|
14
|
+
throw new Error('Invalid public key length');
|
|
15
|
+
case -1012:
|
|
16
|
+
throw new Error('Invalid terminal UUID');
|
|
17
|
+
case -1501:
|
|
18
|
+
throw new Error('Invalid request or credentials');
|
|
19
|
+
case -1002:
|
|
20
|
+
throw new Error('Incorrect request');
|
|
21
|
+
case -1003:
|
|
22
|
+
throw new Error('JSON format error');
|
|
23
|
+
case -20601:
|
|
24
|
+
throw new Error('Incorrect email or password');
|
|
25
|
+
case -20675:
|
|
26
|
+
throw new Error('Cloud token expired or invalid');
|
|
27
|
+
case 9999:
|
|
28
|
+
throw new Error('Device token expired or invalid');
|
|
29
|
+
default:
|
|
30
|
+
throw new Error("Unexpected Error Code: ".concat(errorCode, " (").concat(responseData['msg'], ")"));
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
};
|
|
34
|
+
exports.throwErrorIfFound = throwErrorIfFound;
|
|
35
|
+
var throwTapoCareErrorIfFound = function (responseData) {
|
|
36
|
+
var errorCode = responseData === null || responseData === void 0 ? void 0 : responseData.code;
|
|
37
|
+
if (errorCode) {
|
|
38
|
+
throw new Error("Unexpected Error Code: ".concat(errorCode, " (").concat(responseData['message'], ")"));
|
|
39
|
+
}
|
|
40
|
+
};
|
|
41
|
+
exports.throwTapoCareErrorIfFound = throwTapoCareErrorIfFound;
|
|
42
|
+
var handshake = function (deviceIp) { return tslib_1.__awaiter(void 0, void 0, void 0, function () {
|
|
43
|
+
var keyPair, handshakeRequest, response, setCookieHeader, sessionCookie, deviceKey;
|
|
44
|
+
return tslib_1.__generator(this, function (_a) {
|
|
45
|
+
switch (_a.label) {
|
|
46
|
+
case 0: return [4, (0, cipher_1.generateKeyPair)()];
|
|
47
|
+
case 1:
|
|
48
|
+
keyPair = _a.sent();
|
|
49
|
+
handshakeRequest = {
|
|
50
|
+
method: 'handshake',
|
|
51
|
+
params: {
|
|
52
|
+
key: keyPair.publicKey,
|
|
53
|
+
},
|
|
54
|
+
};
|
|
55
|
+
return [4, (0, axios_1.default)({
|
|
56
|
+
method: 'post',
|
|
57
|
+
url: "http://".concat(deviceIp, "/app"),
|
|
58
|
+
data: handshakeRequest,
|
|
59
|
+
})];
|
|
60
|
+
case 2:
|
|
61
|
+
response = _a.sent();
|
|
62
|
+
(0, exports.throwErrorIfFound)(response.data);
|
|
63
|
+
setCookieHeader = response.headers['set-cookie'][0];
|
|
64
|
+
sessionCookie = setCookieHeader.substring(0, setCookieHeader.indexOf(';'));
|
|
65
|
+
deviceKey = (0, cipher_1.readDeviceKey)(response.data.result.key, keyPair.privateKey);
|
|
66
|
+
return [2, {
|
|
67
|
+
key: deviceKey.subarray(0, 16),
|
|
68
|
+
iv: deviceKey.subarray(16, 32),
|
|
69
|
+
deviceIp: deviceIp,
|
|
70
|
+
sessionCookie: sessionCookie,
|
|
71
|
+
}];
|
|
72
|
+
}
|
|
73
|
+
});
|
|
74
|
+
}); };
|
|
75
|
+
exports.handshake = handshake;
|
|
76
|
+
var securePassthrough = function (deviceRequest, deviceKey) { return tslib_1.__awaiter(void 0, void 0, void 0, function () {
|
|
77
|
+
var encryptedRequest, securePassthroughRequest, response, decryptedResponse;
|
|
78
|
+
return tslib_1.__generator(this, function (_a) {
|
|
79
|
+
switch (_a.label) {
|
|
80
|
+
case 0:
|
|
81
|
+
encryptedRequest = (0, cipher_1.encrypt)(deviceRequest, deviceKey);
|
|
82
|
+
securePassthroughRequest = {
|
|
83
|
+
method: 'securePassthrough',
|
|
84
|
+
params: {
|
|
85
|
+
request: encryptedRequest,
|
|
86
|
+
},
|
|
87
|
+
};
|
|
88
|
+
return [4, (0, axios_1.default)({
|
|
89
|
+
method: 'post',
|
|
90
|
+
url: "http://".concat(deviceKey.deviceIp, "/app?token=").concat(deviceKey.token),
|
|
91
|
+
data: securePassthroughRequest,
|
|
92
|
+
headers: {
|
|
93
|
+
Cookie: deviceKey.sessionCookie,
|
|
94
|
+
},
|
|
95
|
+
})];
|
|
96
|
+
case 1:
|
|
97
|
+
response = _a.sent();
|
|
98
|
+
(0, exports.throwErrorIfFound)(response.data);
|
|
99
|
+
decryptedResponse = (0, cipher_1.decrypt)(response.data.result.response, deviceKey);
|
|
100
|
+
(0, exports.throwErrorIfFound)(decryptedResponse);
|
|
101
|
+
return [2, decryptedResponse.result];
|
|
102
|
+
}
|
|
103
|
+
});
|
|
104
|
+
}); };
|
|
105
|
+
exports.securePassthrough = securePassthrough;
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
declare const _default: "-----BEGIN CERTIFICATE-----\n\nMIIDBzCCAe+gAwIBAgIQT5x0ma7QnINHCQvhnmzR9zANBgkqhkiG9w0BAQsFADAV\n \nMRMwEQYDVQQDEwp0cC1saW5rLUNBMCAXDTE4MDExOTA4Mjc1MloYDzIwNjgwMTE5\n \nMDgzNzUyWjAVMRMwEQYDVQQDEwp0cC1saW5rLUNBMIIBIjANBgkqhkiG9w0BAQEF\n \nAAOCAQ8AMIIBCgKCAQEAuGG8n5zEUN1j5wuvUz4pAIMurhKHbpfUUu+b2acFHKS6\n \niU9hNJWvDyhXcihY5Wz6aq9m4D5SZcgW3k31YoNNtrztDjdg2qw7AaX85S99/G0B\n \nVbIXktrhs34OW19WA/haDwut3dFhLem+gCRRKUXcmuqchZc84dY7JFVfhPcJci4m\n \nsRjLCFNO0ho9OX+MZwfO4BLaeAqKVoAor6rf4BXVtO0xjYHDKO0fb3AWLLJ4EjGe\n \nq6YieqPiYlPFEqRm5PrvBXTm0IuQogygyVpK4LHr/K207ZLyV33DxLLbsUgSEJVn\n \npZUv/WUujXjlIDgxIvyZZCYiXO3dle2/MEvpmZk6JQIDAQABo1EwTzALBgNVHQ8E\n \nBAMCAYYwDwYDVR0TAQH/BAUwAwEB/zAdBgNVHQ4EFgQUxu2iBRTsef5iNnsADVhM\n \nJDQWi6kwEAYJKwYBBAGCNxUBBAMCAQAwDQYJKoZIhvcNAQELBQADggEBAB52Majd\n \n+wo3cb5BsTo63z2Psbbyl4ACMUaw68NxUMy61Oihx3mcLzLJqiIZcKePiHskLqLJ\n \nF7QfT9TqjvizMjFJVgsLuVubUBXKBzqyN+3KKlQci0PO3mH+ObhyaE7BzV+qrS3P\n \ndVTgsCWFv8DkgLTRudSWxL7VwVoedc7lRz5EroGgJ33nRGCR0ngcW919tLTARDQO\n \npULmzulcdWeZgG+0PLX0xjJQIjFEvbOxR1Z+gxMupBz0rWFokmWYrcga8eWiWzjQ\n \nIa3/ASBVJ69srV77trWlfLumkChbXk9i64NXBKnce0Jmll0Y9OC1nMPqrbQKnzcn\n \ndSAA4fejD/qMQn0=\n\n-----END CERTIFICATE-----";
|
|
2
|
+
export default _default;
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.default = "-----BEGIN CERTIFICATE-----\n\nMIIDBzCCAe+gAwIBAgIQT5x0ma7QnINHCQvhnmzR9zANBgkqhkiG9w0BAQsFADAV\n \nMRMwEQYDVQQDEwp0cC1saW5rLUNBMCAXDTE4MDExOTA4Mjc1MloYDzIwNjgwMTE5\n \nMDgzNzUyWjAVMRMwEQYDVQQDEwp0cC1saW5rLUNBMIIBIjANBgkqhkiG9w0BAQEF\n \nAAOCAQ8AMIIBCgKCAQEAuGG8n5zEUN1j5wuvUz4pAIMurhKHbpfUUu+b2acFHKS6\n \niU9hNJWvDyhXcihY5Wz6aq9m4D5SZcgW3k31YoNNtrztDjdg2qw7AaX85S99/G0B\n \nVbIXktrhs34OW19WA/haDwut3dFhLem+gCRRKUXcmuqchZc84dY7JFVfhPcJci4m\n \nsRjLCFNO0ho9OX+MZwfO4BLaeAqKVoAor6rf4BXVtO0xjYHDKO0fb3AWLLJ4EjGe\n \nq6YieqPiYlPFEqRm5PrvBXTm0IuQogygyVpK4LHr/K207ZLyV33DxLLbsUgSEJVn\n \npZUv/WUujXjlIDgxIvyZZCYiXO3dle2/MEvpmZk6JQIDAQABo1EwTzALBgNVHQ8E\n \nBAMCAYYwDwYDVR0TAQH/BAUwAwEB/zAdBgNVHQ4EFgQUxu2iBRTsef5iNnsADVhM\n \nJDQWi6kwEAYJKwYBBAGCNxUBBAMCAQAwDQYJKoZIhvcNAQELBQADggEBAB52Majd\n \n+wo3cb5BsTo63z2Psbbyl4ACMUaw68NxUMy61Oihx3mcLzLJqiIZcKePiHskLqLJ\n \nF7QfT9TqjvizMjFJVgsLuVubUBXKBzqyN+3KKlQci0PO3mH+ObhyaE7BzV+qrS3P\n \ndVTgsCWFv8DkgLTRudSWxL7VwVoedc7lRz5EroGgJ33nRGCR0ngcW919tLTARDQO\n \npULmzulcdWeZgG+0PLX0xjJQIjFEvbOxR1Z+gxMupBz0rWFokmWYrcga8eWiWzjQ\n \nIa3/ASBVJ69srV77trWlfLumkChbXk9i64NXBKnce0Jmll0Y9OC1nMPqrbQKnzcn\n \ndSAA4fejD/qMQn0=\n\n-----END CERTIFICATE-----";
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
export declare const TapoDeviceTypes: {
|
|
2
|
+
ALL: string;
|
|
3
|
+
BULBS: string;
|
|
4
|
+
LIGHTSTRIPS: string;
|
|
5
|
+
PLUGS: string;
|
|
6
|
+
};
|
|
7
|
+
export declare enum ETapoDeviceTypes {
|
|
8
|
+
ALL = "all",
|
|
9
|
+
BULBS = "bulb",
|
|
10
|
+
LIGHTSTRIPS = "lightstrip",
|
|
11
|
+
PLUGS = "plug"
|
|
12
|
+
}
|
|
13
|
+
export declare const DeviceResTypes: {
|
|
14
|
+
BULB: string;
|
|
15
|
+
PLUG: string;
|
|
16
|
+
};
|
|
17
|
+
export declare const DeviceSendValues: {
|
|
18
|
+
BULB: string;
|
|
19
|
+
LIGHTSTRIP: string;
|
|
20
|
+
PLUG: string;
|
|
21
|
+
};
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.DeviceSendValues = exports.DeviceResTypes = exports.ETapoDeviceTypes = exports.TapoDeviceTypes = void 0;
|
|
4
|
+
exports.TapoDeviceTypes = {
|
|
5
|
+
ALL: 'all',
|
|
6
|
+
BULBS: 'bulb',
|
|
7
|
+
LIGHTSTRIPS: 'lightstrip',
|
|
8
|
+
PLUGS: 'plug',
|
|
9
|
+
};
|
|
10
|
+
var ETapoDeviceTypes;
|
|
11
|
+
(function (ETapoDeviceTypes) {
|
|
12
|
+
ETapoDeviceTypes["ALL"] = "all";
|
|
13
|
+
ETapoDeviceTypes["BULBS"] = "bulb";
|
|
14
|
+
ETapoDeviceTypes["LIGHTSTRIPS"] = "lightstrip";
|
|
15
|
+
ETapoDeviceTypes["PLUGS"] = "plug";
|
|
16
|
+
})(ETapoDeviceTypes = exports.ETapoDeviceTypes || (exports.ETapoDeviceTypes = {}));
|
|
17
|
+
exports.DeviceResTypes = {
|
|
18
|
+
BULB: 'IOT.SMARTPLUGSWITCH',
|
|
19
|
+
PLUG: 'IOT.SMARTPLUGSWITCH',
|
|
20
|
+
};
|
|
21
|
+
exports.DeviceSendValues = {
|
|
22
|
+
BULB: 'smartlife.iot.smartbulb.lightingservice',
|
|
23
|
+
LIGHTSTRIP: 'smartlife.iot.lightStrip',
|
|
24
|
+
PLUG: 'system',
|
|
25
|
+
};
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
export declare const isTruly: (value: any) => boolean;
|
|
3
|
+
export declare const isNully: (value: any) => boolean;
|
|
4
|
+
declare function encryptWithHeader(input: any, firstKey?: number): Buffer;
|
|
5
|
+
declare function decrypt(input: any, firstKey?: number): Buffer;
|
|
6
|
+
declare function decryptWithHeader(input: any, firstKey?: number): Buffer;
|
|
7
|
+
export declare function uuidv4(): string;
|
|
8
|
+
declare const _default: {
|
|
9
|
+
checkHost: (host: any) => any;
|
|
10
|
+
createRandomRgb: (min: any, max: any) => [number, number, number];
|
|
11
|
+
bigEndianToLilEndian: (value: any, pad?: any) => any;
|
|
12
|
+
rgbToHsb: (rgbObj: any) => {
|
|
13
|
+
h: number;
|
|
14
|
+
s: number;
|
|
15
|
+
b: number;
|
|
16
|
+
};
|
|
17
|
+
encrypt: (input: any, firstKey?: number) => Buffer;
|
|
18
|
+
encryptWithHeader: typeof encryptWithHeader;
|
|
19
|
+
decrypt: typeof decrypt;
|
|
20
|
+
decryptWithHeader: typeof decryptWithHeader;
|
|
21
|
+
uuidv4: typeof uuidv4;
|
|
22
|
+
};
|
|
23
|
+
export default _default;
|