Skip to content

Commit

Permalink
small improvements & cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
DocBrown101 committed Dec 18, 2024
1 parent 17c0e34 commit f0ce8f3
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 86 deletions.
8 changes: 4 additions & 4 deletions src/Tests/Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageReference Include="xunit" Version="2.6.4" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.6">
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.12.0" />
<PackageReference Include="xunit" Version="2.9.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="3.0.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="6.0.0">
<PackageReference Include="coverlet.collector" Version="6.0.2">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
Expand Down
13 changes: 12 additions & 1 deletion src/Ve.Direct.InfluxDB.Collector/Program.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Threading.Tasks;
using McMaster.Extensions.CommandLineUtils;
Expand Down Expand Up @@ -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);
Expand All @@ -47,5 +48,15 @@ public static int Main(string[] args)

return app.Execute(args);
}

private static void WriteMetricsCallback(Dictionary<string, string> 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("---");
}
}
}
34 changes: 11 additions & 23 deletions src/Ve.Direct.InfluxDB.Collector/ProtocolReader/VEDirectReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace Ve.Direct.InfluxDB.Collector.ProtocolReader
{
public class VEDirectReader : IReader
{
private readonly Dictionary<string, string> dict;
private readonly Dictionary<string, string> serialData;
private readonly string serialPortName;
private readonly char header1;
private readonly char header2;
Expand All @@ -30,7 +30,7 @@ private enum ReadState

public VEDirectReader(string serialPortName)
{
this.dict = new Dictionary<string, string>();
this.serialData = new Dictionary<string, string>();
this.serialPortName = serialPortName ?? SerialPort.GetPortNames().FirstOrDefault() ?? throw new NotSupportedException("No serial port found to read VE.Direct data!");

ConsoleLogger.Info($"Using Port: {this.serialPortName}");
Expand All @@ -46,7 +46,7 @@ public VEDirectReader(string serialPortName)
this.state = ReadState.WAIT_HEADER;
}

public Dictionary<string, string> ProcessInputByte(byte inputByte)
public bool ProcessInputByte(byte inputByte)
{
var inputByteAsChar = Convert.ToChar(inputByte);
if (inputByteAsChar == this.hexmarker && this.state != ReadState.IN_CHECKSUM)
Expand Down Expand Up @@ -83,10 +83,10 @@ public Dictionary<string, string> 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 = "";
}
Expand All @@ -103,7 +103,7 @@ public Dictionary<string, string> 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;
Expand All @@ -117,7 +117,7 @@ public Dictionary<string, string> ProcessInputByte(byte inputByte)
default:
throw new ArgumentOutOfRangeException(string.Format("Unknown readstate {0}", this.state));
}
return null;
return false;
}

public void ReadSerialPortData(Action<Dictionary<string, string>> callbackFunction, CancellationToken ct)
Expand All @@ -135,22 +135,10 @@ public void ReadSerialPortData(Action<Dictionary<string, string>> 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);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,23 +74,11 @@ public void ReadSerialPortData(Action<Dictionary<string, string>> callbackFuncti
}
}

private void ProcessData(Action<Dictionary<string, string>> callbackFunction, Dictionary<string, string> data, List<byte> receivedBytes)
private void ProcessData(Action<Dictionary<string, string>> callbackFunction, Dictionary<string, string> serialData, List<byte> 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
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,47 +4,47 @@ namespace Ve.Direct.InfluxDB.Collector.ProtocolReader
{
public static class VictronDeviceExtension
{
private static readonly Dictionary<string, string> devices = new Dictionary<string, string>
{
{ "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<string, string> 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)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="InfluxDB.Client" Version="4.14.0" />
<PackageReference Include="McMaster.Extensions.CommandLineUtils" Version="4.1.0" />
<PackageReference Include="System.IO.Ports" Version="8.0.0" />
<PackageReference Include="InfluxDB.Client" Version="4.18.0" />
<PackageReference Include="McMaster.Extensions.CommandLineUtils" Version="4.1.1" />
<PackageReference Include="System.IO.Ports" Version="9.0.0" />
</ItemGroup>

</Project>

0 comments on commit f0ce8f3

Please sign in to comment.