Skip to content

Commit 7872fc6

Browse files
committed
upgrade method
1 parent 0bf6fe4 commit 7872fc6

1 file changed

Lines changed: 50 additions & 48 deletions

File tree

packages/helpermodules/update_config.py

Lines changed: 50 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -55,14 +55,6 @@
5555

5656
NO_MODULE = {"type": None, "configuration": {}}
5757

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-
6658

6759
class UpdateConfig:
6860

@@ -2596,71 +2588,81 @@ def upgrade(topic: str, payload) -> None:
25962588
self.__update_topic("openWB/system/datastore_version", 98)
25972589

25982590
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+
25992625
def add_colors_to_logs():
26002626
files = glob.glob(str(self.base_path / "data" / "daily_log") + "/*")
26012627
files.extend(glob.glob(str(self.base_path / "data" / "monthly_log") + "/*"))
26022628
files.sort()
26032629
with ProcessPoolExecutor() as executor:
2604-
executor.map(self.process_file, files)
2630+
executor.map(_add_colors_to_log, files)
26052631

26062632
def upgrade(topic: str, payload) -> Optional[dict]:
26072633
# add vehicle color to vehicle topics
26082634
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}'")
26102636
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}'")
26122638
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']}
26152642
# add property "color" to charge points
26162643
if re.search("^openWB/chargepoint/[0-9]+/config$", topic) is not None:
26172644
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}")
26192646
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}")
26222649
return {topic: config}
26232650
# add property "color" to components
26242651
if re.search("^openWB/system/device/[0-9]+/component/[0-9]+/config$", topic) is not None:
26252652
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}")
26272654
if "color" not in config:
26282655
if "counter" in config.get("type").lower():
2629-
config.update({"color": COLOR_COUNTER})
2656+
config.update({"color": DEFAULT_COLORS['COUNTER']})
26302657
elif "bat" in config.get("type").lower():
2631-
config.update({"color": COLOR_BATTERY})
2658+
config.update({"color": DEFAULT_COLORS['BATTERY']})
26322659
elif "inverter" in config.get("type").lower():
2633-
config.update({"color": COLOR_INVERTER})
2660+
config.update({"color": DEFAULT_COLORS['INVERTER']})
26342661
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}")
26382665
return {topic: config}
26392666
self._loop_all_received_topics(upgrade)
26402667
add_colors_to_logs()
26412668
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

Comments
 (0)