-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
121 lines (101 loc) · 3.73 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
'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(
'0 * * * * *', // cronTime: every minute
() => this.onAppInterval(), // onTick
false, // onComplete
true // start
// timeZone: 'Europe/Amsterdam',
);
}
async onAppInterval() {
this.log(`${this.myAppIdVersion} - onAppInterval`);
// Initialize the arrays to store the devices.
let schedulerDevices = [];
let crontimeDevices = [];
// Attempt to get the scheduler driver and its devices
try {
const schedulerDriver = this.homey.drivers.getDriver('scheduler');
schedulerDevices = schedulerDriver.getDevices();
} catch (err) {
this.log('Warning: Failed to get scheduler driver:', err.message);
}
// Attempt to get the crontime driver and its devices
try {
const crontimeDriver = this.homey.drivers.getDriver('crontime');
crontimeDevices = crontimeDriver.getDevices();
} catch (err) {
this.log('Warning: Failed to get crontime driver:', err.message);
}
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;