I am trying to write my first edge driver that uses TCP. I have code that does an RPC call standalone and it works fine. But when I try to use it in an edge driver, I get this error:
2023-12-03T21:48:24.235878002+00:00 ERROR Hello Mylink Hello Mylink Device thread encountered error: [string “st/dispatcher.lua”]:233: Error encountered while processing event for <Device: 20b800ae-93ee-4b27-b19d-d7cc64982cf5 (Hello Mylink Device 1)>:
arg1: table: 0x1df9f60
[string “init.lua”]:40: Assert 1
Here is the code up to that point:
– require st provided libraries
local capabilities = require “st.capabilities”
local Driver = require “st.driver”
local socket = require ‘socket’
–local socket = require “cosock.socket”
local json = require ‘dkjson’
local log = require “log”
local discovery = require “discovery”
– local variables
local last_req_id = 0
– local tcp = socket.tcp()
– local functions
local function send_shutter_command(device, direction)
for id, value in pairs(device.preferences) do
if id == “ipaddress” then
ipaddress = value
elseif id == “port” then
port = value
elseif id == “targetid” then
targetid = value
elseif id == “auth” then
auth = value
end
end
last_req_id = last_req_id + 1
local request = {
id = last_req_id,
params = {targetID=targetid,auth= auth},
method = direction,
}
local requeststr = assert(json.encode(request))
log.info(requeststr)
log.info("IP Address= " .. ipaddress .. " Port = " .. port)
local tcp = socket.tcp()
assert(tcp,“Assert 1”)
Here is the standalone code that works:
local socket = require ‘socket’
local json = require ‘dkjson’
–send_shutter_command(“mylink.move.up”)
–end
–function send_shutter_command(direction)
local direction = “mylink.move.up”
local sock = socket.tcp()
local ipaddress = “192.168.1.174”
local port = 44100
local targetid = “CC1087E0.0”
local auth = “shutters”
local last_req_id = 0
last_req_id = last_req_id + 1
local request = {
id = last_req_id,
params = {targetID=targetid,auth= auth},
method = direction,
}
local requeststr = assert(json.encode(request))
print(requeststr)
assert(sock:connect(ipaddress, port))
local bytessent, err = sock:send(requeststr..“\n”)
assert(bytessent, “failed to send request”)
assert(bytessent == #requeststr + 1, “request only partially sent”)
sock:settimeout(2)
local line, status, response = sock:receive(‘*a’)
–local response = sock:receive(‘*a’)
print(response)
assert(response, “failed to get response:” .. tostring(err))
sock:close()
I think that I am missing something basic but I don’t understand what it is.
Help is much appreciated.