import unittest
import json
from growattServerSR import map_mix_settings_to_ac_charge_time_period, map_mix_settings_to_ac_discharge_time_period

class TestGrowattServer(unittest.TestCase):

  def test_map_mix_settings_to_ac_charge_time_period(self):
    actual = map_mix_settings_to_ac_charge_time_period(MAP_SETTINGS_OUTPUT)
    self.assertListEqual(EXPECTED_CHARGE_SCHEDULE, actual)

  def test_map_mix_settings_to_ac_discharge_time_period(self):
    actual = map_mix_settings_to_ac_discharge_time_period(MAP_SETTINGS_OUTPUT)
    self.assertListEqual(EXPECTED_DISCHARGE_SCHEDULE, actual)


EXPECTED_CHARGE_SCHEDULE = [
"100",
"50",
"1",
"02", "00",
"05", "00",
"1",
"03", "05",
"06", "00",
"0",
"04", "06",
"07", "00",
"0"]

EXPECTED_DISCHARGE_SCHEDULE = [
"100",
"40",
"16", "00",
"19", "00",
"1",
"00", "00",
"00", "00",
"0",
"00", "00",
"00", "00",
"0"]

MAP_SETTINGS_OUTPUT = {
  "deviceType": 0,
  "msg": "msg",
  "result": 1,
  "dtc": 0,
  "haveMeter": 0,
  "obj": {
    "mixBean": {
      "mix_ac_discharge_frequency": "0",
      "communicationVersion": "ZCBA-0006",
      "singleExport": "0",
      "dischargeTime3": None,
      "bagingTestStep": "0",
      "chargePowerCommand": "100",
      "uwLVRT2EE": "0.0",
      "ccCurrent": "0.0",
      "dischargeTime2": None,
      "dischargeTime1": None,
      "record": None,
      "deviceType": "0",
      "vbatStartforCharge": "58.0",
      "uwHFRTTime2EE": "0",
      "uwReconnectStartSlope": "0.0",
      "outPower": "5.0",
      "powerMaxText": "",
      "backUpEn": "0",
      "cvVoltage": "0.0",
      "pv_pf_cmd_memory_state": "1",
      "energyMonth": "0.0",
      "safetyVersion": "0",
      "acChargeEnable": "1",
      "uwHFRTEE": "0.0",
      "reactiveRate": "100",
      "loadFirstStopSocSet": "10",
      "inPower": "20.0",
      "puEnable": "0",
      "powerMaxTime": None,
      "vbatWarnClr": "5.0",
      "forcedDischargeTimeStop2": "0:0",
      "uwHFRT2EE": "0.0",
      "forcedDischargeTimeStop3": "0:0",
      "forcedDischargeTimeStop1": "19:0",
      "forcedDischargeTimeStop6": "0:0",
      "modbusVersion": "305",
      "forcedDischargeTimeStop4": "0:0",
      "buckUPSVoltSet": "0",
      "forcedDischargeTimeStop5": "0:0",
      "plantId": "0",
      "mix_ct_select": "0",
      "exportLimit": "0",
      "energyMonthText": "0",
      "vbatStopForCharge": "5.75",
      "v1": "207.0",
      "dataLogSn": "KWK1CLP0D6",
      "v2": "211.0",
      "v3": "248.0",
      "batteryType": "1",
      "v4": "253.0",
      "innerVersion": "RAAA212006",
      "groupId": "-1",
      "pv_grid_voltage_high": "262.2",
      "batFirstSwitch3": "0",
      "forcedChargeTimeStop1": "5:0",
      "priorityChoose": "0",
      "vbatStartForDischarge": "48.0",
      "forcedChargeTimeStop2": "6:0",
      "forcedChargeTimeStop3": "7:0",
      "pv_active_p_rate": "100",
      "batFirstSwitch2": "0",
      "batFirstSwitch1": "0",
      "countrySelected": "0",
      "forcedChargeTimeStop4": "0:0",
      "forcedChargeTimeStop5": "0:0",
      "forcedChargeTimeStop6": "0:0",
      "pvPfCmdMemoryState": "1",
      "sgipEn": "0",
      "epsFunEn": "0",
      "uwLFRTTimeEE": "0",
      "imgPath": "./css/img/status_green.gif",
      "region": "0",
      "uwHVRT2EE": "0.0",
      "lastUpdateTime": "Thu Apr 03 22:12:49 CST 2025",
      "pmax": "00",
      "winterOrMaintain": "1",
      "uspFreqSet": "0",
      "tcpServerIp": "8.211.60.146",
      "wselectBaudrate": "0",
      "rrcrEnable": "0",
      "uwLVRT3EE": "0.0",
      "epsFreqSet": "0",
      "activeRate": "100",
      "batSysRateEnergy": "0.0",
      "invVersion": "1",
      "batTempLowerLimitC": "110.0",
      "batTempLowerLimitD": "110.0",
      "safetyNum": "00",
      "userName": None,
      "comAddress": "1",
      "pv_reactive_p_rate": "100",
      "uwLVRTTimeEE": "0",
      "uwHFRTTimeEE": "0",
      "bctAdjust": "0",
      "chargeTime3": None,
      "chargeTime2": None,
      "energyDay": "0.0",
      "chargeTime1": None,
      "statusText": "mix.status.normal",
      "forcedChargeTimeStart2": "3:5",
      "forcedChargeTimeStart3": "4:6",
      "batParallelNum": "0",
      "location": "",
      "forcedChargeTimeStart1": "2:0",
      "vnormal": "360.0",
      "forcedChargeTimeStart6": "0:0",
      "forcedChargeTimeStart4": "0:0",
      "forcedChargeTimeStart5": "0:0",
      "plantname": None,
      "vbatWarning": "480.0",
      "gridFirstSwitch1": "0",
      "gridFirstSwitch2": "0",
      "bctMode": "0",
      "gridFirstSwitch3": "0",
      "lcdLanguage": "1",
      "pv_reactive_p_rate_two": "over",
      "children": None,
      "lost": "false",
      "safety": "05",
      "model": "1683926900000",
      "id": "0",
      "uwHVRTTimeEE": "0",
      "buckUpsFunEn": "1",
      "voltageLowLimit": "184.0",
      "serialNum": "OUCTCGP08S",
      "uwLVRTTime2EE": "0",
      "disChargePowerCommand": "100",
      "powerMax": None,
      "forcedDischargeStopSwitch2": "0",
      "forcedDischargeStopSwitch1": "1",
      "forcedDischargeStopSwitch6": "0",
      "loadFirstControl": "0",
      "uwHVRTEE": "0.0",
      "forcedDischargeStopSwitch5": "0",
      "forcedDischargeStopSwitch4": "0",
      "forcedDischargeStopSwitch3": "0",
      "status": "5",
      "pv_power_factor": "0.0",
      "reactiveDelay": "150.0",
      "parentID": "LIST_KWK1CLP0D6_96",
      "manufacturer": "   New Energy   ",
      "treeID": "ST_OUCTCGP08S",
      "sysTime": "2025-04-03 04:12:56",
      "uwLFRTEE": "0.0",
      "onOff": "1",
      "forcedDischargeTimeStart1": "16:0",
      "mix_off_grid_enable": "1",
      "forcedDischargeTimeStart3": "0:0",
      "forcedDischargeTimeStart2": "0:0",
      "proPto": "0",
      "forcedDischargeTimeStart5": "0:0",
      "forcedDischargeTimeStart4": "0:0",
      "batSerialNum": None,
      "exportLimitPowerRate": "0.0",
      "forcedDischargeTimeStart6": "0:0",
      "oldErrorFlag": "0",
      "floatChargeCurrentLimit": "600.0",
      "newSwVersionFlag": "0",
      "uwLFRTTime2EE": "0",
      "uwNominalGridVolt": "0.0",
      "mix_ac_discharge_voltage": "0",
      "forcedChargeStopSwitch2": "0",
      "forcedChargeStopSwitch1": "1",
      "uwLVRTEE": "0.0",
      "forcedChargeStopSwitch6": "0",
      "forcedChargeStopSwitch5": "0",
      "forcedChargeStopSwitch4": "0",
      "forcedChargeStopSwitch3": "0",
      "updating": "false",
      "batSeriesNum": "0",
      "lastUpdateTimeText": "2025-04-03 22:12:49",
      "vbatStopForDischarge": "4.7",
      "wdisChargeSOCLowLimit2": "40",
      "wdisChargeSOCLowLimit1": "100",
      "wchargeSOCLowLimit1": "100",
      "wchargeSOCLowLimit2": "50",
      "epsVoltSet": "0",
      "level": "4",
      "uwGridWattDelay": "0",
      "pCharge": "0.0",
      "safetyCorrespondNum": "0",
      "batTempUpperLimitC": "60.0",
      "pv_on_off": "1",
      "uwHVRTTime2EE": "0",
      "batTempUpperLimitD": "70.0",
      "pv_grid_voltage_low": "184.0",
      "powerFactor": "0.0",
      "sysTimeText": "2025-04-03 04:12:56",
      "underExcited": "0",
      "timezone": "8.0",
      "voltageHighLimit": "262.2",
      "portName": "port_name",
      "offGridDischargeSOC": "0",
      "modelText": "A0B1D0T0PFU2M6S5",
      "dtc": "3501",
      "pro_pto": "0",
      "treeName": "OUCTCGP08S",
      "alias": "OUCTCGP08S",
      "fwVersion": "RA1.0",
      "addr": "81",
      "pDischarge": "0.0",
      "uwLFRT2EE": "0.0",
      "failsafe": "1",
      "uwLVRTTime3EE": "0",
      "lvVoltage": "0.0",
      "vppOpen": "0",
      "reactivePowerLimit": "48.0",
      "pf_sys_year": "2025-04-03 04:12:56",
      "backflow_setting": "0"
    }
  },
  "normalPower": 0,
  "model": ""
}

unittest.main()