Skip to content

Commit

Permalink
bms data display toggles
Browse files Browse the repository at this point in the history
  • Loading branch information
dj-nitehawk committed Jul 8, 2024
1 parent a966479 commit 7320437
Show file tree
Hide file tree
Showing 10 changed files with 89 additions and 27 deletions.
50 changes: 36 additions & 14 deletions src/Client/Pages/BMS.razor
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<PageTitle>JK BMS Status</PageTitle>

<Loader Enabled=@(status is null)/>
<Loader Enabled=@(status is null) />

@if (status is not null)
{
Expand All @@ -24,8 +24,8 @@
<div class="fs-1 my-1 mx-5 border-top">
@status.CapacityPct%
</div>
<div class="fs-5 m-0 border-top">
@Math.Round(status.AvailableCapacity, 1) Ah / @status.PackCapacity Ah
<div class="fs-5 m-0 border-top" style="cursor: pointer;" @onclick="_ => ShowCapacityKwh = !ShowCapacityKwh">
@(GetPackCapacity())
</div>
</div>
<div class="col bg-light">
Expand All @@ -41,14 +41,14 @@
<div class="fw-bold fs-6 text-secondary">
@($"{status.CRate:0.00} C") / @($"{status.AvgPowerWatts:0} W")
</div>
<div class="fw-normal fs-6 m-0 p-0">
@status.TimeHrs Hrs @status.TimeMins Mins
<div class="fw-normal fs-6 m-0 p-0" style="cursor: pointer;" @onclick="_ => ShowEndDateAndTime = !ShowEndDateAndTime">
@(GetTimeLeft())
</div>
}
@if (status.AvgCurrentAmps == 0)
{
<div class="fs-5 m-0 p-0 text-muted">
Holding<br/>Voltage
Holding<br />Voltage
</div>
}
@if (status.IsWarning)
Expand Down Expand Up @@ -127,6 +127,8 @@
private static event Action<BMSStatus?>? onStatusUpdated;
private static event Action? onStatusRetrievalError;
private static BMSStatus? status;
private static bool ShowEndDateAndTime;
private static bool ShowCapacityKwh;

protected override void OnInitialized()
{
Expand All @@ -146,6 +148,26 @@
StateHasChanged();
}

private string GetTimeLeft()
{
if (ShowEndDateAndTime)
{
return status!.GetTimeString();
}
return $"{status!.TimeHrs} Hrs {status.TimeMins} Mins";
}

private string GetPackCapacity()
{
if (ShowCapacityKwh)
{
var avlCap = Math.Round((status!.AvailableCapacity * status.PackNominalVoltage) / 1000, 1);
var packCap = Math.Round((status!.PackCapacity * status.PackNominalVoltage) / 1000, 1);
return $"{avlCap} kWh / {packCap} kWh";
}
return $"{Math.Round(status!.AvailableCapacity, 1)} Ah / {status!.PackCapacity} Ah";
}

