Skip to content

Commit

Permalink
Merge pull request #33 from phillipivan/master
Browse files Browse the repository at this point in the history
Add Set Value ENUM from Variable
  • Loading branch information
phillipivan authored Sep 17, 2024
2 parents a2b5154 + 101ac60 commit 9665cd1
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 10 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "generic-emberplus",
"version": "2.3.0",
"version": "2.3.1",
"main": "dist/index.js",
"scripts": {
"prepare": "husky",
Expand Down Expand Up @@ -34,7 +34,7 @@
"emberplus-connection": "^0.2.1"
},
"devDependencies": {
"@companion-module/tools": "^2.0.3",
"@companion-module/tools": "^2.0.4",
"eslint": "^9.10.0",
"husky": "^9.1.6",
"lint-staged": "^15.2.0",
Expand Down
43 changes: 40 additions & 3 deletions src/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export enum ActionId {
SetValueString = 'setValueString',
SetValueBoolean = 'setValueBoolean',
SetValueEnum = 'setValueEnum',
SetValueEnumVariable = 'setValueEnumVariable',
MatrixConnect = 'matrixConnect',
MatrixDisconnect = 'matrixDisconnect',
MatrixSetConnection = 'matrixSetConnection',
Expand All @@ -32,6 +33,7 @@ const pathInput: CompanionInputFieldTextInput = {
label: 'Path',
id: 'path',
useVariables: true,
tooltip: `Path may be supplied in decimals: 1.2.3, or with a descriptor and the decimals wrapped in brackets: path/to/ember/element[1.2.3.4]`,
}
const matrixInputs: Array<CompanionInputFieldTextInput | CompanionInputFieldNumber> = [
pathInput,
Expand All @@ -53,10 +55,18 @@ const matrixInputs: Array<CompanionInputFieldTextInput | CompanionInputFieldNumb
},
]

async function resolvePath(self: InstanceBase<EmberPlusConfig>, path: string): Promise<string> {
const pathString: string = await self.parseVariablesInString(path)
if (pathString.includes('[') && pathString.includes(']')) {
return pathString.substring(pathString.indexOf('[') + 1, pathString.indexOf(']'))
}
return pathString
}

const setValue =
(self: InstanceBase<EmberPlusConfig>, emberClient: EmberClient, type: EmberModel.ParameterType) =>
async (action: CompanionActionEvent): Promise<void> => {
const path = await self.parseVariablesInString(action.options['path']?.toString() ?? '')
const path = await resolvePath(self, action.options['path']?.toString() ?? '')
const node = await emberClient.getElementByPath(path)
// TODO - do we handle not found?
if (node && node.contents.type === EmberModel.ElementType.Parameter) {
Expand All @@ -72,7 +82,18 @@ const setValue =
request.response?.catch(() => null) // Ensure the response is 'handled'
} else if (type === EmberModel.ParameterType.Integer) {
const value: number = parseInt(await self.parseVariablesInString(action.options['value']?.toString() ?? ''))
if (isNaN(value)) {
if (isNaN(value) || value > 4294967295 || value < -4294967295) {
return
}
const request = await emberClient.setValue(
node as EmberModel.NumberedTreeNode<EmberModel.Parameter>,
value,
false,
)
request.response?.catch(() => null) // Ensure the response is 'handled'
} else if (type === EmberModel.ParameterType.Enum) {
const value: number = parseInt(await self.parseVariablesInString(action.options['value']?.toString() ?? ''))
if (isNaN(value) || value > 4294967295 || value < 0) {
return
}
const request = await emberClient.setValue(
Expand Down Expand Up @@ -107,7 +128,7 @@ const doMatrixAction =
method: EmberClient['matrixConnect'] | EmberClient['matrixDisconnect'] | EmberClient['matrixSetConnection'],
) =>
async (action: CompanionActionEvent): Promise<void> => {
const path = await self.parseVariablesInString(action.options['path']?.toString() ?? '')
const path = await resolvePath(self, action.options['path']?.toString() ?? '')
self.log('debug', 'Get node ' + path)
const node = await emberClient.getElementByPath(path)
// TODO - do we handle not found?
Expand Down Expand Up @@ -349,6 +370,22 @@ export function GetActionsList(
],
callback: setValue(self, emberClient, EmberModel.ParameterType.Enum),
},
[ActionId.SetValueEnumVariable]: {
name: 'Set Value ENUM from Variable (as Integer)',
options: [
pathInput,
{
type: 'textinput',
label: 'Value',
id: 'value',
required: true,
useVariables: true,
default: '0',
tooltip: 'Return an integer between 0 and 4294967295',
},
],
callback: setValue(self, emberClient, EmberModel.ParameterType.Enum),
},
[ActionId.SetValueString]: {
name: 'Set Value String',
options: [
Expand Down
10 changes: 5 additions & 5 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ __metadata:
languageName: node
linkType: hard

"@companion-module/tools@npm:^2.0.3":
version: 2.0.3
resolution: "@companion-module/tools@npm:2.0.3"
"@companion-module/tools@npm:^2.0.4":
version: 2.0.4
resolution: "@companion-module/tools@npm:2.0.4"
dependencies:
"@eslint/js": "npm:^9.10.0"
eslint-config-prettier: "npm:^9.1.0"
Expand Down Expand Up @@ -52,7 +52,7 @@ __metadata:
companion-generate-manifest: scripts/generate-manifest.js
companion-module-build: scripts/build.js
companion-module-check: scripts/check.js
checksum: 10c0/b69b27071442c92f1df1aa1ddc860556502e2774b12d1cdcae0db162f7b0ca534b1d2050894e188b5c48edcf34e67a3ca7843bd6bb7cc7352abe0bbf4508d543
checksum: 10c0/ce795f8ede524b2984a5c4fc744267fc8b9dab8fa9602295f13348ce340cf9e8ec5ebbb1ab4cf415f8780ba8d89f4d67f936a4d5b9f0539ba9573cdbc129d6d4
languageName: node
linkType: hard

Expand Down Expand Up @@ -1533,7 +1533,7 @@ asn1@evs-broadcast/node-asn1:
resolution: "generic-emberplus@workspace:."
dependencies:
"@companion-module/base": "npm:~1.10.0"
"@companion-module/tools": "npm:^2.0.3"
"@companion-module/tools": "npm:^2.0.4"
"@types/node": "npm:^18.19.45"
emberplus-connection: "npm:^0.2.1"
eslint: "npm:^9.10.0"
Expand Down

0 comments on commit 9665cd1

Please sign in to comment.