package inverter_test import ( "testing" "time" "git.netflux.io/rob/solar-toolkit/inverter" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) func TestDecodeDeviceInfo(t *testing.T) { inBytes := []byte{22, 7, 13, 10, 35, 1, 12, 92, 0, 32, 0, 0, 3, 244, 8, 224, 0, 83, 0, 0, 7, 89, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 0, 0, 2, 2, 9, 63, 0, 123, 19, 135, 0, 0, 11, 129, 255, 255, 255, 255, 255, 255, 127, 255, 255, 255, 255, 255, 255, 255, 255, 255, 127, 255, 255, 255, 0, 1, 0, 0, 11, 129, 0, 0, 3, 237, 127, 255, 255, 255, 255, 255, 255, 255, 9, 62, 0, 5, 19, 135, 0, 1, 0, 0, 0, 0, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 0, 0, 7, 148, 128, 0, 0, 0, 128, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7, 148, 0, 2, 2, 119, 127, 255, 1, 148, 1, 0, 14, 118, 255, 255, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 33, 0, 1, 255, 255, 0, 0, 0, 0, 0, 0, 30, 19, 0, 0, 0, 44, 0, 0, 30, 19, 0, 0, 1, 15, 0, 46, 0, 0, 0, 0, 0, 0, 0, 0, 11, 195, 0, 48, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 64, 0, 0, 0, 2, 8, 64, 71, 0, 3, 0, 0, 255, 255} t.Run("with multi-phase inverter", func(t *testing.T) { inv := inverter.ET{SerialNumber: "foo"} runtimeData, err := inv.DecodeRuntimeData(inBytes) require.NoError(t, err) wantLoc, _ := time.LoadLocation("Europe/Madrid") assert.Equal(t, time.Date(2022, 7, 13, 10, 35, 1, 0, wantLoc), runtimeData.Timestamp) assert.Equal(t, inverter.Voltage(316.4), runtimeData.PV1Voltage) assert.Equal(t, inverter.Current(3.2), runtimeData.PV1Current) assert.Equal(t, inverter.Power(1012), runtimeData.PV1Power) assert.Equal(t, inverter.Voltage(227.2), runtimeData.PV2Voltage) assert.Equal(t, inverter.Current(8.3), runtimeData.PV2Current) assert.Equal(t, inverter.Power(1881), runtimeData.PV2Power) assert.Equal(t, inverter.Power(2893), runtimeData.PVPower) assert.Equal(t, inverter.Voltage(236.7), runtimeData.OnGridL1Voltage) assert.Equal(t, inverter.Current(12.3), runtimeData.OnGridL1Current) assert.Equal(t, inverter.Frequency(49.99), runtimeData.OnGridL1Frequency) assert.Equal(t, inverter.Power(2945), runtimeData.OnGridL1Power) assert.Equal(t, inverter.Voltage(-0.1), runtimeData.OnGridL2Voltage) assert.Equal(t, inverter.Current(-0.1), runtimeData.OnGridL2Current) assert.Equal(t, inverter.Frequency(-0.01), runtimeData.OnGridL2Frequency) assert.Equal(t, inverter.Power(2.147483647e+09), runtimeData.OnGridL2Power) assert.Equal(t, inverter.Voltage(-0.1), runtimeData.OnGridL3Voltage) assert.Equal(t, inverter.Current(-0.1), runtimeData.OnGridL3Current) assert.Equal(t, inverter.Frequency(-0.01), runtimeData.OnGridL3Frequency) assert.Equal(t, inverter.Power(2.147483647e+09), runtimeData.OnGridL3Power) assert.Equal(t, 1, runtimeData.GridMode) assert.Equal(t, inverter.Power(2945), runtimeData.TotalInverterPower) assert.Equal(t, inverter.Power(1005), runtimeData.ActivePower) assert.Equal(t, 2147483647, runtimeData.ReactivePower) assert.Equal(t, -1, runtimeData.ApparentPower) assert.Equal(t, inverter.Voltage(236.6), runtimeData.BackupL1Voltage) assert.Equal(t, inverter.Current(0.5), runtimeData.BackupL1Current) assert.Equal(t, inverter.Frequency(49.99), runtimeData.BackupL1Frequency) assert.Equal(t, 1, runtimeData.LoadModeL1) assert.Equal(t, inverter.Power(0), runtimeData.BackupL1Power) assert.Equal(t, inverter.Voltage(-0.1), runtimeData.BackupL2Voltage) assert.Equal(t, inverter.Current(-0.1), runtimeData.BackupL2Current) assert.Equal(t, inverter.Frequency(-0.01), runtimeData.BackupL2Frequency) assert.Equal(t, -1, runtimeData.LoadModeL2) assert.Equal(t, inverter.Power(-1), runtimeData.BackupL2Power) assert.Equal(t, inverter.Voltage(-0.1), runtimeData.BackupL3Voltage) assert.Equal(t, inverter.Current(-0.1), runtimeData.BackupL3Current) assert.Equal(t, inverter.Frequency(-0.01), runtimeData.BackupL3Frequency) assert.Equal(t, -1, runtimeData.LoadModeL3) assert.Equal(t, inverter.Power(-1), runtimeData.BackupL3Power) assert.Equal(t, inverter.Power(1940), runtimeData.LoadL1) assert.Equal(t, inverter.Power(-2.147483648e+09), runtimeData.LoadL2) assert.Equal(t, inverter.Power(-2.147483648e+09), runtimeData.LoadL3) assert.Equal(t, inverter.Power(0), runtimeData.BackupLoad) assert.Equal(t, inverter.Power(1940), runtimeData.Load) assert.Equal(t, 2, runtimeData.UPSLoad) assert.Equal(t, inverter.Temp(63.1), runtimeData.TemperatureAir) assert.Equal(t, inverter.Temp(3276.7), runtimeData.TemperatureModule) assert.Equal(t, inverter.Temp(40.4), runtimeData.Temperature) assert.Equal(t, 256, runtimeData.FunctionBit) assert.Equal(t, inverter.Voltage(370.2), runtimeData.BusVoltage) assert.Equal(t, inverter.Voltage(-0.1), runtimeData.NBusVoltage) assert.Equal(t, inverter.Voltage(0), runtimeData.BatteryVoltage) assert.Equal(t, inverter.Current(0), runtimeData.BatteryCurrent) assert.Equal(t, 0, runtimeData.BatteryMode) assert.Equal(t, 0, runtimeData.WarningCode) assert.Equal(t, 33, runtimeData.SafetyCountryCode) assert.Equal(t, 131071, runtimeData.WorkMode) assert.Equal(t, 0, runtimeData.OperationCode) assert.Equal(t, 0, runtimeData.ErrorCodes) assert.Equal(t, inverter.Energy(769.9), runtimeData.EnergyGenerationTotal) assert.Equal(t, inverter.Energy(4.4), runtimeData.EnergyGenerationToday) assert.Equal(t, inverter.Energy(769.9), runtimeData.EnergyExportTotal) assert.Equal(t, 271, runtimeData.EnergyExportTotalHours) assert.Equal(t, inverter.Energy(4.6), runtimeData.EnergyExportToday) assert.Equal(t, inverter.Energy(0), runtimeData.EnergyImportTotal) assert.Equal(t, inverter.Energy(0), runtimeData.EnergyImportToday) assert.Equal(t, inverter.Energy(301.1), runtimeData.EnergyLoadTotal) assert.Equal(t, inverter.Energy(4.8), runtimeData.EnergyLoadDay) assert.Equal(t, 0, runtimeData.BatteryChargeTotal) assert.Equal(t, 0, runtimeData.BatteryChargeToday) assert.Equal(t, 0, runtimeData.BatteryDischargeTotal) assert.Equal(t, 0, runtimeData.BatteryDischargeToday) assert.Equal(t, 34095175, runtimeData.DiagStatusCode) assert.Equal(t, inverter.Power(1888), runtimeData.HouseConsumption) }) t.Run("with single-phase inverter", func(t *testing.T) { inv := inverter.ET{SerialNumber: "EHUfoo"} runtimeData, err := inv.DecodeRuntimeData(inBytes) require.NoError(t, err) assert.Equal(t, inverter.Voltage(316.4), runtimeData.PV1Voltage) assert.Equal(t, inverter.Current(3.2), runtimeData.PV1Current) assert.Equal(t, inverter.Power(1012), runtimeData.PV1Power) assert.Equal(t, inverter.Voltage(227.2), runtimeData.PV2Voltage) assert.Equal(t, inverter.Current(8.3), runtimeData.PV2Current) assert.Equal(t, inverter.Power(1881), runtimeData.PV2Power) assert.Equal(t, inverter.Power(2893), runtimeData.PVPower) assert.Equal(t, inverter.Voltage(236.7), runtimeData.OnGridL1Voltage) assert.Equal(t, inverter.Current(12.3), runtimeData.OnGridL1Current) assert.Equal(t, inverter.Frequency(49.99), runtimeData.OnGridL1Frequency) assert.Equal(t, inverter.Power(2945), runtimeData.OnGridL1Power) assert.Equal(t, inverter.Voltage(0), runtimeData.OnGridL2Voltage) assert.Equal(t, inverter.Current(0), runtimeData.OnGridL2Current) assert.Equal(t, inverter.Frequency(0), runtimeData.OnGridL2Frequency) assert.Equal(t, inverter.Power(0), runtimeData.OnGridL2Power) assert.Equal(t, inverter.Voltage(0), runtimeData.OnGridL3Voltage) assert.Equal(t, inverter.Current(0), runtimeData.OnGridL3Current) assert.Equal(t, inverter.Frequency(0), runtimeData.OnGridL3Frequency) assert.Equal(t, inverter.Power(0), runtimeData.OnGridL3Power) assert.Equal(t, inverter.Voltage(236.6), runtimeData.BackupL1Voltage) assert.Equal(t, inverter.Current(0.5), runtimeData.BackupL1Current) assert.Equal(t, inverter.Frequency(49.99), runtimeData.BackupL1Frequency) assert.Equal(t, 1, runtimeData.LoadModeL1) assert.Equal(t, inverter.Power(0), runtimeData.BackupL1Power) assert.Equal(t, inverter.Voltage(0), runtimeData.BackupL2Voltage) assert.Equal(t, inverter.Current(0), runtimeData.BackupL2Current) assert.Equal(t, inverter.Frequency(0), runtimeData.BackupL2Frequency) assert.Equal(t, 0, runtimeData.LoadModeL2) assert.Equal(t, inverter.Power(0), runtimeData.BackupL2Power) assert.Equal(t, inverter.Voltage(0), runtimeData.BackupL3Voltage) assert.Equal(t, inverter.Current(0), runtimeData.BackupL3Current) assert.Equal(t, inverter.Frequency(0), runtimeData.BackupL3Frequency) assert.Equal(t, 0, runtimeData.LoadModeL3) assert.Equal(t, inverter.Power(1940), runtimeData.LoadL1) assert.Equal(t, inverter.Power(0), runtimeData.LoadL2) assert.Equal(t, inverter.Power(0), runtimeData.LoadL3) }) } func TestDecodeMeterData(t *testing.T) { inBytes := []byte{0, 1, 0, 45, 0, 10, 0, 0, 0, 1, 4, 114, 0, 0, 0, 0, 4, 114, 0, 226, 3, 201, 3, 231, 3, 231, 3, 200, 19, 132, 73, 48, 193, 246, 71, 195, 119, 16, 0, 0, 4, 114, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 114, 0, 0, 0, 226, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 226, 0, 0, 4, 151, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 151, 0, 255, 9, 44} t.Run("with single-phase inverter", func(t *testing.T) { inv := inverter.ET{SerialNumber: "foo"} meterData, err := inv.DecodeMeterData(inBytes) require.NoError(t, err) assert.Equal(t, 1, meterData.ComMode) assert.Equal(t, 45, meterData.RSSI) assert.Equal(t, 10, meterData.ManufactureCode) assert.Equal(t, 0, meterData.MeterTestStatus) assert.Equal(t, 1, meterData.MeterCommStatus) assert.Equal(t, inverter.Power(1138), meterData.ActivePowerL1) assert.Equal(t, inverter.Power(0), meterData.ActivePowerL2) assert.Equal(t, inverter.Power(0), meterData.ActivePowerL3) assert.Equal(t, inverter.Power(1138), meterData.ActivePowerTotal) assert.Equal(t, 226, meterData.ReactivePowerTotal) assert.Equal(t, 0.969, meterData.MeterPowerFactor1) assert.Equal(t, 0.999, meterData.MeterPowerFactor2) assert.Equal(t, 0.999, meterData.MeterPowerFactor3) assert.Equal(t, 0.968, meterData.MeterPowerFactor) assert.Equal(t, inverter.Frequency(49.96), meterData.MeterFrequency) assert.Equal(t, inverter.Power(723999.375000), meterData.EnergyExportTotal) assert.Equal(t, inverter.Power(100078.125000), meterData.EnergyImportTotal) assert.Equal(t, inverter.Power(1138), meterData.MeterActivePower1) assert.Equal(t, inverter.Power(0), meterData.MeterActivePower2) assert.Equal(t, inverter.Power(0), meterData.MeterActivePower3) assert.Equal(t, inverter.Power(1138), meterData.MeterActivePowerTotal) assert.Equal(t, 226, meterData.MeterReactivePower1) assert.Equal(t, 0, meterData.MeterReactivePower2) assert.Equal(t, 0, meterData.MeterReactivePower3) assert.Equal(t, 226, meterData.MeterReactivePowerTotal) assert.Equal(t, 1175, meterData.MeterApparentPower1) assert.Equal(t, 0, meterData.MeterApparentPower2) assert.Equal(t, 0, meterData.MeterApparentPower3) assert.Equal(t, 1175, meterData.MeterApparentPowerTotal) assert.Equal(t, 255, meterData.MeterType) assert.Equal(t, 2348, meterData.MeterSoftwareVersion) }) t.Run("with multi-phase inverter", func(t *testing.T) { inv := inverter.ET{SerialNumber: "EHUfoo"} meterData, err := inv.DecodeMeterData(inBytes) require.NoError(t, err) assert.Equal(t, 0.969, meterData.MeterPowerFactor1) assert.Equal(t, 0.0, meterData.MeterPowerFactor2) assert.Equal(t, 0.0, meterData.MeterPowerFactor3) assert.Equal(t, 0.968, meterData.MeterPowerFactor) }) }