-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
153 lines (118 loc) · 3.69 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
"use strict";
const Homey = require("homey");
const niuCloudConnector = require("./lib/niu-cloud-connector");
// const { log } = require("./logger.js");
const _settingsKey = `${Homey.manifest.id}.settings`;
let _niuClient = 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(`${this.homey.manifest.id} - ${this.homey.manifest.version} started...`);
await this.initSettings();
this.log("onInit - Loaded settings", {...this.appSettings, 'USERNAME': 'LOG', PASSWORD: 'LOG'});
}
// -------------------- SETTINGS ----------------------
async initSettings() {
try {
let settingsInitialized = false;
this.homey.settings.getKeys().forEach((key) => {
if (key == _settingsKey) {
settingsInitialized = true;
}
});
if (settingsInitialized) {
this.log("initSettings - Found settings key", _settingsKey);
this.appSettings = this.homey.settings.get(_settingsKey);
if (!_niuClient) {
_niuClient = await this.setNiuClient(this.appSettings);
}
if(this.appSettings.TOKEN) {
this.log("initSettings - setSessionToken", this.appSettings.TOKEN);
await _niuClient.setSessionToken({token: this.appSettings.TOKEN});
}
if(!this.appSettings.REFRESH) {
this.appSettings.REFRESH = 10;
this.saveSettings();
}
return;
}
this.log(`Initializing ${_settingsKey} with defaults`);
this.updateSettings({
USERNAME: "",
PASSWORD: "",
COUNTRY_CODE: "",
TOKEN: "",
REFRESH: 30
});
} catch (err) {
this.error(err);
}
}
updateSettings(settings) {
this.log("updateSettings - New settings:", {...settings, 'USERNAME': 'LOG', PASSWORD: 'LOG'});
_niuClient = undefined;
this.appSettings = settings;
this.saveSettings();
}
saveSettings() {
if (typeof this.appSettings === "undefined") {
this.log("Not saving settings; settings empty!");
return;
}
this.log("Saved settings.");
this.homey.settings.set(_settingsKey, this.appSettings);
}
// -------------------- EUFY LOGIN ----------------------
async niuLogin(data) {
try {
let settings = data;
this.log("niuLogin - New settings:", {...settings, 'USERNAME': 'LOG', PASSWORD: 'LOG'});
this.log(`niuLogin - Found username and password. Logging in to Niu`);
_niuClient = await this.setNiuClient(data);
const token = await _niuClient.createSessionToken({account: settings.USERNAME, password: settings.PASSWORD, countryCode: settings.COUNTRY_CODE});
if(token) {
settings.TOKEN = token;
} else {
return new Error(token.error)
}
this.appSettings = settings;
this.saveSettings();
this.log("niuLogin - Loaded settings", {...this.appSettings, 'USERNAME': 'LOG', PASSWORD: 'LOG'});
return;
} catch (err) {
this.error(err);
return err;
}
}
// ---------------------------- GETTERS/SETTERS ----------------------------------
getSettings() {
return this.appSettings;
}
async setNiuClient() {
try {
return new niuCloudConnector.Client();
} catch (err) {
this.error(err);
}
}
getNiuClient() {
return _niuClient;
}
setDevices(device) {
_devices.push(device);
}
getDevices() {
return _devices;
}
refreshToken() {
this.niuLogin(this.appSettings);
}
}
module.exports = App;