Skip to content

Commit

Permalink
Merge pull request #38 from phillipivan/main
Browse files Browse the repository at this point in the history
Feedback path accepts variables
  • Loading branch information
phillipivan authored Sep 22, 2024
2 parents 06eae4a + 4caf941 commit 6fda424
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 13 deletions.
6 changes: 5 additions & 1 deletion src/actions.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type {
CompanionActionDefinition,
CompanionActionDefinitions,
CompanionFeedbackContext,
CompanionActionEvent,
CompanionInputFieldNumber,
CompanionInputFieldTextInput,
Expand Down Expand Up @@ -56,7 +57,10 @@ const matrixInputs: Array<CompanionInputFieldTextInput | CompanionInputFieldNumb
},
]

async function resolvePath(self: InstanceBase<EmberPlusConfig>, path: string): Promise<string> {
export async function resolvePath(
self: InstanceBase<EmberPlusConfig> | CompanionFeedbackContext,
path: string,
): Promise<string> {
const pathString: string = await self.parseVariablesInString(path)
if (pathString.includes('[') && pathString.includes(']')) {
return pathString.substring(pathString.indexOf('[') + 1, pathString.indexOf(']'))
Expand Down
25 changes: 20 additions & 5 deletions src/feedback.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { combineRgb, InstanceBase } from '@companion-module/base'
import { combineRgb /*InstanceBase*/ } from '@companion-module/base'
import type { CompanionFeedbackDefinition, CompanionFeedbackDefinitions, DropdownChoice } from '@companion-module/base'
import type { EmberPlusInstance } from './index'
import { EmberClient } from 'emberplus-connection'
import { resolvePath } from './actions'
import type { EmberPlusConfig } from './config'
import { EmberPlusState } from './state'

Expand All @@ -14,7 +16,7 @@ export enum FeedbackId {
}

export function GetFeedbacksList(
_self: InstanceBase<EmberPlusConfig>,
_self: EmberPlusInstance, //InstanceBase<EmberPlusConfig>,
_emberClient: EmberClient,
config: EmberPlusConfig,
state: EmberPlusState,
Expand All @@ -35,6 +37,7 @@ export function GetFeedbacksList(
id: 'path',
choices: config.monitoredParameters?.map((item) => <DropdownChoice>{ id: item, label: item }) ?? [],
default: config.monitoredParameters?.find(() => true) ?? 'No paths configured!',
allowCustom: true,
},
{
type: 'number',
Expand All @@ -46,8 +49,12 @@ export function GetFeedbacksList(
default: 0,
},
],
callback: (feedback) => {
return state.parameters.get(feedback.options['path']?.toString() ?? '') == feedback.options['value']?.toString()
callback: async (feedback, context) => {
const path = await resolvePath(context, feedback.options['path']?.toString() ?? '')
return state.parameters.get(path) == feedback.options['value']?.toString()
},
subscribe: async (feedback, context) => {
await _self.registerNewParameter(await resolvePath(context, feedback.options['path']?.toString() ?? ''))
},
},
[FeedbackId.String]: {
Expand All @@ -65,6 +72,7 @@ export function GetFeedbacksList(
id: 'path',
choices: config.monitoredParameters?.map((item) => <DropdownChoice>{ id: item, label: item }) ?? [],
default: config.monitoredParameters?.find(() => true) ?? 'No paths configured!',
allowCustom: true,
},
{
type: 'textinput',
Expand All @@ -76,8 +84,15 @@ export function GetFeedbacksList(
},
],
callback: async (feedback, context) => {
const path = await resolvePath(
_self,
await context.parseVariablesInString(feedback.options['path']?.toString() ?? ''),
)
const value: string = await context.parseVariablesInString(feedback.options['value']?.toString() ?? '')
return state.parameters.get(feedback.options['path']?.toString() ?? '') == value
return state.parameters.get(path) == value
},
subscribe: async (feedback, context) => {
await _self.registerNewParameter(await resolvePath(context, feedback.options['path']?.toString() ?? ''))
},
},
[FeedbackId.Take]: {
Expand Down
40 changes: 33 additions & 7 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const reconnectInterval: number = 300000 //emberplus-connection destroys socket
/**
* Companion instance class for generic EmBER+ Devices
*/
class EmberPlusInstance extends InstanceBase<EmberPlusConfig> {
export class EmberPlusInstance extends InstanceBase<EmberPlusConfig> {
private emberClient!: EmberClient
private config!: EmberPlusConfig
private state!: EmberPlusState
Expand Down Expand Up @@ -46,7 +46,7 @@ class EmberPlusInstance extends InstanceBase<EmberPlusConfig> {
this.setupMatrices()
this.setupMonitoredParams()

this.updateCompanionBits()
this.updateCompanionBits(true)
}

/**
Expand All @@ -64,7 +64,7 @@ class EmberPlusInstance extends InstanceBase<EmberPlusConfig> {
this.setupEmberConnection()
this.setupMatrices()
this.setupMonitoredParams()
this.updateCompanionBits()
this.updateCompanionBits(true)
}

/**
Expand All @@ -83,10 +83,11 @@ class EmberPlusInstance extends InstanceBase<EmberPlusConfig> {
this.emberClient.discard()
}

private updateCompanionBits(): void {
this.setActionDefinitions(GetActionsList(this, this.client, this.config, this.state, this.emberQueue))
private updateCompanionBits(updateAll: boolean): void {
this.setFeedbackDefinitions(GetFeedbacksList(this, this.client, this.config, this.state))
this.setVariableDefinitions(GetVariablesList(this.config))
if (!updateAll) return
this.setActionDefinitions(GetActionsList(this, this.client, this.config, this.state, this.emberQueue))
this.setPresetDefinitions(GetPresetsList())
}

Expand Down Expand Up @@ -197,11 +198,36 @@ class EmberPlusInstance extends InstanceBase<EmberPlusConfig> {
})
}
}

public async registerNewParameter(path: string): Promise<void> {
if (this.config.monitoredParameters?.includes(path) === true) return
this.emberQueue
.add(async () => {
try {
const initial_node = await this.emberClient.getElementByPath(path, (node) => {
this.handleChangedValue(path, node).catch((e) => this.log('error', 'Error handling parameter ' + e))
})
if (initial_node) {
this.log('debug', 'Registered for path "' + path + '"')
if (this.config.monitoredParameters) {
this.config.monitoredParameters.push(path)
this.updateCompanionBits(false)
}
await this.handleChangedValue(path, initial_node)
}
} catch (e) {
this.log('error', 'Failed to subscribe to path "' + path + '": ' + e)
}
})
.catch((e) => {
this.log('debug', `Failed to register parameter: ${e.toString()}`)
})
}
// Track whether actions are being recorded
public handleStartStopRecordActions(isRecording: boolean) {
public handleStartStopRecordActions(isRecording: boolean): void {
this.isRecordingActions = isRecording
}
private async handleChangedValue(path: string, node: TreeElement<EmberElement>) {
public async handleChangedValue(path: string, node: TreeElement<EmberElement>): Promise<void> {
if (node.contents.type == ElementType.Parameter) {
this.log('debug', 'Got parameter value for ' + path + ': ' + (node.contents.value?.toString() ?? ''))
this.state.parameters.set(path, node.contents.value?.toString() ?? '')
Expand Down

0 comments on commit 6fda424

Please sign in to comment.