diff --git a/Code/HanDebugger/HanDebugger/KaifaHanBeta.cs b/Code/HanDebugger/HanDebugger/KaifaHanBeta.cs index 01b421f7..3416731c 100644 --- a/Code/HanDebugger/HanDebugger/KaifaHanBeta.cs +++ b/Code/HanDebugger/HanDebugger/KaifaHanBeta.cs @@ -43,5 +43,28 @@ namespace HanDebugger return new DateTime(year, month, day, hour, minute, second).Subtract(new DateTime(1970, 1, 1, 0, 0, 0, 0)).TotalSeconds; } + + public static int GetInt(int dataPosition, byte[] buffer, int start, int length) + { + const int dataStart = 24; + int value = 0; + int foundPosition = 0; + for (int i = start + dataStart; i < start + length; i++) + { + if (foundPosition == 0) + { + if (buffer[i] == 0x06) + foundPosition = i; + } + else + { + value = value << 8 | + buffer[i]; + if (i == foundPosition + 4) + return value; + } + } + return 0; + } } } diff --git a/Code/HanDebugger/HanDebuggerTest/KaifaHanBetaTest.cs b/Code/HanDebugger/HanDebuggerTest/KaifaHanBetaTest.cs index 530ad0dd..8b5cd00d 100644 --- a/Code/HanDebugger/HanDebuggerTest/KaifaHanBetaTest.cs +++ b/Code/HanDebugger/HanDebuggerTest/KaifaHanBetaTest.cs @@ -45,5 +45,21 @@ namespace HanDebuggerTest double actualList1To2Ratio = (double)packageCount[KaifaHanBeta.List1] / (double)packageCount[KaifaHanBeta.List2]; Assert.AreEqual(targetList1To2Ratio, actualList1To2Ratio, 0.01, "There should be a ratio of List1:List2 of 4"); } + + [TestMethod] + public void TestGetConsumption() + { + List consumption = new List(); + var packages = File.ReadAllLines("ESP 20170918 Raw.txt"); + var lines = packages.Select(line => line.Trim().Split(' ').Select(v => (byte)int.Parse(v, System.Globalization.NumberStyles.HexNumber)).ToArray()).ToArray(); + foreach (var line in lines) + { + if (KaifaHanBeta.GetPackageID(line, 0, line.Length) == KaifaHanBeta.List1) + { + consumption.Add(KaifaHanBeta.GetInt(0, line, 0, line.Length)); + } + } + Assert.AreEqual(1500.0, consumption.Average(), 500.0, "Consumption should be between 1000 and 2000 watts"); + } } }