Skip to content

Commit 1b34d48

Browse files
committed
TG-2 added example for JSONServer.
TG-1 added methods to create requests for server core profile.
1 parent ba1931f commit 1b34d48

File tree

6 files changed

+318
-5
lines changed

6 files changed

+318
-5
lines changed

ocpp-common/src/main/java/eu/chargetime/ocpp/Server.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,8 @@ public void handleError(String uniqueId) {
8787

8888
@Override
8989
public void handleConnectionClosed() {
90-
90+
serverEvents.lostSession(sessions.indexOf(session));
91+
sessions.remove(session);
9192
}
9293

9394
@Override

ocpp-common/src/main/java/eu/chargetime/ocpp/ServerEvents.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ of this software and associated documentation files (the "Software"), to deal
2525
*/
2626

2727
public interface ServerEvents {
28-
void newSession(int identity);
28+
void newSession(int sessionIndex);
2929

30-
void lostSession(int identity);
30+
void lostSession(int sessionIndex);
3131
}
Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
package eu.chargetime.ocpp.test;
2+
3+
import eu.chargetime.ocpp.JSONServer;
4+
import eu.chargetime.ocpp.ServerEvents;
5+
import eu.chargetime.ocpp.feature.profile.ServerCoreEventHandler;
6+
import eu.chargetime.ocpp.feature.profile.ServerCoreProfile;
7+
import eu.chargetime.ocpp.model.Confirmation;
8+
import eu.chargetime.ocpp.model.Request;
9+
import eu.chargetime.ocpp.model.core.*;
10+
11+
import java.util.Calendar;
12+
13+
/*
14+
ChargeTime.eu - Java-OCA-OCPP
15+
Copyright (C) 2015-2016 Thomas Volden <[email protected]>
16+
17+
MIT License
18+
19+
Copyright (c) 2016 Thomas Volden
20+
21+
Permission is hereby granted, free of charge, to any person obtaining a copy
22+
of this software and associated documentation files (the "Software"), to deal
23+
in the Software without restriction, including without limitation the rights
24+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
25+
copies of the Software, and to permit persons to whom the Software is
26+
furnished to do so, subject to the following conditions:
27+
28+
The above copyright notice and this permission notice shall be included in all
29+
copies or substantial portions of the Software.
30+
31+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
32+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
33+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
34+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
35+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
36+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
37+
SOFTWARE.
38+
*/
39+
public class JSONServerSample
40+
{
41+
private JSONServer server;
42+
43+
public void started() throws Exception
44+
{
45+
if (server != null)
46+
return;
47+
48+
// The core profile is mandatory
49+
ServerCoreProfile core = new ServerCoreProfile(new ServerCoreEventHandler() {
50+
@Override
51+
public AuthorizeConfirmation handleAuthorizeRequest(int sessionIndex, AuthorizeRequest request) {
52+
53+
System.out.println(request);
54+
// ... handle event
55+
56+
return new AuthorizeConfirmation();
57+
}
58+
59+
@Override
60+
public BootNotificationConfirmation handleBootNotificationRequest(int sessionIndex, BootNotificationRequest request) {
61+
62+
System.out.println(request);
63+
// ... handle event
64+
65+
return null; // returning null means unsupported feature
66+
}
67+
68+
@Override
69+
public DataTransferConfirmation handleDataTransferRequest(int sessionIndex, DataTransferRequest request) {
70+
71+
System.out.println(request);
72+
// ... handle event
73+
74+
return null; // returning null means unsupported feature
75+
}
76+
77+
@Override
78+
public HeartbeatConfirmation handleHeartbeatRequest(int sessionIndex, HeartbeatRequest request) {
79+
80+
System.out.println(request);
81+
// ... handle event
82+
83+
return null; // returning null means unsupported feature
84+
}
85+
86+
@Override
87+
public MeterValuesConfirmation handleMeterValuesRequest(int sessionIndex, MeterValuesRequest request) {
88+
89+
System.out.println(request);
90+
// ... handle event
91+
92+
return null; // returning null means unsupported feature
93+
}
94+
95+
@Override
96+
public StartTransactionConfirmation handleStartTransactionRequest(int sessionIndex, StartTransactionRequest request) {
97+
98+
System.out.println(request);
99+
// ... handle event
100+
101+
return null; // returning null means unsupported feature
102+
}
103+
104+
@Override
105+
public StatusNotificationConfirmation handleStatusNotificationRequest(int sessionIndex, StatusNotificationRequest request) {
106+
107+
System.out.println(request);
108+
// ... handle event
109+
110+
return null; // returning null means unsupported feature
111+
}
112+
113+
@Override
114+
public StopTransactionConfirmation handleStopTransactionRequest(int sessionIndex, StopTransactionRequest request) {
115+
116+
System.out.println(request);
117+
// ... handle event
118+
119+
return null; // returning null means unsupported feature
120+
}
121+
});
122+
123+
server = new JSONServer(core);
124+
server.open("localhost", 8887, new ServerEvents() {
125+
126+
@Override
127+
public void newSession(int sessionIndex) {
128+
129+
// sessionIndex is used to send messages.
130+
System.out.println("New session " + sessionIndex);
131+
}
132+
133+
@Override
134+
public void lostSession(int sessionIndex) {
135+
136+
System.out.println("Session " + sessionIndex + " lost connection");
137+
}
138+
});
139+
}
140+
141+
public void sendClearCacheRequest() throws Exception {
142+
143+
// Use the feature profile to help create event
144+
ClearCacheRequest request = core.createClearCacheRequest();
145+
146+
// Server returns a promise which will be filled once it receives a confirmation.
147+
// Select the distination client with the sessionIndex integer.
148+
server.send(sessionIndex, request).whenComplete((confirmation, throwable) -> System.out.println(confirmation));
149+
}
150+
151+
}

ocpp-v1_6-test/src/main/java/eu/chargetime/ocpp/test/FakeCentralSystem.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,8 @@ public StopTransactionConfirmation handleStopTransactionRequest(int sessionIndex
131131
}));
132132
server.open("localhost", 8887, new ServerEvents() {
133133
@Override
134-
public void newSession(int identity) {
135-
sessionIndex = identity;
134+
public void newSession(int sessionIndex) {
135+
FakeCentralSystem.this.sessionIndex = sessionIndex;
136136
}
137137

138138
@Override

ocpp-v1_6/src/main/java/eu/chargetime/ocpp/feature/profile/ServerCoreProfile.java

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ of this software and associated documentation files (the "Software"), to deal
2424
SOFTWARE.
2525
*/
2626

27+
import eu.chargetime.ocpp.PropertyConstraintException;
2728
import eu.chargetime.ocpp.feature.*;
2829
import eu.chargetime.ocpp.model.Confirmation;
2930
import eu.chargetime.ocpp.model.Request;
@@ -87,4 +88,59 @@ public Confirmation handleRequest(int sessionIndex, Request request) {
8788

8889
return result;
8990
}
91+
92+
public ChangeAvailabilityRequest createChangeAvailabilityRequest(AvailabilityType type, int connectorId) throws PropertyConstraintException {
93+
ChangeAvailabilityRequest request = new ChangeAvailabilityRequest();
94+
request.setType(type);
95+
request.setConnectorId(connectorId);
96+
return request;
97+
}
98+
99+
public ChangeConfigurationRequest createChangeConfigurationRequest(String key, String value) throws PropertyConstraintException {
100+
ChangeConfigurationRequest request = new ChangeConfigurationRequest();
101+
request.setKey(key);
102+
request.setValue(value);
103+
return request;
104+
}
105+
106+
public ClearCacheRequest createClearCacheRequest() {
107+
return new ClearCacheRequest();
108+
}
109+
110+
public DataTransferRequest createDataTransferRequest(String vendorId) throws PropertyConstraintException {
111+
DataTransferRequest request = new DataTransferRequest();
112+
request.setVendorId(vendorId);
113+
return request;
114+
}
115+
116+
public GetConfigurationRequest createGetConfigurationRequest() {
117+
return new GetConfigurationRequest();
118+
}
119+
120+
public RemoteStartTransactionRequest createRemoteStartTransactionRequest(String idToken) throws PropertyConstraintException {
121+
IdToken idTag = new IdToken();
122+
idTag.setIdToken(idToken);
123+
124+
RemoteStartTransactionRequest request = new RemoteStartTransactionRequest();
125+
request.setIdTag(idTag);
126+
return request;
127+
}
128+
129+
public RemoteStopTransactionRequest createRemoteStopTransactionRequest(Integer transactionId) {
130+
RemoteStopTransactionRequest request = new RemoteStopTransactionRequest();
131+
request.setTransactionId(transactionId);
132+
return request;
133+
}
134+
135+
public ResetRequest createResetRequest(ResetType type) {
136+
ResetRequest request = new ResetRequest();
137+
request.setType(type);
138+
return request;
139+
}
140+
141+
public UnlockConnectorRequest createUnlockConnectorRequest(int connectorId) throws PropertyConstraintException {
142+
UnlockConnectorRequest request = new UnlockConnectorRequest();
143+
request.setConnectorId(connectorId);
144+
return request;
145+
}
90146
}

0 commit comments

Comments
 (0)