-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathapp.js
54 lines (46 loc) · 1.74 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
'use strict';
const { request } = require('urllib'); // This adds 512kB to the app
const { OAuth2App } = require('homey-oauth2app');
const HoiaxOAuth2Client = require('./lib/HoiaxOAuth2Client');
class MyApp extends OAuth2App {
static DEBUG = false;
static OAUTH2_CLIENT = HoiaxOAuth2Client; // Default: OAuth2Client
static OAUTH2_DEBUG = this.DEBUG; // Default: false
static OAUTH2_MULTI_SESSION = false; // Default: false
static OAUTH2_DRIVERS = ['my_driver']; // Default: all drivers
/**
* onInit is called when the app is initialized.
*/
async onOAuth2Init() {
if (this.DEBUG) {
this.log('Hoiax has been initialized');
}
// Notify the user about the new app
const spareGrisVersion = await this.checkSpareGrisVersion();
const userNotifiedSparegris = this.homey.settings.get('userNotifiedSparegris');
if ((!userNotifiedSparegris) && (spareGrisVersion === undefined)) {
this.homey.notifications.createNotification({ excerpt: this.homey.__('info.sparegris') });
this.homey.settings.set('userNotifiedSparegris', 'yes');
}
}
/**
* Checks if Sparegris is installed
* @return the version number if sparegris is installed. Undefined otherwise.
*/
async checkSpareGrisVersion() {
// Can not use the homey.api as such:
// const spareGrisInstalled = this.homey.api.getApiApp('no.sparegris').getInstalled();
// Must use web-api instead:
const webAddress = 'http://localhost/api/app/no.sparegris/getVersion';
try {
const { data, res } = await request(webAddress, { dataType: 'json' });
if (res.status === 200) {
return JSON.parse(data).version;
}
return undefined;
} catch (err) {
return undefined;
}
}
}
module.exports = MyApp;