module("luci.controller.luci-app-ipalarm",package.seeall) local fs=require"nixio.fs" local http=require"luci.http" local uci=require"luci.model.uci".cursor() local syslog = require "nixio".syslog local sys = require "luci.sys" local json = require "luci.json" local string = require "string" local math = math local cjson = require "cjson" local IPALARM_FILE="/app/config/ip_alarm_spon_cfg.json" function index() local root= node() root.target = alias("lnxall") local page = entry({"lnxall"}, alias("lnxall", "ipalarm", "update"), _("lnxall")) entry({"lnxall", "ipalarm"}, alias("lnxall", "ipalarm", "update"), _("lnxall")) entry({"lnxall", "ipalarm", "update"}, call("update_callback"), _("update callback")) end function dumptable(t) if type(t) ~= "table" then syslog("err", "LUCI:-> " .. tostring(t)) return end for k,v in pairs(t) do syslog("err", "LUCI:-> " .. tostring(k) .. "\t" .. tostring(v)) if type(v) == "table" then dumptable(v) end end end function sanity_check_json() -- sanity check local hm = http.getenv("REQUEST_METHOD") local ct = http.getenv("CONTENT_TYPE") if hm == "OPTIONS" then http.header("access-control-allow-credentials", "true") http.header("access-control-allow-methods", "GET, POST, OPTIONS, PUT, PATCH, DELETE") http.header("access-control-allow-origin", "*") http.header("access-control-allow-headers", "content-type") http.header("content-type", "application/json") http.status(200, "OK") http.close() return false elseif hm ~= "POST" or not ct:match("^application/json") then http.status(400, "Bad Request") http.close() return false end return true end function sanity_check_xform() -- sanity check local hm = http.getenv("REQUEST_METHOD") if hm == "OPTIONS" then http.header("access-control-allow-credentials", "true") http.header("access-control-allow-methods", "GET, POST, OPTIONS, PUT, PATCH, DELETE") http.header("access-control-allow-origin", "*") http.header("access-control-allow-headers", "content-type") http.header("content-type", "application/x-www-form-urlencoded") http.status(200, "OK") http.close() return false elseif hm ~= "POST" then http.status(400, "Bad Request") http.close() return false end return true end function sanity_check_get() -- sanity check local hm = http.getenv("REQUEST_METHOD") if hm == "OPTIONS" then http.header("access-control-allow-credentials", "true") http.header("access-control-allow-methods", "GET, POST, OPTIONS, PUT, PATCH, DELETE") http.header("access-control-allow-origin", "*") http.header("access-control-allow-headers", "content-type") http.header("content-type", "application/json") http.status(200, "OK") http.close() return false elseif hm ~= "GET" then http.status(400, "Bad Request") http.close() return false end return true end function response_json(response) http.header("Access-Control-Allow-Origin", "*") http.header("Access-Control-Allow-Credentials", "true") http.prepare_content("application/json") http.write_json(response) http.close() end -- receive the event from ip alarm server function update_callback() -- sanity check -- syslog("debug", "http enter...") if not sanity_check_xform() then return end syslog("debug", "http type:" .. http.formvalue("jsondata[type]") .. " state:" .. http.formvalue("jsondata[state]") .. " sourceid:" .. http.formvalue("jsondata[sourceid]") .. " targetid:" .. http.formvalue("jsondata[targetid]")) -- parse json --local hc = http.content() --local input, rc, err = json.decode(hc) -- process -- local response = {} local file = "/app/config/mqtt_server.json" response["ret"] = 1 local input = {} input["sourceid"] = http.formvalue("jsondata[sourceid]") input["targetid"] = http.formvalue("jsondata[targetid]") input["type"] = tonumber(http.formvalue("jsondata[type]")) input["state"] = tonumber(http.formvalue("jsondata[state]")) if input["type"] == nil or input["state"] == nil then response_json(response) return end local cache_fd=io.open(IPALARM_FILE, 'r+') if cache_fd then cache_fd:seek('set') local str = cache_fd:read("*a") syslog("debug", "http str:" .. str) --print(str) if str and #str > 0 then local ip_alarm = cjson.decode(str) --syslog("debug", "http ip_alarm:" .. ip_alarm[input["sourceid"]].DEV_SN) if ip_alarm[input["sourceid"]] ~= nil then syslog("debug", "http find sn:" .. ip_alarm[input["sourceid"]].DEV_SN .. input["type"]) if input["type"] == 1 then if input["state"] == 1 then input["identifier"] = "online" elseif input["state"] == 2 then input["identifier"] = "offline" else syslog("err", "http not in [online offline]") response_json(response) return end input["sn"] = ip_alarm[input["sourceid"]].DEV_SN input["mi"] = 12345 response["result"] = input["identifier"] syslog("debug", "http mqtt:" .. 'mosquitto_pub -h mqtt.lnxall.com -p 3883 -u localuser -P dywl@galaxy -t ' .. ip_alarm[input["sourceid"]].SERVER_SET .. ' -m ' .. "\'" .. cjson.encode(input) .. "\'") --response["result_data"] = 'mosquitto_pub -h mqtt.lnxall.com -p 3883 -u localuser -P dywl@galaxy -t ' .. ip_alarm[input["sourceid"]].SERVER_SET .. ' -m ' .. "\'" .. cjson.encode(input) .. "\'" sys.exec('mosquitto_pub -h mqtt.lnxall.com -p 3883 -u localuser -P dywl@galaxy -t ' .. ip_alarm[input["sourceid"]].SERVER_SET .. ' -m ' .. "\'" .. cjson.encode(input) .. "\'") elseif input["type"] == 2 then syslog("debug", "http 3 ip_alarm:" .. ip_alarm[input["sourceid"]].SERVER_SET) if input["state"] == 0 then input["identifier"] = "handup" elseif input["state"] == 1 then input["identifier"] = "ring" else syslog("err", "http not in [ring handup]") response_json(response) return end input["sn"] = ip_alarm[input["sourceid"]].DEV_SN input["mi"] = 12345 response["result"] = input["identifier"] syslog("debug", "http mqtt:" .. 'mosquitto_pub -h mqtt.lnxall.com -p 3883 -u localuser -P dywl@galaxy -t ' .. ip_alarm[input["sourceid"]].SERVER_SET .. ' -m ' .. "\'" .. cjson.encode(input) .. "\'") --response["result_data"] = 'mosquitto_pub -h mqtt.lnxall.com -p 3883 -u localuser -P dywl@galaxy -t ' .. ip_alarm[input["sourceid"]].SERVER_SET .. ' -m ' .. "\'" .. cjson.encode(input) .. "\'" sys.exec('mosquitto_pub -h mqtt.lnxall.com -p 3883 -u localuser -P dywl@galaxy -t ' .. ip_alarm[input["sourceid"]].SERVER_SET .. ' -m ' .. "\'" .. cjson.encode(input) .. "\'") else syslog("debug", "http end type unknown") end else syslog("debug", "http end sourceid unknown") end end end syslog("debug", "http out...") -- response -- response_json(response) end