Skip to content

Commit af7a96f

Browse files
committed
Implemented JSON Websocket communication for server. Split transmitter into a Radio and Receiver interfaces to support server logic.
1 parent 2b095f2 commit af7a96f

35 files changed

+920
-403
lines changed

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

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ of this software and associated documentation files (the "Software"), to deal
3939
* Must be overloaded to implement a specific format.
4040
*/
4141
public abstract class Communicator {
42-
protected Transmitter transmitter;
42+
protected Radio radio;
4343

4444
/**
4545
* Convert a formatted string into a {@link Request}/{@link Confirmation}.
@@ -104,8 +104,8 @@ public abstract class Communicator {
104104
*
105105
* @param transmitter Injected {@link Transmitter}
106106
*/
107-
public Communicator(Transmitter transmitter) {
108-
this.transmitter = transmitter;
107+
public Communicator(Radio transmitter) {
108+
this.radio = transmitter;
109109
}
110110

111111
/**
@@ -114,7 +114,8 @@ public Communicator(Transmitter transmitter) {
114114
* @param events handler for call back events.
115115
*/
116116
public void connect(String uri, CommunicatorEvents events) {
117-
transmitter.connect(uri, new EventHandler(events));
117+
if (radio instanceof Transmitter)
118+
((Transmitter) radio).connect(uri, new EventHandler(events));
118119
}
119120

120121
/**
@@ -123,7 +124,8 @@ public void connect(String uri, CommunicatorEvents events) {
123124
* @param events handler for call back events.
124125
*/
125126
public void accept(CommunicatorEvents events) {
126-
transmitter.accept(new EventHandler(events));
127+
if (radio instanceof Receiver)
128+
((Receiver) radio).accept(new EventHandler(events));
127129
}
128130

129131
/**
@@ -134,7 +136,7 @@ public void accept(CommunicatorEvents events) {
134136
* @param request the outgoing {@link Request}
135137
*/
136138
public void sendCall(String uniqueId, String action, Request request) {
137-
transmitter.send(makeCall(uniqueId, action, packPayload(request)));
139+
radio.send(makeCall(uniqueId, action, packPayload(request)));
138140
}
139141

140142
/**
@@ -144,7 +146,7 @@ public void sendCall(String uniqueId, String action, Request request) {
144146
* @param confirmation the outgoing {@link Confirmation}
145147
*/
146148
public void sendCallResult(String uniqueId, Confirmation confirmation) {
147-
transmitter.send(makeCallResult(uniqueId, packPayload(confirmation)));
149+
radio.send(makeCallResult(uniqueId, packPayload(confirmation)));
148150
}
149151

150152
/**
@@ -155,17 +157,17 @@ public void sendCallResult(String uniqueId, Confirmation confirmation) {
155157
* @param errorDescription a associated error description.
156158
*/
157159
public void sendCallError(String uniqueId, String errorCode, String errorDescription) {
158-
transmitter.send(makeCallError(uniqueId, errorCode, errorDescription));
160+
radio.send(makeCallError(uniqueId, errorCode, errorDescription));
159161
}
160162

161163
/**
162164
* Close down the connection. Uses the {@link Transmitter}.
163165
*/
164166
public void disconnect() {
165-
transmitter.disconnect();
167+
radio.disconnect();
166168
}
167169

168-
private class EventHandler implements TransmitterEvents {
170+
private class EventHandler implements RadioEvents {
169171
private final CommunicatorEvents events;
170172

171173
public EventHandler(CommunicatorEvents events) {

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

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

2828
public interface Listener {
29-
void open(ListenerEvents listenerEvents);
29+
void open(String hostname, int port, ListenerEvents listenerEvents);
3030

31+
void close();
3132
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package eu.chargetime.ocpp;/*
2+
ChargeTime.eu - Java-OCA-OCPP
3+
4+
MIT License
5+
6+
Copyright (C) 2016 Thomas Volden <[email protected]>
7+
8+
Permission is hereby granted, free of charge, to any person obtaining a copy
9+
of this software and associated documentation files (the "Software"), to deal
10+
in the Software without restriction, including without limitation the rights
11+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
copies of the Software, and to permit persons to whom the Software is
13+
furnished to do so, subject to the following conditions:
14+
15+
The above copyright notice and this permission notice shall be included in all
16+
copies or substantial portions of the Software.
17+
18+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24+
SOFTWARE.
25+
*/
26+
27+
public interface Radio {
28+
29+
/**
30+
* Disconnect from a node.
31+
*/
32+
void disconnect();
33+
34+
/**
35+
* Send a message to a node.
36+
*
37+
* @param message test message to send.
38+
*/
39+
void send(String message);
40+
41+
}

ocpp-common/src/main/java/eu/chargetime/ocpp/TransmitterEvents.java renamed to ocpp-common/src/main/java/eu/chargetime/ocpp/RadioEvents.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ of this software and associated documentation files (the "Software"), to deal
3030
/**
3131
* Transmitter related events.
3232
*/
33-
public interface TransmitterEvents
33+
public interface RadioEvents
3434
{
3535
/**
3636
* Connection established.
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package eu.chargetime.ocpp;/*
2+
ChargeTime.eu - Java-OCA-OCPP
3+
4+
MIT License
5+
6+
Copyright (C) 2016 Thomas Volden <[email protected]>
7+
8+
Permission is hereby granted, free of charge, to any person obtaining a copy
9+
of this software and associated documentation files (the "Software"), to deal
10+
in the Software without restriction, including without limitation the rights
11+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
copies of the Software, and to permit persons to whom the Software is
13+
furnished to do so, subject to the following conditions:
14+
15+
The above copyright notice and this permission notice shall be included in all
16+
copies or substantial portions of the Software.
17+
18+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24+
SOFTWARE.
25+
*/
26+
27+
public interface Receiver extends Radio {
28+
29+
/**
30+
* Accept an incoming connection request.
31+
*
32+
* @param events connection related events.
33+
*/
34+
void accept(RadioEvents events);
35+
}

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

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,22 +41,23 @@ of this software and associated documentation files (the "Software"), to deal
4141
public abstract class Server extends FeatureHandler {
4242

4343
private ArrayList<Session> sessions;
44+
private Listener listener;
4445

4546
/**
4647
*
4748
*/
48-
public Server() {
49+
public Server(Listener listener) {
50+
this.listener = listener;
4951
this.sessions = new ArrayList<>();
5052
}
5153

5254
/**
5355
* Start listening for clients.
5456
*
55-
* @param listener Inject the listener.
5657
* @param serverEvents Callback handler for server specific events.
5758
*/
58-
public void open(Listener listener, ServerEvents serverEvents) {
59-
listener.open(session -> {
59+
public void open(String hostname, int port, ServerEvents serverEvents) {
60+
listener.open(hostname, port, session -> {
6061
session.accept(new SessionEvents() {
6162
@Override
6263
public Feature findFeatureByAction(String action) {
@@ -99,6 +100,10 @@ public void handleConnectionOpened() {
99100
});
100101
}
101102

103+
public void close() {
104+
listener.close();
105+
}
106+
102107
/**
103108
* Send a message to a client.
104109
*

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

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -30,32 +30,14 @@ of this software and associated documentation files (the "Software"), to deal
3030
/**
3131
* Send and receive data. Maintain connection.
3232
*/
33-
public interface Transmitter
33+
public interface Transmitter extends Radio
3434
{
3535
/**
3636
* Connect to a specific node.
3737
*
3838
* @param uri url and port of the node.
3939
* @param events connection related events.
4040
*/
41-
void connect(String uri, TransmitterEvents events);
41+
void connect(String uri, RadioEvents events);
4242

43-
/**
44-
* Disconnect from a node.
45-
*/
46-
void disconnect();
47-
48-
/**
49-
* Send a message to a node.
50-
*
51-
* @param message test message to send.
52-
*/
53-
void send(String message);
54-
55-
/**
56-
* Accept an incoming connection request.
57-
*
58-
* @param events connection related events.
59-
*/
60-
void accept(TransmitterEvents events);
6143
}

ocpp-common/src/test/java/eu/chargetime/ocpp/test/ServerTest.java

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ of this software and associated documentation files (the "Software"), to deal
3939
*/
4040
public class ServerTest extends TestUtilities {
4141

42+
private static final String LOCALHOST = "localhost";
43+
private static final int PORT = 42;
44+
4245
private Server server;
4346
private Request request;
4447
private SessionEvents sessionEvents;
@@ -62,10 +65,10 @@ public void setup() {
6265
doReturn(request.getClass()).when(feature).getRequestType();
6366
doReturn(TestConfirmation.class).when(feature).getConfirmationType();
6467
when(feature.getAction()).thenReturn(null);
65-
doAnswer(invocation -> listenerEvents = invocation.getArgumentAt(0, ListenerEvents.class)).when(listener).open(any());
68+
doAnswer(invocation -> listenerEvents = invocation.getArgumentAt(2, ListenerEvents.class)).when(listener).open(anyString(), anyInt(), any());
6669
doAnswer(invocation -> sessionEvents = invocation.getArgumentAt(0, SessionEvents.class)).when(session).accept(any());
6770

68-
server = new Server() {
71+
server = new Server(listener) {
6972
};
7073

7174
when(profile.getFeatureList()).thenReturn(aList(feature));
@@ -75,7 +78,7 @@ public void setup() {
7578
@Test
7679
public void newSession_serverIsListening_sessionIsAccepted() {
7780
// Given
78-
server.open(listener, serverEvents);
81+
server.open(LOCALHOST, PORT, serverEvents);
7982

8083
// When
8184
listenerEvents.newSession(session);
@@ -87,7 +90,7 @@ public void newSession_serverIsListening_sessionIsAccepted() {
8790
@Test
8891
public void newSession_serverIsListening_callbackWithIndex0() {
8992
// Given
90-
server.open(listener, serverEvents);
93+
server.open(LOCALHOST, PORT, serverEvents);
9194

9295
// When
9396
listenerEvents.newSession(session);
@@ -103,7 +106,7 @@ public void send_aMessage_isCommunicated() throws Exception {
103106
int sessionIndex = 0;
104107

105108
when(session.sendRequest(any(), any())).thenReturn(someUniqueId);
106-
server.open(listener, serverEvents);
109+
server.open(LOCALHOST, PORT, serverEvents);
107110
listenerEvents.newSession(session);
108111

109112
// When
@@ -116,7 +119,7 @@ public void send_aMessage_isCommunicated() throws Exception {
116119
@Test
117120
public void handleRequest_callsFeatureHandleRequest() {
118121
// Given
119-
server.open(listener, serverEvents);
122+
server.open(LOCALHOST, PORT, serverEvents);
120123
listenerEvents.newSession(session);
121124

122125
// When

ocpp-v1_6-test/src/it/groovy/eu/chargetime/ocpp/test/core/Authorize.groovy

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ import spock.util.concurrent.PollingConditions;
88

99
class Authorize extends Specification
1010
{
11-
@Shared FakeCentralSystem centralSystem = FakeCentralSystem.getInstance();
11+
@Shared
12+
FakeCentralSystem centralSystem = new FakeCentralSystem();
1213
@Shared FakeChargePoint chargePoint = new FakeChargePoint();
1314

1415
def setupSpec() {
@@ -22,11 +23,7 @@ class Authorize extends Specification
2223

2324
def cleanup() {
2425
chargePoint.disconnect();
25-
}
26-
27-
def "No initial request received"() {
28-
expect:
29-
! centralSystem.hasReceivedAuthorizeRequest();
26+
centralSystem.stopped();
3027
}
3128

3229
def "Charge point sends Authorize request and receives a response"() {
@@ -36,24 +33,13 @@ class Authorize extends Specification
3633

3734
then:
3835
conditions.eventually {
39-
assert centralSystem.hasReceivedAuthorizeRequest();
36+
assert centralSystem.hasHandledAuthorizeRequest();
4037
}
4138

42-
when:
43-
centralSystem.sendAuthorizeConfirmation(FakeCentralSystem.AuthorizationStatus.Accepted);
44-
4539
then:
4640
conditions.eventually {
4741
assert chargePoint.hasReceivedAuthorizeConfirmation("Accepted");
4842
}
4943
}
5044

51-
def "A Authorize request isn't seen as a Boot Notification"() {
52-
when:
53-
chargePoint.sendAuthorizeRequest("token");
54-
55-
then:
56-
centralSystem.receivedMessageIsNot("BootNotification");
57-
}
58-
5945
}

ocpp-v1_6-test/src/it/groovy/eu/chargetime/ocpp/test/core/BootNotification.groovy

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
package eu.chargetime.ocpp.test.core
22

33
import eu.chargetime.ocpp.model.core.RegistrationStatus
4-
import eu.chargetime.ocpp.test.FakeCentralSystem
54
import eu.chargetime.ocpp.test.FakeChargePoint
5+
import eu.chargetime.ocpp.test.OldFakeCentralSystem
66
import spock.lang.Shared
77
import spock.lang.Specification
88
import spock.util.concurrent.PollingConditions
99

1010
class BootNotification extends Specification
1111
{
12-
@Shared FakeCentralSystem centralSystem = FakeCentralSystem.getInstance();
12+
@Shared
13+
OldFakeCentralSystem centralSystem = OldFakeCentralSystem.getInstance();
1314
@Shared FakeChargePoint chargePoint = new FakeChargePoint();
1415

1516
def setupSpec() {

0 commit comments

Comments
 (0)