uci-utils-scheduler/examples/schedule.js

76 lines
1.6 KiB
JavaScript

import Schedule from '../src/schedule.js'
//
//
function demoStartAction (duration) {
return new Promise(resolve => {
console.log('starting schedule',this.name)
let stopTO
this.once('abort', () => {
console.log('action was aborted')
resolve('action aborted')
clearTimeout(stopTO)
})
console.log('waiting',duration,'secs duration to complete')
stopTO = setTimeout(()=>{
console.log('action for',this.name, 'has ended')
resolve('normal completion')
this.removeAllListeners('abort')
}, duration*1000)
})
}
function demoStopAction () {
console.log('!!!!!forced stop action for', this.name)
this.emit('abort')
}
const DEMO = {
duration: 5,
schedule:{
enabled: true,
simultaneous: false,
timing: {
hour: 6,
minute: 0,
delta: 12
}
}
}
const sch = new Schedule ({
name: 'demo',
settings : DEMO.schedule
})
sch.rxSubscribe('nextTS','TS',function (val) {
console.log('subscription: nextTS was updated', val, sch.nextDT)
})
console.log(sch.get('settings'))
sch.on('updated', function (x) {
console.log('via emiiter: updated', x)
})
// sch.update(true)
console.log('changed minutes to 33')
sch.settings.timing.minute=33
// console.log(sch.get('settings'))
sch.registerStartAction(demoStartAction,DEMO.duration)
sch.registerStopAction(demoStopAction)
sch.startAction() // start now
// show aborting
setTimeout(()=>{ // start again later
console.log('-------now show aborting a schedule run-----')
sch.startAction()
},DEMO.duration*1000)
setTimeout(()=>{
sch.stopAction() // abort later run early
},(DEMO.duration*2-2)*1000)