public void Dispose()
{
onStatusUpdated -= UpdateState;
Expand All @@ -160,10 +182,10 @@
// which leads to a memory leak/ connection exhaustion.
using var client = new HttpClient
{
BaseAddress = new(basePath),
Timeout = TimeSpan.FromSeconds(5)
};
{
BaseAddress = new(basePath),
Timeout = TimeSpan.FromSeconds(5)
};

var retryDelay = 1000;

Expand All @@ -183,10 +205,10 @@
JsonSerializer.DeserializeAsyncEnumerable<BMSStatus>(
stream,
new JsonSerializerOptions
{
PropertyNameCaseInsensitive = true,
DefaultBufferSize = 64
}))
{
PropertyNameCaseInsensitive = true,
DefaultBufferSize = 64
}))
{
onStatusUpdated?.Invoke(s);
retryDelay = 1000;
Expand Down
13 changes: 13 additions & 0 deletions src/Client/Pages/Settings.razor
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,19 @@
</div>
</div>
</div>
<div class="row border-primary bg-light px-3 mt-1">
<div class="col-6 my-auto fw-bold">
Battery Voltage:
</div>
<div class="col-6 p-2 bg-secondary">
<div class="row">
<div style="width:4.75rem;">
<input type="number" class="form-control bg-light" @bind-value=settings.SystemSpec.BatteryNominalVoltage>
</div>
<div class="col-1 fw-bolder fs-5 m-1 text-white">V</div>
</div>
</div>
</div>
<div class="row border-primary bg-light px-3 mt-1">
<div class="col-6 my-auto fw-bold">
Daylight Start (24hr format):
Expand Down
13 changes: 8 additions & 5 deletions src/Server/BatteryService/JK-BMS-RS485-Service.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using InverterMon.Shared.Models;
using InverterMon.Server.Persistance.Settings;
using InverterMon.Shared.Models;
using SerialPortLib;

namespace InverterMon.Server.BatteryService;
Expand All @@ -12,7 +13,7 @@ public class JkBms
readonly AmpValQueue _recentAmpReadings = new(5); //avg value over 5 readings (~5secs)
readonly SerialPortInput _bms = new();

public JkBms(IConfiguration config, ILogger<JkBms> logger, IWebHostEnvironment env, IHostApplicationLifetime appLife)
public JkBms(UserSettings userSettings, IConfiguration config, ILogger<JkBms> logger, IWebHostEnvironment env, IHostApplicationLifetime appLife)
{
if (env.IsDevelopment())
{
Expand All @@ -21,6 +22,7 @@ public JkBms(IConfiguration config, ILogger<JkBms> logger, IWebHostEnvironment e
return;
}

Status.PackNominalVoltage = userSettings.BatteryNominalVoltage;
var bmsAddress = config["LaunchSettings:JkBmsAddress"] ?? "/dev/ttyUSB0";
_bms.SetPort(bmsAddress);
_bms.ConnectionStatusChanged += ConnectionStatusChanged;
Expand Down Expand Up @@ -126,11 +128,12 @@ void FillDummyData()
Status.PackVoltage = 25.6f;
Status.IsCharging = true;
Status.AvgCurrentAmps = 21.444f;
Status.CapacityPct = 50;
Status.CapacityPct = 48;
Status.PackCapacity = 120;
Status.PackNominalVoltage = 50;
Status.IsWarning = false;
Status.TimeHrs = 3;
Status.TimeMins = 11;
Status.TimeHrs = 24;
Status.TimeMins = 10;
for (byte i = 1; i <= 8; i++)
Status.Cells.Add(i, 1.110f);
}
Expand Down
6 changes: 5 additions & 1 deletion src/Server/Endpoints/Settings/SetSystemSpec/Validator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@ public Validator()
RuleFor(x => x.SunlightEndHour)
.GreaterThanOrEqualTo(0)
.LessThanOrEqualTo(24)
.Must((s, h) => h > s.SunlightStartHour).WithMessage("Sunlight end hour must be later than start hour!"); ;
.Must((s, h) => h > s.SunlightStartHour).WithMessage("Sunlight end hour must be later than start hour!");

RuleFor(x => x.BatteryNominalVoltage)
.GreaterThan(5)
.WithMessage("Battery nominal voltage required!");

//todo: display validation errors on ui
}
Expand Down
1 change: 1 addition & 0 deletions src/Server/Persistance/Database.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ public void RestoreUserSettings()
{
_settings.PV_MaxCapacity = settings.PV_MaxCapacity;
_settings.BatteryCapacity = settings.BatteryCapacity;
_settings.BatteryNominalVoltage = settings.BatteryNominalVoltage;
_settings.SunlightStartHour = settings.SunlightStartHour;
_settings.SunlightEndHour = settings.SunlightEndHour;
}
Expand Down
3 changes: 3 additions & 0 deletions src/Server/Persistance/Settings/UserSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ public class UserSettings
public int Id { get; set; } = 1;
public int PV_MaxCapacity { get; set; } = 1000;
public int BatteryCapacity { get; set; } = 100;
public float BatteryNominalVoltage { get; set; } = 25.6f;
public int SunlightStartHour { get; set; } = 6;
public int SunlightEndHour { get; set; } = 18;
public int[] PVGraphRange => new[] { 0, (SunlightEndHour - SunlightStartHour) * 60 };
Expand All @@ -18,6 +19,7 @@ public SystemSpec ToSystemSpec()
{
PV_MaxCapacity = PV_MaxCapacity,
BatteryCapacity = BatteryCapacity,
BatteryNominalVoltage = BatteryNominalVoltage,
SunlightStartHour = SunlightStartHour,
SunlightEndHour = SunlightEndHour
};
Expand All @@ -27,6 +29,7 @@ public void FromSystemSpec(SystemSpec spec)
Id = 1;
PV_MaxCapacity = spec.PV_MaxCapacity;
BatteryCapacity = spec.BatteryCapacity;
BatteryNominalVoltage = spec.BatteryNominalVoltage;
SunlightStartHour = spec.SunlightStartHour;
SunlightEndHour = spec.SunlightEndHour;
}
Expand Down
4 changes: 2 additions & 2 deletions src/Server/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
bld.Services
.AddSingleton<UserSettings>()
.AddSingleton<CommandQueue>()
.AddSingleton<JkBms>()
.AddSingleton<Database>();
.AddSingleton<Database>()
.AddSingleton<JkBms>();

if (!bld.Environment.IsDevelopment())
{
Expand Down
19 changes: 18 additions & 1 deletion src/Shared/Models/BMSStatus.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Text.Json.Serialization;
using System.Globalization;
using System.Text.Json.Serialization;

namespace InverterMon.Shared.Models;

Expand Down Expand Up @@ -63,4 +64,20 @@ public class BMSStatus

[JsonPropertyName("t")]
public double AvgPowerWatts => Math.Round(AvgCurrentAmps * PackVoltage, 0, MidpointRounding.AwayFromZero);

[JsonPropertyName("u")]
public float PackNominalVoltage { get; set; }

public string GetTimeString()
{
var currentTime = DateTime.UtcNow
.AddHours(5).AddMinutes(30); //only supports IST time zone :-(

var futureTime = currentTime.AddHours(TimeHrs).AddMinutes(TimeMins);

if (futureTime.Date == currentTime.Date)
return futureTime.ToString("h:mm tt");
else
return futureTime.ToString("dddd h:mm tt");
}
}
1 change: 1 addition & 0 deletions src/Shared/Models/SystemSpec.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ public class SystemSpec
{
public int PV_MaxCapacity { get; set; } = 1000;
public int BatteryCapacity { get; set; } = 100;
public float BatteryNominalVoltage { get; set; } = 25.6f;
public int SunlightStartHour { get; set; } = 6;
public int SunlightEndHour { get; set; } = 18;
}
6 changes: 2 additions & 4 deletions src/changelog.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
## changelog

- show decimal point of jk bms temperature sensor values
- show battery power in watts in jk bms section
- improve bms cell voltage display
- improve pv watts display
- toggle for displaying bms battery capacities in kWh
- toggle for displaying bms remaining time as date and time string

0 comments on commit 7320437

Please sign in to comment.