forked from magtimmermans/com.cyclone-software.buienalarm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
218 lines (175 loc) · 6.78 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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
'use strict';
const Homey = require('homey');
const BuienAlarmGrabber = require('./BuienAlarmGrabber.js');
const MINUTE = 60000;
class BuienAlarm extends Homey.App {
onInit() {
this.log(`${Homey.manifest.id} V${Homey.manifest.version} is running...`);
this.isRaining = null;
this.firstTime = true;
this.rainStopTriggered = false;
this.rainStartTriggered = false;
this.data = {};
this.rainpercentage = 0;
this.rainmm = 0;
this.registerBuienAlarmGrabber();
Homey.ManagerGeolocation.on('location', this.registerBuienAlarmGrabber.bind(this));
this.initFlows();
// Start syncing periodically.
this.shouldSync = true;
this.startSyncing();
}
async startSyncing() {
// Prevent more than one syncing cycle.
if (this.syncRunning) return;
// Start syncing.
this.syncRunning = true;
this.log('starting sync');
this.poll();
}
registerBuienAlarmGrabber() {
const latitude = Homey.ManagerGeolocation.getLatitude();
const longitude = Homey.ManagerGeolocation.getLongitude();
this.ba_api = new BuienAlarmGrabber({ lat: latitude, lon: longitude });
}
initFlows() {
this.rainStartTrigger = new Homey.FlowCardTrigger('rain_start').register();
this.rainStopTrigger = new Homey.FlowCardTrigger('rain_stop').register();
this.rainInTrigger = new Homey.FlowCardTrigger('raining_in').register().registerRunListener((args, state) => {
// console.log(`start when ${state.when} = ${args.when}`);
if (state.when === args.when) {
this.log(`regen in ${args.when}`);
return Promise.resolve(true);
} return Promise.resolve(false);
});
this.dryInTrigger = new Homey.FlowCardTrigger('dry_in').register().registerRunListener((args, state) => {
// console.log(`stop when ${state.when} = ${args.when}`);
if (state.when === args.when) {
this.log(`stop regen in ${args.when}`);
return Promise.resolve(true);
} return Promise.resolve(false);
});
this.trgRainPercentage = new Homey.FlowCardTrigger('triggerRainPercentage').register()
.registerRunListener((args, state) => {
if (state.rainpercentageArg > args.rainpercentageArg) {
return Promise.resolve(true);
} return Promise.resolve(false);
});
this.trgtotalRainNextHour = new Homey.FlowCardTrigger('totalRainNextHour').register()
.registerRunListener((args, state) => {
if (state.rainmmArg > args.rainmmArg) {
return Promise.resolve(true);
} return Promise.resolve(false);
});
this.rainCondition = new Homey.FlowCardCondition('is_raining').register()
.registerRunListener(async (args, state) => this.isRaining);
this.rainInCondition = new Homey.FlowCardCondition('raining_in').register()
.registerRunListener(async (args, state) => {
const index = (args.when/5);
return this.data.rainData[index] > 0;
});
this.rainmmCondition = new Homey.FlowCardCondition('is_mmrain').register()
.registerRunListener(async (args, state) => this.rainmm > args.rainmmArg);
this.rrainPercentageCondition = new Homey.FlowCardCondition('is_percrain').register()
.registerRunListener(async (args, state) => this.rainpercentage > args.rainpercentageArg);
}
addMinutesToTime(time, minutes) {
return new Date(time.getTime() + minutes * 60000);
}
// calculate % of rain the comming 2 hours
procentageOfRain(data) {
let total = 0;
for (let index = 0; index < data.length; index++) {
if (data[index] > 0) total++;
}
return (total > 0) ? Math.round((total / 25) * 100) : 0;
}
// calculate % of rain the comming 2 hours
totalRainCommingHour(data) {
let total = 0;
for (let index = 0; index < 12; index++) {
total += data[index];
}
return Math.round(total.toFixed(2) * 100) / 100;
}
async poll() {
// Check if it is raining at this moment
this.isSyncing = true;
const me = this;
try {
const result = await this.ba_api.getForecasts();
me.data = result;
let isRainingNew = result.rainData[0] > 0;
this.rainpercentage = this.procentageOfRain(result.rainData);
this.rainmm = this.totalRainCommingHour(result.rainData);
this.trgRainPercentage.trigger({ rainpercentage: this.rainpercentage }, { rainpercentageArg: this.rainpercentage }).catch(err => this.error(err));
this.trgtotalRainNextHour.trigger({ rainmm: this.rainmm }, { rainmmArg: this.rainmm }).catch(err => this.error(err));
this.log(`Rain Percentage:${this.rainpercentage}, Total rain within hour:${this.rainmm}`);
this.log(`current isRaining: ${this.isRaining} new isRaining: ${isRainingNew}`);
if (this.isRaining === null) {
this.isRaining = isRainingNew;
if (isRainingNew) { this.rainStartTrigger.trigger(); }
} else if (!isRainingNew && this.isRaining === true) {
// stopped raining
this.isRaining = false;
this.log('stopped raining');
this.rainStopTrigger.trigger();
} else if (isRainingNew && this.isRaining === false) {
// starts raining
this.isRaining = true;
this.log('starts raining');
this.rainStartTrigger.trigger();
}
const startDate = new Date(result.unix_timestamp * 1000);
for (let i = 0; i < 8; i += 1) {
let inMinutes = 0;
switch (i) {
case 0: inMinutes = 5; break;
case 1: inMinutes = 10; break;
case 2: inMinutes = 15; break;
case 3: inMinutes = 30; break;
case 4: inMinutes = 45; break;
case 5: inMinutes = 60; break;
case 6: inMinutes = 90; break;
case 7: inMinutes = 120; break;
default:
this.log('Unknown minutes');
}
const index = (inMinutes / 5);
isRainingNew = result.rainData[index] > 0;
const date = this.addMinutesToTime(startDate, inMinutes);
// this.log(`Date: ${date}, startdate:${startDate}`);
this.log(`Index: ${index}, inMinutes ${inMinutes} , isRaining:${this.isRaining} NewRaining: ${isRainingNew}, firstime:${this.firstTime}`);
if ((!isRainingNew && this.isRaining === true && this.rainStopTriggered === false) || (!isRainingNew && this.firstTime === true)) {
this.log(`TRIGGERING FLOW STOP IN: Time: ${date}, raining: ${isRainingNew}`);
this.dryInTrigger.trigger(null, { when: inMinutes.toString() });
this.rainStopTriggered = true;
setTimeout(() => {
this.rainStopTriggered = false;
}, inMinutes * MINUTE);
} else if ((isRainingNew && this.isRaining === false) || (isRainingNew && this.firstTime === true)) {
this.log(`TRIGGERING FLOW START IN: Time: ${date}, raining: ${isRainingNew}`);
this.rainInTrigger.trigger(null, { when: inMinutes.toString() });
// this.rainStartTriggered = true;
// setTimeout(() => {
// this.rainStartTriggered = false;
// }, inMinutes * MINUTE);
}
}
this.firstTime = false;
} catch (e) {
this.error(e);
}
this.isSyncing = false;
// Schedule next sync.
this.timeout = setTimeout(
() => this.poll(),
5 * MINUTE,
);
}
getRainData() {
console.log(this.data);
return this.data;
}
}
module.exports = BuienAlarm;