|
| 1 | +import { NodeCG } from "nodecg-types/types/server"; |
| 2 | +import { Result, emptySuccess, success, error, ServiceBundle } from "nodecg-io-core"; |
| 3 | +import { Atem } from "atem-connection"; |
| 4 | + |
| 5 | +interface AtemServiceConfig { |
| 6 | + address: string; |
| 7 | + port?: number; |
| 8 | + debugBuffers?: boolean; |
| 9 | + disableMultithreaded?: boolean; |
| 10 | + childProcessTimeout?: number; |
| 11 | +} |
| 12 | + |
| 13 | +export type AtemServiceClient = Atem; |
| 14 | + |
| 15 | +module.exports = (nodecg: NodeCG) => { |
| 16 | + new AtemService(nodecg, "atem", __dirname, "../atem-schema.json").register(); |
| 17 | +}; |
| 18 | + |
| 19 | +class AtemService extends ServiceBundle<AtemServiceConfig, AtemServiceClient> { |
| 20 | + async validateConfig(config: AtemServiceConfig): Promise<Result<void>> { |
| 21 | + return new Promise((resolve, reject) => { |
| 22 | + const atem = new Atem(config); |
| 23 | + atem.connect(config.address, config.port); |
| 24 | + atem.on("connected", () => resolve(emptySuccess())); |
| 25 | + atem.on("error", (e) => reject(error(e))); |
| 26 | + }); |
| 27 | + } |
| 28 | + |
| 29 | + async createClient(config: AtemServiceConfig): Promise<Result<AtemServiceClient>> { |
| 30 | + return new Promise((resolve, _) => { |
| 31 | + const atem = new Atem(config); |
| 32 | + atem.connect(config.address, config.port); |
| 33 | + atem.on("connected", () => resolve(success(atem))); |
| 34 | + }); |
| 35 | + } |
| 36 | + |
| 37 | + stopClient(client: AtemServiceClient) { |
| 38 | + client.disconnect(); |
| 39 | + } |
| 40 | + |
| 41 | + removeHandlers(client: AtemServiceClient) { |
| 42 | + client.removeAllListeners(); |
| 43 | + } |
| 44 | +} |
0 commit comments