Logic Machine Forum
Script to Monitor Remote System - - 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: Script to Monitor Remote System - (/showthread.php?tid=4280)



Script to Monitor Remote System - - jamesng - 03.10.2022

Hi

I have a script which I use to monitor the zones in a remote alarm system.    It's a looping script which processes the data coming in from a socket connection.  I need one instance of the script running continuously.

What's the best place to locate this (script below) - is it as a resident script with a 0 interval time or as an init script or somewhere else?

Many thanks in advance

Kind Regards,
James




Code:
-- Initialise socket connection
if not sock then
  require('socket')

  sock, err = socket.connect('192.168.1.100', 2401)
  if sock then
    sock:settimeout(1)
  else
    log('Error connecting ' .. err)
    sleep(5)
  end
end


-- Process data received
if sock then   
 
  while true do
    rec = sock:receive('*l')

    if rec then
      
      data = lmcore.hextostr(rec, true)
      st, addr, len, cmd, event, id, area = data:byte(1, 7)
 
        if (event == 0) then
          log('Door open in Zone ' .. id)
        else
          log('Door closed in Zone ' .. id)
        end
     
    end
  end
 
end



RE: Script to Monitor Remote System - - admin - 03.10.2022

You can make multiple copies of this script as a resident script with sleep time set to 0. init script should not have any blocking or long-running parts.

Also non-blocking connect should be used and possible errors during receive should be checked:
Code:
if not sock then
  sock = require('socket').tcp()
  sock:settimeout(1)

  res, err = sock:connect('192.168.1.100', 2401)

  if not res then
    log('connect failed', err)

    sock:close()
    sock = nil
    os.sleep(1)
  end
end

if sock then    
  res, err = sock:receive('*l')

  if res then
    -- parse res
  elseif err == 'closed' then
    log('connection closed')

    sock:close()
    sock = nil
  end
end



RE: Script to Monitor Remote System - - jamesng - 03.10.2022

Thanks for the script refinements. I’m still a little confused - if this shouldn’t be run a resident or Init script then what type of script would you suggest I create this as?


Kind Regards
James


RE: Script to Monitor Remote System - - admin - 03.10.2022

I've never said it shouldn't be a resident script. Resident script is exactly what is needed for such task.


RE: Script to Monitor Remote System - - jamesng - 03.10.2022

But won’t multiple instances of the same script running cause issues / use up all the available device memory?

What time interval would you suggest or modifications to the script to avoid the above?

Kind Regards
James


RE: Script to Monitor Remote System - - admin - 04.10.2022

You can run multiple instances without any issues. Sleep interval must be set to 0 in this case. Most of the time the script is in a suspended state waiting for data to arrive so it does not consume CPU resources.


RE: Script to Monitor Remote System - - jamesng - 04.10.2022

Thanks for clarifying. Many thanks.