Skip to content

Commit

Permalink
SOF-2202 Expose endpoints for devices make ready and stand down
Browse files Browse the repository at this point in the history
  • Loading branch information
LindvedKrvang committed Sep 9, 2024
1 parent cc5573e commit 55702d0
Show file tree
Hide file tree
Showing 3 changed files with 271 additions and 16 deletions.
4 changes: 4 additions & 0 deletions packages/playout-gateway/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,13 @@
"@sofie-automation/server-core-integration": "link:../server-core-integration",
"@sofie-automation/shared-lib": "link:../shared-lib",
"@tv2media/logger": "^2.0.0",
"@types/koa": "^2.15.0",
"@types/koa-router": "^7.4.8",
"debug": "^4.3.3",
"fast-clone": "^1.5.13",
"influx": "^5.9.3",
"koa": "^2.15.3",
"koa-router": "^12.0.1",
"object-hash": "^3.0.0",
"timeline-state-resolver": "npm:@tv2media/timeline-state-resolver@3.7.9",
"tslib": "^2.4.0",
Expand Down
58 changes: 56 additions & 2 deletions packages/playout-gateway/src/connector.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { TSRHandler, TSRConfig } from './tsrHandler'
import { CoreHandler, CoreConfig } from './coreHandler'
import { TSRConfig, TSRHandler } from './tsrHandler'
import { CoreConfig, CoreHandler } from './coreHandler'
import { Logger } from './logger'
import { Process } from './process'
import { InfluxConfig } from './influxdb'
import { PeripheralDeviceId } from '@sofie-automation/shared-lib/dist/core/model/Ids'
import * as Koa from 'koa'
import * as KoaRouter from 'koa-router'

export interface Config {
process: ProcessConfig
Expand All @@ -12,16 +14,24 @@ export interface Config {
tsr: TSRConfig
influx: InfluxConfig
}

export interface ProcessConfig {
/** Will cause the Node application to blindly accept all certificates. Not recommenced unless in local, controlled networks. */
unsafeSSL: boolean
/** Paths to certificates to load, for SSL-connections */
certificates: string[]
}

export interface DeviceConfig {
deviceId: PeripheralDeviceId
deviceToken: string
}

interface KoaContext {
query: Record<string, string | string[] | undefined>
body: any
}

export class Connector {
private tsrHandler: TSRHandler | undefined
private coreHandler: CoreHandler | undefined
Expand All @@ -30,6 +40,7 @@ export class Connector {

constructor(logger: Logger) {
this.logger = logger.tag(this.constructor.name)
this.setupKoaEndpoints()
}

public async init(config: Config): Promise<void> {
Expand All @@ -42,6 +53,7 @@ export class Connector {
this.logger.info('Initializing Core...')
this.coreHandler = new CoreHandler(this.logger, config.device)
await this.coreHandler.init(config.core, this._process)

this.logger.info('Core initialized')

this.logger.info('Initializing TSR...')
Expand Down Expand Up @@ -81,4 +93,46 @@ export class Connector {
process.exit(0)
}, 10 * 1000)
}

/* eslint-disable @typescript-eslint/no-inferrable-types */
/* eslint-disable @typescript-eslint/no-empty-function */
private setupKoaEndpoints(): void {
const koaRouter: KoaRouter = new KoaRouter()
this.setupDevicesMakeReadyEndpoint(koaRouter)
this.setupDevicesStandDownEndpoint(koaRouter)

const port: number = 3009
const koaApp: Koa = new Koa()

koaApp.use(koaRouter.routes()).use(koaRouter.allowedMethods())
koaApp.listen(port, () => {})
}

/* eslint-disable @typescript-eslint/prefer-as-const */
private setupDevicesMakeReadyEndpoint(router: KoaRouter): KoaRouter {
const okToDestroyStuffQueryParameterName: 'okToDestroyStuff' = 'okToDestroyStuff'
const activeRundownIdParameterName: 'activeRundownId' = 'activeRundownId'

router.post(`/devicesMakeReady`, async (context: KoaContext): Promise<void> => {
const okToDestroyStuff: boolean = context.query[okToDestroyStuffQueryParameterName] === 'true'
const activeRundownId: string | string[] | undefined = context.query[activeRundownIdParameterName]
await this.coreHandler?.devicesMakeReady(
okToDestroyStuff,
Array.isArray(activeRundownId) ? activeRundownId[0] : activeRundownId
)

context.body = 'DevicesMakeReady called!'
})
return router
}

private setupDevicesStandDownEndpoint(router: KoaRouter): KoaRouter {
router.post('/devicesStandDown', async (context: KoaContext): Promise<void> => {
const okToDestroyStuff: boolean = true // We are cleaning up, so it's always okay to destroy stuff.
await this.coreHandler?.devicesStandDown(okToDestroyStuff)

context.body = 'DevicesStandDown Called!'
})
return router
}
}
Loading

0 comments on commit 55702d0

Please sign in to comment.