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