Repository navigation
feat: work out the distance since the last charge for cars that do not report it - #778
Open
arthur-lzrs wants to merge 4 commits into
Open
arthur-lzrs wants to merge 4 commits into
arthur-lzrs wants to merge 4 commits into
Conversation
…t report it Some cars send a distance-since-last-charge counter and some never do, so the Since Charge card on the dashboard simply never appears for the latter, although the readings GarageStack has already stored hold everything needed to count it. For a car whose merged snapshot has no mileageSinceLastCharge, GetMergedLatestAsync now fills it in: the odometer now, less the odometer where the car's last charge ended. The end of the last charge is the later of the end time the car reports (charging/lastEnd) and the last reading that showed it charging; a charge still going counts as ending now, so it reads 0. A charging reading is dated by its poll's own stamp when that is older than its row, so a gateway that starts again and sends an old charge state in a new row does not move the end of the last charge. A time from the future is not believed, an odometer that went backwards gives no distance, and a car with no charge in its readings (its first charge since GarageStack began recording has not happened) gets none, so the card stays out of sight until there is one to count from. A car that reports the counter keeps its own value untouched. The figure comes through /status, the widget endpoint and the live updates alike, since they all read the merged snapshot, so the existing card shows up on its own. A worked-out figure is not passed off as a measured one: the snapshot carries mileageSinceLastChargeCalculated (not stored, merged or exported: it is [NotMapped] and kept out of the field merge and the data export), and the dashboard shows the distance, and the efficiency since the last charge that rests on it, with an approximate sign in front. The card's info text says how the figure is worked out, in every locale. The odometer at the last charge is remembered for a minute (an absent charge too), as it is found by reading back through the readings, up to 400 days. Checked against a car that does report the counter: from the odometer alone, 94 of 102 readings came out exactly the same and the rest one kilometre under, from rounding. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
…ght it The gateway reads a parked car rarely (every two minutes for a few minutes after it stops, then once a day), so a charge can start and end between two reads with no reading saying "charging", and the distance since the last charge then counted on from an older one. A stretch of readings at one odometer value is a car that did not move. When its state of charge climbs by two points or more above its lowest in that stretch, the car was charged, at that odometer. A new odometer value starts a new stretch, so driving, which regenerates and wobbles the figure, never counts. Two points is well clear of what a parked car does in the stored readings (tenths of a point at most, from both cars here) and still finds a top-up worth the name. The latest of this, a reading that showed the car charging and the end time the car reports is the last charge. The scan reads only the readings after the last charge otherwise known, or the last 60 days when none is. Limits: a charge with the car driven before the next read (the odometer changed in between) is not seen this way, and a battery that corrects itself upward at rest by two points would count as a charge, which shows as a shorter distance and is shown with the approximate sign anyway. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
…ument the rules A parked car read at 98.3 % and then at 100 % was charged, but that is 1.7 points, under the two the climb must make to be taken for a charge no poll caught. When the climb ends at a full battery (99.5 % or more) a point is enough, so a top-up to full is not missed by a few tenths. A car already full that stays full still counts as nothing. ARCHITECTURE.md now says how the distance since the last charge is worked out for a car that does not report it: the signs the end of the last charge is taken from, the two points and the one point to full, why the figure is not stored, and what it cannot see. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
…und its lookups A review of the calculated distance since the last charge found what it should not touch and what it costs to find. A plain hybrid never charges from a plug, and an engine topping its battery up at a standstill would have read as one: the repository now gives it nothing (ChargeSessionHistory.Applies, with the car's own type setting first, as the charge history does). Where the distance was worked out, nothing that divides by it or sets it against the car's own counters uses it: the dashboard's efficiency since the last charge (the car's energy counter over a distance that starts elsewhere), the widget's electricSharePercent and the Statistics page's electric share are left to a car that reported the distance itself. The widget carries mileageSinceLastChargeCalculated, as the snapshot does. The lookups are bounded. The last charge is searched for in windows of 7, 30, 120 and 400 days and the search stops at the first that holds one, so a car that charges often reads a week; a reported end time only counts when the time itself is inside the window, so a zero or an epoch says nothing (it used to be able to send the state-of-charge scan back to 1970); that scan never reads further back than 90 days; and "no charge found", the answer that costs most to find, is remembered for two minutes instead of one. The demo's plug-in hybrid no longer sends the distance, so its dashboard shows the figure worked out, with the approximate sign. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Some cars send a distance-since-last-charge counter (
mileageSinceLastCharge, which starts again from zero when a charge ends) and some never do. For the second kind the Since Charge card on the dashboard never appears, although the readings GarageStack already stores hold everything needed to count it. (On my MG4 Urban the counter is never sent; the other car on the account sends it.)For a car whose current state has no
mileageSinceLastCharge, the API now works it out: the odometer now, less the odometer where the car's last charge ended. The dashboard shows it with an approximate sign ("≈ 42 km"), and the card's info (i) says how it is worked out. A car that reports the counter itself keeps its own value, untouched.The end of the last charge is the latest of three signs:
charging/lastEnd);lastChargeStatestamp when that is older than the row, as the charge history does, so a gateway that starts again does not move it). A charge still going counts as ending now, so it reads 0;No charge to count from (a car's first charge since GarageStack began recording has not happened yet) means no figure and the card stays hidden.
How it works
DistanceSinceCharge(Core) holds the rules;TelemetryRepository.GetMergedLatestAsync, the one place the dashboard,/status, the widget and the live updates read the current state from, fills the figure in. No migration, no new column.mileageSinceLastChargeCalculated. It is[NotMapped]: not stored, and kept out of the field merge and the data export. The widget DTO carries it too.electricSharePercentand the Statistics page's electric share.documentation/ARCHITECTURE.mdhas a section with the rules and why the thresholds are what they are.Checked against real data
On a car that does report the counter, replaying the odometer-only calculation against the stored readings: 94 of 102 readings came out exactly the same, and the other 8 one kilometre under (the car counts with decimals the odometer does not show). A parked car's state of charge moved by at most 0.3 points in the stored readings of both cars here, which is why 2 points is the threshold.
Type of Change
Related Issues
None.
How it was tested
DistanceSinceChargeTests): the odometer difference, the end of the last charge from each sign and from the later of them, a time from the future, an epoch, a replayed old charge state, the state-of-charge climb in every shape I could think of (slow charge, a drop then a rise, driving, rows that carry only one of the two values, a climb to full, a car that stays full), plain hybrids, windows, caps and the cache.TelemetryRepositoryPostgresTests).≈ 0 kmwhile it charges.Notes for review
MinimumSocRisePercent, with the reasoning in its comment and in ARCHITECTURE.md. A battery that corrects itself upward at rest by that much would show as a charge, which makes the distance shorter, and it is shown as approximate either way.nl,esandpt-PTis my own draft, so a native check is welcome.dotnet format: on my machinedotnet format GarageStack.slnx --verify-no-changesreports whitespace errors on a cleanmaintoo (115, in files this PR does not touch); compared withmain, this branch adds none.Checklist
pnpm checkinfrontend/, anddotnet test --project src/GarageStack.Tests(see the note ondotnet format)feat: ...,fix(frontend): ...)ITripRepository): done in the demo too, with a step inTripRepositoryContractTests(not applicable)documentation/openapi.jsonis committedsrc/GarageStack.Data/Demo/frontend/src/locales/(one existing text changed, in all five)unraid/garagestack.xmlchanged: synced tojoszz/unraid-community-appsafter merge (not applicable, it did not change)🤖 Generated with Claude Code