-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Ability to detect bad measurements in transactions
The Grizzl-e charger sometimes records incorrect meter values, which have the common feature of an impossibly large jump up from the previous recorded value. This change adds fields to detect and track these incorrect measurements.
- Loading branch information
Showing
11 changed files
with
148 additions
and
52 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,3 +2,4 @@ | |
*.iml | ||
__pycache__ | ||
/test_messages.txt | ||
.DS_Store |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
import csv | ||
import datetime | ||
import logging | ||
import sys | ||
|
||
import pytz | ||
from django.core.management.base import BaseCommand | ||
from ocpp.models import Transaction | ||
|
||
logger = logging.getLogger(__name__) | ||
logging.basicConfig(level=logging.INFO) | ||
|
||
ENERGY_MAX_JUMP = 10000 | ||
DATETIME_FORMAT = "%Y-%m-%d %H:%M:%S" | ||
|
||
|
||
class Command(BaseCommand): | ||
"""Detect and correct unrealistic jumps between meter readings""" | ||
|
||
def add_arguments(self, parser): | ||
parser.add_argument("start", type=datetime.datetime.fromisoformat) | ||
parser.add_argument("end", type=datetime.datetime.fromisoformat) | ||
parser.add_argument("--tz", type=str, default="America/Vancouver") | ||
parser.add_argument("--dry-run", action="store_true") | ||
|
||
def handle(self, *args, **options): | ||
csv_writer = csv.writer(sys.stdout) | ||
tz = pytz.timezone(options["tz"]) | ||
options["start"] = tz.localize(options["start"]) | ||
options["end"] = tz.localize(options["end"]) | ||
transactions = Transaction.objects.filter( | ||
stopped_at__gte=options["start"], stopped_at__lt=options["end"] | ||
).order_by("started_at") | ||
csv_writer.writerow( | ||
[ | ||
"timestamp", | ||
"charge_point", | ||
"transaction.id", | ||
"meter_value.id", | ||
"meter_value.prev", | ||
"meter_value.cur", | ||
"meter_value.delta", | ||
"transaction.meter_stop", | ||
"transaction.meter_correction", | ||
] | ||
) | ||
for transaction in transactions: | ||
meter_correction = 0 | ||
report_rows = [] | ||
prev = None | ||
for cur in transaction.metervalue_set.filter( | ||
measurand="Energy.Active.Import.Register" | ||
).order_by("timestamp"): | ||
if ( | ||
prev | ||
and prev.value | ||
and cur.value | ||
and cur.value - prev.value > ENERGY_MAX_JUMP | ||
): | ||
delta_value = cur.value - prev.value | ||
report_rows.append( | ||
[ | ||
cur.timestamp.astimezone(tz).strftime(DATETIME_FORMAT), | ||
transaction.charge_point, | ||
transaction.id, | ||
cur.id, | ||
round(prev.value, 2), | ||
round(cur.value, 2), | ||
round(delta_value, 2), | ||
] | ||
) | ||
cur.is_incorrect = True | ||
if not options["dry_run"]: | ||
cur.save(update_fields=["is_incorrect"]) | ||
meter_correction += delta_value | ||
prev = cur | ||
if meter_correction: | ||
transaction.meter_correction = -meter_correction | ||
for row in report_rows: | ||
row += [ | ||
round(transaction.meter_stop, 2), | ||
round(transaction.meter_correction, 2), | ||
] | ||
csv_writer.writerow(row) | ||
if not options["dry_run"]: | ||
transaction.save(update_fields=["meter_correction"]) |
This file contains 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
22 changes: 22 additions & 0 deletions
22
be/ocpp/migrations/0007_metervalue_is_incorrect_transaction_meter_correction.py
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
# Generated by Django 5.0.1 on 2024-01-12 09:41 | ||
|
||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
dependencies = [ | ||
("ocpp", "0006_chargepoint_hw_imsi"), | ||
] | ||
|
||
operations = [ | ||
migrations.AddField( | ||
model_name="metervalue", | ||
name="is_incorrect", | ||
field=models.BooleanField(default=False), | ||
), | ||
migrations.AddField( | ||
model_name="transaction", | ||
name="meter_correction", | ||
field=models.IntegerField(default=0), | ||
), | ||
] |
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
#!/bin/sh | ||
|
||
pg_restore --clean -h 127.0.0.1 -v -U levity -d levity -j 2 $1 |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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
This file contains 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