forked from ShellRechargeSolutionsEU/ocpp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJsonClientTestApp.scala
162 lines (128 loc) · 5.37 KB
/
JsonClientTestApp.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package com.thenewmotion.ocpp
package json
package example
import java.net.URI
import scala.concurrent._
import ExecutionContext.Implicits.global
import messages.v1x._
import api._
object JsonClientTestApp extends App {
/*
* First, let's figure out what kind of connection we have to set up: which
* charge point identity, which central system endpoint URI, and which OCPP
* version...
*/
private val chargerId = args.headOption.getOrElse("test-charger")
private val centralSystemUri = if (args.length >= 2) args(1) else "ws://localhost:8080/ocppws"
private val versions =
if (args.length >= 3)
args(2).split(",").map { version =>
Version.withName(version).getOrElse(sys.error(s"Unrecognized version(s): ${args(2)}"))
}.toSeq.collect({ case v: Version1X => v })
else
Seq(Version.V16)
/*
* Then, we create an OcppJsonClient with those settings. Also, we give as an
* argument in a second list a ChargePoint instance that specifies how we want
* to handle incoming requests.
*/
val ocppJsonClient = OcppJsonClient.forVersion1x(chargerId, new URI(centralSystemUri), versions) {
/*
* Here we define how we handle OCPP requests from the Central System to us.
* The example app answers to GetConfiguration requests that it doesn't have
* any configuration. To other requests it just answers that that message
* type is not supported.
*/
new ChargePoint {
def getConfiguration(req: GetConfigurationReq): Future[GetConfigurationRes] =
Future.successful(GetConfigurationRes(
values = List(),
unknownKeys = req.keys
))
def remoteStartTransaction(q: RemoteStartTransactionReq): Future[RemoteStartTransactionRes] =
notSupported("Remote Start Transaction")
def remoteStopTransaction(q: RemoteStopTransactionReq): Future[RemoteStopTransactionRes] =
notSupported("Remote Stop Transaction")
def unlockConnector(q: UnlockConnectorReq): Future[UnlockConnectorRes] =
notSupported("Unlock Connector")
def getDiagnostics(req: GetDiagnosticsReq): Future[GetDiagnosticsRes] =
notSupported("Get Diagnostics")
def changeConfiguration(req: ChangeConfigurationReq): Future[ChangeConfigurationRes] =
notSupported("Change Configuration")
def changeAvailability(req: ChangeAvailabilityReq): Future[ChangeAvailabilityRes] =
notSupported("Change Availability")
def clearCache: Future[ClearCacheRes] =
notSupported("Clear Cache")
def reset(req: ResetReq): Future[ResetRes] =
notSupported("Reset")
def updateFirmware(req: UpdateFirmwareReq): Future[Unit] =
notSupported("Update Firmware")
def sendLocalList(req: SendLocalListReq): Future[SendLocalListRes] =
notSupported("Send Local List")
def getLocalListVersion: Future[GetLocalListVersionRes] =
notSupported("Get Local List Version")
def dataTransfer(q: ChargePointDataTransferReq): Future[ChargePointDataTransferRes] =
notSupported("Data Transfer")
def reserveNow(q: ReserveNowReq): Future[ReserveNowRes] =
notSupported("Reserve Now")
def cancelReservation(q: CancelReservationReq): Future[CancelReservationRes] =
notSupported("Cancel Reservation")
def clearChargingProfile(req: ClearChargingProfileReq): Future[ClearChargingProfileRes] =
notSupported("Clear Charging Profile")
def getCompositeSchedule(req: GetCompositeScheduleReq): Future[GetCompositeScheduleRes] =
notSupported("Get Composite Schedule")
def setChargingProfile(req: SetChargingProfileReq): Future[SetChargingProfileRes] =
notSupported("Set Charging Profile")
def triggerMessage(req: TriggerMessageReq): Future[TriggerMessageRes] =
notSupported("Trigger Message")
def notSupported(opName: String): Future[Nothing] =
Future.failed(OcppException(
PayloadErrorCode.NotSupported,
s"Demo app doesn't support $opName"
))
}
}
/*
* Set up a callback that will print a message once the connection is
* closed
*/
ocppJsonClient.onClose.foreach(_ => println("OCPP connection closed"))
println(s"Connected using OCPP version ${ocppJsonClient.ocppVersion}")
/*
* Now let's send some OCPP requests to that Central System! Just like a real
* charge point!
*/
for {
_ <- ocppJsonClient.send(BootNotificationReq(
chargePointVendor = "The New Motion",
chargePointModel = "Lolo 47.6",
chargePointSerialNumber = Some("123456"),
chargeBoxSerialNumber = None,
firmwareVersion = None,
iccid = None,
imsi = None,
meterType = None,
meterSerialNumber = None))
_ <- ocppJsonClient.send(HeartbeatReq)
_ <- ocppJsonClient.send(StatusNotificationReq(
scope = ChargePointScope,
status = ChargePointStatus.Unavailable(info = None),
timestamp = None,
vendorId = None
))
_ <- ocppJsonClient.send(AuthorizeReq(idTag = "12345678")).map { res =>
if (res.idTag.status == AuthorizationStatus.Accepted)
println("12345678 is authorized.")
else
println("12345678 has been rejected. No power to him!")
}
} yield ()
/*
* Wait for the above code to do its thing (in a real use case, you'd do this
* in a nicer way)
*/
Thread.sleep(7000)
/* Bye, we're done. */
ocppJsonClient.close()
System.exit(0)
}