forked from reinierl/docile-charge-point
-
Notifications
You must be signed in to change notification settings - Fork 32
/
remote-transaction.scala
50 lines (38 loc) · 1.8 KB
/
remote-transaction.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import com.thenewmotion.ocpp.messages._
say("Waiting for remote start message")
val startRequest = expectIncoming(remoteStartTransactionReq.respondingWith(RemoteStartTransactionRes(true)))
val chargeTokenId = startRequest.idTag
say("Received remote start, authorizing...")
val auth = authorize(chargeTokenId).idTag
if (auth.status == AuthorizationStatus.Accepted) {
say("Obtained authorization from Central System; starting transaction")
statusNotification(status = ChargePointStatus.Occupied(Some(OccupancyKind.Preparing)))
val transId = startTransaction(meterStart = 300, idTag = chargeTokenId).transactionId
statusNotification(status = ChargePointStatus.Occupied(Some(OccupancyKind.Charging)))
say(s"Transaction started with ID $transId; awaiting remote stop")
def waitForValidRemoteStop(): Unit = {
val shouldStop =
expectIncoming(
requestMatching({case r: RemoteStopTransactionReq => r.transactionId == transId})
.respondingWith(RemoteStopTransactionRes(_))
)
if (shouldStop) {
say("Received RemoteStopTransaction request; stopping transaction")
()
} else {
say(s"Received RemoteStopTransaction request for other transaction with ID. I'll keep waiting for a stop for $transId.")
waitForValidRemoteStop()
}
}
waitForValidRemoteStop()
// handle UnlockConnectorReq if present
Try(expectIncoming(unlockConnectorReq.respondingWith(UnlockConnectorRes(UnlockStatus.NotSupported))))
statusNotification(status = ChargePointStatus.Occupied(Some(OccupancyKind.Finishing)))
stopTransaction(transactionId = transId, idTag = Some(chargeTokenId))
statusNotification(status = ChargePointStatus.Available())
say("Transaction stopped")
} else {
say("Authorization denied by Central System")
fail("Not authorized")
}
// vim: set ts=4 sw=4 et: