|
| 1 | +import type { BitFireInstance } from './main.js' |
| 2 | +import { |
| 3 | + CompanionActionDefinitions, |
| 4 | + CompanionInputFieldDropdown, |
| 5 | + CompanionInputFieldTextInput |
| 6 | +} from "@companion-module/base/dist/index.js"; |
| 7 | + |
| 8 | +export interface BitfireArgumentChoice { |
| 9 | + [key: string]: string |
| 10 | +} |
| 11 | + |
| 12 | +export interface BitFireCommandArg { |
| 13 | + description: string |
| 14 | + required: boolean, |
| 15 | + default?: string, |
| 16 | + // For right now, number is same as string. |
| 17 | + type: "string" | "number" | "dropdown", |
| 18 | + // Empty object when there are no choices defined. |
| 19 | + choices: BitfireArgumentChoice[] | Record<string, never> |
| 20 | +} |
| 21 | + |
| 22 | +export interface BitFireCommandArgs { |
| 23 | + [argName: string]: BitFireCommandArg |
| 24 | +} |
| 25 | + |
| 26 | +export interface BitFireCommand { |
| 27 | + description: string |
| 28 | + args: BitFireCommandArgs |
| 29 | +} |
| 30 | + |
| 31 | +export interface BitFireProviderMessage { |
| 32 | + /// Provider name |
| 33 | + name: string |
| 34 | + uri: string |
| 35 | + commands: { [commandName: string]: BitFireCommand} |
| 36 | +} |
| 37 | + |
| 38 | +export function validateProviderMessage(msg: unknown): msg is BitFireProviderMessage { |
| 39 | + if (typeof msg !== 'object' || msg === null) return false; |
| 40 | + |
| 41 | + const obj = msg as Record<string, any>; |
| 42 | + |
| 43 | + if (typeof obj.name !== 'string') return false; |
| 44 | + if (typeof obj.uri !== 'string') return false; |
| 45 | + if (typeof obj.commands !== 'object' || obj.commands === null) return false; |
| 46 | + const commands = Object.values(obj.commands); |
| 47 | + return commands.every(validateBitFireCommand); |
| 48 | +} |
| 49 | + |
| 50 | +function validateChoices(choices: any): boolean { |
| 51 | + if(typeof choices !== 'object' || choices === null) return false; |
| 52 | + |
| 53 | + if(Array.isArray(choices)) { |
| 54 | + return choices.every(choice => |
| 55 | + typeof choice === "object" && |
| 56 | + choice !== null && |
| 57 | + !Array.isArray(choice) |
| 58 | + ); |
| 59 | + } |
| 60 | + |
| 61 | + // empty object {}, otherwise any non-null object would pass |
| 62 | + return Object.keys(choices).length === 0; |
| 63 | +} |
| 64 | + |
| 65 | +function validateBitFireCommand(cmd: any): boolean { |
| 66 | + if (typeof cmd !== 'object' || cmd === null) return false; |
| 67 | + if (typeof cmd.description !== 'string') return false; |
| 68 | + if (typeof cmd.args !== 'object' || cmd.args === null) return false; |
| 69 | + |
| 70 | + const validTypes = ["string", "number", "dropdown"]; |
| 71 | + |
| 72 | + return Object.values(cmd.args).every((arg: any) => { |
| 73 | + return ( |
| 74 | + typeof arg === 'object' && arg !== null && |
| 75 | + typeof arg.description === 'string' && |
| 76 | + typeof arg.required === 'boolean' && |
| 77 | + validTypes.includes(arg.type) && |
| 78 | + validateChoices(arg.choices) |
| 79 | + ); |
| 80 | + }); |
| 81 | +} |
| 82 | + |
| 83 | +/** |
| 84 | + * Given a 'set_provider' message from a Macro Engine, attempt to convert it to the `BitFireProviderMessage` interface, |
| 85 | + * and then create `CompanionActionDefinition` objects to update the actions of `self`. |
| 86 | + * @param module - `BitFireInstance` module |
| 87 | + * @param msg - 'set_providers' Macro Engine API response |
| 88 | + */ |
| 89 | +export function handleSetProviderMessage(module: BitFireInstance, msg: unknown): void { |
| 90 | + if(!validateProviderMessage(msg)){ |
| 91 | + module.log('error', `Received invalid provider message.`) |
| 92 | + return; |
| 93 | + } |
| 94 | + |
| 95 | + let bf_provider = msg |
| 96 | + let actions = makeActionsFromProviderMessage(bf_provider, module) |
| 97 | + |
| 98 | + // the module keeps track of all possible actions by provider. we generate a 'master' list to pass to |
| 99 | + // setActionDefinitions everytime a new 'set_provider' message comes in |
| 100 | + module.updateProviders(bf_provider, actions) |
| 101 | + let allProviders = module.getProviders() |
| 102 | + const masterProviderList: CompanionActionDefinitions = Object.assign({}, ...Object.values(allProviders)) |
| 103 | + module.setActionDefinitions(masterProviderList) |
| 104 | +} |
| 105 | + |
| 106 | +/** |
| 107 | + * Create a `CompanionInputField` from a `BitFireCommandArg`. |
| 108 | + * @param name Name of the BF Command. |
| 109 | + * @param args data in the shape of a `BitFireCommandArg` |
| 110 | + */ |
| 111 | +function makeActionInput(name: string, args: BitFireCommandArg): CompanionInputFieldTextInput | CompanionInputFieldDropdown { |
| 112 | + const base = { |
| 113 | + id: name, |
| 114 | + label: name, |
| 115 | + tooltip: args.description, |
| 116 | + } |
| 117 | + |
| 118 | + if(args.type === 'dropdown' && Array.isArray(args.choices)) { |
| 119 | + return { |
| 120 | + ...base, |
| 121 | + type: 'dropdown', |
| 122 | + choices: args.choices.map(choice => { |
| 123 | + const [id, label] = Object.entries(choice)[0] |
| 124 | + return {id,label} |
| 125 | + }), |
| 126 | + // Use default if it's present, otherwise first choice. an empty string as a last resort. |
| 127 | + default: args.default ?? Object.keys(args.choices[0])[0] ?? '', |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + return { |
| 132 | + ...base, |
| 133 | + type: 'textinput' as const, |
| 134 | + // Required should always be set in BitFireCommandArg, but this is safer for potential undefined |
| 135 | + required: args.required === true, |
| 136 | + } |
| 137 | +} |
| 138 | +/** |
| 139 | + * Create `CompanionActionDefinitions` from a `BitFireProviderMessage`. |
| 140 | + * @param providerMessage - 'set_provider' message parsed into a `BitFireProviderMessage` interface |
| 141 | + * @param module - `BitFireInstance` module |
| 142 | + */ |
| 143 | +function makeActionsFromProviderMessage(providerMessage: BitFireProviderMessage, module: BitFireInstance): CompanionActionDefinitions { |
| 144 | + let actions: CompanionActionDefinitions = {} |
| 145 | + |
| 146 | + // iterate over all the commands present in the provider message |
| 147 | + // and create the options portion of a CompanionActionDefinition |
| 148 | + for (const [commandName, bfCommand] of Object.entries(providerMessage.commands)) { |
| 149 | + const options = Object.entries(bfCommand.args).map(([name, args]) => |
| 150 | + makeActionInput(name, args) |
| 151 | + ) |
| 152 | + |
| 153 | + // Some providers share commands (i.e. All providers have a "send" command). Include the |
| 154 | + // provider name so shared commands don't get combined when we generate `masterProviderList`. |
| 155 | + let uniqueID = `${providerMessage.name}_${commandName}`; |
| 156 | + actions[uniqueID] = { |
| 157 | + name: providerMessage.name + ": " + commandName, |
| 158 | + description: bfCommand.description, |
| 159 | + options: options, |
| 160 | + callback: async (event) => { |
| 161 | + module.sendMessage(providerMessage.name, commandName, event.options) |
| 162 | + } |
| 163 | + } |
| 164 | + } |
| 165 | + |
| 166 | + return actions |
| 167 | +} |
0 commit comments