-
Notifications
You must be signed in to change notification settings - Fork 5
/
hardware-relays.js
258 lines (236 loc) · 7.85 KB
/
hardware-relays.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
// Copyrigth (C) Pascal Martin, 2014.
//
// NAME
//
// hardware - a module to hide the interface to generic relay boards.
//
// SYNOPSYS
//
// This module implements an interface to generic relay boards used as
// sprinkler controllers.
//
// Each sprinkler triac or relay is called a "zone" (because it generally
// controls a watering valve, which waters a zone).
//
// This module allows porting the sprinkler software to different
// hardware interfaces. Only one hardware interface is supported at
// a given time: you must have installed the right driver.
//
// This specific implementation supports generic relay boards.
//
// To enable this driver, create 'hardware.js' as a symbolic link to
// 'hardware-relays.js'.
//
// This module depends on 'onoff' because that is one gpio interface
// that is available on BeagleBone and Raspberry Pi (and probably others,
// since it only relies on /sys/class/gpio).
//
// This module do some tricks to workaround a Raspbian issue: access to
// the gpio files is only granted after a short while, in the background.
// The application needs to try again if it failed.
//
// DESCRIPTION
//
// var hardware = require('./hardware');
//
// hardware.configure (hardwareConfig, userConfig, options);
//
// Initialize the hardware module from the configuration.
// This method can be called as often as necessary (typically
// when the user configuration has changed).
//
// hardware.info ();
//
// Return a data structure that describes the hardware managed by
// this driver. The data structure contains the following elements:
// id A short unique identification string for the driver.
// title A human-readable string that describes the hardware.
// zones.add If true, the end-user may add (or remove) zones.
// zones.pin If true, the end-user may set the pin name and active
// state ('on' state).
// zones.max If set, defines the maximum number of zones supported
// by the hardware. If zones.max is defined and zones.add
// is set to false, then the number of zones is fixed.
//
// hardware.userDefined (attribute);
//
// Return true when the user may change the given attribute.
// The supported attributes are:
// "zones" The number of zones.
// "zones.pin" The I/O pin, and the active pin level, for each zone.
//
// hardware.get (attribute);
//
// Return the current value of the given attribute.
// The supported attributes are:
// "zones" The maximum number of zones. (Used only if not user
// defined).
//
// hardware.setZone (zone, on);
//
// Set one zone on (on == true) or off (on == false).
// This may take effect immediately, or only the next time
// function hardware.apply() is called. Each zone is identified
// by a number (identifying zones by name is the responsibility
// of the application layer).
//
// hardware.apply ();
//
// Push the current zone controls to the outside world.
//
// hardware.rainSensor ();
//
// Return true or false, true if rain is detected. Always return
// false if there is no rain sensor.
//
// hardware.button ();
//
// Return true or false, true if button is pressed. Always return
// false if there is no button.
//
// hardware.rainInterrupt (callback);
// hardware.buttonInterrupt (callback);
//
// Set each callback to be called when the corresponding input
// has changed. The parameter to the callback is a Javascript
// structure guaranteed to contain an (oddly named) "output"
// item that contains the value of the input pin.
//
// HARDWARE CONFIGURATION
//
// (None.)
//
// USER CONFIGURATION
//
// production This flag determines if we use the real hardware
// (true) or else a simulation for debug (false).
//
// zones This array of structures defines the name of
// the output pin for each zone.
//
var debugLog = function (text) {}
function verboseLog (text) {
console.log ('[DEBUG] Hardware(relays): '+text);
}
function errorLog (text) {
console.error ('[ERROR] Hardware(relays): '+text);
}
try {
var gpio = require('onoff').Gpio;
}
catch (err) {
errorLog ('cannot access module onoff');
var gpio = null;
}
var piodb = new Object(); // Make sure it exists (simplify validation).
// Raspbian issue: access to the gpio files is only granted
// after a short while, in the background.
// Need to try again if it failed.
function retry(i) {
debugLog ('Setting up zone '+i+' failed, scheduling retry in 0.2 second');
setTimeout (function() {
debugLog ('Retrying zone '+i);
try {
piodb.zones[i].gpio = new gpio(piodb.zones[i].pin, 'out');
piodb.zones[i].ready = true; // No error was raised this time.
exports.setZone(i, piodb.zones[i].value);
}
catch (err) {
retry(i);
}
}, 200);
}
exports.configure = function (config, user, options) {
if (options && options.debug) {
debugLog = verboseLog;
}
if ((! gpio) || (! user.production)) {
debugLog ('using debug GPIO traces');
gpio = null;
}
// Set hardware configuration defaults.
piodb = new Object();
piodb.zones = new Array();
var zonecount = 0;
if (user.zones) {
var zonecount = user.zones.length;
for (var i = 0; i < zonecount; i++) {
piodb.zones[i] = new Object();
piodb.zones[i].on = 0;
piodb.zones[i].off = 1;
if (user.zones[i].on) {
if (user.zones[i].on == 'HIGH') {
piodb.zones[i].on = 1;
piodb.zones[i].off = 0;
} else if (user.zones[i].on == 'LOW') {
piodb.zones[i].on = 0;
piodb.zones[i].off = 1;
} else {
errorLog ('invalid pin level '+user.zones[i].on+', assuming LOW');
piodb.zones[i].on = 0;
piodb.zones[i].off = 1;
}
}
piodb.zones[i].ready = false;
piodb.zones[i].value = false;
piodb.zones[i].pin = user.zones[i].pin;
if (gpio) {
// Raspbian bug: access to the gpio files is only granted
// after a short while, in the background.
// Need to try again if it failed.
try {
piodb.zones[i].gpio = new gpio(piodb.zones[i].pin, 'out');
piodb.zones[i].ready = true; // No error was raised.
exports.setZone(i, piodb.zones[i].value);
}
catch (err) {
retry(i);
}
} else {
piodb.zones[i].ready = true;
}
}
}
}
exports.info = function (attribute) {
return {id:"relays",title:"Generic Relay Board",zones:{add:true,pin:true}};
}
exports.rainSensor = function () {
return false;
}
exports.button = function () {
return false;
}
exports.rainInterrupt = function (callback) {
return null;
}
exports.buttonInterrupt = function (callback) {
return null;
}
exports.setZone = function (zone, on) {
if (! piodb.zones) {
return null;
}
if (! piodb.zones[zone].pin) {
return null;
}
piodb.zones[zone].value = on;
if (! piodb.zones[zone].ready) {
return null; // This pin will be set later, when ready.
}
debugLog ('GPIO '+piodb.zones[zone].pin+' set to '+on);
if (gpio) {
if (on) {
piodb.zones[zone].gpio.writeSync(piodb.zones[zone].on);
} else {
piodb.zones[zone].gpio.writeSync(piodb.zones[zone].off);
}
} else {
if (on) {
debugLog ('GPIO '+piodb.zones[zone].pin+' set to on ('+piodb.zones[zone].on+')');
} else {
debugLog ('GPIO '+piodb.zones[zone].pin+' set to off ('+piodb.zones[zone].off+')');
}
}
}
exports.apply = function () { }