Added logger. Made changes to enable inverter to be polled in debug mode.
This commit is contained in:
parent
67f27b0e23
commit
bbf971a29a
@ -31,6 +31,7 @@ public class Endpoint : EndpointWithoutRequest<object>
|
||||
{
|
||||
while (!c.IsCancellationRequested && !AppLife.ApplicationStopping.IsCancellationRequested)
|
||||
{
|
||||
/*
|
||||
if (Env.IsDevelopment())
|
||||
{
|
||||
var status = new InverterStatus
|
||||
@ -51,6 +52,7 @@ public class Endpoint : EndpointWithoutRequest<object>
|
||||
yield return status;
|
||||
}
|
||||
else
|
||||
*/
|
||||
yield return Inverter.Status;
|
||||
|
||||
await Task.Delay(2000, c);
|
||||
|
||||
@ -27,8 +27,13 @@
|
||||
<UseSystemResourceKeys>true</UseSystemResourceKeys>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Content Remove="nlog.config" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="FastEndpoints.Generator" Version="5.35.0" PrivateAssets="all" IncludeAssets="runtime; build; native; contentfiles; analyzers; buildtransitive" />
|
||||
<PackageReference Include="NLog.Web.AspNetCore" Version="6.0.4" />
|
||||
<PackageReference Include="SerialPortLib" Version="1.1.2" />
|
||||
<PackageReference Include="FastEndpoints" Version="5.35.0" />
|
||||
<PackageReference Include="LiteDB" Version="5.0.21" />
|
||||
@ -42,6 +47,11 @@
|
||||
|
||||
<ItemGroup>
|
||||
<None Include="../changelog.md" Link="changelog.md" />
|
||||
<None Include="nlog.config">
|
||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||
</None>
|
||||
</ItemGroup>
|
||||
|
||||
<ProjectExtensions><VisualStudio><UserProperties appsettings_1json__JsonSchema="" /></VisualStudio></ProjectExtensions>
|
||||
|
||||
</Project>
|
||||
@ -2,10 +2,13 @@
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.IO.Ports;
|
||||
using InverterMon.Shared.Models;
|
||||
using System.Diagnostics;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
|
||||
namespace InverterMon.Server.InverterService;
|
||||
|
||||
sealed class SettingsData
|
||||
sealed class SettingsData
|
||||
{
|
||||
public double BatteryCutOffVoltage { get; init; }
|
||||
public double BatteryCvChargingVoltage { get; init; }
|
||||
@ -22,6 +25,11 @@ sealed class SettingsData
|
||||
SuppressMessage("ReSharper", "MemberCanBeMadeStatic.Global")]
|
||||
public sealed class FelicitySolarInverter
|
||||
{
|
||||
private readonly ILogger<FelicitySolarInverter> _logger;
|
||||
public FelicitySolarInverter(ILogger<FelicitySolarInverter> logger)
|
||||
{
|
||||
_logger = logger;
|
||||
}
|
||||
public InverterStatus Status { get; } = new();
|
||||
|
||||
const byte SlaveAddress = 0x01;
|
||||
@ -159,6 +167,9 @@ public sealed class FelicitySolarInverter
|
||||
// [Slave Address][Function Code 0x03][Start Address Hi][Start Address Lo][Quantity Hi][Quantity Lo][CRC Lo][CRC Hi]
|
||||
|
||||
byte[] frame;
|
||||
using ILoggerFactory factory = LoggerFactory.Create(builder => builder.AddConsole());
|
||||
ILogger logger = factory.CreateLogger("Felicity");
|
||||
logger.LogInformation("ReadRegisters Logging");
|
||||
|
||||
var statusRequest = startAddress == StatusStartAddress && numberOfPoints == StatusRegisterCount;
|
||||
|
||||
@ -181,6 +192,16 @@ public sealed class FelicitySolarInverter
|
||||
_cachedStatusFrame = frame;
|
||||
}
|
||||
|
||||
// debug
|
||||
string str = "";
|
||||
for (int i = 0; i < 8; i++)
|
||||
{
|
||||
str += frame[i].ToString("X") + " ";
|
||||
}
|
||||
Debug.WriteLine("Request frame: " + str);
|
||||
logger.LogInformation("Request frame: {str}", str);
|
||||
// end of debug
|
||||
|
||||
var response = SendModbusRequest(frame);
|
||||
|
||||
if (response.Length == 0)
|
||||
@ -192,6 +213,16 @@ public sealed class FelicitySolarInverter
|
||||
int byteCount = response[2];
|
||||
var expectedDataBytes = numberOfPoints * 2;
|
||||
|
||||
// debug
|
||||
str = "";
|
||||
for (int i = 0; i < response.Length; i++)
|
||||
{
|
||||
str += response[i].ToString("X") + " ";
|
||||
}
|
||||
Debug.WriteLine("Response frame: " + str);
|
||||
logger.LogInformation("Response frame: {str}", str);
|
||||
// end of debug
|
||||
|
||||
if (byteCount != expectedDataBytes)
|
||||
throw new InvalidDataException("Unexpected byte count in response!");
|
||||
|
||||
@ -199,6 +230,16 @@ public sealed class FelicitySolarInverter
|
||||
for (var i = 0; i < numberOfPoints; i++)
|
||||
registers[i] = (short)((response[3 + i * 2] << 8) | response[3 + i * 2 + 1]);
|
||||
|
||||
// debug
|
||||
str = "";
|
||||
for (int i = 0; i < numberOfPoints; i++)
|
||||
{
|
||||
str += registers[i].ToString("X") + " ";
|
||||
}
|
||||
Debug.WriteLine("Registers: " + str);
|
||||
logger.LogInformation("Registers: {str}", str);
|
||||
// end of debug
|
||||
|
||||
return registers;
|
||||
}
|
||||
|
||||
|
||||
@ -6,6 +6,7 @@ using InverterMon.Server.BatteryService;
|
||||
using InverterMon.Server.InverterService;
|
||||
using InverterMon.Server.Persistence;
|
||||
using InverterMon.Server.Persistence.Settings;
|
||||
using NLog.Extensions.Logging;
|
||||
|
||||
//avoid parsing issues with non-english cultures
|
||||
var cultureInfo = new CultureInfo("en-US");
|
||||
@ -14,16 +15,23 @@ CultureInfo.DefaultThreadCurrentUICulture = cultureInfo;
|
||||
|
||||
var bld = WebApplication.CreateBuilder();
|
||||
|
||||
_ = int.TryParse(bld.Configuration["LaunchSettings:WebPort"] ?? "80", out var port);
|
||||
_ = int.TryParse(bld.Configuration["LaunchSettings:WebPort"] ?? "9000", out var port);
|
||||
bld.WebHost.ConfigureKestrel(o => o.Listen(IPAddress.Any, port));
|
||||
|
||||
bld.Services
|
||||
.AddSingleton<UserSettings>()
|
||||
.AddSingleton<Database>()
|
||||
.AddSingleton<FelicitySolarInverter>()
|
||||
.AddSingleton<JkBms>();
|
||||
.AddSingleton<JkBms>()
|
||||
.AddLogging(config =>
|
||||
{
|
||||
config.ClearProviders(); // Optional: Remove default providers
|
||||
config.AddConsole();
|
||||
config.AddDebug();
|
||||
config.AddNLog();
|
||||
});
|
||||
|
||||
if (!bld.Environment.IsDevelopment())
|
||||
//if (!bld.Environment.IsDevelopment())
|
||||
{
|
||||
bld.Services
|
||||
.AddHostedService<StatusRetriever>();
|
||||
@ -46,4 +54,5 @@ app.UseRouting()
|
||||
c.Endpoints.RoutePrefix = "api";
|
||||
c.Binding.ReflectionCache.AddFromInverterMonServer();
|
||||
});
|
||||
|
||||
app.Run();
|
||||
@ -6,7 +6,14 @@
|
||||
},
|
||||
"Logging": {
|
||||
"LogLevel": {
|
||||
"Default": "Information"
|
||||
"Default": "Debug",
|
||||
"Microsoft": "Error"
|
||||
}
|
||||
},
|
||||
"EventLog": {
|
||||
"LogLevel": {
|
||||
"Default": "Warning",
|
||||
"Microsoft": "Warning"
|
||||
}
|
||||
}
|
||||
}
|
||||
23
src/Server/nlog.config
Normal file
23
src/Server/nlog.config
Normal file
@ -0,0 +1,23 @@
|
||||
<?xml version="1.0" encoding="utf-8" ?>
|
||||
<!-- https://www.youtube.com/watch?v=o5u4fE0t79k -->
|
||||
<!-- https://github.com/NLog/NLog/wiki/Configuration-file -->
|
||||
<nlog xmlns="https://www.nlog-project.org/schemas/NLog.xsd"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
|
||||
|
||||
<!-- the targets to write to -->
|
||||
<targets>
|
||||
<!-- write logs to file -->
|
||||
<target name="logfile1" xsi:type="File" fileName="c:\temp\InverterMonServer-${shortdate}-1.log" />
|
||||
<target name="logfile2" xsi:type="File" fileName="c:\temp\InverterMonServer-${shortdate}-2.log" />
|
||||
<target name="logconsole" xsi:type="Console" />
|
||||
<target name="ds" xsi:type="OutputDebugString"/>
|
||||
</targets>
|
||||
|
||||
<!-- rules to map from logger name to target -->
|
||||
<rules>
|
||||
<logger name="FelicitySolarInverter.*" minlevel="Debug" writeTo="logconsole" />
|
||||
<target name="FelicitySolarInverter.*" minlevel="Debug" writeTo="ds"/>
|
||||
<logger name="FelicitySolarInverter.*" minlevel="Trace" writeTo="logfile1" />
|
||||
<logger name="Felicity*" minlevel="Trace" writeTo="logfile2" />
|
||||
</rules>
|
||||
</nlog>
|
||||
BIN
win-x64.zip
Normal file
BIN
win-x64.zip
Normal file
Binary file not shown.
Loading…
Reference in New Issue
Block a user