Skip to content

Commit

Permalink
refactor: migrate triggers to new service
Browse files Browse the repository at this point in the history
  • Loading branch information
cpvalente committed Jan 24, 2025
1 parent 6c4d801 commit 1f71d45
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 13 deletions.
7 changes: 7 additions & 0 deletions apps/server/src/api-data/automation/automation.dao.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
6 changes: 5 additions & 1 deletion apps/server/src/api-data/automation/automation.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
26 changes: 14 additions & 12 deletions apps/server/src/services/runtime-service/RuntimeService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
});
}
}
Expand All @@ -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);
Expand All @@ -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
Expand All @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -473,7 +474,7 @@ class RuntimeService {

if (didStart) {
process.nextTick(() => {
integrationService.dispatch(TimerLifeCycle.onStart);
triggerAutomations(TimerLifeCycle.onStart, newState);
});
}
return didStart;
Expand Down Expand Up @@ -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);
});
}

Expand All @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 1f71d45

Please sign in to comment.