local cjson = require("cjson") pack_count = 5 pack_template = "BMS_协能_RACK_MODBUS.json" pack_protocol = "modbus_tcp" pack_no_head = "Pack" pack_name_head = "电池包" pack_addr = 1 pack_base_addr = 0 pack_interval = 20 pack_channel = "192.168.20.102:502" -- insulator_count = 192 -- insulator_template = "BMS_协能_RACK_MODBUS.json" -- insulator_protocol = "modbus_tcp" -- insulator_no_head = "Insulator" -- insulator_name_head = "绝缘箱" -- insulator_addr = 1 -- insulator_base_addr = 0 -- insulator_interval = 20 -- insulator_channel = "192.168.20.102:502" pcs_count = 3 pcs_template = "BMS_协能_RACK_MODBUS.json" pcs_protocol = "modbus_tcp" pcs_no_head = "PCS" pcs_name_head = "变流器" pcs_addr = 1 pcs_base_addr = 0 pcs_interval = 20 pcs_channel = "192.168.20.102:502" function build_dev_class() local dev_class = {} local dev_others = { type = "OTHER", devs = {} } for i = 1, pack_count do table.insert(dev_others.devs, { no = pack_no_head .. i, name = pack_name_head .. i, channel = pack_channel, protocol = pack_protocol, user_def = "base_addr=" .. pack_base_addr, addr = pack_addr, template = pack_template }) pack_base_addr = pack_base_addr+pack_interval end -- for i = 1, insulator_count do -- table.insert(dev_others.devs, { -- no = insulator_no_head .. i, -- name = insulator_name_head .. i, -- channel = insulator_channel, -- 假设所有绝缘模块都使用相同的通道 -- protocol = insulator_protocol, -- user_def = "base_addr=" .. insulator_base_addr, -- addr = insulator_addr, -- template = insulator_template -- }) -- insulator_base_addr = insulator_base_addr+insulator_interval -- end for i = 1, pcs_count do table.insert(dev_others.devs, { no = pcs_no_head .. i, name = pcs_name_head .. i, channel = pcs_channel, -- 假设所有绝缘模块都使用相同的通道 protocol = pcs_protocol, user_def = "base_addr=" .. pcs_base_addr, addr = pcs_addr, template = pcs_template }) pcs_base_addr = pcs_base_addr+pcs_interval end table.insert(dev_class, dev_others) return dev_class end function generate_json(pack_count, insulator_count) local data = { dev_class = build_dev_class(pack_count, insulator_count) } return cjson.encode(data) end function format_json(json_str) local indent = "" local formatted = "" local in_string = false local in_escape = false for i = 1, #json_str do local c = json_str:sub(i, i) if c == '"' and (i == 1 or json_str:sub(i-1, i-1) ~= '\\') then in_string = not in_string end if not in_string then if c == '{' or c == '[' then formatted = formatted .. c .. "\n" .. indent .. string.rep(" ", #indent + 1) indent = indent .. " " elseif c == '}' or c == ']' then indent = indent:sub(1, -3) formatted = formatted .. "\n" .. indent .. c elseif c == ',' then formatted = formatted .. c .. "\n" .. indent else formatted = formatted .. c end else formatted = formatted .. c -- 处理转义字符 if c == '\\' then in_escape = true elseif in_escape then in_escape = false end end end return formatted end local json_string = generate_json(192, 192) print(json_string) local formatted_json = format_json(json_string) print(formatted_json) local file = io.open("auto_device_config.json", "w") if not file then print("无法打开文件以写入") return end file:write(formatted_json) file:close()