coso_air780epm/c210m/config.lua

254 lines
7.0 KiB
Lua
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

local _C = {
TAG = "【配置】",
CFLAG = false,
}
-- 日志等级
-- log.setLevel("INFO")
-- OTA密钥
PRODUCT_KEY = "0VrZxxSwrI7qE3GU8ddLFww5FbP3DqQu"
-- 默认配置
local default_config = {
-- 联网最长等待时间(秒)
ip_ready_timeout_s = 30,
-- 配置接口地址
config_url = "http://miot.xaxi.cn/app/device/conf/",
-- http请求最长等待时间毫秒
http_timeout_ms = 1000 * 3,
-- 数据上传频率(秒)
d_u = 600,
-- gps上传模式 0不上传1开机上传2跟随数据上传
g_u = 2,
-- 是否需要0TA更新 1更新 0不更新
ota = 1,
-- 屏幕数据刷新频率(秒) 0不刷新并息屏
s_u = 5,
-- 接口请求timeout时间(秒)
t_o = 5
}
function _C.init()
if not fskv.init() then return 1 end
local used, total, kv_count = fskv.status()
if not (used and total and kv_count) then return 2 end
if kv_count == 0 and not fskv.set("config", default_config) then return 3 end
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 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")
url = (type(url) == "string" and #url > 0) and url or default_config.config_url
return url .. mobile.imei()
end
local function set_config_url(v)
if type(v) ~= "string" then
log.warn(COSO, _C.TAG, "缺少 config_url 字段")
return
end
local _config_url = get_config_url()
if v == _config_url then return end
if not fskv.sett("config", "config_url", v) then
log.error(COSO, _C.TAG, "更新失败 config_url", v)
end
log.info(COSO, _C.TAG, "更新成功 config_url", v)
end
local function get_http_timeout_ms()
local timeout = fskv.get("config", "http_timeout_ms")
return (type(timeout) == "number") and timeout or default_config.http_timeout_ms
end
local function set_http_timeout_ms(v)
if type(v) ~= "number" then
log.warn(COSO, _C.TAG, "缺少 http_timeout_ms 字段")
return
end
local _http_timeout_ms = get_http_timeout_ms()
if v == _http_timeout_ms then return end
if v < 1000 or v > 5000 then
log.error(COSO, _C.TAG, "http_timeout_ms 校验失败", v)
return
end
if not fskv.sett("config", "http_timeout_ms", v) then
log.error(COSO, _C.TAG, "更新失败 http_timeout_ms", v)
end
log.info(COSO, _C.TAG, "更新成功 http_timeout_ms", v)
end
local function get_d_u()
local d_u = fskv.get("config", "d_u")
return (type(d_u) == "number") and d_u or default_config.d_u
end
local function set_d_u(v)
if type(v) ~= "number" then
log.warn(COSO, _C.TAG, "缺少 d_u 字段")
return
end
local _d_u = get_d_u()
if v == _d_u then return end
if v < 10 or v > 3600 then
log.error(COSO, _C.TAG, "d_u 校验失败", v)
return
end
if not fskv.sett("config", "d_u", v) then
log.error(COSO, _C.TAG, "更新失败 d_u", v)
end
log.info(COSO, _C.TAG, "更新成功 d_u", v)
end
local function get_g_u()
local g_u = fskv.get("config", "g_u")
return (type(g_u) == "number") and g_u or default_config.g_u
end
local function set_g_u(v)
if type(v) ~= "number" then
log.warn(COSO, _C.TAG, "缺少 g_u 字段")
return
end
local _g_u = get_g_u()
if v == _g_u then return end
if v < 0 or v > 2 then
log.error(COSO, _C.TAG, "g_u 校验失败", v)
return
end
if not fskv.sett("config", "g_u", v) then
log.error(COSO, _C.TAG, "更新失败 g_u", v)
end
log.info(COSO, _C.TAG, "更新成功 g_u", v)
end
local function get_ota()
local ota = fskv.get("config", "ota")
return (type(ota) == "number") and ota or default_config.ota
end
local function set_ota(v)
if type(v) ~= "number" then
log.warn(COSO, _C.TAG, "缺少 ota 字段")
return
end
local _ota = get_ota()
if v == _ota then return end
if v < 0 or v > 1 then
log.error(COSO, _C.TAG, "ota 校验失败", v)
return
end
if not fskv.sett("config", "ota", v) then
log.error(COSO, _C.TAG, "更新失败 ota", v)
end
log.info(COSO, _C.TAG, "更新成功 ota", v)
end
local get_s_u = function()
local s_u = fskv.get("config", "s_u")
return (type(s_u) == "number") and s_u or default_config.s_u
end
local function set_s_u(v)
if type(v) ~= "number" then
log.warn(COSO, _C.TAG, "缺少 s_u 字段")
return
end
local _s_u = get_s_u()
if v == _s_u then return end
if v < 0 or v > 3600 then
log.error(COSO, _C.TAG, "s_u 校验失败", v)
return
end
if not fskv.sett("config", "s_u", v) then
log.error(COSO, _C.TAG, "更新失败 s_u", v)
end
log.info(COSO, _C.TAG, "更新成功 s_u", v)
end
local function get_t_o()
local t_o = fskv.get("config", "t_o")
return (type(t_o) == "number") and t_o or default_config.t_o
end
local function set_t_o(v)
if type(v) ~= "number" then
log.warn(COSO, _C.TAG, "缺少 t_o 字段")
return
end
local _t_o = get_t_o()
if v == _t_o then return end
if v < 1 or v > 5 then
log.error(COSO, _C.TAG, "t_o 校验失败", v)
return
end
if not fskv.sett("config", "t_o", v) then
log.error(COSO, _C.TAG, "更新失败 t_o", v)
end
log.info(COSO, _C.TAG, "更新成功 t_o", v)
end
function _C.get_net_timeout()
return get_ip_ready_timeout_s()
end
function _C.get_d_u()
return get_d_u()
end
function _C.get_g_u()
return get_g_u()
end
function _C.get_ota()
return get_ota()
end
function _C.get_s_u()
return get_s_u()
end
function _C.get_t_o()
return get_t_o()
end
function _C.check()
local _config_url = get_config_url()
local _http_timeout_ms = get_http_timeout_ms()
local code, _, body = http.request("GET", _config_url, nil, nil, { timeout = _http_timeout_ms }).wait()
log.debug(COSO, _C.TAG, _config_url,_http_timeout_ms,code)
if code ~= 200 then return 101 end
body = json.decode(body)
log.debug(COSO, _C.TAG, body)
-- set_ip_ready_timeout_s(body.ip_ready_timeout_s)
set_config_url(body.config_url)
set_http_timeout_ms(body.http_timeout_ms)
set_d_u(body.d_u)
set_g_u(body.g_u)
set_ota(body.ota)
set_s_u(body.s_u)
set_t_o(body.t_o)
end
return _C