From 1f71d4578c825ac08130933d436aa8e34959e49e Mon Sep 17 00:00:00 2001 From: Carlos Valente Date: Sat, 11 Jan 2025 16:02:23 +0100 Subject: [PATCH] refactor: migrate triggers to new service --- .../src/api-data/automation/automation.dao.ts | 7 +++++ .../api-data/automation/automation.service.ts | 6 ++++- .../runtime-service/RuntimeService.ts | 26 ++++++++++--------- 3 files changed, 26 insertions(+), 13 deletions(-) diff --git a/apps/server/src/api-data/automation/automation.dao.ts b/apps/server/src/api-data/automation/automation.dao.ts index 1aa602561..815c58b9c 100644 --- a/apps/server/src/api-data/automation/automation.dao.ts +++ b/apps/server/src/api-data/automation/automation.dao.ts @@ -17,6 +17,13 @@ export function getAutomationSettings(): AutomationSettings { return structuredClone(getDataProvider().getAutomation()); } +/** + * Gets the enabled status of the automations + */ +export function getAutomationsEnabled(): boolean { + return getAutomationSettings().enabledAutomations; +} + /** * Gets a copy of the stored automations */ diff --git a/apps/server/src/api-data/automation/automation.service.ts b/apps/server/src/api-data/automation/automation.service.ts index c725efd94..73e246acd 100644 --- a/apps/server/src/api-data/automation/automation.service.ts +++ b/apps/server/src/api-data/automation/automation.service.ts @@ -12,13 +12,17 @@ import { getState, type RuntimeState } from '../../stores/runtimeState.js'; import { emitOSC } from './clients/osc.client.js'; import { emitHTTP } from './clients/http.client.js'; -import { getAutomations, getBlueprints } from './automation.dao.js'; +import { getAutomations, getAutomationsEnabled, getBlueprints } from './automation.dao.js'; import { isOntimeCloud } from '../../externals.js'; /** * Exposes a method for triggering actions based on a TimerLifeCycle event */ export function triggerAutomations(event: TimerLifeCycle, state: RuntimeState) { + if (!getAutomationsEnabled()) { + return; + } + const automations = getAutomations(); const triggerAutomations = automations.filter((automation) => automation.trigger === event); if (triggerAutomations.length === 0) { diff --git a/apps/server/src/services/runtime-service/RuntimeService.ts b/apps/server/src/services/runtime-service/RuntimeService.ts index a4bded621..ae5d64089 100644 --- a/apps/server/src/services/runtime-service/RuntimeService.ts +++ b/apps/server/src/services/runtime-service/RuntimeService.ts @@ -36,6 +36,7 @@ import { integrationService } from '../integration-service/IntegrationService.js import { getForceUpdate, getShouldClockUpdate, getShouldTimerUpdate } from './rundownService.utils.js'; import { skippedOutOfEvent } from '../timerUtils.js'; +import { triggerAutomations } from '../../api-data/automation/automation.service.js'; /** * Service manages runtime status of app @@ -77,11 +78,11 @@ class RuntimeService { if (timerPhaseChanged) { if (newState.timer.phase === TimerPhase.Warning) { process.nextTick(() => { - integrationService.dispatch(TimerLifeCycle.onWarning); + triggerAutomations(TimerLifeCycle.onWarning, newState); }); } else if (newState.timer.phase === TimerPhase.Danger) { process.nextTick(() => { - integrationService.dispatch(TimerLifeCycle.onDanger); + triggerAutomations(TimerLifeCycle.onDanger, newState); }); } } @@ -97,7 +98,7 @@ class RuntimeService { } else if (hasTimerFinished) { // if the timer has finished, we need to load next and keep rolling process.nextTick(() => { - integrationService.dispatch(TimerLifeCycle.onFinish); + triggerAutomations(TimerLifeCycle.onFinish, newState); }); this.handleLoadNext(); this.rollLoaded(keepOffset); @@ -117,7 +118,7 @@ class RuntimeService { // 3. find if we need to process actions related to the timer finishing if (newState.timer.playback === Playback.Play && hasTimerFinished) { process.nextTick(() => { - integrationService.dispatch(TimerLifeCycle.onFinish); + triggerAutomations(TimerLifeCycle.onFinish, newState); }); // handle end action if there was a timer playing @@ -137,7 +138,7 @@ class RuntimeService { const shouldUpdateTimer = getShouldTimerUpdate(this.lastIntegrationTimerValue, newState.timer.current); if (shouldUpdateTimer) { process.nextTick(() => { - integrationService.dispatch(TimerLifeCycle.onUpdate); + triggerAutomations(TimerLifeCycle.onUpdate, newState); }); this.lastIntegrationTimerValue = newState.timer.current ?? -1; @@ -147,7 +148,7 @@ class RuntimeService { const shouldUpdateClock = getShouldClockUpdate(this.lastIntegrationClockUpdate, newState.clock); if (shouldUpdateClock) { process.nextTick(() => { - integrationService.dispatch(TimerLifeCycle.onClock); + triggerAutomations(TimerLifeCycle.onClock, newState); }); this.lastIntegrationClockUpdate = newState.clock; @@ -294,7 +295,7 @@ class RuntimeService { if (success) { logger.info(LogOrigin.Playback, `Loaded event with ID ${event.id}`); process.nextTick(() => { - integrationService.dispatch(TimerLifeCycle.onLoad); + triggerAutomations(TimerLifeCycle.onLoad, runtimeState.getState()); }); } return success; @@ -473,7 +474,7 @@ class RuntimeService { if (didStart) { process.nextTick(() => { - integrationService.dispatch(TimerLifeCycle.onStart); + triggerAutomations(TimerLifeCycle.onStart, newState); }); } return didStart; @@ -526,7 +527,7 @@ class RuntimeService { const newState = runtimeState.getState(); logger.info(LogOrigin.Playback, `Play Mode ${newState.timer.playback.toUpperCase()}`); process.nextTick(() => { - integrationService.dispatch(TimerLifeCycle.onPause); + triggerAutomations(TimerLifeCycle.onPause, newState); }); } @@ -545,7 +546,7 @@ class RuntimeService { const newState = runtimeState.getState(); logger.info(LogOrigin.Playback, `Play Mode ${newState.timer.playback.toUpperCase()}`); process.nextTick(() => { - integrationService.dispatch(TimerLifeCycle.onStop); + triggerAutomations(TimerLifeCycle.onStop, newState); }); return true; @@ -593,16 +594,17 @@ class RuntimeService { try { const rundown = getRundown(); const result = runtimeState.roll(rundown); + const newState = runtimeState.getState(); if (result.eventId !== previousState.eventNow?.id) { logger.info(LogOrigin.Playback, `Loaded event with ID ${result.eventId}`); process.nextTick(() => { - integrationService.dispatch(TimerLifeCycle.onLoad); + triggerAutomations(TimerLifeCycle.onLoad, newState); }); } if (result.didStart) { process.nextTick(() => { - integrationService.dispatch(TimerLifeCycle.onStart); + triggerAutomations(TimerLifeCycle.onStart, newState); }); } } catch (error) {