|
55 | 55 |
|
56 | 56 | NO_MODULE = {"type": None, "configuration": {}} |
57 | 57 |
|
58 | | -# Default colors |
59 | | -COLOR_CHARGEPOINT = "#007bff" # Default color for charge points: blue |
60 | | -COLOR_VEHICLE = "#17a2b8" # Default color for vehicles: teal |
61 | | -COLOR_INVERTER = "#28a745" # Default color for inverters: green |
62 | | -COLOR_COUNTER = "#dc3545" # Default color for counters: red |
63 | | -COLOR_BATTERY = "#ffc107" # Default color for batteries: yellow |
64 | | -COLOR_UNKNOWN = "#000000" # Default color for unknown components: black |
65 | | - |
66 | 58 |
|
67 | 59 | class UpdateConfig: |
68 | 60 |
|
@@ -2596,71 +2588,81 @@ def upgrade(topic: str, payload) -> None: |
2596 | 2588 | self.__update_topic("openWB/system/datastore_version", 98) |
2597 | 2589 |
|
2598 | 2590 | def upgrade_datastore_98(self) -> None: |
| 2591 | + DEFAULT_COLORS = { |
| 2592 | + "CHARGEPOINT": "#007bff", |
| 2593 | + "VEHICLE": "#17a2b8", |
| 2594 | + "INVERTER": "#28a745", |
| 2595 | + "COUNTER": "#dc3545", |
| 2596 | + "BATTERY": "#ffc107", |
| 2597 | + "UNKNOWN": "#000000" |
| 2598 | + } |
| 2599 | + |
| 2600 | + def _add_colors_to_log(file): |
| 2601 | + colors = {} |
| 2602 | + with open(file, "r+") as jsonFile: |
| 2603 | + content_raw = jsonFile.read() |
| 2604 | + content = json.loads(content_raw) |
| 2605 | + if "colors" in content: |
| 2606 | + return |
| 2607 | + for key in content["names"].keys(): |
| 2608 | + if "bat" in key: |
| 2609 | + colors[key] = DEFAULT_COLORS["BATTERY"] |
| 2610 | + elif "counter" in key: |
| 2611 | + colors[key] = DEFAULT_COLORS["COUNTER"] |
| 2612 | + elif "cp" in key: |
| 2613 | + colors[key] = DEFAULT_COLORS["CHARGEPOINT"] |
| 2614 | + elif "ev" in key: |
| 2615 | + colors[key] = DEFAULT_COLORS["VEHICLE"] |
| 2616 | + elif "inverter" in key: |
| 2617 | + colors[key] = DEFAULT_COLORS["INVERTER"] |
| 2618 | + else: |
| 2619 | + colors[key] = DEFAULT_COLORS["UNKNOWN"] |
| 2620 | + content["colors"] = colors |
| 2621 | + jsonFile.seek(0) |
| 2622 | + jsonFile.write(json.dumps(content)) |
| 2623 | + jsonFile.truncate() |
| 2624 | + |
2599 | 2625 | def add_colors_to_logs(): |
2600 | 2626 | files = glob.glob(str(self.base_path / "data" / "daily_log") + "/*") |
2601 | 2627 | files.extend(glob.glob(str(self.base_path / "data" / "monthly_log") + "/*")) |
2602 | 2628 | files.sort() |
2603 | 2629 | with ProcessPoolExecutor() as executor: |
2604 | | - executor.map(self.process_file, files) |
| 2630 | + executor.map(_add_colors_to_log, files) |
2605 | 2631 |
|
2606 | 2632 | def upgrade(topic: str, payload) -> Optional[dict]: |
2607 | 2633 | # add vehicle color to vehicle topics |
2608 | 2634 | if re.search("^openWB/vehicle/[0-9]+/name$", topic) is not None: |
2609 | | - log.debug(f"Received vehicle name topic {topic}") |
| 2635 | + log.debug(f"Received vehicle name topic '{topic}'") |
2610 | 2636 | vehicle_color_topic = topic.replace("/name", "/color") |
2611 | | - log.debug(f"Checking for vehicle color topic {vehicle_color_topic}") |
| 2637 | + log.debug(f"Checking for vehicle color topic '{vehicle_color_topic}'") |
2612 | 2638 | if vehicle_color_topic not in self.all_received_topics: |
2613 | | - log.debug(f"Adding vehicle color topic {vehicle_color_topic} with value '{COLOR_VEHICLE}'") |
2614 | | - return {vehicle_color_topic: COLOR_VEHICLE} |
| 2639 | + log.debug(f"Adding vehicle color topic '{vehicle_color_topic}'" |
| 2640 | + f" with value: '{DEFAULT_COLORS['VEHICLE']}'") |
| 2641 | + return {vehicle_color_topic: DEFAULT_COLORS['VEHICLE']} |
2615 | 2642 | # add property "color" to charge points |
2616 | 2643 | if re.search("^openWB/chargepoint/[0-9]+/config$", topic) is not None: |
2617 | 2644 | config = decode_payload(payload) |
2618 | | - log.debug(f"Received charge point config topic {topic} with payload {payload}") |
| 2645 | + log.debug(f"Received charge point config topic '{topic}' with payload: {payload}") |
2619 | 2646 | if "color" not in config: |
2620 | | - config.update({"color": COLOR_CHARGEPOINT}) |
2621 | | - log.debug(f"Added color to charge point config {config}") |
| 2647 | + config.update({"color": DEFAULT_COLORS['CHARGEPOINT']}) |
| 2648 | + log.debug(f"Added color to charge point config: {config}") |
2622 | 2649 | return {topic: config} |
2623 | 2650 | # add property "color" to components |
2624 | 2651 | if re.search("^openWB/system/device/[0-9]+/component/[0-9]+/config$", topic) is not None: |
2625 | 2652 | config = decode_payload(payload) |
2626 | | - log.debug(f"Received component config topic {topic} with payload {payload}") |
| 2653 | + log.debug(f"Received component config topic '{topic}' with payload: {payload}") |
2627 | 2654 | if "color" not in config: |
2628 | 2655 | if "counter" in config.get("type").lower(): |
2629 | | - config.update({"color": COLOR_COUNTER}) |
| 2656 | + config.update({"color": DEFAULT_COLORS['COUNTER']}) |
2630 | 2657 | elif "bat" in config.get("type").lower(): |
2631 | | - config.update({"color": COLOR_BATTERY}) |
| 2658 | + config.update({"color": DEFAULT_COLORS['BATTERY']}) |
2632 | 2659 | elif "inverter" in config.get("type").lower(): |
2633 | | - config.update({"color": COLOR_INVERTER}) |
| 2660 | + config.update({"color": DEFAULT_COLORS['INVERTER']}) |
2634 | 2661 | else: |
2635 | | - log.warning(f"Unknown component type {config.get('type')} for topic {topic}.") |
2636 | | - config.update({"color": COLOR_UNKNOWN}) |
2637 | | - log.debug(f"Updated component config with color {config}") |
| 2662 | + log.warning(f"Unknown component type '{config.get('type')}' for topic '{topic}'.") |
| 2663 | + config.update({"color": DEFAULT_COLORS['UNKNOWN']}) |
| 2664 | + log.debug(f"Updated component config with color: {config}") |
2638 | 2665 | return {topic: config} |
2639 | 2666 | self._loop_all_received_topics(upgrade) |
2640 | 2667 | add_colors_to_logs() |
2641 | 2668 | self.__update_topic("openWB/system/datastore_version", 99) |
2642 | | - |
2643 | | - def process_file(self, file): |
2644 | | - colors = {} |
2645 | | - with open(file, "r+") as jsonFile: |
2646 | | - content_raw = jsonFile.read() |
2647 | | - content = json.loads(content_raw) |
2648 | | - if "colors" in content: |
2649 | | - return |
2650 | | - for key in content["names"].keys(): |
2651 | | - if "bat" in key: |
2652 | | - colors[key] = COLOR_BATTERY |
2653 | | - elif "counter" in key: |
2654 | | - colors[key] = COLOR_COUNTER |
2655 | | - elif "cp" in key: |
2656 | | - colors[key] = COLOR_CHARGEPOINT |
2657 | | - elif "ev" in key: |
2658 | | - colors[key] = COLOR_VEHICLE |
2659 | | - elif "inverter" in key: |
2660 | | - colors[key] = COLOR_INVERTER |
2661 | | - else: |
2662 | | - colors[key] = COLOR_UNKNOWN |
2663 | | - content["colors"] = colors |
2664 | | - jsonFile.seek(0) |
2665 | | - jsonFile.write(json.dumps(content)) |
2666 | | - jsonFile.truncate() |
|
0 commit comments