Logic Machine Forum
Execution of a resident script - Printable Version

+- Logic Machine Forum (https://forum.logicmachine.net)
+-- Forum: LogicMachine eco-system (https://forum.logicmachine.net/forumdisplay.php?fid=1)
+--- Forum: Scripting (https://forum.logicmachine.net/forumdisplay.php?fid=8)
+--- Thread: Execution of a resident script (/showthread.php?tid=4749)



Execution of a resident script - a455115 - 04.05.2023

Hello,

Is it possible for a resident script to be executed from a file that is uploaded to a device and not be visible in the window of the LM?


RE: Execution of a resident script - admin - 04.05.2023

You can use a user library with "Keep source" disabled.


RE: Execution of a resident script - a455115 - 04.05.2023

Can you give me some details?
How do I activate it? Can I change it if I need to?
Sorry for the stupid questions!


RE: Execution of a resident script - admin - 04.05.2023

See page 39: https://openrb.com/wp-content/uploads/2022/05/LM-manual_3.2022.pdf

User library example:
Code:
return function()
-- your code here
end

Resident script example (change my_user_library to your user library name):
Code:
fn = require('user.my_user_library')
fn()



RE: Execution of a resident script - a455115 - 04.05.2023

Thank you so much!
Am I to understand that if I have the library saved, make changes and set it to the same name, it will overwrite the original library? Is this code backed up with the project?


RE: Execution of a resident script - admin - 04.05.2023

Without "Keep source" only the compiled bytecode library is saved. The editor will be empty in this case because the source is not kept. You can put new code there and it will overwrite the current library. The backup will include such library but without the source code of course.


RE: Execution of a resident script - a455115 - 04.05.2023

This is exactly what I needed.
Do I have to write this code (fn = require('user.my_user_library')fn()) in a resident script to call the library?


RE: Execution of a resident script - admin - 04.05.2023

Yes, this code is needed. It can also be shortened like this:
Code:
require('user.my_user_library')()



RE: Execution of a resident script - a455115 - 18.05.2023

I couldn't do something. Can you give a concrete example of how to put this sample code into a library, running as a resident script with 0 sleep time?
Code:
if not pirs then
  pirs = {
    { input = '1/1/1', output = '2/1/1' },
    { input = '1/1/2', output = '2/1/2' },
  }
end

now = os.time()
for _, pir in ipairs(pirs) do
  key = 'pir_timer_' .. pir.input
  delta = now - storage.get(key, 0)

  -- set output to 0% after 1 hour
  if delta > 60 * 60 then
    grp.checkwrite(pir.output, 0)
  -- set output to 20% after 30 minutes
  elseif delta > 30 * 60 then
    grp.checkwrite(pir.output, 20)
  end



RE: Execution of a resident script - Daniel - 18.05.2023

An example. I created user library with name 'test'

This is the library:
Code:
return function()
log('test')
end


Each time it is called it will create log 'test'

To cal it create a resident or any other script and use this code.

Code:
fn = require('user.test')
fn()



RE: Execution of a resident script - a455115 - 19.05.2023

Thanks a lot, now I understand how it works.