* Perform an action on the thing. * * @param {String} actionName Name of the action * @param {Object} input Any action inputs * @returns {Object} The action that was created.
(
actionName: string,
input: InputType | null
)
| 449 | * @returns {Object} The action that was created. |
| 450 | */ |
| 451 | performAction<InputType = any>( |
| 452 | actionName: string, |
| 453 | input: InputType | null |
| 454 | ): Action<InputType>|undefined { |
| 455 | input = input || null; |
| 456 | |
| 457 | if (!this.availableActions.hasOwnProperty(actionName)) { |
| 458 | return; |
| 459 | } |
| 460 | |
| 461 | const actionType = this.availableActions[actionName]; |
| 462 | |
| 463 | if (actionType.metadata.hasOwnProperty('input')) { |
| 464 | const schema = JSON.parse(JSON.stringify(actionType.metadata.input)); |
| 465 | |
| 466 | if (schema.hasOwnProperty('properties')) { |
| 467 | const props: { [propertyName: string]: any }[] = |
| 468 | Object.values(schema.properties); |
| 469 | |
| 470 | for (const prop of props) { |
| 471 | delete prop.title; |
| 472 | delete prop.unit; |
| 473 | delete prop['@type']; |
| 474 | } |
| 475 | } |
| 476 | |
| 477 | const valid = ajv.validate(schema, input); |
| 478 | if (!valid) { |
| 479 | return; |
| 480 | } |
| 481 | } |
| 482 | |
| 483 | const action: Action<InputType> = new actionType.class(this, input); |
| 484 | action.setHrefPrefix(this.hrefPrefix); |
| 485 | this.actionNotify(action); |
| 486 | this.actions[actionName].push(action); |
| 487 | return action; |
| 488 | } |
| 489 | |
| 490 | /** |
| 491 | * Remove an existing action. |
no test coverage detected