Skip to content

Commit 8905fe6

Browse files
committed
lazy load charge current values
1 parent 1e07c93 commit 8905fe6

File tree

3 files changed

+46
-55
lines changed

3 files changed

+46
-55
lines changed

src/Client/Pages/Settings.razor

Lines changed: 44 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -88,19 +88,22 @@
8888
<button class="btn btn-secondary dropdown-toggle" type="button" id="combinedcurrent" data-bs-toggle="dropdown">
8989
@Sanitize(settings.MaxCombinedChargeCurrent) A
9090
</button>
91-
<ul class="dropdown-menu dropdown-menu">
92-
@foreach (var val in chargeAmpereValues!.CombinedAmpereValues)
91+
@if (chargeAmpereValues != null)
9392
{
94-
<li>
95-
<a
96-
class="dropdown-item @(settings.MaxCombinedChargeCurrent == val ? "active" : "")"
97-
@onclick="()=>SetSetting(Setting.CombinedChargeCurrent,val)"
98-
>
99-
@Sanitize(val) A
100-
</a>
101-
</li>
93+
<ul class="dropdown-menu dropdown-menu">
94+
@foreach (var val in chargeAmpereValues!.CombinedAmpereValues)
95+
{
96+
<li>
97+
<a
98+
class="dropdown-item @(settings.MaxCombinedChargeCurrent == val ? "active" : "")"
99+
@onclick="()=>SetSetting(Setting.CombinedChargeCurrent,val)"
100+
>
101+
@Sanitize(val) A
102+
</a>
103+
</li>
104+
}
105+
</ul>
102106
}
103-
</ul>
104107
</div>
105108
}
106109
</div>
@@ -123,16 +126,19 @@
123126
@Sanitize(settings.MaxACChargeCurrent) A
124127
</button>
125128
<ul class="dropdown-menu dropdown-menu">
126-
@foreach (var val in chargeAmpereValues!.UtilityAmpereValues)
129+
@if (chargeAmpereValues != null)
127130
{
128-
<li>
129-
<a
130-
class="dropdown-item @(settings.MaxACChargeCurrent == val ? "active" : "")"
131-
@onclick="()=>SetSetting(Setting.UtilityChargeCurrent,val)"
132-
>
133-
@Sanitize(val) A
134-
</a>
135-
</li>
131+
@foreach (var val in chargeAmpereValues!.UtilityAmpereValues)
132+
{
133+
<li>
134+
<a
135+
class="dropdown-item @(settings.MaxACChargeCurrent == val ? "active" : "")"
136+
@onclick="()=>SetSetting(Setting.UtilityChargeCurrent,val)"
137+
>
138+
@Sanitize(val) A
139+
</a>
140+
</li>
141+
}
136142
}
137143
</ul>
138144
</div>
@@ -291,57 +297,42 @@
291297

292298
@code{
293299
private static ChargeAmpereValues? chargeAmpereValues;
294-
private static CurrentSettings? settings;
300+
private CurrentSettings? settings;
295301
private Button currentButton = Button.None;
296302
private bool isSuccess;
297303
private string inProgressSetting = "";
298304

299-
public static async Task RetrieveChargeAmpereValues(string basePath)
305+
protected override async Task OnInitializedAsync()
300306
{
301-
using var client = new HttpClient
302-
{
303-
BaseAddress = new Uri(basePath),
304-
Timeout = TimeSpan.FromSeconds(10)
305-
};
306-
307-
var retryDelay = (int)TimeSpan.FromSeconds(1).TotalMilliseconds;
308-
var maxDelay = (int)TimeSpan.FromMinutes(1).TotalMilliseconds;
307+
settings = await Http.GetFromJsonAsync<CurrentSettings>("api/settings/get-setting-values");
308+
StateHasChanged();
309309

310-
while (true)
310+
_ = Task.Run(async () =>
311311
{
312-
try
312+
if (chargeAmpereValues is null)
313313
{
314+
using var client = new HttpClient
315+
{
316+
BaseAddress = new Uri(Http.BaseAddress?.ToString() ?? "/"),
317+
Timeout = TimeSpan.FromSeconds(10)
318+
};
319+
314320
chargeAmpereValues = await client.GetFromJsonAsync<ChargeAmpereValues>("api/settings/get-charge-ampere-values");
315321

316322
// some inverters only seem to support one of the two commands over usb
317-
if(chargeAmpereValues?.CombinedAmpereValues.Any() is true &&
318-
chargeAmpereValues?.UtilityAmpereValues.Any() is false)
323+
if (chargeAmpereValues?.CombinedAmpereValues.Any() is true &&
324+
chargeAmpereValues?.UtilityAmpereValues.Any() is false)
319325
{
320326
chargeAmpereValues.UtilityAmpereValues = chargeAmpereValues.CombinedAmpereValues;
321327
}
322-
if(chargeAmpereValues?.CombinedAmpereValues.Any() is false &&
323-
chargeAmpereValues?.UtilityAmpereValues.Any() is true)
328+
if (chargeAmpereValues?.CombinedAmpereValues.Any() is false &&
329+
chargeAmpereValues?.UtilityAmpereValues.Any() is true)
324330
{
325331
chargeAmpereValues.CombinedAmpereValues = chargeAmpereValues.UtilityAmpereValues;
326332
}
327-
328-
break;
329-
}
330-
catch (Exception)
331-
{
332-
if (retryDelay > maxDelay)
333-
break;
334-
335-
await Task.Delay(retryDelay);
336-
retryDelay += 250;
333+
StateHasChanged();
337334
}
338-
}
339-
}
340-
341-
protected override async Task OnInitializedAsync()
342-
{
343-
settings = await Http.GetFromJsonAsync<CurrentSettings>("api/settings/get-setting-values");
344-
StateHasChanged();
335+
});
345336
}
346337

347338
private async Task SetChargePriority(string priority)

src/Client/Program.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,4 @@
1212
});
1313
_ = InverterMon.Client.Pages.Index.StartStatusStreaming(builder.HostEnvironment.BaseAddress);
1414
_ = InverterMon.Client.Pages.BMS.StartStatusStreaming(builder.HostEnvironment.BaseAddress);
15-
_ = InverterMon.Client.Pages.Settings.RetrieveChargeAmpereValues(builder.HostEnvironment.BaseAddress);
1615
await builder.Build().RunAsync();

src/changelog.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
## changelog
2-
- support for reading and writing troublesome commands with jblanch/mpp-solar in `TroubleMode`
2+
- support for reading and writing troublesome commands with jblanch/mpp-solar in `TroubleMode`
3+
- lazy load charging current ampere values on settings page load

0 commit comments

Comments
 (0)