[ST Edge] Issue with HTTP request in Edge drivers

I have a luxure HTTP server listening on the hub (similar to the lightbulb-lan-esp8266 example), and I have no issue accessing http://IP:port on my browser.

However, when I http.request to the hub-IP:port, it [string “socket”]:1406: timeout. Is this a bug?

I have no issue if I http.request other devices, but just not the hub IP:port.

I want to know whether the server is running or not.

Hi, @hongtat.

Can you give more details about your implementation, please?

As I understand you have a server similar to this one, and you make an HTTP request to that server within the same driver?

Could you share the snippet code of the implementation that causes this error, please?

Hi @nayelyz .

Yes. Within the same driver.
I’m able to access the http://hub:port/ via a browser, but HTTP request to it within the same driver will get a timeout.

-- https://github.com/SmartThingsDevelopers/SampleDrivers/blob/main/lightbulb-lan-esp8266/driver/src/server.lua
-- Added this
    server:get('/', function(req, res)
        res:send('Hello world!')
    end)

---

function lifecycle_handler.init(driver, device)    
  device.thread:call_on_schedule(
    300,
    function ()
        return commands.ping(
                driver.server.ip,
                driver.server.port,
                device)
    end,
    'Ping schedule')
end

---

function command_handler.ping(address, port, device)
  local addr = tostring(address .. ':' .. port)
  local success, data = command_handler.send_lan_command(addr, 'GET', '/')
end

function command_handler.send_lan_command(address, method, path, body)
    local dest_url = 'http://'..address .. path
    local query = neturl.buildQuery(body or {})
    local res_body = {}

    local _, code = http.request({
        method=method,
        url=dest_url..'?'..query,
        sink=ltn12.sink.table(res_body),
    })

    -- Handle response
    if code == 200 then
        print('commands:send_lan_command] Response: "' .. code .. '", url: "' .. dest_url .. '", query: "' .. utils.stringify_table(query) .. '"')
        return true, res_body
    else
        print('[commands:send_lan_command] Response error: "' .. code .. '"')
    end
    return false, nil
end

Sorry for the late response.

I was able to replicate the error, you mention. I’m making other tests to see what can be causing the issue.
I’ll also check if there are any restrictions on doing internal requests in the driver with the engineering team, but due to the holidays, their answer might take some time.

3 Likes

Ok, so, the issue is that when we do this:

driver:register_channel_handler(server.sock, function ()
  server:tick()
end)

The server is running in the same thread as lifecycle_handler.init, so, we need to spawn the server on its own thread as follows:

local cosock = require('cosock')

local server = lux.Server.new_with(cosock.socket.tcp(), { env = 'debug' })

server:listen()

cosock.spawn(function()
  while true do
    server:tick(log.error)
  end
end, "server run loop")

server:get('/', function(req, res)
  res:send('hello world')
end)
--sample of making the request:
local body, code, headers, msg = assert(http.request(string.format("http://...", server:get_ip(), server.port)))

Please, let me know your results

1 Like

Hi @nayelyz

I’m afraid I’m unable to get it to work. I’m still getting [string "socket"]:1406: timeout.
I’ll send you a DM.

Has anyone noticed that the luxure library included with edge is different from the GitHub version.

If I try to use response:status(200):send() with the ST luxury library it fails with 500 http error (something about trying to call a number as a function). If I try using the GitHub luxure and try to cosock asyncify it, it fails with some socket error.

@nayelyz @TAustin @blueyetisoftware @orangebucket

@RBoy - Try -

res:set_status(200):set_content_type('application/json'):send(json.encode({key = 'value'}))

res:send('HTTP/1.1 200 OK')

1 Like

Thanks, yes I did that and also raised this issue with the dev. I can’t find the status method in the source code. Maybe a typo in the doc - the source seems to indicate that it defaults to error 500 when there’s an internal error

I’ve found an issue with this and hub firmware 0.46.004, infact I found many issues with the 46.4 firmware including cosock issues (everything is working great with 45.11 firmware). Are you by any chance running firmware 46.4?

1 Like

I am so glad I found this thread - I was going nuts. I originally used the same example to set up a local luxure server on the hub, and it was definitely working fine a couple months ago. But a few people have started posting about having issues, and today I finally got around to testing it and discovered trying to hit my POST endpoint from Postman, the request just spins endlessly, with nothing showing in the CLI logs.

I changed the server spin up flow to mirror the example posted by @nayelyz above, and it works again.

I am not exactly sure what this means, but I’m guessing something changed with cosock/luxure within the last month or two, so if that sample code hasn’t been update, it might need to be.

For what it’s worth, I’m on 046.00005.