-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
132 lines (120 loc) · 3.31 KB
/
server.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
122
123
124
125
126
127
128
129
130
131
132
'use strict'
const path = require('path')
const domapic = require('domapic-service')
const gpioIn = require('gpio-in-domapic')
const gpioOut = require('gpio-out-domapic')
const pluginConfigs = require('./lib/plugins.json')
const options = require('./lib/options')
const {
ABILITY_NAME,
RELAYS_ABILITY_NAME,
WAYS_OPTION,
RELAY_GPIO_1_OPTION,
RELAY_GPIO_2_OPTION,
SENSOR_GPIO_OPTION,
DEBOUNCE_OPTION,
REVERSE_STATE_OPTION,
INVERT_RELAYS_OPTION
} = require('./lib/statics')
domapic.createModule({
packagePath: path.resolve(__dirname),
customConfig: options
}).then(async dmpcModule => {
const config = await dmpcModule.config.get()
const reverse = config[REVERSE_STATE_OPTION]
const ways = config[WAYS_OPTION]
const statusSensor = new gpioIn.Gpio(dmpcModule, {
}, {
gpio: SENSOR_GPIO_OPTION,
debounceTimeout: DEBOUNCE_OPTION
})
const relay1 = new gpioOut.Gpio(dmpcModule, {
initialStatus: true,
rememberLastStatus: true
}, {
gpio: RELAY_GPIO_1_OPTION,
invert: INVERT_RELAYS_OPTION
})
let relay2 = null
if (ways === 4) {
if (!config[RELAY_GPIO_2_OPTION]) {
throw new Error('Please define a gpio for second relay')
}
relay2 = new gpioOut.Gpio(dmpcModule, {
initialStatus: true,
rememberLastStatus: true
}, {
gpio: RELAY_GPIO_2_OPTION,
invert: INVERT_RELAYS_OPTION
})
}
const statusSensorValue = value => reverse ? !value : value
const toggleSwitch = async () => {
const targetStatus = !relay1.status
await relay1.setStatus(targetStatus)
if (relay2) {
await relay2.setStatus(targetStatus)
}
dmpcModule.events.emit(RELAYS_ABILITY_NAME, targetStatus)
}
await dmpcModule.register({
[ABILITY_NAME]: {
description: 'Switch with status sensor',
data: {
type: 'boolean'
},
action: {
description: 'Change switch position to make sensor match with desired status',
handler: async newState => {
if (statusSensorValue(statusSensor.status) !== newState) {
await toggleSwitch()
}
return newState
}
},
state: {
description: 'Sensor status',
handler: () => statusSensorValue(statusSensor.status)
},
event: {
description: 'Sensor has changed status'
}
},
[RELAYS_ABILITY_NAME]: {
description: 'Relays status',
data: {
type: 'boolean'
},
action: {
description: 'Switch relays',
handler: async newState => {
await relay1.setStatus(newState)
if (relay2) {
await relay2.setStatus(newState)
}
dmpcModule.events.emit(RELAYS_ABILITY_NAME, newState)
return newState
}
},
state: {
description: 'Current relays status',
handler: () => relay1.status
},
event: {
description: 'Relays have changed status'
}
}
})
await dmpcModule.addPluginConfig(pluginConfigs)
await statusSensor.init()
await relay1.init()
if (relay2) {
await relay2.init()
}
statusSensor.events.on(gpioIn.Gpio.eventNames.CHANGE, newValue => {
const value = statusSensorValue(newValue)
dmpcModule.tracer.debug('Sensor has changed status', value)
dmpcModule.events.emit(ABILITY_NAME, value)
})
return dmpcModule.start()
})