-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathapp.js
140 lines (112 loc) · 4.36 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
const Homey = require('homey');
const fetch = require('node-fetch');
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.flowConditions();
await this.setSettings();
this.currentStatus = 'Online';
this.isFailureRate = 0;
this.verifyConnection();
}
async setSettings() {
this.appSettings = this.homey.settings.get('settings');
if(!this.appSettings) {
this.log("setSettings - Appsettings not found - setings default");
this.homey.settings.set('settings', {
host: 'https://cloudflare.com',
interval: 15,
failure_rate: 3
});
this.appSettings = this.homey.settings.get('settings');
}
if('host' in this.appSettings === false) {
await this.homey.settings.set('settings', {
...this.appSettings,
host: 'https://cloudflare.com'
});
}
if('interval' in this.appSettings === false) {
await this.homey.settings.set('settings', {
...this.appSettings,
interval: 15
});
}
if('interval' in this.appSettings && this.appSettings.interval < 10) {
await this.homey.settings.set('settings', {
...this.appSettings,
interval: 10
});
}
if('failure_rate' in this.appSettings === false) {
await this.homey.settings.set('settings', {
...this.appSettings,
failure_rate: 3
});
}
if('failure_rate' in this.appSettings && this.appSettings.failure_rate < 3) {
await this.homey.settings.set('settings', {
...this.appSettings,
failure_rate: 3
});
}
}
async verifyConnection() {
this.appSettings = this.homey.settings.get('settings');
this.log("verifyConnection - appSettings:", this.appSettings);
this.log('verifyConnection - Performing web request to ', this.appSettings.host);
const isFailure = await fetch(this.appSettings.host, {
method: 'FET',
cache: 'no-cache',
headers: { 'Content-Type': 'application/json' },
referrerPolicy: 'no-referrer'
})
.then(() => false)
.catch(() => true);
if(isFailure) {
this.isFailureRate += 1;
this.log('verifyConnection Failed - FailureRate: ', this.isFailureRate);
} else {
this.isFailureRate = 0;
this.log('verifyConnection Succes - FailureRate: ', this.isFailureRate);
this.flowTrigger(false);
}
if(this.isFailureRate >= this.appSettings.failure_rate) {
this.flowTrigger(true);
}
const that = this;
setTimeout(() => {
that.verifyConnection();
}, this.appSettings.interval * 1000);
}
flowTrigger(isFailure = false) {
try {
const triggerType = isFailure ? 'connection_lost' : 'connection_restored';
const triggerStatus = isFailure ? 'Offline' : 'Online';
this.log(`flowTrigger - ${triggerStatus} ...`);
if (this.currentStatus != triggerStatus) {
this.currentStatus = triggerStatus;
this.log('flowTrigger - type: ' + triggerType);
this.homey.app.trigger_network = this.homey.flow.getTriggerCard(triggerType);
this.homey.app.trigger_network
.trigger()
.catch(this.error)
.then(this.log(`flowTrigger - ${triggerType} triggered succesfully`));
}
} catch (err) {
this.log('flowTrigger - error: ', err);
}
}
async flowConditions() {
const connection_online_offline = this.homey.flow.getConditionCard('connection_online_offline');
connection_online_offline.registerRunListener(async () => this.currentStatus === 'Online');
}
}
module.exports = App;