11using HidSharp ;
2+ using System . Diagnostics ;
23using System . Text ;
34using 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 )
0 commit comments