From e26fe6ac7e508b7b28a1654bad33afc24cef2171 Mon Sep 17 00:00:00 2001 From: javalikescript Date: Sat, 1 Feb 2025 08:15:54 +0100 Subject: [PATCH] Add extension actions --- lha/restEngine.lua | 24 +++++++++++++++++++++++- lha/schema-extension.json | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+), 1 deletion(-) diff --git a/lha/restEngine.lua b/lha/restEngine.lua index dc7a892..b20e1a8 100644 --- a/lha/restEngine.lua +++ b/lha/restEngine.lua @@ -162,6 +162,28 @@ local REST_EXTENSIONS = { extension:publishEvent('poll') end end, + action = { + ['{index}(extension, index, requestJson)?method=POST'] = function(exchange, extension, index, arguments) + local actions = extension:getManifest('actions') + index = math.tointeger(index) + local action = index and actions and actions[index] + if action and action.method then + local method = extension[action.method] + if type(method) ~= 'function' then + HttpExchange.internalServerError(exchange, 'The action method is not available') + elseif extension:isActive() ~= action.active then + HttpExchange.badRequest(exchange, 'The extension active state does not match') + elseif (action.arguments and #action.arguments or 0) == #arguments then + HttpExchange.badRequest(exchange, 'The action arguments are invalid') + else + return method(extension, arguments) + end + else + HttpExchange.notFound(exchange) + end + return false + end + }, ['test(extension)?method=POST'] = function(exchange, extension) extension:publishEvent('test') end, @@ -366,7 +388,7 @@ local REST_THINGS = { return false end end, - ['{thingId}'] = { + ['{thingId}'] = { -- TODO review routing [''] = function(exchange) local engine = exchange:getAttribute('engine') local thingId = exchange:getAttribute('thingId') diff --git a/lha/schema-extension.json b/lha/schema-extension.json index 6997a64..504bc5d 100644 --- a/lha/schema-extension.json +++ b/lha/schema-extension.json @@ -1,6 +1,38 @@ { "additionalProperties": false, "properties": { + "actions": { + "title": "The extension actions", + "type": "array", + "items": { + "type": "object", + "properties": { + "name": { + "title": "The name of the action", + "type": "string", + "required": true + }, + "description": { + "title": "The description of the action", + "type": "string" + }, + "method": { + "title": "The name of the extension method to trigger the action", + "type": "string", + "required": true + }, + "arguments": { + "title": "The list of schema defining the arguments to pass to the method", + "type": "array" + }, + "active": { + "default": true, + "title": "Indicates that the action is available when the extension is enabled", + "type": "boolean" + } + } + } + }, "config": { "title": "The extension default configuration", "type": "object"