mirror of
https://github.com/cpvalente/ontime.git
synced 2026-09-10 16:49:41 +00:00
refactor: migrate triggers to new service
This commit is contained in:
committed by
Carlos Valente
parent
6c4d80158c
commit
1f71d4578c
@@ -17,6 +17,13 @@ export function getAutomationSettings(): AutomationSettings {
|
|||||||
return structuredClone(getDataProvider().getAutomation());
|
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
|
* Gets a copy of the stored automations
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -12,13 +12,17 @@ import { getState, type RuntimeState } from '../../stores/runtimeState.js';
|
|||||||
|
|
||||||
import { emitOSC } from './clients/osc.client.js';
|
import { emitOSC } from './clients/osc.client.js';
|
||||||
import { emitHTTP } from './clients/http.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';
|
import { isOntimeCloud } from '../../externals.js';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Exposes a method for triggering actions based on a TimerLifeCycle event
|
* Exposes a method for triggering actions based on a TimerLifeCycle event
|
||||||
*/
|
*/
|
||||||
export function triggerAutomations(event: TimerLifeCycle, state: RuntimeState) {
|
export function triggerAutomations(event: TimerLifeCycle, state: RuntimeState) {
|
||||||
|
if (!getAutomationsEnabled()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
const automations = getAutomations();
|
const automations = getAutomations();
|
||||||
const triggerAutomations = automations.filter((automation) => automation.trigger === event);
|
const triggerAutomations = automations.filter((automation) => automation.trigger === event);
|
||||||
if (triggerAutomations.length === 0) {
|
if (triggerAutomations.length === 0) {
|
||||||
|
|||||||
@@ -36,6 +36,7 @@ import { integrationService } from '../integration-service/IntegrationService.js
|
|||||||
|
|
||||||
import { getForceUpdate, getShouldClockUpdate, getShouldTimerUpdate } from './rundownService.utils.js';
|
import { getForceUpdate, getShouldClockUpdate, getShouldTimerUpdate } from './rundownService.utils.js';
|
||||||
import { skippedOutOfEvent } from '../timerUtils.js';
|
import { skippedOutOfEvent } from '../timerUtils.js';
|
||||||
|
import { triggerAutomations } from '../../api-data/automation/automation.service.js';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Service manages runtime status of app
|
* Service manages runtime status of app
|
||||||
@@ -77,11 +78,11 @@ class RuntimeService {
|
|||||||
if (timerPhaseChanged) {
|
if (timerPhaseChanged) {
|
||||||
if (newState.timer.phase === TimerPhase.Warning) {
|
if (newState.timer.phase === TimerPhase.Warning) {
|
||||||
process.nextTick(() => {
|
process.nextTick(() => {
|
||||||
integrationService.dispatch(TimerLifeCycle.onWarning);
|
triggerAutomations(TimerLifeCycle.onWarning, newState);
|
||||||
});
|
});
|
||||||
} else if (newState.timer.phase === TimerPhase.Danger) {
|
} else if (newState.timer.phase === TimerPhase.Danger) {
|
||||||
process.nextTick(() => {
|
process.nextTick(() => {
|
||||||
integrationService.dispatch(TimerLifeCycle.onDanger);
|
triggerAutomations(TimerLifeCycle.onDanger, newState);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -97,7 +98,7 @@ class RuntimeService {
|
|||||||
} else if (hasTimerFinished) {
|
} else if (hasTimerFinished) {
|
||||||
// if the timer has finished, we need to load next and keep rolling
|
// if the timer has finished, we need to load next and keep rolling
|
||||||
process.nextTick(() => {
|
process.nextTick(() => {
|
||||||
integrationService.dispatch(TimerLifeCycle.onFinish);
|
triggerAutomations(TimerLifeCycle.onFinish, newState);
|
||||||
});
|
});
|
||||||
this.handleLoadNext();
|
this.handleLoadNext();
|
||||||
this.rollLoaded(keepOffset);
|
this.rollLoaded(keepOffset);
|
||||||
@@ -117,7 +118,7 @@ class RuntimeService {
|
|||||||
// 3. find if we need to process actions related to the timer finishing
|
// 3. find if we need to process actions related to the timer finishing
|
||||||
if (newState.timer.playback === Playback.Play && hasTimerFinished) {
|
if (newState.timer.playback === Playback.Play && hasTimerFinished) {
|
||||||
process.nextTick(() => {
|
process.nextTick(() => {
|
||||||
integrationService.dispatch(TimerLifeCycle.onFinish);
|
triggerAutomations(TimerLifeCycle.onFinish, newState);
|
||||||
});
|
});
|
||||||
|
|
||||||
// handle end action if there was a timer playing
|
// handle end action if there was a timer playing
|
||||||
@@ -137,7 +138,7 @@ class RuntimeService {
|
|||||||
const shouldUpdateTimer = getShouldTimerUpdate(this.lastIntegrationTimerValue, newState.timer.current);
|
const shouldUpdateTimer = getShouldTimerUpdate(this.lastIntegrationTimerValue, newState.timer.current);
|
||||||
if (shouldUpdateTimer) {
|
if (shouldUpdateTimer) {
|
||||||
process.nextTick(() => {
|
process.nextTick(() => {
|
||||||
integrationService.dispatch(TimerLifeCycle.onUpdate);
|
triggerAutomations(TimerLifeCycle.onUpdate, newState);
|
||||||
});
|
});
|
||||||
|
|
||||||
this.lastIntegrationTimerValue = newState.timer.current ?? -1;
|
this.lastIntegrationTimerValue = newState.timer.current ?? -1;
|
||||||
@@ -147,7 +148,7 @@ class RuntimeService {
|
|||||||
const shouldUpdateClock = getShouldClockUpdate(this.lastIntegrationClockUpdate, newState.clock);
|
const shouldUpdateClock = getShouldClockUpdate(this.lastIntegrationClockUpdate, newState.clock);
|
||||||
if (shouldUpdateClock) {
|
if (shouldUpdateClock) {
|
||||||
process.nextTick(() => {
|
process.nextTick(() => {
|
||||||
integrationService.dispatch(TimerLifeCycle.onClock);
|
triggerAutomations(TimerLifeCycle.onClock, newState);
|
||||||
});
|
});
|
||||||
|
|
||||||
this.lastIntegrationClockUpdate = newState.clock;
|
this.lastIntegrationClockUpdate = newState.clock;
|
||||||
@@ -294,7 +295,7 @@ class RuntimeService {
|
|||||||
if (success) {
|
if (success) {
|
||||||
logger.info(LogOrigin.Playback, `Loaded event with ID ${event.id}`);
|
logger.info(LogOrigin.Playback, `Loaded event with ID ${event.id}`);
|
||||||
process.nextTick(() => {
|
process.nextTick(() => {
|
||||||
integrationService.dispatch(TimerLifeCycle.onLoad);
|
triggerAutomations(TimerLifeCycle.onLoad, runtimeState.getState());
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
return success;
|
return success;
|
||||||
@@ -473,7 +474,7 @@ class RuntimeService {
|
|||||||
|
|
||||||
if (didStart) {
|
if (didStart) {
|
||||||
process.nextTick(() => {
|
process.nextTick(() => {
|
||||||
integrationService.dispatch(TimerLifeCycle.onStart);
|
triggerAutomations(TimerLifeCycle.onStart, newState);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
return didStart;
|
return didStart;
|
||||||
@@ -526,7 +527,7 @@ class RuntimeService {
|
|||||||
const newState = runtimeState.getState();
|
const newState = runtimeState.getState();
|
||||||
logger.info(LogOrigin.Playback, `Play Mode ${newState.timer.playback.toUpperCase()}`);
|
logger.info(LogOrigin.Playback, `Play Mode ${newState.timer.playback.toUpperCase()}`);
|
||||||
process.nextTick(() => {
|
process.nextTick(() => {
|
||||||
integrationService.dispatch(TimerLifeCycle.onPause);
|
triggerAutomations(TimerLifeCycle.onPause, newState);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -545,7 +546,7 @@ class RuntimeService {
|
|||||||
const newState = runtimeState.getState();
|
const newState = runtimeState.getState();
|
||||||
logger.info(LogOrigin.Playback, `Play Mode ${newState.timer.playback.toUpperCase()}`);
|
logger.info(LogOrigin.Playback, `Play Mode ${newState.timer.playback.toUpperCase()}`);
|
||||||
process.nextTick(() => {
|
process.nextTick(() => {
|
||||||
integrationService.dispatch(TimerLifeCycle.onStop);
|
triggerAutomations(TimerLifeCycle.onStop, newState);
|
||||||
});
|
});
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
@@ -593,16 +594,17 @@ class RuntimeService {
|
|||||||
try {
|
try {
|
||||||
const rundown = getRundown();
|
const rundown = getRundown();
|
||||||
const result = runtimeState.roll(rundown);
|
const result = runtimeState.roll(rundown);
|
||||||
|
const newState = runtimeState.getState();
|
||||||
if (result.eventId !== previousState.eventNow?.id) {
|
if (result.eventId !== previousState.eventNow?.id) {
|
||||||
logger.info(LogOrigin.Playback, `Loaded event with ID ${result.eventId}`);
|
logger.info(LogOrigin.Playback, `Loaded event with ID ${result.eventId}`);
|
||||||
process.nextTick(() => {
|
process.nextTick(() => {
|
||||||
integrationService.dispatch(TimerLifeCycle.onLoad);
|
triggerAutomations(TimerLifeCycle.onLoad, newState);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
if (result.didStart) {
|
if (result.didStart) {
|
||||||
process.nextTick(() => {
|
process.nextTick(() => {
|
||||||
integrationService.dispatch(TimerLifeCycle.onStart);
|
triggerAutomations(TimerLifeCycle.onStart, newState);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
|||||||
Reference in New Issue
Block a user