-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
88 lines (82 loc) · 2.21 KB
/
Copy pathserver.js
File metadata and controls
88 lines (82 loc) · 2.21 KB
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
'use strict'
const path = require('path')
const domapic = require('domapic-service')
const gpioOut = require('gpio-out-domapic')
const {
GPIO,
INITIAL_STATUS,
INVERT,
REMEMBER_LAST_STATUS,
PRESS_TIME
} = require('./lib/statics')
const options = require('./lib/options')
const pluginConfigs = require('./lib/plugins')
domapic.createModule({
packagePath: path.resolve(__dirname),
customConfig: options
}).then(async dmpcModule => {
let pressing = false
const config = await dmpcModule.config.get()
const relay = new gpioOut.Gpio(dmpcModule, {}, {
gpio: GPIO,
initialStatus: INITIAL_STATUS,
invert: INVERT,
rememberLastStatus: REMEMBER_LAST_STATUS
})
await dmpcModule.register({
switch: {
description: 'Handle the relay status',
data: {
type: 'boolean'
},
event: {
description: 'The relay status has changed'
},
state: {
description: 'Current relay status',
handler: () => relay.status
},
action: {
description: 'Switches on/off the relay',
handler: async (newStatus) => {
await relay.setStatus(newStatus)
dmpcModule.events.emit('switch', newStatus)
return newStatus
}
}
},
toggle: {
description: 'Toggles the relay status',
action: {
description: 'Toggles the relay status',
handler: async () => {
const newStatus = await relay.toggle()
dmpcModule.events.emit('switch', newStatus)
return Promise.resolve()
}
}
},
shortPress: {
description: 'Inverts the relay status during a defined period of time',
action: {
description: 'Inverts the relay status briefly',
handler: async () => {
if (pressing) {
return Promise.resolve()
}
pressing = true
const previousStatus = relay.status
await relay.toggle()
setTimeout(() => {
relay.setStatus(previousStatus)
pressing = false
}, config[PRESS_TIME])
return Promise.resolve()
}
}
}
})
await dmpcModule.addPluginConfig(pluginConfigs)
await relay.init()
return dmpcModule.start()
})