main
			
			
		
		
							parent
							
								
									76c798cddd
								
							
						
					
					
						commit
						18d92884a6
					
				| 
						 | 
				
			
			@ -1,5 +1,6 @@
 | 
			
		|||
local _C = {
 | 
			
		||||
    TAG = "【配置】"
 | 
			
		||||
    TAG = "【配置】",
 | 
			
		||||
    CFLAG = false,
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
-- 日志等级
 | 
			
		||||
| 
						 | 
				
			
			@ -9,7 +10,7 @@ PRODUCT_KEY = "0VrZxxSwrI7qE3GU8ddLFww5FbP3DqQu"
 | 
			
		|||
-- 默认配置
 | 
			
		||||
local default_config = {
 | 
			
		||||
    -- 联网最长等待时间(秒)
 | 
			
		||||
    -- ip_ready_timeout_s = 30,
 | 
			
		||||
    ip_ready_timeout_s = 30,
 | 
			
		||||
    -- 配置接口地址
 | 
			
		||||
    config_url = "http://miot.xaxi.cn/app/device/conf/",
 | 
			
		||||
    -- http请求最长等待时间(毫秒)
 | 
			
		||||
| 
						 | 
				
			
			@ -34,27 +35,27 @@ function _C.init()
 | 
			
		|||
    return 0
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
-- local function get_ip_ready_timeout_s()
 | 
			
		||||
--     local timeout = fskv.get("config", "ip_ready_timeout_s")
 | 
			
		||||
--     return (type(timeout) == "number") and timeout or default_config.ip_ready_timeout_s
 | 
			
		||||
-- end
 | 
			
		||||
local function get_ip_ready_timeout_s()
 | 
			
		||||
    local timeout = fskv.get("config", "ip_ready_timeout_s")
 | 
			
		||||
    return (type(timeout) == "number") and timeout or default_config.ip_ready_timeout_s
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
-- local function set_ip_ready_timeout_s(v)
 | 
			
		||||
--     if type(v) ~= "number" then
 | 
			
		||||
--         log.warn(COSO, _C.TAG, "缺少 ip_ready_timeout_s 字段")
 | 
			
		||||
--         return
 | 
			
		||||
--     end
 | 
			
		||||
--     local _ip_ready_timeout_s = get_ip_ready_timeout_s()
 | 
			
		||||
--     if v == _ip_ready_timeout_s then return end
 | 
			
		||||
--     if v < 10 or v > 30 then
 | 
			
		||||
--         log.error(COSO, _C.TAG, "ip_ready_timeout_s 校验失败", v)
 | 
			
		||||
--         return
 | 
			
		||||
--     end
 | 
			
		||||
--     if not fskv.sett("config", "ip_ready_timeout_s", v) then
 | 
			
		||||
--         log.error(COSO, _C.TAG, "更新失败 ip_ready_timeout_s", v)
 | 
			
		||||
--     end
 | 
			
		||||
--     log.info(COSO, _C.TAG, "更新成功 ip_ready_timeout_s", v)
 | 
			
		||||
-- end
 | 
			
		||||
local function set_ip_ready_timeout_s(v)
 | 
			
		||||
    if type(v) ~= "number" then
 | 
			
		||||
        log.warn(COSO, _C.TAG, "缺少 ip_ready_timeout_s 字段")
 | 
			
		||||
        return
 | 
			
		||||
    end
 | 
			
		||||
    local _ip_ready_timeout_s = get_ip_ready_timeout_s()
 | 
			
		||||
    if v == _ip_ready_timeout_s then return end
 | 
			
		||||
    if v < 10 or v > 30 then
 | 
			
		||||
        log.error(COSO, _C.TAG, "ip_ready_timeout_s 校验失败", v)
 | 
			
		||||
        return
 | 
			
		||||
    end
 | 
			
		||||
    if not fskv.sett("config", "ip_ready_timeout_s", v) then
 | 
			
		||||
        log.error(COSO, _C.TAG, "更新失败 ip_ready_timeout_s", v)
 | 
			
		||||
    end
 | 
			
		||||
    log.info(COSO, _C.TAG, "更新成功 ip_ready_timeout_s", v)
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
local function get_config_url()
 | 
			
		||||
    local url = fskv.get("config", "config_url")
 | 
			
		||||
| 
						 | 
				
			
			@ -207,9 +208,9 @@ local function set_t_o(v)
 | 
			
		|||
    log.info(COSO, _C.TAG, "更新成功 t_o", v)
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
-- function _C.get_net_timeout()
 | 
			
		||||
--     return get_ip_ready_timeout_s()
 | 
			
		||||
-- end
 | 
			
		||||
function _C.get_net_timeout()
 | 
			
		||||
    return get_ip_ready_timeout_s()
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
function _C.get_d_u()
 | 
			
		||||
    return get_d_u()
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,5 +1,7 @@
 | 
			
		|||
require("globals")
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
local function start()
 | 
			
		||||
    local wakeup_sta = {
 | 
			
		||||
        [0] = "上电开机",
 | 
			
		||||
| 
						 | 
				
			
			@ -11,10 +13,14 @@ local function start()
 | 
			
		|||
    }
 | 
			
		||||
    local wakeup, sleep = pm.lastReson()
 | 
			
		||||
    log.info(COSO, "【设备开机】",wakeup_sta[wakeup],slp_sta[sleep], rtos.buildDate(), rtos.firmware())
 | 
			
		||||
    local code, connet_time
 | 
			
		||||
    code = cfg.init()
 | 
			
		||||
    if code ~= 0 then return sta.handling(code) end
 | 
			
		||||
    log.info(COSO, "【数据库】", "初始化完成")
 | 
			
		||||
    if sleep > 0 then
 | 
			
		||||
        mobile.flymode(0,false)
 | 
			
		||||
    end
 | 
			
		||||
    local code, connet_time = net.init(30)
 | 
			
		||||
    code, connet_time = net.init(cfg.get_net_timeout())
 | 
			
		||||
    if code ~= 0 then return sta.handling(code) end
 | 
			
		||||
    log.info(COSO, "【网络】", "连接成功", connet_time)
 | 
			
		||||
    sys.publish("config_check")
 | 
			
		||||
| 
						 | 
				
			
			@ -22,31 +28,27 @@ end
 | 
			
		|||
 | 
			
		||||
local function configTask()
 | 
			
		||||
    sys.waitUntil("config_check")
 | 
			
		||||
    log.info(COSO, "【检查配置")
 | 
			
		||||
    local code = cfg.init()
 | 
			
		||||
    if code ~= 0 then return sta.handling(code) end
 | 
			
		||||
    log.info(COSO, "【数据库】", "初始化完成")
 | 
			
		||||
    cfg.check()
 | 
			
		||||
    log.debug(COSO, "【DEV】", cfg.get_net_timeout())
 | 
			
		||||
    log.debug(COSO, "【DEV】", cfg.get_d_u())
 | 
			
		||||
    log.debug(COSO, "【DEV】", cfg.get_g_u())
 | 
			
		||||
    log.debug(COSO, "【DEV】", cfg.get_ota())
 | 
			
		||||
    log.debug(COSO, "【DEV】", cfg.get_s_u())
 | 
			
		||||
    log.debug(COSO, "【DEV】", cfg.get_t_o())
 | 
			
		||||
    sys.publish("get_data")
 | 
			
		||||
    cfg.check()
 | 
			
		||||
    if cfg.get_ota() then
 | 
			
		||||
        sys.publish("ota_update")
 | 
			
		||||
    else
 | 
			
		||||
        sys.publish("get_data")
 | 
			
		||||
    end
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
local function dataTask()
 | 
			
		||||
    sys.waitUntil("get_data")
 | 
			
		||||
    sys.publish("report_data")
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
local function reportTask()
 | 
			
		||||
    sys.waitUntil("report_data")
 | 
			
		||||
    sleep.goPSM(10)
 | 
			
		||||
    report.upload(data.getData())
 | 
			
		||||
    sleep.goPSM(10 or cfg.get_s_u())
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
sys.taskInit(start)
 | 
			
		||||
sys.taskInit(configTask)
 | 
			
		||||
sys.taskInit(dataTask)
 | 
			
		||||
sys.taskInit(reportTask)
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -0,0 +1,34 @@
 | 
			
		|||
local _C = {
 | 
			
		||||
    TAG = "【数据获取】",
 | 
			
		||||
    data = {
 | 
			
		||||
        net = {},
 | 
			
		||||
    },
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
local function get_net_data()
 | 
			
		||||
 | 
			
		||||
    _C.data.net = {
 | 
			
		||||
        imei = mobile.imei(),
 | 
			
		||||
        imsi = mobile.imsi(),
 | 
			
		||||
        sn = mobile.sn():toHex(),
 | 
			
		||||
        muid = mobile.muid(),
 | 
			
		||||
        iccid = mobile.iccid(),
 | 
			
		||||
        csq = mobile.csq(),
 | 
			
		||||
        rssi = mobile.rssi(),
 | 
			
		||||
        rsrq = mobile.rsrq(),
 | 
			
		||||
        rsrp = mobile.rsrp(),
 | 
			
		||||
        snr = mobile.snr(),
 | 
			
		||||
        simid = mobile.simid(),
 | 
			
		||||
    }
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
function _C.getData()
 | 
			
		||||
    get_net_data()
 | 
			
		||||
    return _C.data
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
function _C.updateData()
 | 
			
		||||
    
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
return _C
 | 
			
		||||
| 
						 | 
				
			
			@ -6,4 +6,6 @@ cfg     = require("config")
 | 
			
		|||
 | 
			
		||||
net = require("net")
 | 
			
		||||
ota = require("ota")
 | 
			
		||||
data = require("data")
 | 
			
		||||
sleep = require("sleep")
 | 
			
		||||
report = require("report")
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,20 +1,24 @@
 | 
			
		|||
local _C = {
 | 
			
		||||
    TAG = "【4G网络】"
 | 
			
		||||
    TAG = "【4G网络】",
 | 
			
		||||
    READY = false
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
sys.subscribe("IP_READY", function()
 | 
			
		||||
    _C.READY = true
 | 
			
		||||
end)
 | 
			
		||||
 | 
			
		||||
function _C.init(v)
 | 
			
		||||
    if _C.READY then
 | 
			
		||||
        return 0, 0
 | 
			
		||||
    end
 | 
			
		||||
    local start_time = os.clock()
 | 
			
		||||
    local result
 | 
			
		||||
    while not (result or ((os.clock() - start_time) * 100 > v)) do
 | 
			
		||||
        result = sys.waitUntil("IP_READY", 990)
 | 
			
		||||
        if not result then
 | 
			
		||||
            log.warn(COSO, _C.TAG, "联网中:", (os.clock() - start_time) * 100, "超时:", v)
 | 
			
		||||
            sys.wait(10)
 | 
			
		||||
    while not (_C.READY or ((os.clock() - start_time) * 100 > v)) do
 | 
			
		||||
        if not _C.READY then
 | 
			
		||||
            sys.wait(1000)
 | 
			
		||||
            log.info(COSO, _C.TAG, "联网中:", (os.clock() - start_time) * 100, "超时:", v)
 | 
			
		||||
        end
 | 
			
		||||
    end
 | 
			
		||||
    return result and 0 or 3, (os.clock() - start_time) * 100
 | 
			
		||||
    return _C.READY and 0 or 3, (os.clock() - start_time) * 100
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
return _C
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -2,4 +2,14 @@ local _C = {
 | 
			
		|||
    TAG = "【上报数据】"
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
function _C.upload(v)
 | 
			
		||||
    log.info(COSO, _C.TAG, json.encode(v)) 
 | 
			
		||||
    local code, _, body = http.request("POST", "http://miot.xaxi.cn/app/device/data/", nil, json.encode(v), { timeout = cfg.get_t_o() }).wait()
 | 
			
		||||
    log.debug(COSO, _C.TAG, code, body)
 | 
			
		||||
    if code ~= 200 then return 1 end
 | 
			
		||||
    body = json.decode(body)
 | 
			
		||||
    if body.code ~= 0 then return 2 end
 | 
			
		||||
    return 0
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
return _C
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in New Issue