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.

TCP Socket Connection for Legrand
#27
Use this. Changing mapping will require full script restart via disable/enable.
Code:
if not sock then
  mapping = {
    ['11'] = '1/1/11',
  }

  function parselighting(what, where)
    local addr = mapping[ where ]
    what = tonumber(what) or -1

    if addr and 0 <= what and what <= 10 then
      grp.checkwrite(addr, what * 10)
    end
  end

  function parse(msg)
    local who, what, where = unpack(msg:split('*'))
    who = tonumber(who)

    -- lighting
    if who == 1 then
      parselighting(what, where)
    end
  end

  function read(sock)
    local buf = {}

    while true do
      local ch, err = sock:receive(1)

      if ch then
        local pr = buf[ #buf ]

        buf[ #buf + 1 ] = ch

        if ch == '#' and pr == '#' then
          break
        end

      else
        return nil, err
      end
    end

    if #buf > 0 and buf[ 1 ] == '*' then
      return table.concat(buf, '', 2, #buf - 2)
    end
  end

  sock = require('socket').tcp()
  sock:settimeout(1)

  res, err = sock:connect('192.168.1.247', 20000)

  if res then
    sock:send('*99*1##')
  else
    log('connect failed', err)

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

-- socket handler
if sock then    
  res, err = read(sock)

  if res then
    parse(res)
  elseif err == 'closed' then
    sock:close()
    sock = nil
  end
end

This example partially implements parsing of lighting type messages (off and dimming).
Docs for all types: https://developer.legrand.com/documentat...or-myhome/
Reply


Messages In This Thread
RE: TCP Socket Connection for Legrand - by admin - 15.06.2022, 09:55

Forum Jump: