You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何为Python存储设备信息的JSON配置新增多个属性字段

sACN设备配置存储改造方案

原有逻辑

当前仅在JSON配置中存储绑定了sACN universe的对象的device_id,对应代码如下:

for device_index in range(device_count):
    device_name = sdk.get_device_info(device_index)
    device_type = device_name.type
    if device_name.id not in conf:
        universe = get_free_universe()
        conf.update({device_name.id: universe}) 
        print(f"conf= {conf}")
    else:
        universe = conf[device_name.id] #main.py line 368
    save_config(DEVICE_PATH)

当前生成的JSON结构为:

{
    "3192efa109cfb5d86f09a82a7cc00c5d": 4,
    "42aa42a0bb5fcee780fb1be13dfcb873": 5,
    "4b80e1817076307b36c58c31118f6696": 1,
    "62c13e2db726382e9c66d9f69020ab5e": 6,
    "a51da6fe155f299a3fc474c22310cde9": 2,
    "b5ff59af43d6c3572a41d7693b5bec1c": 3
}

改造需求

需要每个device_id对应的条目同时存储universe和device_name.model属性,目标JSON格式如下(原示例语法问题已修正):

{
  "3192efa109cfb5d86f09a82a7cc00c5d": {
     "universe": 4,
     "model": "Vengance RGB PRO"
  },
  "42aa42a0bb5fcee780fb1be13dfcb873": {
     "universe": 5,
     "model": "M65 PRO"
  }
}

现有配置加载逻辑

conf变量通过conf = load_config(DEVICE_PATH)定义,load_config()函数实现如下:

def load_config(config_path):
    if not os.path.isfile(config_path):  #Create the file if not present
        open(config_path, "w+")
    if config_path == MQTT_PATH:
         with open(config_path) as f:  #load the config file
            try:
                return json.load(f)
            except json.JSONDecodeError:
                data = {}
                data['enable_MQTT'] = True
                data['ip'] =""
                data['port']= 1883
                data['username'] =""
                data['password'] =""
                data['base_topic'] =""
                with open(config_path, "w", encoding="utf-8") as f:  # Save config
                    json.dump(
                        data,
                        f, 
                        ensure_ascii=False,
                        sort_keys=False,
                        indent=4
                        )
                print(f"MQTT Config Created, please edit {MQTT_PATH} and restart this program!")
                print("For Home Assistant Auto Discovery, set base_topic to homeassistant!")
                sys.exit()
                
    with open(config_path) as f:  #load the config file
        try:
            return json.load(f)
        except json.JSONDecodeError:
            return {}

之前尝试的错误及报错

尝试执行conf = list(conf)将conf转为列表,运行时触发如下报错:

File "c:\Users\tenn0\Documents\Projects\iCue2sACN-mqtt\src\main.py", line 368, in <module>
    universe = conf[device_name.id] 
TypeError: list indices must be integers or slices, not str

报错位置为第一段代码中标注的main.py第368行。

最终解决方案

使用字典嵌套方案即可满足需求,对应修改两处逻辑即可:

  1. 新增设备配置时的写入逻辑修改为:
conf.update({device_name.id: {"model": device_name.model, "universe": universe}})
  1. 原有读取universe的逻辑同步修改,将第368行代码改为:
universe = conf[device_name.id]["universe"]

内容的提问来源于stack exchange,提问作者tenn0

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.29 04:06:03