109 lines
3 KiB
JavaScript
109 lines
3 KiB
JavaScript
|
import Schedule from '../src/schedule.js'
|
||
|
import Runner from '../src/runner.js'
|
||
|
|
||
|
import delay from 'delay'
|
||
|
|
||
|
const HOUR = 0
|
||
|
const MINUTE = 0
|
||
|
const DELTA = 5
|
||
|
const DURATION = 4
|
||
|
const DEV = true // if true delta and duration are seconds
|
||
|
const NZONES = 1
|
||
|
|
||
|
|
||
|
// MUST be promise returning and NOT an arrow function
|
||
|
function startPromise (zone,activeSch) {
|
||
|
console.log('promise return start function', this.evName)
|
||
|
return new Promise(resolve => {
|
||
|
console.log('starting>>>',zone.id)
|
||
|
console.log('---waiting-----', zone.duration,'secs duration to complete')
|
||
|
const tick = setInterval(()=>{
|
||
|
console.log('duration tick')
|
||
|
}, 1000)
|
||
|
this.once('abort:'+activeSch.runID,()=> {
|
||
|
clearTimeout(run)
|
||
|
console.log('aborting run>>>', activeSch.runID)
|
||
|
})
|
||
|
const run = setTimeout(()=>{
|
||
|
console.log('stopping>>>',activeSch.runID)
|
||
|
clearInterval(tick)
|
||
|
this.removeAllListeners('abort:'+activeSch.runID)
|
||
|
resolve()
|
||
|
}, zone.duration*1000)
|
||
|
})
|
||
|
}
|
||
|
|
||
|
function stop (zone,activeSch) {
|
||
|
console.log('aborting run for action id',activeSch.runID, 'zone',zone.id)
|
||
|
this.emit('abort:'+activeSch.runID)
|
||
|
}
|
||
|
|
||
|
async function startAsync (zone,activeSch) {
|
||
|
// console.log(arguments)
|
||
|
console.log('async start function')
|
||
|
console.log('-----starting-------',zone.id, activeSch.runID)
|
||
|
console.log('---waiting-----', zone.duration,'secs duration to complete')
|
||
|
const tick = setInterval(()=>{
|
||
|
console.log('duration tick')
|
||
|
}, 1000)
|
||
|
const duration = delay(zone.duration*1000)
|
||
|
this.once('abort:'+activeSch.runID,()=> {
|
||
|
console.log('aborting run>>>', activeSch.runID)
|
||
|
duration.clear()
|
||
|
})
|
||
|
await duration
|
||
|
clearInterval(tick)
|
||
|
this.removeAllListeners('abort:'+activeSch.runID)
|
||
|
console.log('stopping', activeSch.runID)
|
||
|
}
|
||
|
|
||
|
|
||
|
(async () => {
|
||
|
|
||
|
let zones = []
|
||
|
let schs = []
|
||
|
for (let zone=0; zone < NZONES; zone++) {
|
||
|
let opts = {dev:DEV, simultaneous:(zone % 2), name:'sch-zone-'+(zone+1),id:'zone-'+(zone+1) ,hour:HOUR,minute:MINUTE,delta:DELTA, duration:DURATION}
|
||
|
zones[zone] = opts
|
||
|
console.log(zones[zone].id,'>hr:min:delta:duration:simultaneous=',
|
||
|
zones[zone].hour,
|
||
|
zones[zone].minute,
|
||
|
zones[zone].delta,
|
||
|
zones[zone].duration,
|
||
|
zones[zone].simultaneous)
|
||
|
|
||
|
schs[zone] = new Schedule (opts)
|
||
|
schs[zone].on('update',val => {
|
||
|
console.log('just updated schedule')
|
||
|
console.dir(val)
|
||
|
})
|
||
|
schs[zone].update()
|
||
|
|
||
|
console.log('register action !!!!!!!!!!!!!!!!!!!!!')
|
||
|
schs[zone].registerStartAction(startAsync,zones[zone])
|
||
|
schs[zone].registerStopAction(stop,zones[zone])
|
||
|
console.log('register action done!!!!!!!!!!!!!!!!!!!!!')
|
||
|
|
||
|
}
|
||
|
|
||
|
const runner = new Runner ({
|
||
|
name: 'irrigation',
|
||
|
// one: true,
|
||
|
schedules: schs,
|
||
|
})
|
||
|
|
||
|
runner.start()
|
||
|
const cd = setInterval(()=>{
|
||
|
console.log('runner, next schedule trigger in', runner.countdown)
|
||
|
},1000)
|
||
|
setTimeout(() => {
|
||
|
runner.stop(true)
|
||
|
clearInterval(cd)},
|
||
|
15*1000
|
||
|
)
|
||
|
|
||
|
})().catch(err => {
|
||
|
console.error('FATAL: UNABLE TO START SYSTEM!\n',err)
|
||
|
// process.kill(process.pid, 'SIGTERM')
|
||
|
})
|