local mqtt_client = require "application.mqtt_client" local devices = require "main.devices" local event = require "main.event" local cjson = require "cjson" local sys = require "sys" local sensors = {} local ADIO local PowerSwitch local MultiSwitch local SafeElectricity function sensors.get_do_sn() local nodes = devices.get_nodes() for _, v in pairs(nodes) do if v.template.name == "ADIO" then ADIO = v end end end function sensors.get_multi_node(node_sn) local nodes = devices.get_nodes() for _, v in pairs(nodes) do if v.sn == node_sn then MultiSwitch = v end end end function sensors.get_safe_node(node_sn) local nodes = devices.get_nodes() for _, v in pairs(nodes) do if v.sn == node_sn then SafeElectricity = v end end end function sensors.get_asw_sn() local nodes = devices.get_nodes() for _, v in pairs(nodes) do if v.template.name == "PowerSwitch" then PowerSwitch = v end end end function sensors.get_do_status() if ADIO == nil then sensors.get_do_sn() end if ADIO ~= nil then local dev_sn = GW_SN .. "_" .. ADIO.sn local data = {} data.identifier = "get_do_status" data.sn = dev_sn data.timestamp = os.time() data.mi = os.time() mqtt_client.send("ipc/" .. GW_SN .. "/" .. ADIO.port .. "/device/" .. dev_sn .. "/data/Set_Rglt", cjson.encode(data)) --sys.waitUntil(GET_DO_ACK, 1000) --log_print.info(sys.waitUntil(GET_DO_ACK, 2000)) end end function sensors.set_do_output(value) if ADIO == nil then sensors.get_do_sn() end if ADIO ~= nil then local dev_sn = GW_SN .. "_" .. ADIO.sn local data = {} data.identifier = "set_do_status" data.do_status = value data.sn = dev_sn data.timestamp = os.time() data.mi = os.time() mqtt_client.send("ipc/" .. GW_SN .. "/" .. ADIO.port .. "/device/" .. dev_sn .. "/data/Set_Rglt", cjson.encode(data)) if sys.waitUntil(SET_DO_ACK, 1000) then return true end end end function sensors.get_fan_speed() if ADIO == nil then sensors.get_do_sn() end if ADIO ~= nil then local dev_sn = GW_SN .. "_" .. ADIO.sn local data = {} data.identifier = "get_fan_speed" data.sn = dev_sn data.timestamp = os.time() data.mi = os.time() mqtt_client.send("ipc/" .. GW_SN .. "/" .. ADIO.port .. "/device/" .. dev_sn .. "/data/Set_Rglt", cjson.encode(data)) sys.waitUntil(GET_SPEED_ACK, 1000) --log_print.info(sys.waitUntil(GET_DO_ACK, 2000)) end end function sensors.set_fan_tmp(tmp1, tmp2) if ADIO == nil then sensors.get_do_sn() end if ADIO ~= nil then local dev_sn = GW_SN .. "_" .. ADIO.sn local data = {} data.identifier = "set_fan_tmp" data.sn = dev_sn data.fan1_tmp = tmp1 data.fan2_tmp = tmp2 data.timestamp = os.time() data.mi = os.time() mqtt_client.send("ipc/" .. GW_SN .. "/" .. ADIO.port .. "/device/" .. dev_sn .. "/data/Set_Rglt", cjson.encode(data)) sys.waitUntil(SET_TMP_ACK, 1000) --log_print.info(sys.waitUntil(GET_DO_ACK, 2000)) end end function sensors.set_contactor_sync_rtc(year, month, day, hour, minute, seconds, week) if ADIO == nil then sensors.get_do_sn() end if ADIO ~= nil then local dev_sn = GW_SN .. "_" .. ADIO.sn local data = {} data.identifier = "sync_time" data.sn = dev_sn data.year = year data.month = month data.day = day data.hour = hour data.minute = minute data.seconds = seconds data.week = week data.timestamp = os.time() data.mi = os.time() mqtt_client.send("ipc/" .. GW_SN .. "/" .. ADIO.port .. "/device/" .. dev_sn .. "/data/Set_Rglt", cjson.encode(data)) --sys.waitUntil(SET_TMP_ACK, 1000) --log_print.info(sys.waitUntil(GET_DO_ACK, 2000)) end end function sensors.set_contactor_mode(mode) if ADIO == nil then sensors.get_do_sn() end if ADIO ~= nil then local dev_sn = GW_SN .. "_" .. ADIO.sn local data = {} data.identifier = "set_mode" data.sn = dev_sn data.mode = mode data.timestamp = os.time() data.mi = os.time() mqtt_client.send("ipc/" .. GW_SN .. "/" .. ADIO.port .. "/device/" .. dev_sn .. "/data/Set_Rglt", cjson.encode(data)) --sys.waitUntil(SET_TMP_ACK, 1000) --log_print.info(sys.waitUntil(GET_DO_ACK, 2000)) end end function sensors.set_contactor_sch(contactor) if ADIO == nil then sensors.get_do_sn() end if ADIO ~= nil then local dev_sn = GW_SN .. "_" .. ADIO.sn local data = {} data.identifier = "set_sch" data.sn = dev_sn data.KA1_k_h = contactor.KA1.k.hh data.KA1_g_h = contactor.KA1.g.hh data.KA2_k_h = contactor.KA2.k.hh data.KA2_g_h = contactor.KA2.g.hh data.KA3_k_h = contactor.KA3.k.hh data.KA3_g_h = contactor.KA3.g.hh data.KA4_k_h = contactor.KA4.k.hh data.KA4_g_h = contactor.KA4.g.hh data.KA5_k_h = contactor.KA5.k.hh data.KA5_g_h = contactor.KA5.g.hh data.KA6_k_h = contactor.KA6.k.hh data.KA6_g_h = contactor.KA6.g.hh data.KA7_k_h = contactor.KA7.k.hh data.KA7_g_h = contactor.KA7.g.hh data.KA8_k_h = contactor.KA8.k.hh data.KA8_g_h = contactor.KA8.g.hh data.KA1_k_s = contactor.KA1.k.mm data.KA1_g_s = contactor.KA1.g.mm data.KA2_k_s = contactor.KA2.k.mm data.KA2_g_s = contactor.KA2.g.mm data.KA3_k_s = contactor.KA3.k.mm data.KA3_g_s = contactor.KA3.g.mm data.KA4_k_s = contactor.KA4.k.mm data.KA4_g_s = contactor.KA4.g.mm data.KA5_k_s = contactor.KA5.k.mm data.KA5_g_s = contactor.KA5.g.mm data.KA6_k_s = contactor.KA6.k.mm data.KA6_g_s = contactor.KA6.g.mm data.KA7_k_s = contactor.KA7.k.mm data.KA7_g_s = contactor.KA7.g.mm data.KA8_k_s = contactor.KA8.k.mm data.KA8_g_s = contactor.KA8.g.mm data.timestamp = os.time() data.mi = os.time() mqtt_client.send("ipc/" .. GW_SN .. "/" .. ADIO.port .. "/device/" .. dev_sn .. "/data/Set_Rglt", cjson.encode(data)) --sys.waitUntil(SET_TMP_ACK, 1000) --log_print.info(sys.waitUntil(GET_DO_ACK, 2000)) end end function sensors.get_multi_do_feedback(node_sn) if MultiSwitch == nil then sensors.get_multi_node(node_sn) end if MultiSwitch ~= nil then local dev_sn = GW_SN .. "_" .. MultiSwitch.sn local data = {} data.identifier = "do_feedback" data.sn = dev_sn data.timestamp = os.time() data.mi = os.time() mqtt_client.send("ipc/" .. GW_SN .. "/" .. MultiSwitch.port .. "/device/" .. dev_sn .. "/data/Set_Rglt", cjson.encode(data)) MultiSwitch = nil if sys.waitUntil(MULTI_DO_FEEDBACK_ACK, 1000) then return true end end end function sensors.get_multi_do_current(node_sn) if MultiSwitch == nil then sensors.get_multi_node(node_sn) end if MultiSwitch ~= nil then local dev_sn = GW_SN .. "_" .. MultiSwitch.sn local data = {} data.identifier = "do_current" data.sn = dev_sn data.timestamp = os.time() data.mi = os.time() mqtt_client.send("ipc/" .. GW_SN .. "/" .. MultiSwitch.port .. "/device/" .. dev_sn .. "/data/Set_Rglt", cjson.encode(data)) MultiSwitch = nil if sys.waitUntil(MULTI_DO_CURRENT_ACK, 1000) then return true end end end function sensors.set_multi_do_output(node_sn, value) if MultiSwitch == nil then sensors.get_multi_node(node_sn) end if MultiSwitch ~= nil then local dev_sn = GW_SN .. "_" .. MultiSwitch.sn local data = {} data.identifier = "do_ctrl" data.do_ctrl = value data.sn = dev_sn data.timestamp = os.time() data.mi = os.time() mqtt_client.send("ipc/" .. GW_SN .. "/" .. MultiSwitch.port .. "/device/" .. dev_sn .. "/data/Set_Rglt", cjson.encode(data)) MultiSwitch = nil if sys.waitUntil(MULTI_SET_DO_ACK, 1000) then return true end end end function sensors.set_multi_sync_rtc(node_sn, year, month, day, hour, minute, seconds) if MultiSwitch == nil then sensors.get_multi_node(node_sn) end if MultiSwitch ~= nil then local dev_sn = GW_SN .. "_" .. MultiSwitch.sn local data = {} data.identifier = "sync_rtc" data.year = year data.month = month data.day = day data.hour = hour data.minute = minute data.seconds = seconds data.sn = dev_sn data.timestamp = os.time() data.mi = os.time() mqtt_client.send("ipc/" .. GW_SN .. "/" .. MultiSwitch.port .. "/device/" .. dev_sn .. "/data/Set_Rglt", cjson.encode(data)) MultiSwitch = nil --if sys.waitUntil(MULTI_SYNC_RTC_ACK, 1000) then return true end end end function sensors.set_multi_order(node_sn, value) if MultiSwitch == nil then sensors.get_multi_node(node_sn) end if MultiSwitch ~= nil then local dev_sn = GW_SN .. "_" .. MultiSwitch.sn local data = {} data.identifier = "order" data.order = value data.sn = dev_sn data.timestamp = os.time() data.mi = os.time() mqtt_client.send("ipc/" .. GW_SN .. "/" .. MultiSwitch.port .. "/device/" .. dev_sn .. "/data/Set_Rglt", cjson.encode(data)) MultiSwitch = nil if sys.waitUntil(MULTI_SEND_ORDER_ACK, 1000) then return true end end end -- function sensors.set_multi_strategy_K1(node_sn, m_d, k1_time, K1_DO) if MultiSwitch == nil then sensors.get_multi_node(node_sn) end if MultiSwitch ~= nil then local dev_sn = GW_SN .. "_" .. MultiSwitch.sn local data = {} data.identifier = "set_sch_K1" data.m_d = m_d data.k1_time = k1_time data.K1_DO = K1_DO data.sn = dev_sn data.timestamp = os.time() data.mi = os.time() mqtt_client.send("ipc/" .. GW_SN .. "/" .. MultiSwitch.port .. "/device/" .. dev_sn .. "/data/Set_Rglt", cjson.encode(data)) MultiSwitch = nil if sys.waitUntil(MULTI_SET_SCH_ACK, 1000) then return true end end end function sensors.set_multi_strategy_K1_K2(node_sn, m_d, k1_time, K1_DO, k2_time, K2_DO) if MultiSwitch == nil then sensors.get_multi_node(node_sn) end if MultiSwitch ~= nil then local dev_sn = GW_SN .. "_" .. MultiSwitch.sn local data = {} data.identifier = "set_sch_K1_K2" data.m_d = m_d data.k1_time = k1_time data.K1_DO = K1_DO data.k2_time = k2_time data.K2_DO = K2_DO data.sn = dev_sn data.timestamp = os.time() data.mi = os.time() mqtt_client.send("ipc/" .. GW_SN .. "/" .. MultiSwitch.port .. "/device/" .. dev_sn .. "/data/Set_Rglt", cjson.encode(data)) MultiSwitch = nil if sys.waitUntil(MULTI_SET_SCH_ACK, 1000) then return true end end end function sensors.set_multi_strategy_K1_K3(node_sn, m_d, k1_time, K1_DO, k2_time, K2_DO, k3_time, K3_DO) if MultiSwitch == nil then sensors.get_multi_node(node_sn) end if MultiSwitch ~= nil then local dev_sn = GW_SN .. "_" .. MultiSwitch.sn local data = {} data.identifier = "set_sch_K1_K3" data.m_d = m_d data.k1_time = k1_time data.K1_DO = K1_DO data.k2_time = k2_time data.K2_DO = K2_DO data.k3_time = k3_time data.K3_DO = K3_DO data.sn = dev_sn data.timestamp = os.time() data.mi = os.time() mqtt_client.send("ipc/" .. GW_SN .. "/" .. MultiSwitch.port .. "/device/" .. dev_sn .. "/data/Set_Rglt", cjson.encode(data)) MultiSwitch = nil if sys.waitUntil(MULTI_SET_SCH_ACK, 1000) then return true end end end function sensors.set_multi_strategy_K1_K4(node_sn, m_d, k1_time, K1_DO, k2_time, K2_DO, k3_time, K3_DO, k4_time, K4_DO) if MultiSwitch == nil then sensors.get_multi_node(node_sn) end if MultiSwitch ~= nil then local dev_sn = GW_SN .. "_" .. MultiSwitch.sn local data = {} data.identifier = "set_sch_K1_K4" data.m_d = m_d data.k1_time = k1_time data.K1_DO = K1_DO data.k2_time = k2_time data.K2_DO = K2_DO data.k3_time = k3_time data.K3_DO = K3_DO data.k4_time = k4_time data.K4_DO = K4_DO data.sn = dev_sn data.timestamp = os.time() data.mi = os.time() mqtt_client.send("ipc/" .. GW_SN .. "/" .. MultiSwitch.port .. "/device/" .. dev_sn .. "/data/Set_Rglt", cjson.encode(data)) MultiSwitch = nil if sys.waitUntil(MULTI_SET_SCH_ACK, 1000) then return true end end end function sensors.safe_set_threshold(node_sn, overvol_threshold, overcur_threshold, overtemp_threshold, leakcur_threshold, overload_threshold) if SafeElectricity == nil then sensors.get_safe_node(node_sn) end if SafeElectricity ~= nil then local dev_sn = GW_SN .. "_" .. SafeElectricity.sn local data = {} data.identifier = "set_threshold" data.overvol_threshold = overvol_threshold data.overcur_threshold = overcur_threshold data.vc_over_time = 10 data.overtemp_threshold = overtemp_threshold data.temp_over_time = 10 data.leakcur_threshold = leakcur_threshold data.leakcur_over_time = 0.1 data.overload_threshold = overload_threshold data.overload_over_time = 10 data.power_threshold = 22000 data.power_over_time = 10 data.flag = 63 -- 默认:所有保护功能使能 data.sn = dev_sn data.timestamp = os.time() data.mi = os.time() mqtt_client.send("ipc/" .. GW_SN .. "/" .. SafeElectricity.port .. "/device/" .. dev_sn .. "/data/Set_Rglt", cjson.encode(data)) SafeElectricity = nil --if sys.waitUntil(MULTI_SET_SCH_ACK, 1000) then return true end end end function sensors.unlock(chn) local nodes = devices.get_nodes() local locks = {} for k, v in pairs(nodes) do if v.template.name == "SmartLock" then locks[#locks + 1] = v end end for _, v in pairs(locks) do if devices.getChn(v.sn) == chn then local dev_sn = GW_SN .. "_" .. v.sn local data = {} data.identifier = "unlock" data.sn = dev_sn data.timestamp = os.time() data.mi = os.time() data.term_addr = v.address mqtt_client.send("ipc/" .. GW_SN .. "/" .. v.port .. "/device/" .. dev_sn .. "/data/Set_Rglt", cjson.encode(data)) break end end end function sensors.set_lock_delay(delay) local nodes = devices.get_nodes() local locks = {} for k, v in pairs(nodes) do if v.template.name == "SmartLock" then locks[#locks + 1] = v end end for _, v in pairs(locks) do local dev_sn = GW_SN .. "_" .. v.sn local data = {} data.identifier = "set_lock_delay" data.sn = dev_sn data.timestamp = os.time() data.mi = os.time() data.term_addr = v.address data.lock_delay = delay mqtt_client.send("ipc/" .. GW_SN .. "/" .. v.port .. "/device/" .. dev_sn .. "/data/Set_Rglt", cjson.encode(data)) end end function sensors.set_chn(data) local nodes = devices.get_nodes() local config = cjson.decode(data) for _, v in pairs(config) do local sn = string.split(v.Sn, "_")[2] local node = nodes[sn] if node ~= nil then devices.setChn(node.sn, node.template.name, v.Channel) end end devices.saveChnConfig() end function sensors.set_camera(data) CameraConfig.enable = data.enable local config = cjson.decode(data.Config) CameraConfig.time = data.time CameraConfig.count = data.count for k, v in pairs(config) do CameraConfig.Config[k].di = v.di CameraConfig.Config[k].camera = v.camera end devices.saveCameraConfig() end function sensors.set_asw_output(ch, value) if PowerSwitch == nil then sensors.get_asw_sn() end if PowerSwitch ~= nil then if ch ~= 0 then local data = {} data.identifier = "switch_control" data.switch_address = ch - 1 data.state = value == 0 and 0 or 65280 data.sn = PowerSwitch.sn data.timestamp = os.time() data.mi = os.time() mqtt_client.send("ipc/" .. GW_SN .. "/" .. PowerSwitch.port .. "/device/" .. PowerSwitch.sn .. "/data/Set_Rglt", cjson.encode(data)) if sys.waitUntil(SET_AWS_ACK, 2000) then return true end else local data = {} data.identifier = "batch_control" data.control_value = value data.sn = PowerSwitch.sn data.timestamp = os.time() data.mi = os.time() mqtt_client.send("ipc/" .. GW_SN .. "/" .. PowerSwitch.port .. "/device/" .. PowerSwitch.sn .. "/data/Set_Rglt", cjson.encode(data)) if sys.waitUntil(SET_AWS_ACK, 2000) then return true end end end end function sensors.get_asw_exsist() if PowerSwitch == nil then sensors.get_asw_sn() end if PowerSwitch == nil then return end local data = {} data.identifier = "exsist_switch" data.sn = PowerSwitch.sn data.timestamp = os.time() data.mi = os.time() mqtt_client.send("ipc/" .. GW_SN .. "/" .. PowerSwitch.port .. "/device/" .. PowerSwitch.sn .. "/data/Set_Rglt", cjson.encode(data)) if sys.waitUntil(SET_AWS_ACK, 2000) then return true end end -- 为了使用之前封装好的接口,设备配置文件,前摄像头必须是1 ,后摄像头必须是2 function sensors.get_camera_image(chn) -- 1 前摄像头;2 后摄像头 local nodes = devices.get_nodes() local cameras = {} for k, v in pairs(nodes) do if v.template.name == "SDBCamera" then cameras[#cameras + 1] = v end end for _, v in pairs(cameras) do if devices.getChn(v.sn) == chn then log_print.info("take photo " .. v.sn) local dev_sn = GW_SN .. "_" .. v.sn local data = {} data.identifier = "get_jpg" data.sn = dev_sn data.timestamp = os.time() data.mi = os.time() data.term_addr = "" data.name = os.date("%Y-%m-%d_%H%M%S_", os.time()) .. (chn == 1 and "front" or "back") mqtt_client.send("ipc/" .. GW_SN .. "/" .. v.port .. "/device/" .. dev_sn .. "/data/Set_Rglt", cjson.encode(data)) break end end end return sensors