intval3/src/sequence/index.ts

116 lines
2.2 KiB
TypeScript
Raw Normal View History

'use strict'
2019-10-16 14:58:46 +00:00
import uuid from 'uuid/v4';
const log = require('../log')('seq');
2019-10-31 02:32:52 +00:00
import { delay } from '../delay';
2019-10-16 14:58:46 +00:00
const MAX_INTEGER = 2147483647;
interface Options {
len? : number;
2019-10-31 02:32:52 +00:00
multiple? : number;
delay? : number;
2019-10-16 14:58:46 +00:00
}
2019-10-31 02:32:52 +00:00
interface Loop {
count : number;
max : number;
delay : number;
}
2019-10-16 14:58:46 +00:00
2019-10-31 02:32:52 +00:00
/** Object sequence features */
export class Sequence {
private id : string
2019-10-16 14:58:46 +00:00
2019-10-31 02:32:52 +00:00
private active : boolean = false
2019-10-16 14:58:46 +00:00
2019-10-31 02:32:52 +00:00
private delay : number = 0
private count : number = 0
2019-10-31 02:32:52 +00:00
private intval : any
2019-10-31 02:32:52 +00:00
/**
* @constructor
*
* Create a sequencer object from class
**/
2019-10-31 02:32:52 +00:00
constructor (intval : any) {
2019-10-31 04:16:25 +00:00
this.intval = intval
//push button callback
2019-10-31 02:32:52 +00:00
this.intval.sequence = function () {
if (this.active) {
this.stop()
return false
} else {
this.start()
return true
}
}
}
2019-10-31 02:32:52 +00:00
2019-10-16 14:58:46 +00:00
/**
* Start running a "sequence" of frames. Shoots a continuous sequence
* of single frames with a delay in between each one.
**/
2019-10-31 02:32:52 +00:00
public async start (options : Options) {
let len : number = typeof options.len !== 'undefined' ? options.len : MAX_INTEGER
let multiple : number = typeof options.multiple !== 'undefined' ? options.multiple : 1
this.id = uuid()
2019-10-31 03:53:11 +00:00
this.delay = typeof options.delay !== 'undefined' ? options.delay : this.intval._state.frame.delay
2019-10-16 14:58:46 +00:00
this.count = 0
this.active = true
2019-10-31 03:15:40 +00:00
log.info({ id : this.id, started : true })
2019-10-31 02:32:52 +00:00
for (let i = 0; i < len; i++) {
if (multiple > 1) {
for (let x = 0; x < multiple; x++) {
await this.intval.frame()
log.info('frame', { id : this.id, count : this.count })
2019-10-31 02:32:52 +00:00
this.count++
}
} else {
await this.intval.frame()
log.info('frame', { id : this.id, count : this.count })
2019-10-31 02:32:52 +00:00
this.count++
}
2019-10-31 03:15:40 +00:00
if (this.delay > 0 && i < len - 1) {
2019-10-31 02:32:52 +00:00
await delay(this.delay)
}
2019-10-31 02:32:52 +00:00
if (!this.active) {
break
}
}
this._stop()
}
/**
* Public method to stop a sequence from the web or ble interface
**/
public stop () {
this.active = false
}
2019-10-31 02:32:52 +00:00
/**
* Stop a running sequence and reset counter and delay
**/
private _stop = function () {
2019-10-16 14:58:46 +00:00
this.active = false
this.count = 0
2019-10-31 02:32:52 +00:00
this.delay = 0
2019-10-31 03:15:40 +00:00
2020-05-19 21:06:36 +00:00
this.intval._state.sequence = false;
log.info('_stop', { id : this.id, stopped : true })
}
}
2019-10-31 02:32:52 +00:00
module.exports = Sequence