forked from martijnpoppen/com.eufylife.security
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
171 lines (136 loc) · 4.17 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
"use strict";
const Homey = require("homey");
const { PushRegisterService, HttpService, sleep } = require("eufy-node-client");
const flowTriggers = require("./lib/flow/triggers.js");
const flowActions = require("./lib/flow/actions.js");
const _settingsKey = `${Homey.manifest.id}.settings`;
const _settings = Homey.ManagerSettings;
let _httpService = undefined;
let _devices = [];
class App extends Homey.App {
log() {
console.log.bind(this, "[log]").apply(this, arguments);
}
error() {
console.error.bind(this, "[error]").apply(this, arguments);
}
// -------------------- INIT ----------------------
async onInit() {
this.log(`${Homey.manifest.id} started...`);
await this.initSettings();
this.log("- Loaded settings", this.appSettings);
if (this.appSettings.LOCAL_STATION_IP) {
flowActions.init(this.appSettings);
}
if (this.appSettings.CREDENTIALS) {
flowTriggers.init(this.appSettings);
}
}
// -------------------- SETTINGS ----------------------
async initSettings() {
try {
let settingsInitialized = false;
_settings.getKeys().forEach((key) => {
if (key == _settingsKey) {
settingsInitialized = true;
}
});
if (settingsInitialized) {
this.log("Found settings key", _settingsKey);
this.appSettings = _settings.get(_settingsKey);
if (!_httpService) {
_httpService = await this.setHttpService(this.appSettings);
}
return;
}
this.log(`Initializing ${_settingsKey} with defaults`);
this.updateSettings({
USERNAME: "",
PASSWORD: "",
DSK_KEY: "",
P2P_DID: "",
ACTOR_ID: "",
STATION_SN: "",
LOCAL_STATION_IP: "",
SET_CREDENTIALS: true,
CREDENTIALS: "",
});
} catch (err) {
this.error(err);
}
}
updateSettings(settings) {
this.log("New settings:", settings);
if (!!settings.USERNAME && !!settings.PASSWORD) {
this.eufyLogin(settings);
} else {
_httpService = undefined;
this.appSettings = settings;
this.saveSettings();
}
}
saveSettings() {
if (typeof this.appSettings === "undefined") {
this.log("Not saving settings; settings empty!");
return;
}
this.log("Saved settings.");
_settings.set(_settingsKey, this.appSettings);
}
// -------------------- EUFY LOGIN ----------------------
async eufyLogin(data) {
try {
let settings = data;
this.log("New settings:", settings);
this.log(`Found username and password. Logging in to Eufy`);
_httpService = await this.setHttpService(data);
const hubs = await _httpService.listHubs();
this.log(`Logged in. Found station`, hubs[0].station_sn);
settings.P2P_DID = hubs[0].p2p_did;
settings.ACTOR_ID = hubs[0].member.action_user_id;
settings.STATION_SN = hubs[0].station_sn;
settings.LOCAL_STATION_IP = hubs[0].ip_addr;
const dsk = await _httpService.stationDskKeys(settings.STATION_SN);
settings.DSK_KEY = dsk.dsk_keys[0].dsk_key;
if (!settings.CREDENTIALS && settings.SET_CREDENTIALS) {
this.log(`Found SET_CREDENTIALS. Registering pushService`);
const pushService = new PushRegisterService();
settings.CREDENTIALS = await pushService.createPushCredentials();
}
this.appSettings = settings;
this.saveSettings();
this.log("- Loaded settings", this.appSettings);
if (settings.LOCAL_STATION_IP) {
flowActions.init(this.appSettings);
}
if (settings.CREDENTIALS) {
flowTriggers.init(this.appSettings);
}
return;
} catch (err) {
this.error(err);
return err;
}
}
// ---------------------------- GETTERS/SETTERS ----------------------------------
getSettings() {
return this.appSettings;
}
async setHttpService(settings) {
try {
return new HttpService(settings.USERNAME, settings.PASSWORD);
} catch (err) {
this.error(err);
}
}
getHttpService() {
return _httpService;
}
setDevices(device) {
_devices.push(device);
}
getDevices() {
return _devices;
}
}
module.exports = App;