forked from glennsp/com.mill
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
55 lines (45 loc) · 1.18 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
// eslint-disable-next-line import/no-unresolved
const Homey = require('homey');
const Mill = require('./lib/mill');
const { debug } = require('./lib/util');
class MillApp extends Homey.App {
onInit() {
this.millApi = new Mill();
this.user = null;
this.isAuthenticated = false;
this.isAuthenticating = false;
debug(`${Homey.manifest.id} is running..`);
}
async connectToMill() {
const username = Homey.ManagerSettings.get('username');
const password = Homey.ManagerSettings.get('password');
return this.authenticate(username, password);
}
async authenticate(username, password) {
if (username && password && !this.isAuthenticating) {
try {
this.isAuthenticating = true;
this.user = await this.millApi.login(username, password) || null;
this.isAuthenticated = true;
debug('Mill authenticated');
return true;
} finally {
this.isAuthenticating = false;
}
}
return false;
}
clear() {
this.user = null;
}
isConnected() {
return this.isAuthenticated;
}
getUser() {
return this.user;
}
getMillApi() {
return this.millApi;
}
}
module.exports = MillApp;