uci-utils-scheduler/src/schedule.js

113 lines
3.7 KiB
JavaScript

// import to from 'await-to-js'
import { EventEmitter } from 'events'
import moment from 'moment'
import 'moment-duration-format'
// Single Device Scheduler User instance for each unique action
// see Schedulers to create a group with "collision protection"
class UCISchedule extends EventEmitter {
constructor(opts) {
super(opts)
this.name = opts.name || 'test'
this.id = opts.id || this.name.replace(/ /g, '_')
this.desc = opts.desc
this.hour = opts.hour || 0
this.dev = opts.dev
this.minute = opts.minute || 0
this.delta = opts.delta || 6 // time to next trigger in hours
this.duration = opts.duration || 10 // in minutes
// computed values
this.nextTS = 0 // the next trigger time in seconds
this.enabled = opts.enabled || true
this.active = false
// this.active = false // if schedule is currently active
// this.on('active',active=>this.active=active)
this.simultaneous = opts.simulanteous // if true delay this scheduled event until current scheduled event completes
// this.lastActiveTS
this._startAction = opts.startAction || defaultStartAction.bind(this) // single (or array) of function(s)
this._stopAction = opts.stopAction || defaultStopAction.bind(this) // single (or array) of function(s)
this.update()
}
get countdown() {
return moment.duration(parseInt((this.nextTS - Math.floor(Date.now()/1000))),'seconds').format('hh:mm:ss')
}
// get countdownActive() {
// if (this.active) return moment.duration(parseInt((this.duration*60 + this.nextTS - Math.floor(Date.now()/1000))),'seconds').format('hh:mm:ss')
//
// }
get countdownMS() {
return (this.nextTS*1000 - Date.now())
}
get durationMS() {
return (this.duration*(this.dev ? 1 :60)*1000)
}
get nextDT() { return moment(this.nextTS*1000).format('ddd, MMM Do h:mm A') }
// get activeDuration() {
// return moment.duration((Math.floor(Date.now()/1000) - this.nextTS),'seconds').format('hh:mm:ss')
// }
// get activeRemaining() {
// return moment.duration((this.nextTS - Math.floor(Date.now()/1000) + this.duration * 60),'seconds').format('hh:mm:ss')
// }
update() { // all TS in seconds
// console.log('updating',this.hour,this.minute,this.delta, this.dev)
let baseTS = this.hour*3600+this.minute*60
let dt = new Date()
let intoDayTS = (dt.getSeconds() + (60 * dt.getMinutes()) + (60 * 60 * dt.getHours()))
let nowTS = Math.floor(Date.now()/1000)
this.nextTS = nowTS - intoDayTS + baseTS
if (!this.dev) {
while (nowTS > this.nextTS) {
// console.log(`now ${nowTS} is beyond next ${this.nextTS} adding delta ${this.delta} hours`)
this.nextTS += this.delta * 3600
}
} else this.nextTS = nowTS + this.delta
// console.log(baseTS,intoDayTS,nowTS, nowTS-intoDayTS,this.nextTS)
this.emit('update',{id:this.id, ts:this.nextTS,dt:this.nextDT,countdown:this.countdown,simultaneous:this.simultaneous})
}
registerStartAction(func) {
//
// if (!Array.isArray(func)) func = [func]
this._startAction = func
}
registerStopAction(func) {
// if (!Array.isArray(func)) func = [func]
this._stopAction = func
}
// todo support a array of functions
async startAction (data) {
this.active = true
this.on('active',true)
this._startAction(data)
}
async stopAction (data) {
this.active = false
this.on('active',false)
this._stopAction(data)
}
}
function defaultStartAction () {
console.log('start action for', this.name,moment(Date.now()).format('ddd, MMM Do h:mm:ss A'))
}
function defaultStopAction () {
console.log('stop action for', this.name,moment(Date.now()).format('ddd, MMM Do h:mm:ss A'))
}
export default UCISchedule
export {UCISchedule as Schedule}