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
#31
Hello Admin,

Would you help me to refactor the code so that it can use all the features of Open web net ? I tried to redo it myself for the temperature, but I'm sure it's not the right way since the string is different there.

Code:
if not sock then
  mappingtemp = {
    ['1'] = '1/1/1',
    ['2'] = '1/1/2',

  }
  mapping = {
   
    ['3'] = '1/1/3',
    ['4'] = '1/1/4',
    ['5'] = '1/1/5',
    ['6'] = '1/1/6',
    ['7'] = '1/1/7',
    ['8'] = '1/1/8',
    ['9'] = '1/1/9',
    ['10'] = '1/1/10',
    ['11'] = '1/1/11',
    ['12'] = '1/1/12',
    ['13'] = '1/1/13',
    ['14'] = '1/1/14',
    ['15'] = '1/1/15',
    ['16'] = '1/1/16',
    ['17'] = '1/1/17',
    ['22'] = '1/1/22',
    ['23'] = '1/1/23',
    ['51'] = '1/1/51',
    ['0215'] = '1/1/52',
  }
 
   function parselighting(what, where)
    log("Ok")
    local addr = mapping[ where ]
    what = tonumber(what) or -1

    if addr and 0 <= what and what <= 10 then
      grp.checkwrite(addr, what)
    end
  end
 
 
 
 
  function parsetemperature(where, value)
    local addr = mappingtemp[ where ]
    log(where)
    value = tonumber(value) or -1

    if addr and 0 <= value and value <= 4050 then
      value = value * 0.1
      grp.checkwrite(addr, value)
      log("done",value)
    end
  end

 
 
 
 
 
  function parse(msg)
    log (msg)
    length = string.len(msg)
   
    if length == 11 then
    log("length",length)
    local who, where, dimension, value = unpack(msg:split('*'))
    if who == "#4" then
    who = string.sub(who,2,2)
    who = tonumber(who)
    parsetemperature(where, value)
end
    end
     if length == 6 then
      log("length",length)
       local who, what, where = unpack(msg:split('*'))
            who = tonumber(who)
       -- lighting
        if who == 1 then
      parselighting(what, where)
      log(what)
      log(where)
    end
  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.195', 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
    log(res)
    parse(res)
  elseif err == 'closed' then
    sock:close()
    sock = nil
  end
end
Reply


Messages In This Thread
RE: TCP Socket Connection for Legrand - by a455115 - 31.08.2022, 07:32

Forum Jump: