Skip to content

Commit

Permalink
Merge pull request #95 from e-flux-platform/add-soc
Browse files Browse the repository at this point in the history
  • Loading branch information
Bholtland authored Sep 4, 2024
2 parents 1eade9c + 9211fac commit 70c9a2e
Show file tree
Hide file tree
Showing 9 changed files with 131 additions and 52 deletions.
15 changes: 14 additions & 1 deletion src/lib/ChargeStation/eventHandlers/ocpp-16/send-meter-values.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { ChargeStationEventHandler } from 'lib/ChargeStation/eventHandlers';
import { MeterValuesRequest } from 'schemas/ocpp/1.6/MeterValues';
import clock from 'lib/ChargeStation/clock';

const sendMeterValues: ChargeStationEventHandler = async ({
chargepoint,
session,
}) => {
const now = clock.now()

await chargepoint.writeCall<MeterValuesRequest>('MeterValues', {
chargepoint.writeCall<MeterValuesRequest>('MeterValues', {
connectorId: session.connectorId,
transactionId: Number(session.transactionId),
meterValue: [
Expand All @@ -23,6 +24,18 @@ const sendMeterValues: ChargeStationEventHandler = async ({
},
],
},
{
timestamp: now.toISOString(),
sampledValue: [
{
value: session.stateOfCharge.toString(),
context: 'Sample.Periodic',
measurand: 'SoC',
location: 'Outlet',
unit: 'Percent'
}
]
}
],
});
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
import { sleep } from '../../../../utils/csv';
import {ChargeStationEventHandler} from "lib/ChargeStation/eventHandlers";
import {StartTransactionRequest} from "schemas/ocpp/1.6/StartTransaction";

export default async function sendStartTransaction({ chargepoint, session }) {
const sendStartTransaction: ChargeStationEventHandler = async ({
chargepoint,
session,
}) => {
chargepoint.sessions[session.connectorId].isStartingSession = true;
await sleep(1000);
await chargepoint.writeCall(
chargepoint.writeCall<StartTransactionRequest>(
'StartTransaction',
{
connectorId: session.connectorId,
Expand All @@ -15,3 +20,5 @@ export default async function sendStartTransaction({ chargepoint, session }) {
session
);
}

export default sendStartTransaction;

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import {sleep} from '../../../../utils/csv';
import {ChargeStationEventHandler} from "lib/ChargeStation/eventHandlers";
import {StopTransactionRequest} from "schemas/ocpp/1.6/StopTransaction";

const sendStopTransaction: ChargeStationEventHandler = async ({
chargepoint,
session,
}) => {
chargepoint.sessions[session.connectorId].isStoppingSession = true;

chargepoint.sessions[session.connectorId].tickInterval?.stop();
await sleep(1000);

chargepoint.writeCall<StopTransactionRequest>(
'StopTransaction',
{
idTag: session.options.uid,
meterStop: Math.round(session.kwhElapsed * 1000),
timestamp: session.now().toISOString(),
reason: 'EVDisconnected',
transactionId: Number(session.transactionId),
transactionData: [
{
timestamp: session.now().toISOString(),
sampledValue: [
{
value: session.kwhElapsed.toString(),
context: 'Sample.Periodic',
format: 'Raw',
measurand: 'Energy.Active.Import.Register',
location: 'Outlet',
unit: 'kWh',
},
],
},
{
timestamp: session.now().toISOString(),
sampledValue: [
{
value: session.stateOfCharge.toString(),
context: 'Transaction.End',
location: 'Outlet',
unit: 'Percent',
measurand: 'SoC',
}
]
}
],
},
session
);
}

export default sendStopTransaction;
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,27 @@ const sendStartTransaction: ChargeStationEventHandler = async ({
},
meterValue: [
{
sampledValue: [
timestamp: startTime,
sampledValue: [
{
value: Math.round(session.kwhElapsed * 1000),
context: 'Transaction.Begin',
unitOfMeasure: { unit: 'kWh' },
},

],
timestamp: startTime,
},
{
timestamp: startTime,
sampledValue: [
{
value: session.stateOfCharge,
context: 'Transaction.Begin',
unitOfMeasure: { unit: 'Percent' },
measurand: 'SoC',
}
]
}
],
evse: { id: evseId, connectorId },
idToken: { idToken: session.options.uid, type: 'ISO14443' },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const sendStopTransaction: ChargeStationEventHandler = async ({

const now = clock.now().toISOString();

await chargepoint.writeCall(
chargepoint.writeCall(
'TransactionEvent',
{
eventType: 'Ended',
Expand All @@ -28,25 +28,36 @@ const sendStopTransaction: ChargeStationEventHandler = async ({
},
meterValue: [
{
sampledValue: [
timestamp: session.startTime.toISOString(),
sampledValue: [
{
value: 0.0,
context: 'Transaction.Begin',
unitOfMeasure: { unit: 'kWh' },
},
],
timestamp: session.startTime.toISOString(),
},
{
sampledValue: [
timestamp: now,
sampledValue: [
{
value: session.kwhElapsed,
context: 'Transaction.End',
unitOfMeasure: { unit: 'kWh' },
},
],
timestamp: now,
},
{
timestamp: now,
sampledValue: [
{
value: session.stateOfCharge,
context: 'Transaction.End',
unitOfMeasure: { unit: 'Percent' },
measurand: 'SoC',
}
],
}
],
evse: { id: 1, connectorId: session.connectorId },
idToken: { idToken: session.options.uid, type: 'ISO14443' },
Expand All @@ -56,7 +67,7 @@ const sendStopTransaction: ChargeStationEventHandler = async ({

await sleep(1000);

await chargepoint.writeCall('StatusNotification', {
chargepoint.writeCall('StatusNotification', {
timestamp: clock.now().toISOString(),
connectorStatus: 'Available',
evseId: 1,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import { ChargeStationEventHandler } from 'lib/ChargeStation/eventHandlers';

import { TransactionEventRequest } from 'schemas/ocpp/2.0/TransactionEventRequest';

import clock from '../../clock';

const sendTransationEventUpdated: ChargeStationEventHandler = ({
Expand All @@ -21,7 +19,8 @@ const sendTransationEventUpdated: ChargeStationEventHandler = ({
},
meterValue: [
{
sampledValue: [
timestamp: now.toISOString(),
sampledValue: [
{
value: Number(session.kwhElapsed.toFixed(3)),
context: 'Sample.Periodic',
Expand All @@ -30,8 +29,19 @@ const sendTransationEventUpdated: ChargeStationEventHandler = ({
unitOfMeasure: { unit: 'kWh' },
},
],
timestamp: now.toISOString(),
},
{
timestamp: now.toISOString(),
sampledValue: [
{
value: session.stateOfCharge,
context: 'Sample.Periodic',
measurand: 'SoC',
location: 'Outlet',
unitOfMeasure: { unit: 'Percent' }
}
]
}
],
evse: { id: 1, connectorId: session.connectorId },
});
Expand Down
9 changes: 8 additions & 1 deletion src/lib/ChargeStation/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,10 @@ export class Session {
return this.chargeStation.currentStatus[this.connectorId];
}

get stateOfCharge(): number {
return this.carBatteryStateOfCharge ;
}

now(): Date {
return clock.now();
}
Expand All @@ -498,10 +502,13 @@ export class Session {
}
const amountKwhToCharge = (this.maxPowerKw / 3600) * secondsElapsed;

this.carBatteryStateOfCharge += amountKwhToCharge / this.carBatteryKwh * 100;

const carNeededKwh =
this.carBatteryKwh -
this.carBatteryKwh * (this.carBatteryStateOfCharge / 100);
const chargeLimitReached = this.kwhElapsed >= carNeededKwh;

const chargeLimitReached = carNeededKwh <= 0;

console.info(
`Charge session tick (connectorId=${this.connectorId}, carNeededKwh=${carNeededKwh}, chargeLimitReached=${chargeLimitReached}, amountKwhToCharge=${amountKwhToCharge}, currentStatus=${this.connectorStatus}`
Expand Down
2 changes: 1 addition & 1 deletion src/lib/ChargeStation/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,4 @@ export function summarizeCommandParams({

export function toCamelCase(value: string) {
return value.charAt(0).toLowerCase() + value.slice(1);
}
}

0 comments on commit 70c9a2e

Please sign in to comment.