forked from anthonywebb/sprinkler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhardware-null.js
145 lines (132 loc) · 4.25 KB
/
hardware-null.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
// Copyrigth (C) Pascal Martin, 2014.
//
// NAME
//
// hardware - a module to hide the absence of hardware interface.
//
// SYNOPSYS
//
// This module implements an interface to the board that controls
// the sprinkler (typically triacs or relays that control the solenoids).
//
// 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 module implements a null driver, used for debugging purposes.
//
// To enable this driver, create 'hardware.js' as a symbolic link to
// 'hardware-null.js'.
//
//
// 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.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
//
// zones An array of structures containing one item named
// 'name' (the name of the zone).
//
var piodb = null;
var debugLog = function (text) {}
function verboseLog (text) {
console.log ('[DEBUG] Hardware(null): '+text);
}
exports.configure = function (config, user, options) {
if (options && options.debug) {
debugLog = verboseLog;
}
piodb = new Object();
var zonecount = user.zones.length;
piodb.zones = new Array();
for(var i = 0; i < zonecount; i++) {
piodb.zones[i] = new Object();
piodb.zones[i].name = user.zones[i].name;
debugLog ('configuring zone '+piodb.zones[i].name+' (#'+i+')');
}
}
exports.info = function (attribute) {
return {id:"null",title:"Null Debug Driver",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) {
return null;
}
if (on) {
debugLog ('zone '+piodb.zones[zone].name+' (#'+zone+') set to on');
} else {
debugLog ('zone '+piodb.zones[zone].name+' (#'+zone+') set to off');
}
}
exports.apply = function () {
debugLog ('apply');
}