Skip to content

Commit 3be0002

Browse files
committed
insert transaction of unknown of an idTag and block it (closes #13)
reason: Starting with OCPP 1.6, a charging station can be configured to allow unknown idTags when offline. Use case: An offline charging station decides to allow an unknown idTag to start a transaction. Later, when it is online, it sends a StartTransactionRequest with this idTag. If we do not insert this idTag, the transaction details will not be inserted into DB and we will lose valuable information.
1 parent 85f2000 commit 3be0002

2 files changed

Lines changed: 32 additions & 4 deletions

File tree

src/main/java/de/rwth/idsg/steve/repository/impl/OcppServerRepositoryImpl.java

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import static jooq.steve.db.tables.Connector.CONNECTOR;
2727
import static jooq.steve.db.tables.ConnectorMeterValue.CONNECTOR_METER_VALUE;
2828
import static jooq.steve.db.tables.ConnectorStatus.CONNECTOR_STATUS;
29+
import static jooq.steve.db.tables.OcppTag.OCPP_TAG;
2930
import static jooq.steve.db.tables.Transaction.TRANSACTION;
3031

3132
/**
@@ -170,6 +171,9 @@ public Integer insertTransaction(InsertTransactionParams p) {
170171

171172
insertIgnoreConnector(ctx, p.getChargeBoxId(), p.getConnectorId());
172173

174+
// it is important to insert idTag before transaction, since the transaction table references it
175+
boolean unknownTagInserted = insertIgnoreIdTag(p);
176+
173177
SelectConditionStep<Record1<Integer>> connectorPkQuery =
174178
DSL.select(CONNECTOR.CONNECTOR_PK)
175179
.from(CONNECTOR)
@@ -189,6 +193,10 @@ public Integer insertTransaction(InsertTransactionParams p) {
189193
.fetchOne()
190194
.getTransactionPk();
191195

196+
if (unknownTagInserted) {
197+
log.warn("The transaction '{}' contains an unknown idTag '{}' which was inserted into DB as to prevent information loss and has been blocked", transactionId, p.getIdTag());
198+
}
199+
192200
// -------------------------------------------------------------------------
193201
// Step 2 for OCPP 1.5: A startTransaction may be related to a reservation
194202
// -------------------------------------------------------------------------
@@ -201,7 +209,7 @@ public Integer insertTransaction(InsertTransactionParams p) {
201209
// Step 3: Set connector status to "Occupied"
202210
// -------------------------------------------------------------------------
203211

204-
insertConnectorStatus(connectorPkQuery, p.getStartTimestamp(), p.getStatusUpdate());
212+
insertConnectorStatus(ctx, connectorPkQuery, p.getStartTimestamp(), p.getStatusUpdate());
205213

206214
return transactionId;
207215
});
@@ -234,7 +242,7 @@ public void updateTransaction(UpdateTransactionParams p) {
234242
.from(TRANSACTION)
235243
.where(TRANSACTION.TRANSACTION_PK.equal(p.getTransactionId()));
236244

237-
insertConnectorStatus(connectorPkQuery, p.getStopTimestamp(), p.getStatusUpdate());
245+
insertConnectorStatus(ctx, connectorPkQuery, p.getStopTimestamp(), p.getStatusUpdate());
238246
}
239247

240248
// -------------------------------------------------------------------------
@@ -250,7 +258,8 @@ public void updateTransaction(UpdateTransactionParams p) {
250258
* notification will be used as current. Or, if this transaction data was sent to us for a failed push from the past
251259
* and we have a "more recent" status, it will still be the current status.
252260
*/
253-
private void insertConnectorStatus(SelectConditionStep<Record1<Integer>> connectorPkQuery,
261+
private void insertConnectorStatus(DSLContext ctx,
262+
SelectConditionStep<Record1<Integer>> connectorPkQuery,
254263
DateTime timestamp,
255264
TransactionStatusUpdate statusUpdate) {
256265
ctx.insertInto(CONNECTOR_STATUS)
@@ -276,6 +285,25 @@ private void insertIgnoreConnector(DSLContext ctx, String chargeBoxIdentity, int
276285
}
277286
}
278287

288+
/**
289+
* Use case: An offline charging station decides to allow an unknown idTag to start a transaction. Later, when it
290+
* is online, it sends a StartTransactionRequest with this idTag. If we do not insert this idTag, the transaction
291+
* details will not be inserted into DB and we will lose valuable information.
292+
*/
293+
private boolean insertIgnoreIdTag(InsertTransactionParams p) {
294+
String note = "This unknown idTag was used in a transaction that started @ " + p.getStartTimestamp()
295+
+ ". It was reported @ " + DateTime.now() + ".";
296+
297+
int count = ctx.insertInto(OCPP_TAG)
298+
.set(OCPP_TAG.ID_TAG, p.getIdTag())
299+
.set(OCPP_TAG.NOTE, note)
300+
.set(OCPP_TAG.BLOCKED, true)
301+
.onDuplicateKeyIgnore() // Important detail
302+
.execute();
303+
304+
return count == 1;
305+
}
306+
279307
private int getConnectorPkFromConnector(DSLContext ctx, String chargeBoxIdentity, int connectorId) {
280308
return ctx.select(CONNECTOR.CONNECTOR_PK)
281309
.from(CONNECTOR)

src/main/java/de/rwth/idsg/steve/service/CentralSystemService16_Service.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,8 +152,8 @@ public StartTransactionResponse startTransaction(StartTransactionRequest paramet
152152
.reservationId(parameters.getReservationId())
153153
.build();
154154

155-
IdTagInfo info = ocppTagService.getIdTagInfo(parameters.getIdTag());
156155
Integer transactionId = ocppServerRepository.insertTransaction(params);
156+
IdTagInfo info = ocppTagService.getIdTagInfo(parameters.getIdTag());
157157

158158
return new StartTransactionResponse()
159159
.withIdTagInfo(info)

0 commit comments

Comments
 (0)