| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104 |
- #!/usr/bin/env python
- """自动为所有模板计算并填充 Redis key"""
- import json
- from pathlib import Path
- # X11 机型配置
- X11_TYPE_ID = "201c51890c31c308050100218008390000000000000000000000000000000040"
- X11_ZONE_IDS = ["2", "3"]
- # 模板类型到 second_key 的映射
- TEMPLATE_TYPE_MAP = {
- "wash": {
- "common": "broadcast-template-jinja:single_card:wash:common",
- "time_popular": "broadcast-template-jinja:single_card:wash:time_popular",
- "time_custom": "broadcast-template-jinja:single_card:wash:time_custom",
- "time_weather": "broadcast-template-jinja:single_card:wash:time_weather",
- "time_season": "broadcast-template-jinja:single_card:wash:time_season",
- "time_care": "broadcast-template-jinja:single_card:wash:time_care",
- "time_environmental": "broadcast-template-jinja:single_card:wash:time_environmental",
- "time_health": "broadcast-template-jinja:single_card:wash:time_health",
- },
- "dry": {
- "common": "broadcast-template-jinja:single_card:dry:common",
- "time_popular": "broadcast-template-jinja:single_card:dry:time_popular",
- "time_custom": "broadcast-template-jinja:single_card:dry:time_custom",
- "time_weather": "broadcast-template-jinja:single_card:dry:time_weather",
- "time_season": "broadcast-template-jinja:single_card:dry:time_season",
- "time_care": "broadcast-template-jinja:single_card:dry:time_care",
- "time_environmental": "broadcast-template-jinja:single_card:dry:time_environmental",
- }
- }
- def generate_first_keys(device_type: str, template_type: str, type_id: str, zone_ids: list) -> list:
- """生成一级 key 列表"""
- keys = []
-
- if template_type == "common":
- # common: broadcast-template-jinja:{type_id}:{zone_id}:common
- for zone_id in zone_ids:
- keys.append(f"broadcast-template-jinja:{type_id}:{zone_id}:common")
- else:
- # 其他类型: broadcast-template-jinja:{type_id}:{zone_id}:time_made_popularity:{rec_type}
- # rec_type: 1=popular, 2=custom
- rec_type_map = {
- "time_popular": "1",
- "time_custom": "2",
- "time_weather": "1,2", # 天气可能对应多个
- "time_season": "1,2",
- "time_care": "1,2",
- "time_environmental": "1,2",
- "time_health": "1,2",
- }
-
- rec_types = rec_type_map.get(template_type, "1,2").split(",")
- for zone_id in zone_ids:
- for rec_type in rec_types:
- keys.append(f"broadcast-template-jinja:{type_id}:{zone_id}:{template_type}:{rec_type}")
-
- return keys
- def main():
- data_path = Path(__file__).parent.parent / "backend" / "data" / "templates.json"
-
- with open(data_path, 'r', encoding='utf-8') as f:
- templates = json.load(f)
-
- updated_count = 0
-
- for tpl in templates:
- device_type = tpl.get("device_type", "wash")
- template_type = tpl.get("template_type", "common")
- machine_model = tpl.get("machine_model", "x11")
-
- # 跳过复用模板(复用模板的 key 由源模板决定)
- if tpl.get("reusable_from"):
- continue
-
- # 计算二级 key
- second_key = TEMPLATE_TYPE_MAP.get(device_type, {}).get(template_type)
- if not second_key:
- print(f"⚠️ 未知类型: {machine_model}/{device_type}/{template_type} - {tpl['name']}")
- continue
-
- # 计算一级 keys
- first_keys = generate_first_keys(device_type, template_type, X11_TYPE_ID, X11_ZONE_IDS)
-
- # 更新模板
- old_second = tpl.get("redis_key_second", "")
- old_first = tpl.get("redis_keys_first", [])
-
- if old_second != second_key or old_first != first_keys:
- tpl["redis_key_second"] = second_key
- tpl["redis_keys_first"] = first_keys
- updated_count += 1
- print(f"✅ {tpl['name']}: {second_key}")
-
- # 保存
- with open(data_path, 'w', encoding='utf-8') as f:
- json.dump(templates, f, ensure_ascii=False, indent=2)
-
- print(f"\n✅ 完成!更新了 {updated_count} 个模板的 Redis key")
- if __name__ == "__main__":
- main()
|