-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.js
353 lines (303 loc) · 13.2 KB
/
index.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
let Service, Characteristic
const packageJson = require('./package.json')
const schedule = require('node-schedule')
const request = require('request')
const ip = require('ip')
const http = require('http')
module.exports = function (homebridge) {
Service = homebridge.hap.Service
Characteristic = homebridge.hap.Characteristic
homebridge.registerAccessory('homebridge-web-sprinklers', 'WebSprinklers', WebSprinklers)
}
function WebSprinklers (log, config) {
this.log = log
this.name = config.name
this.apiroute = config.apiroute
this.zones = config.zones || 6
this.pollInterval = config.pollInterval || 300
this.listener = config.listener || false
this.port = config.port || 2000
this.requestArray = ['state']
this.disableScheduling = config.disableScheduling || false
this.disableAdaptiveWatering = config.disableAdaptiveWatering || false
this.latitude = config.latitude
this.longitude = config.longitude
this.key = config.key
this.restrictedDays = config.restrictedDays || []
this.restrictedMonths = config.restrictedMonths || []
this.sunriseOffset = config.sunriseOffset || 0
this.lowThreshold = config.lowThreshold || 10
this.highThreshold = config.highThreshold || 20
this.rainThreshold = config.rainThreshold || 2.3
this.defaultDuration = config.defaultDuration || 20
this.maxDuration = config.maxDuration || 30
this.cycles = config.cycles || 2
this.zonePercentages = config.zonePercentages || new Array(this.zones).fill(100)
this.valveAccessory = []
this.zoneDuration = []
this.manufacturer = config.manufacturer || packageJson.author
this.serial = config.serial || this.apiroute
this.model = config.model || packageJson.name
this.firmware = config.firmware || packageJson.version
this.username = config.username || null
this.password = config.password || null
this.timeout = config.timeout || 3000
this.http_method = config.http_method || 'GET'
if (this.username != null && this.password != null) {
this.auth = {
user: this.username,
pass: this.password
}
}
if (this.listener) {
this.server = http.createServer(function (request, response) {
const baseURL = 'http://' + request.headers.host + '/'
const url = new URL(request.url, baseURL)
if (this.requestArray.includes(url.pathname.substr(1))) {
try {
this.log.debug('Handling request')
response.end('Handling request')
this._httpHandler(url.searchParams.get('zone'), url.pathname.substr(1), url.searchParams.get('value'))
} catch (e) {
this.log.warn('Error parsing request: %s', e.message)
}
} else {
this.log.warn('Invalid request: %s', request.url)
response.end('Invalid request')
}
}.bind(this))
this.server.listen(this.port, function () {
this.log('Listen server: http://%s:%s', ip.address(), this.port)
}.bind(this))
}
this.service = new Service.IrrigationSystem(this.name)
}
WebSprinklers.prototype = {
identify: function (callback) {
this.log('Identify requested!')
callback()
},
_httpRequest: function (url, body, method, callback) {
request({
url: url,
body: body,
method: this.http_method,
timeout: this.timeout,
rejectUnauthorized: false,
auth: this.auth
},
function (error, response, body) {
callback(error, response, body)
})
},
_getStatus: function (callback) {
const url = this.apiroute + '/status'
this.log.debug('Getting status: %s', url)
this._httpRequest(url, '', 'GET', function (error, response, responseBody) {
if (error) {
this.log.warn('Error getting status: %s', error.message)
this.service.getCharacteristic(Characteristic.Active).updateValue(new Error('Polling failed'))
callback(error)
} else {
this.service.getCharacteristic(Characteristic.Active).updateValue(1)
this.log.debug('Device response: %s', responseBody)
try {
const json = JSON.parse(responseBody)
for (let zone = 1; zone <= this.zones; zone++) {
const value = json[zone - 1].state
this.log.debug('Zone %s | Updated state to: %s', zone, value)
this.valveAccessory[zone].getCharacteristic(Characteristic.Active).updateValue(value)
this.valveAccessory[zone].getCharacteristic(Characteristic.InUse).updateValue(value)
}
callback()
} catch (e) {
this.log.warn('Error parsing status: %s', e.message)
}
}
}.bind(this))
},
_httpHandler: function (zone, characteristic, value) {
switch (characteristic) {
case 'state': {
this.valveAccessory[zone].getCharacteristic(Characteristic.Active).updateValue(value)
this.valveAccessory[zone].getCharacteristic(Characteristic.InUse).updateValue(value)
this.log('Zone %s | Updated %s to: %s', zone, characteristic, value)
break
}
default: {
this.log.warn('Zone %s | Unknown characteristic "%s" with value "%s"', zone, characteristic, value)
}
}
},
_calculateSchedule: function (callback) {
const url = 'https://api.openweathermap.org/data/3.0/onecall?lat=' + this.latitude + '&lon=' + this.longitude + '&exclude=current,hourly&units=metric&appid=' + this.key
this.log.debug('Retrieving weather data: %s', url)
this._httpRequest(url, '', this.http_method, function (error, response, responseBody) {
if (error) {
this.log.warn('Error getting weather data: %s', error)
setTimeout(() => {
this._calculateSchedule(function () {})
}, 60000)
callback(error)
} else {
this.log.debug('Weather data: %s', responseBody)
let json
try {
json = JSON.parse(responseBody)
} catch (error) {
setTimeout(() => {
this._calculateSchedule(function () {})
}, 60000)
return this.log.error('Error parsing weather data: %s', error)
}
const today = {}
today.summary = json.daily[0].weather[0].description
today.sunrise = new Date(json.daily[0].sunrise * 1000)
today.min = json.daily[0].temp.min
today.max = json.daily[0].temp.max
today.rain = ('rain' in json.daily[0]) ? json.daily[0].rain : 0
today.clouds = json.daily[0].clouds
const tomorrow = {}
tomorrow.summary = json.daily[1].weather[0].description
tomorrow.sunrise = new Date(json.daily[1].sunrise * 1000)
tomorrow.min = json.daily[1].temp.min
tomorrow.max = json.daily[1].temp.max
tomorrow.rain = ('rain' in json.daily[1]) ? json.daily[1].rain : 0
tomorrow.clouds = json.daily[1].clouds
this.log('----------------------------------------------')
this.log('Today summary: %s', today.summary)
this.log('Today sunrise: %s', today.sunrise.toLocaleString())
this.log('Today min temp: %s °C', today.min)
this.log('Today max temp: %s °C', today.max)
this.log('Today rain: %s mm', today.rain)
this.log('Today cloud cover: %s %', today.clouds)
this.log('----------------------------------------------')
this.log('Tomorrow summary: %s', tomorrow.summary)
this.log('Tomorrow sunrise: %s', tomorrow.sunrise.toLocaleString())
this.log('Tomorrow min temp: %s °C', tomorrow.min)
this.log('Tomorrow max temp: %s °C', tomorrow.max)
this.log('Tomorrow rain: %s mm', tomorrow.rain)
this.log('Tomorrow cloud cover: %s %', tomorrow.clouds)
this.log('----------------------------------------------')
let maximumTotal
if (this.disableAdaptiveWatering) {
maximumTotal = this.zones * this.defaultDuration
} else {
maximumTotal = this.zones * this.maxDuration
}
const earliestToday = new Date(today.sunrise.getTime() - (maximumTotal + this.sunriseOffset) * 60000)
let waterDay
if (earliestToday.getTime() > Date.now()) {
waterDay = today
} else {
waterDay = tomorrow
}
if (!this.restrictedDays.includes(waterDay.sunrise.getDay()) && !this.restrictedMonths.includes(waterDay.sunrise.getMonth()) && today.rain < this.rainThreshold && tomorrow.rain < this.rainThreshold && waterDay.min > this.lowThreshold && waterDay.max > this.highThreshold) {
let zoneMaxDuration = this.defaultDuration
if (!this.disableAdaptiveWatering) {
const highDiff = waterDay.max - this.highThreshold
const lowDiff = this.highThreshold - waterDay.min
const cloudPercentage = 100 - (waterDay.clouds / 3)
zoneMaxDuration = (((this.defaultDuration + (highDiff - lowDiff)) / 100) * cloudPercentage) - waterDay.rain
if (zoneMaxDuration > this.maxDuration) {
zoneMaxDuration = this.maxDuration
}
}
for (let zone = 1; zone <= this.zones; zone++) {
this.zoneDuration[zone] = ((zoneMaxDuration / this.cycles) / 100) * this.zonePercentages[zone - 1]
}
const totalTime = this.zoneDuration.reduce((a, b) => a + b, 0) * this.cycles
const startTime = new Date(waterDay.sunrise.getTime() - (totalTime + this.sunriseOffset) * 60000)
const finishTime = new Date(startTime.getTime() + totalTime * 60000)
this.log('Watering starts: %s', startTime.toLocaleString())
this.log('Watering finishes: %s', finishTime.toLocaleString())
this.log('Total watering time: %s minutes', Math.round(totalTime))
this.log('Zone max duration: %s minutes', Math.round(zoneMaxDuration))
this.log('----------------------------------------------')
for (let zone = 1; zone <= this.zones; zone++) {
this.log('Zone %s | %sx %s minute cycles', zone, this.cycles, Math.round(this.zoneDuration[zone]))
}
schedule.scheduleJob(startTime, function () {
this.log('Starting water cycle 1/%s', this.cycles)
this._wateringCycle(1, 1)
}.bind(this))
this.service.getCharacteristic(Characteristic.ProgramMode).updateValue(1)
} else {
this.log('No schedule set, recalculation: %s', waterDay.sunrise.toLocaleString())
this.service.getCharacteristic(Characteristic.ProgramMode).updateValue(0)
schedule.scheduleJob(waterDay.sunrise, function () {
this._calculateSchedule(function () {})
}.bind(this))
}
this.log('----------------------------------------------')
callback()
}
}.bind(this))
},
_wateringCycle: function (zone, cycle) {
this.valveAccessory[zone].setCharacteristic(Characteristic.Active, 1)
setTimeout(() => {
this.valveAccessory[zone].setCharacteristic(Characteristic.Active, 0)
const nextZone = zone + 1
if (nextZone <= this.zones) {
this._wateringCycle(nextZone, cycle)
} else {
const nextCycle = cycle + 1
if (nextCycle <= this.cycles) {
this._wateringCycle(1, nextCycle)
this.log('Starting watering cycle %s/%s', nextCycle, this.cycles)
} else {
this.log('Watering finished')
this._calculateSchedule(function () {})
}
}
}, this.zoneDuration[zone] * 60000)
},
setActive: function (zone, value, callback) {
const url = this.apiroute + '/setState?zone=' + zone + '&value=' + value
this.log.debug('Zone %s | Setting state: %s', zone, url)
this._httpRequest(url, '', this.http_method, function (error, response, responseBody) {
if (error) {
this.log.warn('Zone %s | Error setting state: %s', zone, error.message)
callback(error)
} else {
this.log('Zone %s | Set state to %s', zone, value)
this.valveAccessory[zone].getCharacteristic(Characteristic.InUse).updateValue(value)
callback()
}
}.bind(this))
},
getServices: function () {
this.service.getCharacteristic(Characteristic.ProgramMode).updateValue(0)
this.service.getCharacteristic(Characteristic.Active).updateValue(1)
this.service.getCharacteristic(Characteristic.InUse).updateValue(0)
this.informationService = new Service.AccessoryInformation()
this.informationService
.setCharacteristic(Characteristic.Manufacturer, this.manufacturer)
.setCharacteristic(Characteristic.Model, this.model)
.setCharacteristic(Characteristic.SerialNumber, this.serial)
.setCharacteristic(Characteristic.FirmwareRevision, this.firmware)
const services = [this.informationService, this.service]
for (let zone = 1; zone <= this.zones; zone++) {
const accessory = new Service.Valve('Zone', zone)
accessory
.setCharacteristic(Characteristic.ServiceLabelIndex, zone)
.setCharacteristic(Characteristic.ValveType, 1)
accessory
.getCharacteristic(Characteristic.Active)
.on('set', this.setActive.bind(this, zone))
this.valveAccessory[zone] = accessory
this.service.addLinkedService(accessory)
services.push(accessory)
}
this.log('Initialized %s zones', this.zones)
if (!this.disableScheduling) {
this._calculateSchedule(function () {})
}
this._getStatus(function () {})
setInterval(function () {
this._getStatus(function () {})
}.bind(this), this.pollInterval * 1000)
return services
}
}