fill_redis_keys.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. #!/usr/bin/env python
  2. """自动为所有模板计算并填充 Redis key"""
  3. import json
  4. from pathlib import Path
  5. # X11 机型配置
  6. X11_TYPE_ID = "201c51890c31c308050100218008390000000000000000000000000000000040"
  7. X11_ZONE_IDS = ["2", "3"]
  8. # 模板类型到 second_key 的映射
  9. TEMPLATE_TYPE_MAP = {
  10. "wash": {
  11. "common": "broadcast-template-jinja:single_card:wash:common",
  12. "time_popular": "broadcast-template-jinja:single_card:wash:time_popular",
  13. "time_custom": "broadcast-template-jinja:single_card:wash:time_custom",
  14. "time_weather": "broadcast-template-jinja:single_card:wash:time_weather",
  15. "time_season": "broadcast-template-jinja:single_card:wash:time_season",
  16. "time_care": "broadcast-template-jinja:single_card:wash:time_care",
  17. "time_environmental": "broadcast-template-jinja:single_card:wash:time_environmental",
  18. "time_health": "broadcast-template-jinja:single_card:wash:time_health",
  19. },
  20. "dry": {
  21. "common": "broadcast-template-jinja:single_card:dry:common",
  22. "time_popular": "broadcast-template-jinja:single_card:dry:time_popular",
  23. "time_custom": "broadcast-template-jinja:single_card:dry:time_custom",
  24. "time_weather": "broadcast-template-jinja:single_card:dry:time_weather",
  25. "time_season": "broadcast-template-jinja:single_card:dry:time_season",
  26. "time_care": "broadcast-template-jinja:single_card:dry:time_care",
  27. "time_environmental": "broadcast-template-jinja:single_card:dry:time_environmental",
  28. }
  29. }
  30. def generate_first_keys(device_type: str, template_type: str, type_id: str, zone_ids: list) -> list:
  31. """生成一级 key 列表"""
  32. keys = []
  33. if template_type == "common":
  34. # common: broadcast-template-jinja:{type_id}:{zone_id}:common
  35. for zone_id in zone_ids:
  36. keys.append(f"broadcast-template-jinja:{type_id}:{zone_id}:common")
  37. else:
  38. # 其他类型: broadcast-template-jinja:{type_id}:{zone_id}:time_made_popularity:{rec_type}
  39. # rec_type: 1=popular, 2=custom
  40. rec_type_map = {
  41. "time_popular": "1",
  42. "time_custom": "2",
  43. "time_weather": "1,2", # 天气可能对应多个
  44. "time_season": "1,2",
  45. "time_care": "1,2",
  46. "time_environmental": "1,2",
  47. "time_health": "1,2",
  48. }
  49. rec_types = rec_type_map.get(template_type, "1,2").split(",")
  50. for zone_id in zone_ids:
  51. for rec_type in rec_types:
  52. keys.append(f"broadcast-template-jinja:{type_id}:{zone_id}:{template_type}:{rec_type}")
  53. return keys
  54. def main():
  55. data_path = Path(__file__).parent.parent / "backend" / "data" / "templates.json"
  56. with open(data_path, 'r', encoding='utf-8') as f:
  57. templates = json.load(f)
  58. updated_count = 0
  59. for tpl in templates:
  60. device_type = tpl.get("device_type", "wash")
  61. template_type = tpl.get("template_type", "common")
  62. machine_model = tpl.get("machine_model", "x11")
  63. # 跳过复用模板(复用模板的 key 由源模板决定)
  64. if tpl.get("reusable_from"):
  65. continue
  66. # 计算二级 key
  67. second_key = TEMPLATE_TYPE_MAP.get(device_type, {}).get(template_type)
  68. if not second_key:
  69. print(f"⚠️ 未知类型: {machine_model}/{device_type}/{template_type} - {tpl['name']}")
  70. continue
  71. # 计算一级 keys
  72. first_keys = generate_first_keys(device_type, template_type, X11_TYPE_ID, X11_ZONE_IDS)
  73. # 更新模板
  74. old_second = tpl.get("redis_key_second", "")
  75. old_first = tpl.get("redis_keys_first", [])
  76. if old_second != second_key or old_first != first_keys:
  77. tpl["redis_key_second"] = second_key
  78. tpl["redis_keys_first"] = first_keys
  79. updated_count += 1
  80. print(f"✅ {tpl['name']}: {second_key}")
  81. # 保存
  82. with open(data_path, 'w', encoding='utf-8') as f:
  83. json.dump(templates, f, ensure_ascii=False, indent=2)
  84. print(f"\n✅ 完成!更新了 {updated_count} 个模板的 Redis key")
  85. if __name__ == "__main__":
  86. main()