User Tools

Site Tools


http_get_custom

HTTP request implemention using custom protocol

The following example sends a HTTP string template to the TCP port upon the register change in the custom protocol. Once the response comes, is is copied in the response global variable which is returned to the system with the readRegister function.

get_example = [[
GET /api/register-values HTTP/1.1
Host: 127.0.0.1
X-WH-APIKEY:5D78CB6F0B0ACC526F2CB8BF65B471A2E594D309
X-WH-CONNECTIONS:  1
Accept: application/json
Content-Type: application/json
 
]]
response = "no data"
 
function createDevices ()
  addDevice({name = "S",  shift = 0, base = 10, xtraFields = {}})
end
 
function onScanStart ()
  -- no actions on scan start 
end
 
function readRegister (reg, device, unitId)
  return response
end
 
function writeRegister (reg, device, unitId, newValue)
 
   sendString(get_example)        
 
   local s = read_string() 
 
   if (not s) or (#s == 0) then 
       return false 
   else 
       response = s              -- copy response to global 
       return true 
   end 
 
end
 
function read_string()
    local str = {}
 
    repeat 
        local char_ = readString(1)
        if char_ then 
            -- DEBUG("Read char = " .. char_)
            table.insert(str, char_) 
        end 
    until not char_
    local response = table.concat(str)
    DEBUG("read_string going to return " .. response)
    return response
end