forked from anthonywebb/sprinkler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wateringindex.js
363 lines (314 loc) · 10.5 KB
/
wateringindex.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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
// Copyrigth (C) Pascal Martin, 2014.
//
// NAME
//
// wateringindex - a module to access the watering index from the Internet.
//
// SYNOPSYS
//
// This module implements an interface to web sites distributing the
// evapotranspiration-based watering index.
// This index helps adjust the watering duration for all zones (the
// adjustment is calculated as a percentage of the configured duration).
//
// This module queries the web site at a scheduled time.
// The update is asynchronous: it is recommended to refresh the watering
// index information a few minutes before a program is activated.
//
// This module support the following watering index sources:
//
// WATERDEX The watering index from waterdex.com. This uses
// the ZIP code to find the proper index.
//
// MWDSOCAL The watering index from the Metropolitain Water District
// of Southern California. The index is valid for the Los
// Angeles area.
//
// DESCRIPTION
//
// var wateringindex = require('./wateringindex');
//
// wateringindex.configure (config, options);
//
// Initialize the weather module from the user configuration.
// This method can be called as often as necessary (typically
// when the configuration has changed).
//
// wateringindex.refresh ();
//
// Query the web site for updates. The refresh is executed only at
// the scheduled time, or else at 6 hours interval.
//
// wateringindex.status ();
//
// Return the latest status of the weather data service: true if
// an update was successfully completed on the last attempt, false
// otherwise.
//
// wateringindex.updated ();
//
// Return the time of the latest successful weather data update.
//
// wateringindex.enabled ();
//
// Return true if the weather adjustment feature is both enabled
// and data is available.
//
// wateringindex.adjust (duration);
//
// return the weather-adjusted watering duration.
//
// wateringindex.adjustment ();
//
// return the raw weather adjustment ratio (not subject to mix/max
// limits).
//
// wateringindex.source ();
//
// return a name identifying the source of the watering index.
//
// CONFIGURATION
//
// zipcode The local USPS zipcode.
//
// wateringindex The weather module configuration object.
// If missing, the weather module is disabled.
//
// wateringindex.provider Which provider to use to get the watering index
// information. (Optional, default 'waterdex'.)
//
// wateringindex.refresh When to refresh watering information. This is
// an array of times of day (hour[:min]). One cannot
// schedule two refresh within the same hour. The
// minute part is used to control when, within each
// hour, the refresh occurs, This is typically used
// to control if the refresh occurs at the beginning
// or at the end of the hour period.
//
// wateringindex.adjust Parameters for the weather adjustment formula.
// This is a data structure with the following fields:
// min: minimal adjustment (default: 30)
// max: maximal adjustment (default: 150)
//
var http = require('http');
var received = null;
var wateringindexData = null;
var lastUpdate = 0
var updateInterval = 6 * 3600000; // 6 hours in milliseconds.
var refreshSchedule = new Array();;
var enable = false;
var raintrigger = null;
var webRequest = null;
var adjustParameters = new Object();
var wateringProviders = {
waterdex: {
id: 'WATERDEX',
url: 'http://wi.waterdex.com/waterdex/index?zipcode={ZIP}&tmpl=waterdex',
extract: function (text) {
text = text.toLowerCase();
text = text.substring (text.search(/<h4><p>/),
text.search(/<\/p><\/h4>/));
var index = text.substring (text.search(/[0-9]{2,3}%/));
index = index.substring (0, index.search(/%/));
return parseInt(index, 10);
}
},
mwdsocal: {
id: 'MWDSOCAL',
url: 'http://www.mwdh2o.com/RSS/rsswi.xml',
extract: function (text) {
debugLog ('Searching index in '+text);
text = text.toLowerCase();
while (text.search(/<item>/)) {
var item = text.substring (text.search(/<item>/),
text.search(/<\/item>/));
text = text.substring (text.search(/<\/item>/));
debugLog ('Found item '+item);
if (item.search('daily watering index')) {
item = item.substring (item.search(/<description>/),
item.search(/<\/description>/));
item = item.substring (item.search(/[0-9]/));
var index = parseInt(item, 10);
debugLog ('found MWDSOCAL watering index '+index);
return index;
}
}
errorLog ('No daily index found in MWDSOCAL RSS data');
return 100;
}
}
};
var url = null;
var provider = null;
var extractWateringIndex = wateringProviders.waterdex.extract;
var debugLog = function (text) {}
function verboseLog (text) {
console.log ('[DEBUG] WateringIndex: '+text);
}
function errorLog (text) {
console.log ('[ERROR] WateringIndex: '+text);
}
function restoreDefaults () {
url = null;
enable = false;
raintrigger = null;
refreshSchedule = new Array();
provider = 'waterdex';
adjustParameters.min = 30;
adjustParameters.max = 150;
}
restoreDefaults();
exports.configure = function (config, options) {
if (options && options.debug) {
debugLog = verboseLog;
}
var oldprovider = provider;
restoreDefaults();
if (! config.wateringindex) return;
if (config.wateringindex.provider) {
provider = config.wateringindex.provider.toLowerCase();
if (!wateringProviders[provider]) {
errorLog ('invalid provider '+provider+', falling back to default');
restoreDefaults();
}
}
if (oldprovider != provider) {
debugLog ('changing provider from '+oldprovider+' to '+provider);
wateringindexData = null; // Data was from the previous provider.
} else {
debugLog ('same provider '+provider);
}
url = wateringProviders[provider].url.replace ('\{ZIP\}', config.zipcode);
debugLog ('watering index '+provider+' URL: '+url);
extractWateringIndex = wateringProviders[provider].extract;
if (config.wateringindex.refresh) {
for (var i = 0; i < config.wateringindex.refresh.length; i++) {
var option = config.wateringindex.refresh[i].split(':');
if ((option.length > 0) && (option.length <= 2)) {
var j = refreshSchedule.length;
refreshSchedule[j] = new Object();
refreshSchedule[j].hour = option[0] - 0;
if (option.length > 1) {
refreshSchedule[j].minute = option[1] - 0;
} else {
refreshSchedule[j].minute = 0;
}
refreshSchedule[j].armed = true;
}
}
}
if (config.wateringindex.adjust) {
if (config.wateringindex.adjust.min) {
adjustParameters.min = config.wateringindex.adjust.min - 0;
}
if (config.wateringindex.adjust.max) {
adjustParameters.max = config.wateringindex.adjust.max - 0;
}
}
enable = config.wateringindex.enable;
if (wateringindexData) {
// Force a refresh soon, but not immediately (to avoid overloading
// the watering index web site).
// (Do it in 10 minutes.)
lastUpdate = new Date().getTime() - updateInterval + 600000;
} else {
// We have no existing data anyway, ask for data now.
getWateringIndexNow();
}
}
function toBeRefreshed (now) {
var hour = now.getHours();
var minute = now.getMinutes();
var result = false;
for (var i = 0; i < refreshSchedule.length; i++) {
if (refreshSchedule[i].hour == hour) {
if (refreshSchedule[i].armed) {
if (minute >= refreshSchedule[i].minute) {
refreshSchedule[i].armed = false;
result = true; // ---- One scheduled time has come.
}
}
} else {
refreshSchedule[i].armed = true;
}
}
return result;
}
function getWateringIndex () {
if (! enable) return;
var now = new Date();
var time = now.getTime();
// Throttle when to request for information, to avoid being blocked.
// Two options: user-scheduled refresh times, or else periodic.
if (refreshSchedule.length > 0) {
if (toBeRefreshed(now)) {
getWateringIndexNow();
}
} else if (time > lastUpdate + updateInterval) {
getWateringIndexNow();
}
}
function getWateringIndexNow () {
if (! enable) return;
lastUpdate = new Date().getTime();
debugLog ('checking for update..');
received = "";
webRequest = http.request(url, function(res) {
res.on('data', function(d) {
received = received + d.toString();
});
res.on('end', function(d) {
wateringindexData = new Object();
wateringindexData.waterindex = extractWateringIndex (received);
wateringindexData.updated = new Date();
debugLog ('received update');
received = null;
});
});
webRequest.on('error', function(e) {
received = null;
wateringindexData = null;
});
webRequest.end();
webRequest = null;
}
exports.refresh = function () {
if (url) {
getWateringIndex();
}
}
exports.status = function () {
if (wateringindexData) {
return true;
}
return false;
}
exports.updated = function () {
if (wateringindexData) {
return wateringindexData.updated;
}
return {};
}
exports.enabled = function () {
if (wateringindexData) {
return enable;
}
return false;
}
function adjustment () {
if (wateringindexData) {
return wateringindexData.waterindex;
}
return 100;
}
exports.adjust = function (duration) {
if (wateringindexData == null) return duration;
var minadjusted = ((duration * adjustParameters.min) + 50) / 100;
var maxadjusted = ((duration * adjustParameters.max) + 50) / 100;
var adjusted = ((duration * adjustment()) + 50) / 100;
return Math.floor(Math.min(Math.max(minadjusted, adjusted), maxadjusted));
}
exports.adjustment = adjustment;
exports.source = function() {
return wateringProviders[provider].id;
}