local fileOpt = require "storage.fileOpt" local cjson = require "cjson" require "storage.default_conf" require "main.global" local configuration = {} local config_table = { {name = "SDB", file = CONFIG_FILE_NAME, default = SDB_CONFIG_DEFAULT, variable = "SdbConfig"}, -- 配置参数 {name = "Warn", file = WARNING_CFG_FILE_NAME, default = WARN_CONFIG_DEFAULT, variable = "WarnConfig"}, -- 告警参数配置 {name = "Device", file = DEVICE_CFG_FILE_NAME, default = {}, variable = "DeviceConfig"}, -- 设备通道配置 {name = "WarnStatus", file = WARNING_STAT_FILE_NAME, default = WARN_STATUS_DEFAULT, variable = "WarnStatus"}, -- 告警状态 {name = "Camera", file = CAMERA_CONFIG_FILE_NAME, default = CAMERA_CONFIG_DEFAULT, variable = "CameraConfig"} -- 摄像头关联配置 } local function checkNewKeys(value, default) local isDifferent = false for k, v in pairs(default) do if value[k] then -- Oh, there is. Do nothing else if type(v) == "table" then isDifferent = checkNewKeys(value[k], v) or isDifferent else value[k] = v isDifferent = isDifferent or true end end end return isDifferent end function configuration.init() for _, v in pairs(config_table) do local LoadVariable = fileOpt.read(v.file, true) if not LoadVariable or LoadVariable == "" then LoadVariable = cjson.encode(v.default or {}) fileOpt.write(v.file, LoadVariable, true) end LoadVariable = cjson.decode(LoadVariable) if checkNewKeys(LoadVariable, v.default) then fileOpt.write(v.file, cjson.encode(LoadVariable), true) end loadstring(v.variable .. "=LoadVariable")() --log_print.info(cjson.encode(loadstring("return " .. v.variable)())) end local sdbConfig = fileOpt.read(CONFIG_FILE_NAME, true) if sdbConfig == nil then fileOpt.write(CONFIG_FILE_NAME, cjson.encode(SDB_CONFIG_DEFAULT), true) else SdbConfig = cjson.decode(sdbConfig) end local warnConfig = fileOpt.read(WARNING_CFG_FILE_NAME, true) if warnConfig == nil then fileOpt.write(WARNING_CFG_FILE_NAME, cjson.encode(WARN_CONFIG_DEFAULT), true) else WarnConfig = cjson.decode(warnConfig) end local deviceConfig = fileOpt.read(DEVICE_CFG_FILE_NAME, true) if deviceConfig == nil then fileOpt.write(DEVICE_CFG_FILE_NAME, cjson.encode({}), true) else DeviceConfig = cjson.decode(deviceConfig) end local cameraConfig = fileOpt.read(CAMERA_CONFIG_FILE_NAME, true) if cameraConfig == nil then fileOpt.write(CAMERA_CONFIG_FILE_NAME, cjson.encode(CAMERA_CONFIG_DEFAULT), true) else CameraConfig = cjson.decode(cameraConfig) end local warnStatus = fileOpt.read(WARNING_STAT_FILE_NAME, true) if warnStatus == nil then fileOpt.write(WARNING_STAT_FILE_NAME, cjson.encode({}), true) else WarnStatus = cjson.decode(warnStatus) end end function configuration.set(name, data) for k, v in pairs(config_table) do if v.name == name then if type(data) == "table" then data = cjson.encode(data) end fileOpt.write(v.file, data, true) end end end -- function configuration.readSdb() -- if sdbConfig == nil then -- local data = fileOpt.read(CONFIG_FILE_NAME, true) -- if(data == nil) then -- configuration.init() -- data = fileOpt.read(CONFIG_FILE_NAME, true) -- end -- sdbConfig = cjson.decode(data) -- end -- return sdbConfig -- end -- function configuration.setSdb(data) -- sdbConfig = data -- fileOpt.write(CONFIG_FILE_NAME, cjson.encode(data), true) -- end -- function configuration.readWarn() -- if WarnConfig == nil then -- local data = fileOpt.read(WARNING_CFG_FILE_NAME, true) -- if(data == nil) then -- configuration.init() -- data = fileOpt.read(WARNING_CFG_FILE_NAME, true) -- end -- WarnConfig = cjson.decode(data) -- end -- return WarnConfig -- end -- function configuration.setWarn(data) -- WarnConfig = data -- fileOpt.write(WARNING_CFG_FILE_NAME, cjson.encode(data), true) -- end -- function configuration.readDevice() -- if deviceConfig == nil then -- local data = fileOpt.read(DEVICE_CFG_FILE_NAME, true) -- if(data == nil) then -- configuration.init() -- data = fileOpt.read(DEVICE_CFG_FILE_NAME, true) -- end -- deviceConfig = cjson.decode(data) -- log_print.info("device config", deviceConfig) -- end -- return deviceConfig -- end -- function configuration.setDevice(data) -- deviceConfig = data -- fileOpt.write(DEVICE_CFG_FILE_NAME, cjson.encode(data), true) -- end return configuration