mirror of
https://github.com/cpvalente/ontime.git
synced 2026-08-09 17:33:55 +00:00
55d1aca9b6
* refactor: cleanup routes * style: smaller base font * chore: upgrade dependencies * chore: lock node version to electron * refactor: pass HTTP to integration controller (#652) * refactor: deprecate onair control * refactor: remove playback router * Several project files user folder (#617) * chore: automated screenshots (#667) * feat: app settings (#658) * refactor: remove deprecated event data (#674) * Studio clock (#663) --------- Co-authored-by: Carlos Valente <carlosvalente@pm.me> * Feat: reorder events with alt+ctrl + arrow up/down (#645) * Warning and danger per event (#677) --------- Co-authored-by: Fabian Posenau <fabian@fphome.de> * refactor: stabilise actionHandler (#683) Co-authored-by: Fabian Posenau <fabian@fphome.de> * improvement: hide seconds (#675) * wip: overview (#688) * fix: focus cursor (#695) * refactor: update lower third (#665) * Refactor/time formatting (#696) --------- Co-authored-by: Carlos Valente <34649812+cpvalente@users.noreply.github.com> Co-authored-by: Carlos Valente <carlosvalente@pm.me> * feat: multiple selection (#703) --------- Co-authored-by: asharonbaltazar <asharonbaltazar@outlook.com> Co-authored-by: Alex <ac@omnivox.dk> * fix: test - go to `Edit mode` befor tying to click `Event options` button (#708) * refactor: runtime service (#715) * fix: issue with loosing cursor position on message (#719) * remove info panel (#721) * Event editor continue (#722) * update API - part (#709) --------- Co-authored-by: Carlos Valente <34649812+cpvalente@users.noreply.github.com> Co-authored-by: Carlos Valente <carlosvalente@pm.me> * refactor: update timers (#729) * feat: many timers (#706) --------- Co-authored-by: arc-alex <ac@omnivox.dk> * refactor: excel cleanup (#734) * refactor: allow import of blocks and skip import (#735) * Project manager (#697) * refactor: UI for linking events (#763) * upgraded pipeline actions (#777) * Over under (#771) * custom fields (#744) --------- Co-authored-by: Carlos Valente <carlosvalente@pm.me> * Sheets settings (#774) --------- Co-authored-by: arc-alex <ac@omnivox.dk> * style: tweaks to lower thirds (#785) * refactor: delays account for gaps (#784) * refactor: partial state updates (#780) * feat: generate crash report (#787) * Sheet use limited input device auth flow (#782) --------- Co-authored-by: cv <34649812+cpvalente@users.noreply.github.com> Co-authored-by: Carlos Valente <carlosvalente@pm.me> * Custom fields views (#789) * refactor: deprecate presenter and subtitle (#795) * refactor: organise API around resources (#798) --------- Co-authored-by: Bianca Procopio <biancahprocopio@gmail.com> * Time to end (#804) * Skip fixes (#805) * fix: onair derives from playback * Param nav (#822) --------- Co-authored-by: Alex Christoffer Rasmussen <ac@omnivox.dk> * refactor: download files from interface (#831) * Quick options (#814) * End pause (#832) * chore: bump node version in docker (#834) * refactor: follow in run mode (#840) * fix: uncaught error in http integration (#837) * Apply project (#843) Co-authored-by: Matteo Gheza <matteo.gheza07@gmail.com> Co-authored-by: Ary <arylmoraesn@gmail.com> Co-authored-by: Alex Christoffer Rasmussen <ac@omnivox.dk> Co-authored-by: Fabian Posenau <19673098+kellhogs@users.noreply.github.com> Co-authored-by: Fabian Posenau <fabian@fphome.de> Co-authored-by: Alex Rohleder <alexrohleder96@gmail.com> Co-authored-by: asharonbaltazar <asharonbaltazar@outlook.com> Co-authored-by: Bianca Procopio <biancahprocopio@gmail.com> Co-authored-by: Fabian Posenau <fabianpos99+github@gmail.com>
458 lines
13 KiB
TypeScript
458 lines
13 KiB
TypeScript
import { MaybeNumber, OntimeEvent, Playback, Runtime, TimerState, TimerType } from 'ontime-types';
|
|
import { calculateDuration, dayInMs } from 'ontime-utils';
|
|
|
|
import { clock } from '../services/Clock.js';
|
|
import { RestorePoint } from '../services/RestoreService.js';
|
|
|
|
import {
|
|
getCurrent,
|
|
getExpectedEnd,
|
|
getExpectedFinish,
|
|
getRollTimers,
|
|
getRuntimeOffset,
|
|
skippedOutOfEvent,
|
|
updateRoll,
|
|
} from '../services/timerUtils.js';
|
|
import { timerConfig } from '../config/config.js';
|
|
|
|
const initialRuntime: Runtime = {
|
|
selectedEventIndex: null,
|
|
numEvents: 0,
|
|
offset: null,
|
|
plannedStart: 0,
|
|
plannedEnd: 0,
|
|
actualStart: null,
|
|
expectedEnd: null,
|
|
};
|
|
|
|
const initialTimer: TimerState = {
|
|
addedTime: 0,
|
|
current: null,
|
|
duration: null,
|
|
elapsed: null,
|
|
expectedFinish: null, // TODO: expected finish could account for midnight, we cleanup in the clients
|
|
finishedAt: null,
|
|
playback: Playback.Stop,
|
|
secondaryTimer: null,
|
|
startedAt: null,
|
|
};
|
|
|
|
export type RuntimeState = {
|
|
clock: number; // realtime clock
|
|
eventNow: OntimeEvent | null;
|
|
publicEventNow: OntimeEvent | null;
|
|
eventNext: OntimeEvent | null;
|
|
publicEventNext: OntimeEvent | null;
|
|
runtime: Runtime;
|
|
timer: TimerState;
|
|
// private properties of the timer calculations
|
|
_timer: {
|
|
totalDelay: number; // this value comes from rundown service
|
|
pausedAt: MaybeNumber;
|
|
secondaryTarget: MaybeNumber;
|
|
};
|
|
};
|
|
|
|
const runtimeState: RuntimeState = {
|
|
clock: clock.timeNow(),
|
|
eventNow: null,
|
|
publicEventNow: null,
|
|
eventNext: null,
|
|
publicEventNext: null,
|
|
runtime: initialRuntime,
|
|
timer: { ...initialTimer },
|
|
_timer: {
|
|
totalDelay: 0,
|
|
pausedAt: null,
|
|
secondaryTarget: null,
|
|
},
|
|
};
|
|
|
|
export function getState(): Readonly<RuntimeState> {
|
|
return runtimeState;
|
|
}
|
|
|
|
export function clear() {
|
|
runtimeState.eventNow = null;
|
|
runtimeState.publicEventNow = null;
|
|
runtimeState.eventNext = null;
|
|
runtimeState.publicEventNext = null;
|
|
|
|
runtimeState.runtime = {
|
|
...initialRuntime,
|
|
// persist session stuff
|
|
actualStart: runtimeState.runtime.actualStart,
|
|
numEvents: runtimeState.runtime.numEvents,
|
|
};
|
|
|
|
runtimeState.timer.playback = Playback.Stop;
|
|
runtimeState.clock = clock.timeNow();
|
|
runtimeState.timer = { ...initialTimer };
|
|
|
|
// we maintain the total delay
|
|
runtimeState._timer.pausedAt = null;
|
|
runtimeState._timer.secondaryTarget = null;
|
|
}
|
|
|
|
/**
|
|
* Utility to allow modifying the state from the outside
|
|
* @param newState
|
|
*/
|
|
function patchTimer(newState: Partial<TimerState>) {
|
|
for (const key in newState) {
|
|
if (key in runtimeState.timer) {
|
|
runtimeState.timer[key] = newState[key];
|
|
}
|
|
}
|
|
}
|
|
|
|
type RundownData = {
|
|
numEvents: number;
|
|
firstStart: MaybeNumber;
|
|
lastEnd: MaybeNumber;
|
|
totalDelay: number;
|
|
totalDuration: number;
|
|
};
|
|
|
|
/**
|
|
* Utility, allows updating data derived from the rundown
|
|
* @param playableRundown
|
|
*/
|
|
export function updateRundownData(rundownData: RundownData) {
|
|
runtimeState._timer.totalDelay = rundownData.totalDelay;
|
|
|
|
runtimeState.runtime.numEvents = rundownData.numEvents;
|
|
runtimeState.runtime.plannedStart = rundownData.firstStart;
|
|
runtimeState.runtime.plannedEnd = rundownData.firstStart + rundownData.totalDuration;
|
|
runtimeState.runtime.expectedEnd = getExpectedEnd(runtimeState);
|
|
}
|
|
|
|
/**
|
|
* Loads a given event into state
|
|
* @param event
|
|
* @param rundown
|
|
* @param initialData
|
|
*/
|
|
export function load(
|
|
event: OntimeEvent,
|
|
rundown: OntimeEvent[],
|
|
initialData?: Partial<TimerState & RestorePoint>,
|
|
): boolean {
|
|
clear();
|
|
|
|
const eventIndex = rundown.findIndex((eventInMemory) => eventInMemory.id === event.id);
|
|
|
|
runtimeState.runtime.selectedEventIndex = eventIndex;
|
|
|
|
loadNow(event, rundown);
|
|
loadNext(rundown);
|
|
|
|
runtimeState.clock = clock.timeNow();
|
|
runtimeState.timer.playback = Playback.Armed;
|
|
runtimeState.timer.duration = calculateDuration(event.timeStart, event.timeEnd);
|
|
runtimeState.timer.current = getCurrent(runtimeState);
|
|
|
|
if (initialData) {
|
|
patchTimer(initialData);
|
|
|
|
const firstStart = initialData?.firstStart;
|
|
if (firstStart === null || typeof firstStart === 'number') {
|
|
runtimeState.runtime.actualStart = firstStart;
|
|
runtimeState.runtime.offset = getRuntimeOffset(runtimeState);
|
|
runtimeState.runtime.expectedEnd = getExpectedEnd(runtimeState);
|
|
}
|
|
}
|
|
|
|
return event.id === runtimeState.eventNow?.id;
|
|
}
|
|
|
|
export function loadNow(event: OntimeEvent, playableEvents: OntimeEvent[]) {
|
|
runtimeState.eventNow = event;
|
|
|
|
// check if current is also public
|
|
if (event.isPublic) {
|
|
runtimeState.publicEventNow = event;
|
|
} else {
|
|
// assume there is no public event
|
|
runtimeState.publicEventNow = null;
|
|
|
|
// if there is nothing before, return
|
|
if (!runtimeState.runtime.selectedEventIndex) {
|
|
return;
|
|
}
|
|
|
|
// iterate backwards to find it
|
|
for (let i = runtimeState.runtime.selectedEventIndex; i >= 0; i--) {
|
|
if (playableEvents[i].isPublic) {
|
|
runtimeState.publicEventNow = playableEvents[i];
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
export function loadNext(playableEvents: OntimeEvent[]) {
|
|
// assume there are no next events
|
|
runtimeState.eventNext = null;
|
|
runtimeState.publicEventNext = null;
|
|
|
|
if (runtimeState.runtime.selectedEventIndex === null) {
|
|
return;
|
|
}
|
|
|
|
const numEvents = playableEvents.length;
|
|
|
|
if (runtimeState.runtime.selectedEventIndex < numEvents - 1) {
|
|
let nextPublic = false;
|
|
let nextProduction = false;
|
|
|
|
for (let i = runtimeState.runtime.selectedEventIndex + 1; i < numEvents; i++) {
|
|
// if we have not set private
|
|
if (!nextProduction) {
|
|
runtimeState.eventNext = playableEvents[i];
|
|
nextProduction = true;
|
|
}
|
|
|
|
// if event is public
|
|
if (playableEvents[i].isPublic) {
|
|
runtimeState.publicEventNext = playableEvents[i];
|
|
nextPublic = true;
|
|
}
|
|
|
|
// Stop if both are set
|
|
if (nextPublic && nextProduction) break;
|
|
}
|
|
}
|
|
}
|
|
|
|
export function resume(restorePoint: RestorePoint, event: OntimeEvent, rundown: OntimeEvent[]) {
|
|
load(event, rundown, restorePoint);
|
|
}
|
|
|
|
/**
|
|
* We only pass an event if we are hot reloading
|
|
* @param {OntimeEvent} event only passed if we are changing the data if a playing timer
|
|
*/
|
|
export function reload(event?: OntimeEvent) {
|
|
if (event) {
|
|
runtimeState.eventNow = event;
|
|
|
|
// update data which is duplicate between eventNow and timer objects
|
|
runtimeState.timer.duration = calculateDuration(runtimeState.eventNow.timeStart, runtimeState.eventNow.timeEnd);
|
|
runtimeState.timer.current = getCurrent(runtimeState);
|
|
runtimeState.timer.expectedFinish = getExpectedFinish(runtimeState);
|
|
return;
|
|
}
|
|
runtimeState.timer.playback = Playback.Armed;
|
|
|
|
runtimeState.timer.duration = calculateDuration(runtimeState.eventNow.timeStart, runtimeState.eventNow.timeEnd);
|
|
runtimeState.timer.current = runtimeState.timer.duration;
|
|
runtimeState.timer.elapsed = null;
|
|
|
|
runtimeState.timer.startedAt = null;
|
|
runtimeState.timer.finishedAt = null;
|
|
runtimeState._timer.pausedAt = null;
|
|
runtimeState.timer.addedTime = 0;
|
|
|
|
runtimeState.timer.expectedFinish = getExpectedFinish(runtimeState);
|
|
}
|
|
|
|
/**
|
|
* Used in situations when we want to reload all events
|
|
* without interrupting timer
|
|
* @param eventNow
|
|
* @param playableEvents
|
|
*/
|
|
export function reloadAll(eventNow: OntimeEvent, playableEvents: OntimeEvent[]) {
|
|
loadNow(eventNow, playableEvents);
|
|
loadNext(playableEvents);
|
|
reload(eventNow);
|
|
}
|
|
|
|
export function start(state: RuntimeState = runtimeState): boolean {
|
|
if (state.eventNow === null) {
|
|
return false;
|
|
}
|
|
if (state.timer.playback === Playback.Play) {
|
|
return false;
|
|
}
|
|
state.clock = clock.timeNow();
|
|
state.timer.secondaryTimer = null;
|
|
state._timer.secondaryTarget = null;
|
|
|
|
// add paused time if it exists
|
|
if (state._timer.pausedAt) {
|
|
const timeToAdd = state.clock - state._timer.pausedAt;
|
|
state.timer.addedTime += timeToAdd;
|
|
state._timer.pausedAt = null;
|
|
}
|
|
|
|
if (state.timer.startedAt === null) {
|
|
state.timer.startedAt = state.clock;
|
|
}
|
|
|
|
state.timer.playback = Playback.Play;
|
|
state.timer.expectedFinish = getExpectedFinish(state);
|
|
state.timer.elapsed = 0;
|
|
|
|
// update runtime delays: over - under
|
|
if (state.runtime.actualStart === null) {
|
|
state.runtime.actualStart = state.clock;
|
|
}
|
|
|
|
state.runtime.offset = getRuntimeOffset(state);
|
|
state.runtime.expectedEnd = state.runtime.plannedEnd + state.runtime.offset;
|
|
|
|
return true;
|
|
}
|
|
|
|
export function pause(state: RuntimeState = runtimeState): boolean {
|
|
if (state.timer.playback !== Playback.Play) {
|
|
return false;
|
|
}
|
|
|
|
state.timer.playback = Playback.Pause;
|
|
state.clock = clock.timeNow();
|
|
state._timer.pausedAt = state.clock;
|
|
return true;
|
|
}
|
|
|
|
export function stop(state: RuntimeState = runtimeState): boolean {
|
|
if (state.timer.playback === Playback.Stop) {
|
|
return false;
|
|
}
|
|
|
|
clear();
|
|
runtimeState.runtime.actualStart = null;
|
|
runtimeState.runtime.expectedEnd = null;
|
|
return true;
|
|
}
|
|
|
|
export function addTime(amount: number) {
|
|
if (runtimeState.timer.current === null) {
|
|
return false;
|
|
}
|
|
|
|
runtimeState.timer.addedTime += amount;
|
|
runtimeState.timer.expectedFinish += amount;
|
|
runtimeState.timer.current += amount;
|
|
|
|
// handle edge cases
|
|
const willGoNegative = amount < 0 && Math.abs(amount) > runtimeState.timer.current;
|
|
const hasFinished = runtimeState.timer.finishedAt !== null;
|
|
if (willGoNegative && !hasFinished) {
|
|
runtimeState.timer.finishedAt = clock.timeNow();
|
|
} else {
|
|
const willGoPositive = runtimeState.timer.current < 0 && runtimeState.timer.current + amount > 0;
|
|
if (willGoPositive) {
|
|
runtimeState.timer.finishedAt = null;
|
|
}
|
|
}
|
|
|
|
// update runtime delays: over - under
|
|
runtimeState.runtime.offset = getRuntimeOffset(runtimeState);
|
|
runtimeState.runtime.expectedEnd = getExpectedEnd(runtimeState);
|
|
|
|
return true;
|
|
}
|
|
|
|
export type UpdateResult = {
|
|
hasTimerFinished: boolean;
|
|
shouldCallRoll: boolean;
|
|
};
|
|
|
|
export function update(): UpdateResult {
|
|
let hasTimerFinished = false;
|
|
let shouldCallRoll = false; // we also need to call roll if a secondary timer has finished
|
|
|
|
const previousTime = runtimeState.clock;
|
|
runtimeState.clock = clock.timeNow();
|
|
|
|
// we call integrations if we update timers
|
|
if (runtimeState.timer.playback === Playback.Roll) {
|
|
const result = onRollUpdate();
|
|
shouldCallRoll = result.doRoll;
|
|
hasTimerFinished = result.isFinished;
|
|
} else if (runtimeState.timer.startedAt !== null) {
|
|
// we only update timer if a timer has been started
|
|
const result = onPlayUpdate();
|
|
hasTimerFinished = result.isFinished;
|
|
} else if (runtimeState.eventNow?.timerType === TimerType.TimeToEnd) {
|
|
// or if we are in a time-to-end timer
|
|
runtimeState.timer.current = getCurrent(runtimeState);
|
|
runtimeState.timer.duration = runtimeState.timer.current;
|
|
}
|
|
|
|
// update offset
|
|
runtimeState.runtime.offset = getRuntimeOffset(runtimeState);
|
|
|
|
return {
|
|
hasTimerFinished,
|
|
shouldCallRoll,
|
|
};
|
|
|
|
function onRollUpdate() {
|
|
const hasSkippedOutOfEvent = skippedOutOfEvent(runtimeState, previousTime, timerConfig.skipLimit);
|
|
if (hasSkippedOutOfEvent) {
|
|
return { doRoll: true };
|
|
}
|
|
const { updatedTimer, updatedSecondaryTimer, doRollLoad, isFinished } = updateRoll(runtimeState);
|
|
runtimeState.timer.current = updatedTimer;
|
|
runtimeState.timer.secondaryTimer = updatedSecondaryTimer;
|
|
runtimeState.timer.elapsed = runtimeState.timer.duration - runtimeState.timer.current;
|
|
|
|
return { doRoll: doRollLoad, isFinished };
|
|
}
|
|
|
|
function onPlayUpdate() {
|
|
let isFinished = false;
|
|
runtimeState.timer.current = getCurrent(runtimeState);
|
|
const finishedNow =
|
|
runtimeState.timer.current <= timerConfig.triggerAhead && runtimeState.timer.finishedAt === null;
|
|
|
|
if (runtimeState.timer.playback === Playback.Play && finishedNow) {
|
|
runtimeState.timer.finishedAt = runtimeState.clock;
|
|
isFinished = true;
|
|
} else {
|
|
runtimeState.timer.expectedFinish = getExpectedFinish(runtimeState);
|
|
}
|
|
|
|
runtimeState.timer.elapsed = runtimeState.timer.duration - runtimeState.timer.current;
|
|
|
|
return { isFinished };
|
|
}
|
|
}
|
|
|
|
export function roll(rundown: OntimeEvent[]) {
|
|
clear();
|
|
|
|
runtimeState.runtime.numEvents = rundown.length;
|
|
const { nextEvent, currentEvent } = getRollTimers(rundown, runtimeState.clock);
|
|
|
|
if (currentEvent) {
|
|
// there is something running, load
|
|
runtimeState.timer.secondaryTimer = null;
|
|
runtimeState._timer.secondaryTarget = null;
|
|
|
|
// account for event that finishes the day after
|
|
const endTime =
|
|
currentEvent.timeEnd < currentEvent.timeStart ? currentEvent.timeEnd + dayInMs : currentEvent.timeEnd;
|
|
|
|
// when we load a timer in roll, we do the same things as before
|
|
// but also pre-populate some data as to the running state
|
|
load(currentEvent, rundown, {
|
|
startedAt: currentEvent.timeStart,
|
|
expectedFinish: currentEvent.timeEnd,
|
|
current: endTime - runtimeState.clock,
|
|
});
|
|
} else if (nextEvent) {
|
|
// account for day after
|
|
const nextStart = nextEvent.timeStart < runtimeState.clock ? nextEvent.timeStart + dayInMs : nextEvent.timeStart;
|
|
// nothing now, but something coming up
|
|
runtimeState.timer.secondaryTimer = nextStart - runtimeState.clock;
|
|
runtimeState._timer.secondaryTarget = nextStart;
|
|
}
|
|
|
|
runtimeState.timer.playback = Playback.Roll;
|
|
}
|