-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
99 lines (79 loc) · 3.05 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
'use strict';
const Homey = require('homey');
// CronJob from https://github.com/kelektiv/node-cron
const { CronJob } = require('cron');
// App specific modules.
const flowActions = require('./lib/flows/actions');
const flowConditions = require('./lib/flows/conditions');
const flowTriggers = require('./lib/flows/triggers');
class AlarmUtils extends Homey.App {
async onInit() {
this.myAppIdVersion = `${this.homey.manifest.id}/${this.homey.manifest.version}`;
this.log(`${this.myAppIdVersion} - onInit - starting...`);
// Init device flow cards.
await flowActions.init(this);
await flowConditions.init(this);
await flowTriggers.init(this);
this.initAppCronJob();
this.log(`${this.myAppIdVersion} - onInit - started.`);
}
async onUninit() {
this.log(`${this.myAppIdVersion} - onUninit - stopping...`);
this.appCronJob.stop();
this.appCronJob = undefined;
this.log(`${this.myAppIdVersion} - onUninit - stopped.`);
}
async initAppCronJob() {
this.log(`${this.myAppIdVersion} - initAppCronjob`);
// create cronjob with cronTime and timeZone, do not start yet
this.appCronJob = new CronJob({
cronTime: '0 * * * * *',
onTick: () => {
this.onAppInterval();
},
start: false,
// timeZone: 'Europe/Amsterdam',
});
this.appCronJob.start();
}
async onAppInterval() {
// this.log(`${this.myAppIdVersion} - onAppInterval`);
// Get all configured scheduler devices.
const schedulerDriver = this.homey.drivers.getDriver('scheduler');
const schedulerDevices = schedulerDriver.getDevices();
const crontimeDriver = this.homey.drivers.getDriver('crontime');
const crontimeDevices = crontimeDriver.getDevices();
const devices = [...schedulerDevices, ...crontimeDevices];
// Iterate over all active scheduler devices.
devices.forEach((device) => {
if (device.getCapabilityValue('is_enabled') === true) {
const dateTimeNow = new Date();
const tz = this.homey.clock.getTimezone();
const nextScheduledTime = device.getNextScheduledTime();
const dateTimeNext = new Date(nextScheduledTime);
const scheduledTime = dateTimeNext.toLocaleString('en-US', {
hour: '2-digit', minute: '2-digit', hour12: false, timeZone: tz,
});
const yyyy = dateTimeNext.toLocaleString('en-US', { year: 'numeric', timeZone: tz });
const mm = dateTimeNext.toLocaleString('en-US', { month: '2-digit', timeZone: tz });
const dd = dateTimeNext.toLocaleString('en-US', { day: '2-digit', timeZone: tz });
const scheduledDate = `${yyyy}-${mm}-${dd}`;
const state = { minutes: Math.abs(Math.floor((dateTimeNext - dateTimeNow) / 60000)) + 1 };
const tokens = {
name: device.getName(),
date: scheduledDate,
time: scheduledTime,
next: nextScheduledTime,
minutesToNext: state.minutes,
};
// If next trigger is in the future trigger the device_schedule_trigger_in flow card.
if (dateTimeNext > dateTimeNow) {
this.homey.flow.getDeviceTriggerCard('device_schedule_trigger_in')
.trigger(device, tokens, state)
.catch(this.error);
}
}
});
}
}
module.exports = AlarmUtils;