Skip to content

Commit a605997

Browse files
committed
wip: boilerplate for diffing and things
1 parent 0fadc8a commit a605997

File tree

2 files changed

+92
-3
lines changed

2 files changed

+92
-3
lines changed

packages/mos-gateway/src/mosHandler.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,7 @@ export class MosHandler {
300300
deviceEntry.statusHandler.dispose()
301301
}
302302
deviceEntry.statusHandler = new MosStatusHandler(
303+
this._logger,
303304
mosDevice,
304305
coreMosHandler,
305306
deviceEntry.deviceOptions.statuses
Lines changed: 91 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,101 @@
11
import type { IMOSDevice } from '@mos-connection/connector'
22
import type { MosDeviceStatusesConfig } from './generated/devices'
33
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')
414

515
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.')
878
}
979

1080
dispose(): void {
11-
// TODO
81+
this.#destroyed = true
82+
83+
this.#observer?.stop()
84+
if (this.#subId) this.#coreMosHandler.core.unsubscribe(this.#subId)
1285
}
1386
}
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

Comments
 (0)