-- Created by jiaqiang.ye@lnxall.com -- Simple YOUCAN script demonstration -- 2023/06/08 -- global variables local g_gwsn = "son/of/gun" local g_topic = "what/the/hell" local g_port = "what/the/fuck" local g_devsn = "hello/world" local function flush_stdio() io.stdout:flush() io.stderr:flush() end function lnxall_initfunc(topic) print("Warning, YOUCAN edge script loaded!") if type(topic) == "string" then g_topic = topic print("Using topic prefix: " .. topic) local gwsn, port, devsn = string.match(topic, "^ipc/([^/]+)/([^/]+)/device/([^/]+)/") if type(devsn) == "string" then g_gwsn, g_port, g_devsn = gwsn, port, devsn print("Using GWSN: " .. gwsn) print("Using connection port: " .. port) print("Using DEVSN: " .. devsn) end end flush_stdio() return 0 end local function pack_json(tagnode, svrname, reper) local payload = { ["sn"] = g_devsn, ["time"] = os.time(), ["identifier"] = svrname, ["data_type"] = 2, ["mi"] = 0, ["report_period"] = type(reper) == "number" and reper or 0, ["port"] = g_port, ["report"] = 0, ["res"] = 0, ["tag_node"] = tagnode, } return cjson.encode(payload) end -- input is a Lua table -- output should be a (JSON) string function fu_autoreport(tags) print("Entering function fu_autoreport", type(tags)) flush_stdio() return cjson.encode(tags) end -- input is a JSON string -- output should be a JSON string function fd_autoreport(tags) print("Entering function fd_autoreport", type(tags)) flush_stdio() return tags end function fu_candown1(tags) print("Entering function fu_candown1", type(tags)) io.stdout:flush() local out = {} out[#out + 1] = g_topic .. "/candown1" out[#out + 1] = pack_json(cjson.encode(tags), 'candown1') out[#out + 1] = g_topic .. "/autoreport" out[#out + 1] = pack_json('{"rval_0":1949,"rval_1":1978,"rval_2":2001,"rval_3":2023}', 'autoreport') return table.concat(out, string.char(0x1b)) end function fd_candown1(tags) print("Entering function fd_candown1", type(tags)) flush_stdio() return tags end