changeset 6212:3a974dbf4740

lua: new SetStableStatus function
author Alain Mazy <am@orthanc.team>
date Wed, 25 Jun 2025 15:08:50 +0200
parents 55b41faaaec3
children e64c3ae969e4 c6ae4244f075
files NEWS OrthancServer/Sources/LuaScripting.cpp OrthancServer/Sources/LuaScripting.h
diffstat 3 files changed, 62 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/NEWS	Wed Jun 25 14:14:47 2025 +0200
+++ b/NEWS	Wed Jun 25 15:08:50 2025 +0200
@@ -1,6 +1,12 @@
 Pending changes in the mainline
 ===============================
 
+General
+-------
+
+* Lua: new "SetStableStatus" function.
+
+
 Plugin SDK
 ----------
 
--- a/OrthancServer/Sources/LuaScripting.cpp	Wed Jun 25 14:14:47 2025 +0200
+++ b/OrthancServer/Sources/LuaScripting.cpp	Wed Jun 25 15:08:50 2025 +0200
@@ -570,6 +570,60 @@
     return 1;
   }
 
+  // Syntax in Lua: SetStableStatus(resourceId, true)
+  int LuaScripting::SetStableStatus(lua_State* state)
+  {
+    ServerContext* serverContext = GetServerContext(state);
+    if (serverContext == NULL)
+    {
+      LOG(ERROR) << "Lua: The Orthanc API is unavailable";
+      lua_pushnil(state);
+      return 1;
+    }
+
+    // Check the types of the arguments
+    int nArgs = lua_gettop(state);
+    if (nArgs < 1 || nArgs > 3 ||
+        !lua_isstring(state, 1) ||                 // Resource
+        !lua_isboolean(state, 2))                  // newStateIsStable
+    {
+      LOG(ERROR) << "Lua: Bad parameters to SetStableStatus()";
+      lua_pushnil(state);
+      return 1;
+    }
+
+    const char* resourceId = lua_tostring(state, 1);
+    bool newStateIsStable = lua_toboolean(state, 2);
+    
+    try
+    {
+      bool hasStateChanged = false;
+
+      if (serverContext->GetIndex().SetStableStatus(hasStateChanged, resourceId, newStateIsStable))
+      {
+        if (hasStateChanged)
+        {
+          lua_pushboolean(state, 1);
+        }
+        else
+        {
+          lua_pushboolean(state, 0);
+        }
+
+        return 1;
+      }
+    }
+    catch (OrthancException& e)
+    {
+      LOG(ERROR) << "Lua: " << e.What();
+    }
+
+    LOG(ERROR) << "Lua: Error in SetStableStatus() for Resource: " << resourceId;
+    lua_pushnil(state);
+
+    return 1;
+  }
+
 
   // Syntax in Lua: GetOrthancConfiguration()
   int LuaScripting::GetOrthancConfiguration(lua_State *state)
@@ -760,6 +814,7 @@
     lua_.RegisterFunction("RestApiPut", RestApiPut);
     lua_.RegisterFunction("RestApiDelete", RestApiDelete);
     lua_.RegisterFunction("GetOrthancConfiguration", GetOrthancConfiguration);
+    lua_.RegisterFunction("SetStableStatus", SetStableStatus);
 
     LOG(INFO) << "Initializing Lua for the event handler";
     LoadGlobalConfiguration();
--- a/OrthancServer/Sources/LuaScripting.h	Wed Jun 25 14:14:47 2025 +0200
+++ b/OrthancServer/Sources/LuaScripting.h	Wed Jun 25 15:08:50 2025 +0200
@@ -62,6 +62,7 @@
     static int RestApiPut(lua_State *state);
     static int RestApiDelete(lua_State *state);
     static int GetOrthancConfiguration(lua_State *state);
+    static int SetStableStatus(lua_State* state);
 
     size_t ParseOperation(LuaJobManager::Lock& lock,
                           const std::string& operation,