-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathArduino Alarm Controller Smart App[coolkev@smartthings-alarm].js
68 lines (54 loc) · 2.62 KB
/
Arduino Alarm Controller Smart App[coolkev@smartthings-alarm].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
const { SmartApp } = require('@smartthings/smartapp')
module.exports = new SmartApp()
.enableEventLogging(2)
.configureI18n()
.page('mainPage', (context, page, configData) => {
})
.updated(async (context, updateData) => {
context.api.schedules.runIn('checkHeartbeat', delay);
})
.subscribedEventHandler('zonestatusChanged', (context, event) => {
console.log("zonestatusChanged ${event.value}")
let parts = event.value.split()
let zonetype = parts[0]
if (zonetype == 'heartbeat') {
state.lastHeartbeat = this.now()
console.log("received heartbeat: ${state.lastHeartbeat}")
} else {
let zone = parts[1]
let status = parts[2]
let deviceName = "zone$zone"
let typeSettingName = "typezone$zone"
if (zonetype == 'wireless') {
deviceName = "wirelesszone$zone"
typeSettingName = "wirelesszonetype$zone"
}
console.log("$zonetype zone $zone status=$status")
let device = this.getChildDevice(deviceName)
if (device) {
console.log("$device statusChanged $status")
let zoneType = settings[ typeSettingName ]
if (zoneType == null || zoneType == '') {
zoneType = 'Open/Closed Sensor'
}
let eventName = 'contact'
if (zonetype == 'wireless') {
status = status == '0' ? 'open' : 'closed'
}
if (zoneType == 'Motion Detector') {
eventName = 'motion'
status = status == 'open' ? 'active' : 'inactive'
}
device.sendEvent(['name': eventName , 'value': status , 'isStateChange': true])
} else {
console.log("couldn't find device for zone $zone")
}
}
})
.scheduledEventHandler('checkHeartbeat', (context, event) => {
let elapsed = this.now() - state.lastHeartbeat
console.log("checkHeartbeat elapsed: $elapsed")
if (elapsed > 30000) {
this.sendPush('Arduino Alarm appears to be offline - haven\'t received a heartbeat in over 5 minutes')
}
})