forked from runely/jslogic-homey
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
94 lines (79 loc) · 3.13 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
'use strict'
const Homey = require('homey')
const moment = require('./lib/moment-datetime')
const getNextTimeout = require('./lib/get-next-timeout-ms')
const { flow: { actions, conditions, triggers } } = Homey.manifest
const tokens = []
class JSLogic extends Homey.App {
async onInit () {
this.log(`${Homey.manifest.name.en} v${Homey.manifest.version} is running on ${this.homey.version}...`)
// flow tokens
tokens.push(await this.homey.flow.createToken('formatted_date', { type: 'string', title: this.homey.__('flowTokens.formatted_date') }))
// timezone
const timezone = this.homey.clock.getTimezone()
// register action runlisteners
actions.forEach(({ id }) => {
this.log('Adding runListener for action', id)
this.homey.flow.getActionCard(id)
.registerRunListener(async (args, state) => {
const action = require(`./handlers/actions/${id}`)
const result = await action(timezone, tokens, args)
return result
})
})
// register condition runlisteners
conditions.forEach(({ id }) => {
this.log('Adding runListener for condition', id)
this.homey.flow.getConditionCard(id)
.registerRunListener(async (args, state) => {
const condition = require(`./handlers/conditions/${id}`)
const result = await condition({
timezone,
args,
app: this.homey
})
return result
})
})
// register trigger runlisteners
triggers.forEach(({ id }) => {
this.log('Adding runlistener for trigger', id)
this.homey.flow.getTriggerCard(id)
.registerRunListener((args, state) => {
const trigger = require(`./handlers/triggers/${id}`)
const result = trigger({
args,
state,
app: this.homey
})
return result
})
})
this.homey.on('unload', () => {
if (!this.timeouts) return
Object.getOwnPropertyNames(this.timeouts).forEach(timeout => {
try {
this.homey.clearTimeout(timeout)
} catch {}
})
})
// registers a timeout to trigger the "date_month_becomes" card at 00:00 every night
const dateMonthBecomes = () => {
const now = moment({ timezone })
const nextTimeout = getNextTimeout(timezone)
this.log('dateMonthBecomes: Triggering "date_month_becomes" card')
this.homey.flow.getTriggerCard('date_month_becomes').trigger(null, { date: now.get('date'), month: now.get('month') })
try {
this.homey.clearTimeout(this.timeouts.dateMonthBecomes)
} catch {}
this.timeouts.dateMonthBecomes = this.homey.setTimeout(dateMonthBecomes, nextTimeout)
this.log(`dateMonthBecomes: Next timeout ${moment({ timezone }).add(nextTimeout, 'milliseconds').format('DD.MM.YY HH:mm:ss')}`)
}
const nextTimeout = getNextTimeout(timezone)
this.timeouts = {
dateMonthBecomes: this.homey.setTimeout(dateMonthBecomes, nextTimeout)
}
this.log(`onInit/dateMonthBecomes: Next timeout ${moment({ timezone }).add(nextTimeout, 'milliseconds').format('DD.MM.YY HH:mm:ss')}`)
}
}
module.exports = JSLogic