lua scripting integration

edited September 2 in Questions & Answers
I'm working on adding additional log points for certain events like modulation of flow rate by the user. I tired using the lua api, but had trouble registering the event callbacks like `server:registerRunAtStartup(log.init())`
, which fails like this: `Lua file /var/lib/Repetier-Server/lua/log.lua returns error /var/lib/Repetier-Server/lua/log.lua:63: attempt to index a nil value (global 'server') + when calling setup() `

 I've tried assigning ` rs = RepetierServer() ` and ` server = rs.server() ` , but the method is still not accessible. 
Do I need to require the server.lua script? or is the method built-in?



Script structure:
rs = RepetierServer() 
local log = {} 

log.foo = function()
end 

log.bar = function() 
end 

log.init = function() 
    server:registerRunAtStartup(log.foo) 
    server:registerAction('doBar', log.bar) 
end 

function setup() 
    log.init() 
end 

function loop()
    return 
end



Comments

  • rs is defined already since server module is included first to offer predefined stuff.

    local log = {} is called in global scope so not sure if local does anything here.

    Main problem is your lua location I think. In the lua directory you add monitor scripts that run in their own context and are printer independent. You want them included in modules like that:
    logs/lua/logs.lua

    That way rs/server is defined and it is assigned to your ptinter. Just forget endless loops. All calls must end quickly or you might block your printer. Idea is to register some hooks where you need modification, execute what is needed when it happens and let server continue handling the printer.

  • great, that worked well.
    I had the script in the wrong directory. As a module it executes as expected.
    The loop function was only added, because an error was thrown without it, when placing the script in the storage directory. This also was resolved.

    I was wondering though how to pass arguments to the configured action:
    In the touchscreen frontend I used the connectionHandler.send method to trigger the action successfully, but was not able to access the payload:

    //PrinterAction.ts
    setLogEntry(slug: string, payload: object): Promise<any> {
        return this.connectionHandler.send("logWrite", payload, slug);
    }

    the action in the lua script looks something like this:

    --logs.lua
    log = {}
    ...

    log.write_log = function (payload)
    ...
    end

    ...
    server:registerAction('logWrite', log.write_log)

    But trying to access the payload or registering the action with an argument was not successful. All I got was a memory pointer(?) like this "AccessContext (0xf691ccf0)" or this error:
    "Call of rs:toJsonObject without table"



  • From docs:

    server:registerAction

    Call Type: function(string action, callback f)->void
    Static: false

    Actions are the main method of starting actions from the frontend. Actions get send over the websocket and take a json payload and get a json response back. Make sure your action name is unique, e.g. by prefixing your module name. Callback function has signature (AccessContext access, Table data, Table result)->Table. data are the json parameter send. result is the result table that also should be returned. This is what the use will get back. For anything that does not return json data you need to use a dyn request instead.

    Note: Until version 1.4.10 the callback had the signature (Printer printer, Table data, Table result)->Table.


    You see first parameter is access and second parameter would contain data. With access you can easily check permissions, access user, ...



  • thanks alot, yeah I did not realize the documentation existed and only worked off of this. This solved it!
  • Ok, yes old docs do not match and miss a lot of functions added since then:-)
Sign In or Register to comment.