diff --git a/src/Tests/Tests.csproj b/src/Tests/Tests.csproj index 8b86662..66984f3 100644 --- a/src/Tests/Tests.csproj +++ b/src/Tests/Tests.csproj @@ -7,13 +7,13 @@ - - - + + + runtime; build; native; contentfiles; analyzers; buildtransitive all - + runtime; build; native; contentfiles; analyzers; buildtransitive all diff --git a/src/Ve.Direct.InfluxDB.Collector/Program.cs b/src/Ve.Direct.InfluxDB.Collector/Program.cs index d847662..b58e24d 100644 --- a/src/Ve.Direct.InfluxDB.Collector/Program.cs +++ b/src/Ve.Direct.InfluxDB.Collector/Program.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using System.ComponentModel; using System.Threading.Tasks; using McMaster.Extensions.CommandLineUtils; @@ -26,7 +27,7 @@ public static int Main(string[] args) switch (config.Output) { case CollectorConfiguration.OutputDefinition.Console: - reader.ReadSerialPortData(null, cancellationToken); + reader.ReadSerialPortData(WriteMetricsCallback, cancellationToken); break; case CollectorConfiguration.OutputDefinition.Influx: var metricsCompositor = new MetricsCompositor(config); @@ -47,5 +48,15 @@ public static int Main(string[] args) return app.Execute(args); } + + private static void WriteMetricsCallback(Dictionary serialData) + { + foreach (var kvp in serialData) + { + var outputValue = kvp.Key.ToLower() == "pid" ? kvp.Value.GetVictronDeviceNameByPid() : kvp.Value; + Console.WriteLine("KeyValue: {0} - {1}", kvp.Key, outputValue); + } + Console.WriteLine("---"); + } } } diff --git a/src/Ve.Direct.InfluxDB.Collector/ProtocolReader/VEDirectReader.cs b/src/Ve.Direct.InfluxDB.Collector/ProtocolReader/VEDirectReader.cs index c94e720..299c6ae 100644 --- a/src/Ve.Direct.InfluxDB.Collector/ProtocolReader/VEDirectReader.cs +++ b/src/Ve.Direct.InfluxDB.Collector/ProtocolReader/VEDirectReader.cs @@ -8,7 +8,7 @@ namespace Ve.Direct.InfluxDB.Collector.ProtocolReader { public class VEDirectReader : IReader { - private readonly Dictionary dict; + private readonly Dictionary serialData; private readonly string serialPortName; private readonly char header1; private readonly char header2; @@ -30,7 +30,7 @@ private enum ReadState public VEDirectReader(string serialPortName) { - this.dict = new Dictionary(); + this.serialData = new Dictionary(); this.serialPortName = serialPortName ?? SerialPort.GetPortNames().FirstOrDefault() ?? throw new NotSupportedException("No serial port found to read VE.Direct data!"); ConsoleLogger.Info($"Using Port: {this.serialPortName}"); @@ -46,7 +46,7 @@ public VEDirectReader(string serialPortName) this.state = ReadState.WAIT_HEADER; } - public Dictionary ProcessInputByte(byte inputByte) + public bool ProcessInputByte(byte inputByte) { var inputByteAsChar = Convert.ToChar(inputByte); if (inputByteAsChar == this.hexmarker && this.state != ReadState.IN_CHECKSUM) @@ -83,10 +83,10 @@ public Dictionary ProcessInputByte(byte inputByte) if (inputByteAsChar == this.header1) { this.state = ReadState.WAIT_HEADER; - if (this.dict.ContainsKey(this.key)) - this.dict[this.key] = this.value; + if (this.serialData.ContainsKey(this.key)) + this.serialData[this.key] = this.value; else - this.dict.Add(this.key, this.value); + this.serialData.Add(this.key, this.value); this.key = ""; this.value = ""; } @@ -103,7 +103,7 @@ public Dictionary ProcessInputByte(byte inputByte) if (this.bytes_sum == 0) { this.bytes_sum = 0; - return this.dict; + return true; } ConsoleLogger.Info($"Warning: bytes_sum = {this.bytes_sum}"); this.bytes_sum = 0; @@ -117,7 +117,7 @@ public Dictionary ProcessInputByte(byte inputByte) default: throw new ArgumentOutOfRangeException(string.Format("Unknown readstate {0}", this.state)); } - return null; + return false; } public void ReadSerialPortData(Action> callbackFunction, CancellationToken ct) @@ -135,22 +135,10 @@ public void ReadSerialPortData(Action> callbackFuncti continue; } - var packet = this.ProcessInputByte(inputByte); - if (packet != null) + var allBytesReceived = this.ProcessInputByte(inputByte); + if (allBytesReceived) { - if (callbackFunction == null) - { - foreach (var kvp in packet) - { - var outputValue = kvp.Key.ToLower() == "pid" ? kvp.Value.GetVictronDeviceNameByPid() : kvp.Value; - Console.WriteLine("KeyValue: {0} - {1}", kvp.Key, outputValue); - } - Console.WriteLine("---"); - } - else - { - callbackFunction(packet); - } + callbackFunction(this.serialData); } } } diff --git a/src/Ve.Direct.InfluxDB.Collector/ProtocolReader/VEDirectReaderWithChecksum.cs b/src/Ve.Direct.InfluxDB.Collector/ProtocolReader/VEDirectReaderWithChecksum.cs index 2483501..cc00631 100644 --- a/src/Ve.Direct.InfluxDB.Collector/ProtocolReader/VEDirectReaderWithChecksum.cs +++ b/src/Ve.Direct.InfluxDB.Collector/ProtocolReader/VEDirectReaderWithChecksum.cs @@ -74,23 +74,11 @@ public void ReadSerialPortData(Action> callbackFuncti } } - private void ProcessData(Action> callbackFunction, Dictionary data, List receivedBytes) + private void ProcessData(Action> callbackFunction, Dictionary serialData, List receivedBytes) { if (this.IsChecksumValid(receivedBytes)) { - if (callbackFunction == null) - { - foreach (var entry in data) - { - var outputValue = entry.Key.ToLower() == "pid" ? entry.Value.GetVictronDeviceNameByPid() : entry.Value; - Console.WriteLine("KeyValue: {0} - {1}", entry.Key, outputValue); - } - Console.WriteLine("---"); - } - else - { - callbackFunction(data); - } + callbackFunction(serialData); } else { diff --git a/src/Ve.Direct.InfluxDB.Collector/ProtocolReader/VictronDeviceExtension.cs b/src/Ve.Direct.InfluxDB.Collector/ProtocolReader/VictronDeviceExtension.cs index b6ab228..b89cf24 100644 --- a/src/Ve.Direct.InfluxDB.Collector/ProtocolReader/VictronDeviceExtension.cs +++ b/src/Ve.Direct.InfluxDB.Collector/ProtocolReader/VictronDeviceExtension.cs @@ -4,47 +4,47 @@ namespace Ve.Direct.InfluxDB.Collector.ProtocolReader { public static class VictronDeviceExtension { - private static readonly Dictionary devices = new Dictionary - { - { "0x300", "BlueSolar MPPT 70/15" }, - { "0xA040", "BlueSolar MPPT 75/50" }, - { "0xA041", "BlueSolar MPPT 150/35" }, - { "0xA042", "BlueSolar MPPT 75/15" }, - { "0xA043", "BlueSolar MPPT 100/15" }, - { "0xA044", "BlueSolar MPPT 100/30" }, - { "0xA045", "BlueSolar MPPT 100/50" }, - { "0xA046", "BlueSolar MPPT 150/70" }, - { "0xA047", "BlueSolar MPPT 150/100" }, - { "0xA049", "BlueSolar MPPT 100/50 rev2" }, - { "0xA04A", "BlueSolar MPPT 100/30 rev2" }, - { "0xA04B", "BlueSolar MPPT 150/35 rev2" }, - { "0xA04C", "BlueSolar MPPT 75/10" }, - { "0xA04D", "BlueSolar MPPT 150/45" }, - { "0xA04E", "BlueSolar MPPT 150/60" }, - { "0xA04F", "BlueSolar MPPT 150/85" }, - { "0xA050", "SmartSolar MPPT 250/100" }, - { "0xA051", "SmartSolar MPPT 150/100*" }, - { "0xA052", "SmartSolar MPPT 150/85*" }, - { "0xA053", "SmartSolar MPPT 75/15" }, - { "0xA054", "SmartSolar MPPT 75/10" }, - { "0xA055", "SmartSolar MPPT 100/15" }, - { "0xA056", "SmartSolar MPPT 100/30" }, - { "0xA057", "SmartSolar MPPT 100/50" }, - { "0xA058", "SmartSolar MPPT 150/35" }, - { "0xA059", "SmartSolar MPPT 150/100 rev2" }, - { "0xA05A", "SmartSolar MPPT 150/85 rev2" }, - { "0xA05B", "SmartSolar MPPT 250/70" }, - { "0xA05C", "SmartSolar MPPT 250/85" }, - { "0xA05D", "SmartSolar MPPT 250/60" }, - { "0xA05E", "SmartSolar MPPT 250/45" }, - { "0xA05F", "SmartSolar MPPT 100/20" }, - { "0xA060", "SmartSolar MPPT 100/20 48V" }, - { "0xA061", "SmartSolar MPPT 150/45" }, - { "0xA062", "SmartSolar MPPT 150/60" }, - { "0xA063", "SmartSolar MPPT 150/70" }, - { "0xA064", "SmartSolar MPPT 250/85 rev2" }, - { "0xA065", "SmartSolar MPPT 250/100 rev2" }, - }; + private static readonly Dictionary devices = new() + { + { "0x300", "BlueSolar MPPT 70/15" }, + { "0xA040", "BlueSolar MPPT 75/50" }, + { "0xA041", "BlueSolar MPPT 150/35" }, + { "0xA042", "BlueSolar MPPT 75/15" }, + { "0xA043", "BlueSolar MPPT 100/15" }, + { "0xA044", "BlueSolar MPPT 100/30" }, + { "0xA045", "BlueSolar MPPT 100/50" }, + { "0xA046", "BlueSolar MPPT 150/70" }, + { "0xA047", "BlueSolar MPPT 150/100" }, + { "0xA049", "BlueSolar MPPT 100/50 rev2" }, + { "0xA04A", "BlueSolar MPPT 100/30 rev2" }, + { "0xA04B", "BlueSolar MPPT 150/35 rev2" }, + { "0xA04C", "BlueSolar MPPT 75/10" }, + { "0xA04D", "BlueSolar MPPT 150/45" }, + { "0xA04E", "BlueSolar MPPT 150/60" }, + { "0xA04F", "BlueSolar MPPT 150/85" }, + { "0xA050", "SmartSolar MPPT 250/100" }, + { "0xA051", "SmartSolar MPPT 150/100*" }, + { "0xA052", "SmartSolar MPPT 150/85*" }, + { "0xA053", "SmartSolar MPPT 75/15" }, + { "0xA054", "SmartSolar MPPT 75/10" }, + { "0xA055", "SmartSolar MPPT 100/15" }, + { "0xA056", "SmartSolar MPPT 100/30" }, + { "0xA057", "SmartSolar MPPT 100/50" }, + { "0xA058", "SmartSolar MPPT 150/35" }, + { "0xA059", "SmartSolar MPPT 150/100 rev2" }, + { "0xA05A", "SmartSolar MPPT 150/85 rev2" }, + { "0xA05B", "SmartSolar MPPT 250/70" }, + { "0xA05C", "SmartSolar MPPT 250/85" }, + { "0xA05D", "SmartSolar MPPT 250/60" }, + { "0xA05E", "SmartSolar MPPT 250/45" }, + { "0xA05F", "SmartSolar MPPT 100/20" }, + { "0xA060", "SmartSolar MPPT 100/20 48V" }, + { "0xA061", "SmartSolar MPPT 150/45" }, + { "0xA062", "SmartSolar MPPT 150/60" }, + { "0xA063", "SmartSolar MPPT 150/70" }, + { "0xA064", "SmartSolar MPPT 250/85 rev2" }, + { "0xA065", "SmartSolar MPPT 250/100 rev2" }, + }; public static string GetVictronDeviceNameByPid(this string self) { diff --git a/src/Ve.Direct.InfluxDB.Collector/Ve.Direct.InfluxDB.Collector.csproj b/src/Ve.Direct.InfluxDB.Collector/Ve.Direct.InfluxDB.Collector.csproj index 28ce532..2758f7a 100644 --- a/src/Ve.Direct.InfluxDB.Collector/Ve.Direct.InfluxDB.Collector.csproj +++ b/src/Ve.Direct.InfluxDB.Collector/Ve.Direct.InfluxDB.Collector.csproj @@ -7,9 +7,9 @@ - - - + + + \ No newline at end of file