Feat/end action (#308)

* feat: end action

---------

Co-authored-by: Fabian Posenau <fabian@fphome.de>
Co-authored-by: cv <34649812+cpvalente@users.noreply.github.com>
Co-authored-by: Fabian Posenau <19673098+kellhogs@users.noreply.github.com>
This commit is contained in:
Fabian Posenau
2023-03-16 19:27:15 +01:00
committed by GitHub
parent 76c8f8a4d5
commit 73533600a0
13 changed files with 79 additions and 45 deletions
@@ -98,7 +98,7 @@ export function dispatchFromAdapter(type: string, payload: unknown, source?: 'os
}
case 'startid': {
if (!payload) {
if (!payload || typeof payload !== 'string') {
throw new Error(`Event ID not recognised: ${payload}`);
}
PlaybackService.startById(payload);
+2 -2
View File
@@ -1,11 +1,11 @@
import { OntimeBlock, OntimeDelay, OntimeEvent, SupportedEvent, TimerType } from 'ontime-types';
import { EndAction, OntimeBlock, OntimeDelay, OntimeEvent, SupportedEvent, TimerType } from 'ontime-types';
export const event: Omit<OntimeEvent, 'id'> = {
title: '',
subtitle: '',
presenter: '',
note: '',
timerBehaviour: 'start-end',
endAction: EndAction.Continue,
timerType: TimerType.CountDown,
timeStart: 0,
timeEnd: 0,
+34 -9
View File
@@ -1,4 +1,4 @@
import { Playback } from 'ontime-types';
import { OntimeEvent, Playback } from 'ontime-types';
import { eventLoader, EventLoader } from '../classes/event-loader/EventLoader.js';
import { eventStore } from '../stores/EventStore.js';
@@ -13,10 +13,10 @@ import { logger } from '../classes/Logger.js';
export class PlaybackService {
/**
* makes calls for loading and starting given event
* @param {object} event
* @param {OntimeEvent} event
* @return {boolean} success
*/
static loadEvent(event) {
static loadEvent(event: OntimeEvent): boolean {
let success = false;
if (!event) {
logger.error('PLAYBACK', 'No event found');
@@ -36,7 +36,7 @@ export class PlaybackService {
* @param {string} eventId
* @return {boolean} success
*/
static startById(eventId) {
static startById(eventId: string): boolean {
const event = EventLoader.getEventWithId(eventId);
const success = PlaybackService.loadEvent(event);
if (success) {
@@ -51,7 +51,7 @@ export class PlaybackService {
* @param {number} eventIndex
* @return {boolean} success
*/
static startByIndex(eventIndex) {
static startByIndex(eventIndex: number): boolean {
const event = EventLoader.getEventAtIndex(eventIndex);
const success = PlaybackService.loadEvent(event);
if (success) {
@@ -66,7 +66,7 @@ export class PlaybackService {
* @param {string} eventId
* @return {boolean} success
*/
static loadById(eventId) {
static loadById(eventId: string): boolean {
const event = EventLoader.getEventWithId(eventId);
const success = PlaybackService.loadEvent(event);
if (success) {
@@ -80,7 +80,7 @@ export class PlaybackService {
* @param {number} eventIndex
* @return {boolean} success
*/
static loadByIndex(eventIndex) {
static loadByIndex(eventIndex: number): boolean {
const event = EventLoader.getEventAtIndex(eventIndex);
const success = PlaybackService.loadEvent(event);
if (success) {
@@ -104,14 +104,28 @@ export class PlaybackService {
/**
* Loads event after currently selected
* @param {string} [fallbackAction] - 'stop', 'pause'
* @return {boolean} success
*/
static loadNext() {
static loadNext(fallbackAction?: 'stop' | 'pause'): boolean {
const nextEvent = eventLoader.findNext();
if (nextEvent) {
const success = PlaybackService.loadEvent(nextEvent);
if (success) {
logger.info('PLAYBACK', `Loaded event with ID ${nextEvent.id}`);
return true;
}
} else if (fallbackAction === 'stop') {
logger.info('PLAYBACK', `No next event found! Stopping playback`);
PlaybackService.stop();
return false;
} else if (fallbackAction === 'pause') {
logger.info('PLAYBACK', `No next event found! Pausing playback`);
PlaybackService.pause();
return false;
} else {
logger.info('PLAYBACK', `No next event found! Continuing playback`);
return false;
}
}
@@ -126,6 +140,17 @@ export class PlaybackService {
}
}
/**
* Starts playback on next event
* @param {string} [fallbackAction] - 'stop', 'pause'
*/
static startNext(fallbackAction?: 'stop' | 'pause') {
const success = PlaybackService.loadNext(fallbackAction);
if (success) {
PlaybackService.start();
}
}
/**
* Pauses playback on selected event
*/
@@ -190,7 +215,7 @@ export class PlaybackService {
* Adds delay to current event
* @param {number} delayTime time in minutes
*/
static setDelay(delayTime) {
static setDelay(delayTime: number) {
if (eventTimer.loadedTimerId) {
const delayInMs = delayTime * 1000 * 60;
eventTimer.delay(delayInMs);
+16 -2
View File
@@ -1,4 +1,4 @@
import { Playback, TimerLifeCycle, TimerState } from 'ontime-types';
import { EndAction, Playback, TimerLifeCycle, TimerState } from 'ontime-types';
import { eventStore } from '../stores/EventStore.js';
import { PlaybackService } from './PlaybackService.js';
@@ -47,6 +47,7 @@ export class TimerService {
selectedEventId: null,
duration: null,
timerType: null,
endAction: null,
};
this.loadedTimerId = null;
this.pausedTime = 0;
@@ -78,6 +79,7 @@ export class TimerService {
// update relevant information and force update
this.timer.duration = timer.duration;
this.timer.timerType = timer.timerType;
this.timer.endAction = timer.endAction;
// this might not be ideal
this.timer.finishedAt = null;
@@ -117,6 +119,7 @@ export class TimerService {
this.timer.current = timer.duration;
this.playback = Playback.Armed;
this.timer.timerType = timer.timerType;
this.timer.endAction = timer.endAction;
this.pausedTime = 0;
this.pausedAt = 0;
@@ -210,7 +213,7 @@ export class TimerService {
* Delays running timer by given amount
* @param {number} amount
*/
delay(amount) {
delay(amount: number) {
if (!this.loadedTimerId) {
return;
}
@@ -289,6 +292,7 @@ export class TimerService {
this.pausedTime,
this.timer.clock,
);
this.timer.elapsed = getElapsed(this.timer.startedAt, this.timer.clock);
}
}
@@ -303,6 +307,16 @@ export class TimerService {
_onFinish() {
eventStore.set('timer', this.timer);
integrationService.dispatch(TimerLifeCycle.onFinish);
if (this.timer.endAction === EndAction.Stop) {
PlaybackService.stop();
} else if (this.timer.endAction === EndAction.LoadNext) {
// we need to delay here to put this action in the queue stack. otherwise it won't be executed properly
setTimeout(() => {
PlaybackService.loadNext();
}, 0);
} else if (this.timer.endAction === EndAction.PlayNext) {
PlaybackService.startNext();
}
}
roll(currentEvent, nextEvent, timers) {
+1 -1
View File
@@ -310,7 +310,7 @@ export const validateEvent = (eventArgs) => {
presenter: makeString(e.presenter, d.presenter),
timeStart: start,
timeEnd: end,
timerBehaviour: makeString(e.timerBehaviour, d.timerBehaviour),
endAction: makeString(e.endAction, d.endAction),
timerType: makeString(e.timerType, d.timerType),
duration: validateDuration(start, end),
isPublic: typeof e.isPublic === 'boolean' ? e.isPublic : d.isPublic,