Skip to content

Commit 0786ac7

Browse files
committed
change retry strategy
1 parent d77cff4 commit 0786ac7

File tree

6 files changed

+34
-22
lines changed

6 files changed

+34
-22
lines changed

src/Client/InverterMon.Client.csproj

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@
1414
</PropertyGroup>
1515

1616
<ItemGroup>
17-
<PackageReference Include="AntDesign.Charts" Version="0.3.0" />
18-
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="7.0.2" />
19-
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.DevServer" Version="7.0.2" PrivateAssets="all" />
17+
<PackageReference Include="AntDesign.Charts" Version="0.3.1" />
18+
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="7.0.5" />
19+
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.DevServer" Version="7.0.5" PrivateAssets="all" />
2020
</ItemGroup>
2121

2222
<ItemGroup>

src/InverterMonWindow/InverterMonWindow.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
</PropertyGroup>
1010

1111
<ItemGroup>
12-
<PackageReference Include="Microsoft.Web.WebView2" Version="1.0.1518.46" />
12+
<PackageReference Include="Microsoft.Web.WebView2" Version="1.0.1722.32" />
1313
</ItemGroup>
1414

1515
</Project>

src/Server/InverterMon.Server.csproj

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,10 @@
2828
<ItemGroup>
2929
<PackageReference Include="HidSharp" Version="2.1.0" />
3030
<PackageReference Include="SerialPortLib" Version="1.1.1" />
31-
<PackageReference Include="FastEndpoints" Version="5.7.0" />
32-
<PackageReference Include="FastEndpoints.Swagger" Version="5.7.0" />
33-
<PackageReference Include="LiteDB" Version="5.0.15" />
34-
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.Server" Version="7.0.2" />
31+
<PackageReference Include="FastEndpoints" Version="5.8.1" />
32+
<PackageReference Include="FastEndpoints.Swagger" Version="5.8.1" />
33+
<PackageReference Include="LiteDB" Version="5.0.16" />
34+
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.Server" Version="7.0.5" />
3535
</ItemGroup>
3636

3737
<ItemGroup>

src/Server/InverterService/CommandExecutor.cs

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using HidSharp;
2+
using System.Diagnostics;
23
using System.Text;
34
using ICommand = InverterMon.Server.InverterService.Commands.ICommand;
45

@@ -19,8 +20,16 @@ public CommandExecutor(CommandQueue queue, IConfiguration config, ILogger<Comman
1920

2021
log.LogInformation("connecting to the inverter...");
2122

22-
while (!Connect())
23+
var sw = new Stopwatch();
24+
sw.Start();
25+
26+
while (!Connect() && sw.Elapsed.TotalMinutes <= 5)
2327
Thread.Sleep(10000);
28+
29+
if (sw.Elapsed.TotalMinutes >= 5)
30+
{
31+
log.LogInformation("inverter connecting timed out!");
32+
}
2433
}
2534

2635
private bool Connect()
@@ -39,14 +48,17 @@ private bool Connect()
3948
}
4049
else
4150
{
42-
log.LogInformation("inverter connected!");
51+
log.LogInformation("connected to inverter at: [{adr}]", dev.Device.DevicePath);
4352
return true;
4453
}
4554
}
4655

4756
protected override async Task ExecuteAsync(CancellationToken c)
4857
{
49-
while (!c.IsCancellationRequested)
58+
var delay = 0;
59+
var timeout = TimeSpan.FromMinutes(5);
60+
61+
while (!c.IsCancellationRequested && delay <= timeout.TotalMilliseconds)
5062
{
5163
var cmd = queue.GetCommand();
5264
if (cmd is not null)
@@ -55,23 +67,23 @@ protected override async Task ExecuteAsync(CancellationToken c)
5567
{
5668
await ExecuteCommand(cmd, dev!, c);
5769
queue.IsAcceptingCommands = true;
70+
delay = 0;
5871
queue.RemoveCommand();
5972
}
6073
catch (Exception x)
6174
{
6275
queue.IsAcceptingCommands = false;
63-
log.LogError("command error: {msg}", x.Message);
64-
dev!.Close();
65-
dev.Dispose();
66-
log.LogInformation("exiting...");
67-
Environment.Exit(0);
76+
log.LogError("command execution failed: [{msg}]", x.Message);
77+
await Task.Delay(delay += 1000);
6878
}
6979
}
7080
else
7181
{
7282
await Task.Delay(500, c);
7383
}
7484
}
85+
86+
log.LogError("command execution halted due to excessive failures!");
7587
}
7688

7789
private static async Task ExecuteCommand(ICommand command, Stream port, CancellationToken c)

src/Server/appsettings.json

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
{
22
"LaunchSettings": {
3-
"DeviceAddress": "/dev/hidraw0",
4-
"JkBmsAddress": "/dev/ttyUSB0",
3+
"DeviceAddress": "/dev/ttyUSB0",
4+
"JkBmsAddress": "/dev/ttyUSB1",
55
"WebPort": 80,
66
"TroubleMode": "no"
77
},
88
"Logging": {
99
"LogLevel": {
1010
"Default": "Information",
11-
"Microsoft.AspNetCore": "Warning"
11+
"Microsoft.AspNetCore": "Warning",
12+
"Microsoft.Hosting.Lifetime": "Error"
1213
}
1314
}
14-
}
15+
}

src/changelog.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
11
## changelog
2-
- add potential support for serial devices (ex: `/dev/ttyUSB*`)
3-
- add read-only support for troublesome inverters (set `"TroubleMode": "yes"` in `appsettings.json`)
2+
- improve command execution retry strategy

0 commit comments

Comments
 (0)