This forum uses cookies
This forum makes use of cookies to store your login information if you are registered, and your last visit if you are not. Cookies are small text documents stored on your computer; the cookies set by this forum can only be used on this website and pose no security risk. Cookies on this forum also track the specific topics you have read and when you last read them. Please confirm that you accept these cookies being set.

Script to Monitor Remote System -
#1
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
Reply
#2
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
Reply
#3
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
Reply
#4
I've never said it shouldn't be a resident script. Resident script is exactly what is needed for such task.
Reply
#5
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
Reply
#6
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.
Reply
#7
Thanks for clarifying. Many thanks.
Reply


Forum Jump: