Skip to content

Commit

Permalink
feat: add custom commands
Browse files Browse the repository at this point in the history
  • Loading branch information
mint-dewit committed Apr 11, 2024
1 parent 278eb2b commit 9763f84
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/CasparCG.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ import {
BeginParameters,
CommitParameters,
DiscardParameters,
CustomCommandParameters,
} from './parameters'

export class CasparCG extends BasicCasparCGAPI {
Expand Down Expand Up @@ -582,5 +583,11 @@ export class CasparCG extends BasicCasparCGAPI {
params,
})
}
async sendCustom(params: CustomCommandParameters): Promise<APIRequest<Commands.Custom>> {
return this.executeCommand({
command: Commands.Custom,
params,
})
}
}
export type APIRequest<C extends Commands> = SendResult<CReturnType<C>>
18 changes: 18 additions & 0 deletions src/__tests__/serializers.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { serializers, serializersV21 } from '../serializers'
import {
CgAddCommand,
Commands,
CustomCommand,
InfoChannelCommand,
InfoCommand,
InfoLayerCommand,
Expand Down Expand Up @@ -251,4 +252,21 @@ describe('serializers', () => {

expect(result).toBe(`MIXER 1-10 FILL 0.1 0.2 0.7 0.8 20`)
})

it('should serialize a custom', () => {
const command: CustomCommand = {
command: Commands.Custom,
params: {
command: 'INFO 1',
},
}

const serialized = serializers[Commands.Custom].map((fn) => fn(command.command, command.params))

expect(serialized).toHaveLength(serializers[Commands.Custom].length)

const result = serialized.filter((l) => l !== '').join(' ')

expect(result).toBe('INFO 1')
})
})
7 changes: 7 additions & 0 deletions src/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ import {
CommitParameters,
PingParameters,
DiscardParameters,
CustomCommandParameters,
} from './parameters'

export enum Commands {
Expand Down Expand Up @@ -184,6 +185,8 @@ export enum Commands {
Begin = 'BEGIN',
Commit = 'COMMIT',
Discard = 'DISCARD',

Custom = 'CUSTOM',
}

export interface Command<Cmd extends Commands, Params> {
Expand Down Expand Up @@ -301,6 +304,8 @@ export interface AllTypedCommands {
[Commands.Begin]: TypedResponseCommand<Commands.Begin, BeginParameters, unknown>
[Commands.Commit]: TypedResponseCommand<Commands.Commit, CommitParameters, unknown>
[Commands.Discard]: TypedResponseCommand<Commands.Discard, DiscardParameters, unknown>

[Commands.Custom]: TypedResponseCommand<Commands.Custom, CustomCommandParameters, unknown>
}

export type LoadbgCommand = AllTypedCommands[Commands.Loadbg]['command']
Expand Down Expand Up @@ -391,6 +396,7 @@ export type PingCommand = AllTypedCommands[Commands.Ping]['command']
export type BeginCommand = AllTypedCommands[Commands.Begin]['command']
export type CommitCommand = AllTypedCommands[Commands.Commit]['command']
export type DiscardCommand = AllTypedCommands[Commands.Discard]['command']
export type CustomCommand = AllTypedCommands[Commands.Custom]['command']

export type AMCPCommand =
| LoadbgCommand
Expand Down Expand Up @@ -481,3 +487,4 @@ export type AMCPCommand =
| BeginCommand
| CommitCommand
| DiscardCommand
| CustomCommand
4 changes: 4 additions & 0 deletions src/parameters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -534,3 +534,7 @@ export type PingParameters = Empty
export type BeginParameters = Empty
export type CommitParameters = Empty
export type DiscardParameters = Empty

export interface CustomCommandParameters {
command: string
}
5 changes: 5 additions & 0 deletions src/serializers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
CGLayer,
CgUpdateParameters,
ClipParameters,
CustomCommandParameters,
DecklinkParameters,
HtmlParameters,
MixerTween,
Expand Down Expand Up @@ -114,6 +115,8 @@ const mixerTweenSerializer = (_: Commands, { tween, duration }: MixerTween) =>
const mixerSimpleValueSerializer = (_: Commands, { value }: { value: number | boolean | string }) =>
value !== undefined ? (typeof value === 'boolean' ? (value ? '1' : '0') : value + '') : ''

const customCommandSerializer = (_: Commands, { command }: CustomCommandParameters) => command

const optional: <T, Y extends object>(fn: (command: T, params: Y) => string) => (command: T, params: Y) => string =
(fn) => (command, params) => {
const keys = Object.keys(params)
Expand Down Expand Up @@ -469,6 +472,8 @@ export const serializers: Readonly<Serializers<AMCPCommand>> = {
[Commands.Begin]: [commandNameSerializer],
[Commands.Commit]: [commandNameSerializer],
[Commands.Discard]: [commandNameSerializer],

[Commands.Custom]: [customCommandSerializer],
}

export const serializersV21: Readonly<Serializers<AMCPCommand>> = {
Expand Down

0 comments on commit 9763f84

Please sign in to comment.