|
1 | 1 | import type { IMOSDevice } from '@mos-connection/connector' |
2 | 2 | import type { MosDeviceStatusesConfig } from './generated/devices' |
3 | 3 | import type { CoreMosDeviceHandler } from './CoreMosDeviceHandler' |
| 4 | +import { |
| 5 | + type Observer, |
| 6 | + PeripheralDevicePubSub, |
| 7 | + PeripheralDevicePubSubCollectionsNames, |
| 8 | + stringifyError, |
| 9 | + SubscriptionId, |
| 10 | +} from '@sofie-automation/server-core-integration' |
| 11 | +import type { IngestRundownStatus } from '@sofie-automation/shared-lib/dist/ingest/rundownStatus' |
| 12 | +import { RundownId } from '@sofie-automation/shared-lib/dist/core/model/Ids' |
| 13 | +import type winston = require('winston') |
4 | 14 |
|
5 | 15 | export class MosStatusHandler { |
6 | | - constructor(mosDevice: IMOSDevice, coreMosHandler: CoreMosDeviceHandler, config: MosDeviceStatusesConfig) { |
7 | | - // TODO |
| 16 | + readonly #logger: winston.Logger |
| 17 | + readonly #mosDevice: IMOSDevice |
| 18 | + readonly #coreMosHandler: CoreMosDeviceHandler |
| 19 | + |
| 20 | + #subId: SubscriptionId | undefined |
| 21 | + #observer: Observer<IngestRundownStatus> | undefined |
| 22 | + |
| 23 | + #destroyed = false |
| 24 | + |
| 25 | + readonly #lastStatuses = new Map<RundownId, IngestRundownStatus>() |
| 26 | + |
| 27 | + constructor( |
| 28 | + logger: winston.Logger, |
| 29 | + mosDevice: IMOSDevice, |
| 30 | + coreMosHandler: CoreMosDeviceHandler, |
| 31 | + config: MosDeviceStatusesConfig |
| 32 | + ) { |
| 33 | + if (!config.enabled) throw new Error('MosStatusHandler is not enabled') |
| 34 | + |
| 35 | + this.#logger = logger |
| 36 | + this.#mosDevice = mosDevice |
| 37 | + this.#coreMosHandler = coreMosHandler |
| 38 | + |
| 39 | + coreMosHandler.core |
| 40 | + .autoSubscribe(PeripheralDevicePubSub.ingestDeviceRundownStatus, coreMosHandler.core.deviceId, undefined) // nocommit - does this need a token? |
| 41 | + .then((subId) => { |
| 42 | + this.#subId = subId |
| 43 | + |
| 44 | + if (this.#destroyed) coreMosHandler.core.unsubscribe(subId) |
| 45 | + }) |
| 46 | + .catch((e) => { |
| 47 | + this.#logger.error(`Error subscribing to ingestDeviceRundownStatus: ${stringifyError(e)}`) |
| 48 | + }) |
| 49 | + |
| 50 | + // Setup the observer immediately, which will trigger a resync upon the documents being added |
| 51 | + this.#observer = coreMosHandler.core.observe(PeripheralDevicePubSubCollectionsNames.ingestRundownStatus) |
| 52 | + this.#observer.added = (id) => this.#rundownChanged(id) |
| 53 | + this.#observer.changed = (id) => this.#rundownChanged(id) |
| 54 | + this.#observer.removed = (id) => this.#rundownChanged(id) |
| 55 | + } |
| 56 | + |
| 57 | + #rundownChanged(id: RundownId): void { |
| 58 | + const collection = this.#coreMosHandler.core.getCollection( |
| 59 | + PeripheralDevicePubSubCollectionsNames.ingestRundownStatus |
| 60 | + ) |
| 61 | + |
| 62 | + const newStatuses = collection.findOne(id) |
| 63 | + const previousStatuses = this.#lastStatuses.get(id) |
| 64 | + |
| 65 | + // Update the last statuses store |
| 66 | + if (newStatuses) { |
| 67 | + this.#lastStatuses.set(id, newStatuses) |
| 68 | + } else { |
| 69 | + this.#lastStatuses.delete(id) |
| 70 | + } |
| 71 | + |
| 72 | + const statusDiff = diffStatuses(previousStatuses, newStatuses) |
| 73 | + if (statusDiff.length === 0) return |
| 74 | + |
| 75 | + // nocommit - send statuses to MOS device. with a queue? |
| 76 | + |
| 77 | + throw new Error('Method not implemented.') |
8 | 78 | } |
9 | 79 |
|
10 | 80 | dispose(): void { |
11 | | - // TODO |
| 81 | + this.#destroyed = true |
| 82 | + |
| 83 | + this.#observer?.stop() |
| 84 | + if (this.#subId) this.#coreMosHandler.core.unsubscribe(this.#subId) |
12 | 85 | } |
13 | 86 | } |
| 87 | + |
| 88 | +interface StoryStatusItem { |
| 89 | + storyId: string |
| 90 | + mosStatus: string |
| 91 | +} |
| 92 | + |
| 93 | +function diffStatuses( |
| 94 | + previousStatuses: IngestRundownStatus | undefined, |
| 95 | + newStatuses: IngestRundownStatus | undefined |
| 96 | +): StoryStatusItem[] { |
| 97 | + if (!previousStatuses && !newStatuses) return [] |
| 98 | + |
| 99 | + // TODO |
| 100 | + return [] |
| 101 | +} |
0 commit comments