|
| 1 | +"""Test the divert_sim process""" |
| 2 | + |
| 3 | +# pylint: disable=line-too-long |
| 4 | + |
| 5 | +from os import path |
| 6 | +import os |
| 7 | +from subprocess import PIPE, Popen |
| 8 | +from datetime import datetime |
| 9 | + |
| 10 | +OPENEVSE_STATE_STARTING = 0 |
| 11 | +OPENEVSE_STATE_NOT_CONNECTED = 1 |
| 12 | +OPENEVSE_STATE_CONNECTED = 2 |
| 13 | +OPENEVSE_STATE_CHARGING = 3 |
| 14 | +OPENEVSE_STATE_VENT_REQUIRED = 4 |
| 15 | +OPENEVSE_STATE_DIODE_CHECK_FAILED = 5 |
| 16 | +OPENEVSE_STATE_GFI_FAULT = 6 |
| 17 | +OPENEVSE_STATE_NO_EARTH_GROUND = 7 |
| 18 | +OPENEVSE_STATE_STUCK_RELAY = 8 |
| 19 | +OPENEVSE_STATE_GFI_SELF_TEST_FAILED = 9 |
| 20 | +OPENEVSE_STATE_OVER_TEMPERATURE = 0 |
| 21 | +OPENEVSE_STATE_OVER_CURRENT = 1 |
| 22 | +OPENEVSE_STATE_SLEEPING = 4 |
| 23 | +OPENEVSE_STATE_DISABLED = 5 |
| 24 | + |
| 25 | +KWH_ROUNDING = 2 |
| 26 | + |
| 27 | +summary_filename = 'summary.csv' |
| 28 | + |
| 29 | +def setup_summary(postfix: str = ''): |
| 30 | + global summary_filename |
| 31 | + """Create the output directory""" |
| 32 | + print("Setting up test environment") |
| 33 | + if not path.exists('output'): |
| 34 | + os.mkdir('output') |
| 35 | + summary_filename = 'summary'+postfix+'.csv' |
| 36 | + with open(path.join('output', summary_filename), 'w', encoding="utf-8") as summary_file: |
| 37 | + summary_file.write('"Dataset","Config","Total Solar (kWh)","Total EV Charge (kWh)","Charge from solar (kWh)","Charge from grid (kWh)","Number of charges","Min time charging","Max time charging","Total time charging"\n') |
| 38 | + |
| 39 | +def run_simulation(dataset: str, |
| 40 | + output: str, |
| 41 | + config: bool = False, grid_ie_col: bool = False, |
| 42 | + solar_col: bool = False, voltage_col: bool = False, |
| 43 | + separator: str = ',', is_kw: bool = False) -> None: |
| 44 | + """Run the divert_sim process on the given dataset and return the results""" |
| 45 | + line_number = 0 |
| 46 | + |
| 47 | + last_date = None |
| 48 | + last_state = 0 |
| 49 | + charge_start_date = None |
| 50 | + |
| 51 | + total_solar_wh = 0 |
| 52 | + total_ev_wh = 0 |
| 53 | + wh_from_solar = 0 |
| 54 | + wh_from_grid = 0 |
| 55 | + number_of_charges = 0 |
| 56 | + min_time_charging = 0 |
| 57 | + max_time_charging = 0 |
| 58 | + total_time_charging = 0 |
| 59 | + |
| 60 | + print("Testing dataset: " + dataset) |
| 61 | + |
| 62 | + # Read in the dataset and pass to the divert_sim process |
| 63 | + with open(path.join('data', dataset+'.csv'), 'r', encoding="utf-8") as input_data: |
| 64 | + with open(path.join('output', output+'.csv'), 'w', encoding="utf-8") as output_data: |
| 65 | + # open the divert_sim process |
| 66 | + command = ["./divert_sim"] |
| 67 | + if config: |
| 68 | + command.append("-c") |
| 69 | + command.append(config) |
| 70 | + if grid_ie_col: |
| 71 | + command.append("-g") |
| 72 | + command.append(str(grid_ie_col)) |
| 73 | + if solar_col: |
| 74 | + command.append("-s") |
| 75 | + command.append(str(solar_col)) |
| 76 | + if voltage_col: |
| 77 | + command.append("-v") |
| 78 | + command.append(str(voltage_col)) |
| 79 | + if separator: |
| 80 | + command.append("--sep") |
| 81 | + command.append(separator) |
| 82 | + if is_kw: |
| 83 | + command.append("--kw") |
| 84 | + |
| 85 | + divert_process = Popen(command, stdin=input_data, stdout=PIPE, |
| 86 | + stderr=PIPE, universal_newlines=True) |
| 87 | + while True: |
| 88 | + output = divert_process.stdout.readline() |
| 89 | + if output == '' and divert_process.poll() is not None: |
| 90 | + break |
| 91 | + if output: |
| 92 | + output_data.write(output) |
| 93 | + line_number += 1 |
| 94 | + if line_number > 1: |
| 95 | + # read in the csv line |
| 96 | + csv_line = output.split(',') |
| 97 | + date = datetime.strptime(csv_line[0], '%d/%m/%Y %H:%M:%S') |
| 98 | + solar = float(csv_line[1]) |
| 99 | + # grid_ie = float(csv_line[2]) |
| 100 | + # pilot = int(csv_line[3]) |
| 101 | + charge_power = float(csv_line[4]) |
| 102 | + # min_charge_power = float(csv_line[5]) |
| 103 | + state = int(csv_line[6]) |
| 104 | + # smoothed_available = float(csv_line[7]) |
| 105 | + |
| 106 | + if last_date is not None: |
| 107 | + # Get the difference between this date and last date |
| 108 | + diff = date - last_date |
| 109 | + |
| 110 | + hours = diff.seconds / 3600 |
| 111 | + |
| 112 | + total_solar_wh += solar * hours |
| 113 | + ev_wh = charge_power * hours |
| 114 | + total_ev_wh += charge_power * hours |
| 115 | + charge_from_solar_wh = min(solar, charge_power) * hours |
| 116 | + wh_from_solar += charge_from_solar_wh |
| 117 | + wh_from_grid += ev_wh - charge_from_solar_wh |
| 118 | + |
| 119 | + if state == OPENEVSE_STATE_CHARGING and last_state != OPENEVSE_STATE_CHARGING: |
| 120 | + number_of_charges += 1 |
| 121 | + charge_start_date = date |
| 122 | + |
| 123 | + if state != OPENEVSE_STATE_CHARGING and last_state == OPENEVSE_STATE_CHARGING: |
| 124 | + this_session_time_charging = (date - charge_start_date).seconds |
| 125 | + total_time_charging += this_session_time_charging |
| 126 | + if min_time_charging == 0 or this_session_time_charging < min_time_charging: |
| 127 | + min_time_charging = this_session_time_charging |
| 128 | + if this_session_time_charging > max_time_charging: |
| 129 | + max_time_charging = this_session_time_charging |
| 130 | + |
| 131 | + last_date = date |
| 132 | + last_state = state |
| 133 | + |
| 134 | + solar_kwh=total_solar_wh / 1000 |
| 135 | + ev_kwh=total_ev_wh / 1000 |
| 136 | + kwh_from_solar=wh_from_solar / 1000 |
| 137 | + kwh_from_grid=wh_from_grid / 1000 |
| 138 | + |
| 139 | + if config is False or config.startswith('{'): |
| 140 | + config = "Default" |
| 141 | + |
| 142 | + with open(path.join('output', summary_filename), 'a', encoding="utf-8") as summary_file: |
| 143 | + summary_file.write(f'"{dataset}","{config}",{solar_kwh},{ev_kwh},{kwh_from_solar},{kwh_from_grid},{number_of_charges},{min_time_charging},{max_time_charging},{total_time_charging}\n') |
| 144 | + |
| 145 | + |
| 146 | + return (round(solar_kwh, KWH_ROUNDING), |
| 147 | + round(ev_kwh, KWH_ROUNDING), |
| 148 | + round(kwh_from_solar, KWH_ROUNDING), |
| 149 | + round(kwh_from_grid, KWH_ROUNDING), |
| 150 | + number_of_charges, |
| 151 | + min_time_charging, |
| 152 | + max_time_charging, |
| 153 | + total_time_charging) |
0 commit comments