intval3/lib/sequence/index.js

86 lines
2.7 KiB
JavaScript
Raw Normal View History

2019-10-11 21:53:41 +00:00
'use strict';
2019-10-16 14:58:46 +00:00
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
2019-10-16 14:58:46 +00:00
const v4_1 = __importDefault(require("uuid/v4"));
2019-10-11 21:53:41 +00:00
const log = require('../log')('seq');
2019-10-31 02:32:52 +00:00
const delay_1 = require("../delay");
const MAX_INTEGER = 2147483647;
2017-12-20 23:40:51 +00:00
/** Object sequence features */
class Sequence {
2019-10-31 02:32:52 +00:00
/**
* @constructor
*
* Create a sequencer object from class
**/
constructor(intval) {
2019-10-16 14:58:46 +00:00
this.active = false;
2019-10-31 02:32:52 +00:00
this.delay = 0;
2019-10-16 14:58:46 +00:00
this.count = 0;
2019-10-31 02:32:52 +00:00
/**
* Stop a running sequence and reset counter and delay
**/
this._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;
log.info('_stop', { id: this.id, stopped: true });
2019-10-31 02:32:52 +00:00
};
this.intval = intval;
2019-10-31 04:16:25 +00:00
//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-11 21:53:41 +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
async start(options) {
let len = typeof options.len !== 'undefined' ? options.len : MAX_INTEGER;
let multiple = typeof options.multiple !== 'undefined' ? options.multiple : 1;
2019-10-16 14:58:46 +00:00
this.id = v4_1.default();
2019-10-31 03:53:11 +00:00
this.delay = typeof options.delay !== 'undefined' ? options.delay : this.intval._state.frame.delay;
2019-10-31 02:32:52 +00:00
this.count = 0;
this.active = true;
2019-10-31 03:16:34 +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-16 14:58:46 +00:00
this.count++;
}
2019-10-11 21:53:41 +00:00
}
else {
2019-10-31 02:32:52 +00:00
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:16:34 +00:00
if (this.delay > 0 && i < len - 1) {
2019-10-31 02:32:52 +00:00
await delay_1.delay(this.delay);
}
if (!this.active) {
break;
2019-10-11 21:53:41 +00:00
}
}
this._stop();
}
/**
* Public method to stop a sequence from the web or ble interface
**/
stop() {
this.active = false;
2019-10-11 21:53:41 +00:00
}
}
2019-10-31 02:32:52 +00:00
exports.Sequence = Sequence;
module.exports = Sequence;
2019-10-11 21:53:41 +00:00
//# sourceMappingURL=index.js.map