Skip to content

Commit d3e768a

Browse files
committed
Added java docs to common module.
1 parent 3998455 commit d3e768a

20 files changed

+321
-45
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public abstract class Client
5151
private Session session;
5252

5353
/**
54-
* Constructor
54+
* Handle required injections.
5555
*
5656
* @param session Inject session object
5757
* @see Session

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

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public abstract class Communicator {
4848
* @param payload the raw formatted payload.
4949
* @param type the expected return type.
5050
* @return the unpacked payload.
51-
* @throws Exception error occurred while converting.
51+
* @throws Exception error occurred while converting.
5252
*/
5353
public abstract <T> T unpackPayload(String payload, Class<T> type) throws Exception;
5454

@@ -61,9 +61,9 @@ public abstract class Communicator {
6161
public abstract String packPayload(Object payload);
6262

6363
/**
64-
* Create a call result envelope to transmit to the server.
64+
* Create a call result envelope to transmit.
6565
*
66-
* @param uniqueId the id the server expects.
66+
* @param uniqueId the id the receiver expects.
6767
* @param payload packed payload.
6868
* @return a fully packed message ready to send.
6969
*/
@@ -72,17 +72,17 @@ public abstract class Communicator {
7272
/**
7373
* Create a call envelope to transmit to the server.
7474
*
75-
* @param uniqueId the id the server must reply with.
75+
* @param uniqueId the id the receiver must reply with.
7676
* @param action action name of the feature.
7777
* @param payload packed payload.
7878
* @return a fully packed message ready to send.
7979
*/
8080
protected abstract String makeCall(String uniqueId, String action, String payload);
8181

8282
/**
83-
* Create a call error envelope to transmit to the server.
83+
* Create a call error envelope to transmit.
8484
*
85-
* @param uniqueId the id the server expects.
85+
* @param uniqueId the id the receiver expects.
8686
* @param errorCode an OCPP error code.
8787
* @param errorDescription an associated error description.
8888
* @return a fully packed message ready to send.
@@ -91,16 +91,16 @@ public abstract class Communicator {
9191

9292
/**
9393
* Identify an incoming call and parse it into one of the following:
94-
* {@link CallMessage} a request from the server.
95-
* {@link CallResultMessage} a response from the server.
94+
* {@link CallMessage} a request.
95+
* {@link CallResultMessage} a response.
9696
*
97-
* @param message raw message from server
97+
* @param message the raw message
9898
* @return CallMessage or {@link CallResultMessage}
9999
*/
100100
protected abstract Message parse(String message);
101101

102102
/**
103-
* Constructore
103+
* Handle required injections.
104104
*
105105
* @param transmitter Injected {@link Transmitter}
106106
*/
@@ -140,9 +140,9 @@ public void disconnected() {
140140
}
141141

142142
/**
143-
* Send a new {@link Request} to the server.
143+
* Send a new {@link Request}.
144144
*
145-
* @param uniqueId the id the server should use to reply.
145+
* @param uniqueId the id the receiver should use to reply.
146146
* @param action action name of the {@link eu.chargetime.ocpp.feature.Feature}.
147147
* @param request the outgoing {@link Request}
148148
*/
@@ -151,19 +151,19 @@ public void sendCall(String uniqueId, String action, Request request) {
151151
}
152152

153153
/**
154-
* Send a {@link Confirmation} reply to a server {@link Request}.
154+
* Send a {@link Confirmation} reply to a {@link Request}.
155155
*
156-
* @param uniqueId the id the server expects.
156+
* @param uniqueId the id the receiver expects.
157157
* @param confirmation the outgoing {@link Confirmation}
158158
*/
159159
public void sendCallResult(String uniqueId, Confirmation confirmation) {
160160
transmitter.send(makeCallResult(uniqueId, packPayload(confirmation)));
161161
}
162162

163163
/**
164-
* Send an error to the server.
164+
* Send an error.
165165
*
166-
* @param uniqueId the id the server expects a response to.
166+
* @param uniqueId the id the receiver expects a response to.
167167
* @param errorCode an OCPP error Code
168168
* @param errorDescription a associated error description.
169169
*/
@@ -172,7 +172,7 @@ public void sendCallError(String uniqueId, String errorCode, String errorDescrip
172172
}
173173

174174
/**
175-
* Disconnect from the server. Uses the {@link Transmitter}.
175+
* Close down the connection. Uses the {@link Transmitter}.
176176
*/
177177
public void disconnect() {
178178
transmitter.disconnect();

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

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,19 +32,43 @@ of this software and associated documentation files (the "Software"), to deal
3232
*/
3333
public interface CommunicatorEvents {
3434
/**
35-
* Handle call result from the server.
36-
* Use the unique id to identify the confirmation type, you can choose to use the {@link Communicator}s unpackPayload feature.
35+
* Handle call result.
36+
* <p>
37+
* Hint: Use the id to identify the confirmation type, you can then choose to use the {@link Communicator}s unpackPayload method.
3738
*
3839
* @param id unique id used to identify the original request.
3940
* @param payload raw payload.
4041
*/
4142
void onCallResult(String id, String payload);
4243

44+
/**
45+
* Handle call.
46+
* <p>
47+
* Hint: Use the action name to identify the request, you can then choose to use {@link Communicator}s unpackPayload method.
48+
*
49+
* @param id unique id used to reply to server.
50+
* @param action action name used to identify the feature.
51+
* @param payload raw payload.
52+
*/
4353
void onCall(String id, String action, String payload);
4454

55+
/**
56+
* Handle call error.
57+
* <p>
58+
* Hint: Use the id to identify the original call. You can use {@link Communicator}s unpackPayload method.
59+
*
60+
* @param id unique id used to identify the original request.
61+
* @param payload raw error payload.
62+
*/
4563
void onError(String id, String payload);
4664

65+
/**
66+
* The connection was disconnected.
67+
*/
4768
void onDisconnected();
4869

70+
/**
71+
* A connection was established.
72+
*/
4973
void onConnected();
5074
}

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

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package eu.chargetime.ocpp;
22

3-
/**
3+
/*
44
ChargeTime.eu - Java-OCA-OCPP
55
Copyright (C) 2015-2016 Thomas Volden <[email protected]>
66
@@ -26,25 +26,34 @@ of this software and associated documentation files (the "Software"), to deal
2626
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2727
SOFTWARE.
2828
*/
29+
30+
/**
31+
* Exception used when validating fields.
32+
*/
2933
public class PropertyConstraintException extends Exception {
3034
private final String fieldKey;
3135
private final Object fieldValue;
36+
private final String message;
3237

3338
@Override
3439
public String getMessage() {
3540
return message;
3641
}
3742

43+
/**
44+
* @return value of the failing field.
45+
*/
3846
public Object getFieldValue() {
3947
return fieldValue;
4048
}
4149

50+
/**
51+
* @return name of the failing field.
52+
*/
4253
public String getFieldKey() {
4354
return fieldKey;
4455
}
4556

46-
private final String message;
47-
4857
public PropertyConstraintException(String fieldKey, Object fieldValue) {
4958
this(fieldKey, fieldValue, null);
5059
}

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

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import java.util.HashMap;
66
import java.util.UUID;
77

8-
/**
8+
/*
99
ChargeTime.eu - Java-OCA-OCPP
1010
Copyright (C) 2015-2016 Thomas Volden <[email protected]>
1111
@@ -31,6 +31,10 @@ of this software and associated documentation files (the "Software"), to deal
3131
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
3232
SOFTWARE.
3333
*/
34+
35+
/**
36+
* Class to store and restore requests based on a unique id.
37+
*/
3438
public class Queue
3539
{
3640
private HashMap<String, Request> requestQueue;
@@ -39,12 +43,26 @@ public Queue () {
3943
requestQueue = new HashMap<>();
4044
}
4145

46+
/**
47+
* Store a {@link Request} and get a unique identifier to fetch it later on.
48+
*
49+
* @param request the {@link Request}.
50+
* @return a unique identifier used to fetch the request.
51+
*/
4252
public String store(Request request) {
4353
String ticket = UUID.randomUUID().toString();
4454
requestQueue.put(ticket, request);
4555
return ticket;
4656
}
4757

58+
/**
59+
* Restore a {@link Request} using a unique identifier.
60+
* The identifier can only be used once.
61+
* If no Request was found, null is returned.
62+
*
63+
* @param ticket unique identifier returned when {@link Request} was initially stored.
64+
* @return the stored {@link Request}
65+
*/
4866
public Request restoreRequest(String ticket) {
4967
Request request = null;
5068
try {

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

Lines changed: 49 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
import java.util.concurrent.CompletableFuture;
88

9-
/**
9+
/*
1010
ChargeTime.eu - Java-OCA-OCPP
1111
Copyright (C) 2015-2016 Thomas Volden <[email protected]>
1212
@@ -32,38 +32,73 @@ of this software and associated documentation files (the "Software"), to deal
3232
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
3333
SOFTWARE.
3434
*/
35+
36+
/**
37+
* Unites outgoing {@link Request} with incoming {@link Confirmation}s or errors.
38+
* Catches errors and responds with error messages.
39+
*/
3540
public class Session {
3641

3742
private Communicator communicator;
3843
private Queue queue;
3944
private SessionEvents events;
4045

46+
/**
47+
* Handles required injections.
48+
*
49+
* @param communicator send and receive messages.
50+
* @param queue store and restore requests based on unique ids.
51+
*/
4152
public Session(Communicator communicator, Queue queue) {
4253
this.communicator = communicator;
4354
this.queue = queue;
4455
}
4556

57+
/**
58+
* Send a {@link Request}.
59+
*
60+
* @param action action name to identify the feature.
61+
* @param payload the {@link Request} payload to send
62+
* @return unique identification to identify the request.
63+
*/
4664
public String sendRequest(String action, Request payload) {
4765
String uuid = queue.store(payload);
4866
communicator.sendCall(uuid, action, payload);
4967
return uuid;
5068
}
5169

70+
/**
71+
* Send a {@link Confirmation} to a {@link Request}
72+
*
73+
* @param uniqueId the unique identification the receiver expects.
74+
* @param confirmation the {@link Confirmation} payload to send.
75+
*/
5276
public void sendConfirmation(String uniqueId, Confirmation confirmation) {
5377
communicator.sendCallResult(uniqueId, confirmation);
5478
}
5579

56-
private Class<? extends Confirmation> getConfirmationType(String uniqueId) {
80+
private Class<? extends Confirmation> getConfirmationType(String uniqueId) throws UnsupportedFeatureException {
5781
Request request = queue.restoreRequest(uniqueId);
5882
Feature feature = events.findFeatureByRequest(request);
83+
if (feature == null)
84+
throw new UnsupportedFeatureException();
5985
return feature.getConfirmationType();
6086
}
6187

88+
/**
89+
* Connect to a specific uri, provided a call back handler for connection related events.
90+
*
91+
* @param uri url and port of the remote system.
92+
* @param eventHandler call back handler for connection related events.
93+
*/
6294
public void open(String uri, SessionEvents eventHandler) {
6395
this.events = eventHandler;
6496
communicator.connect(uri, new CommunicatorEventHandler());
6597
}
6698

99+
/**
100+
* Close down the connection.
101+
*/
67102
public void close() {
68103
communicator.disconnect();
69104
}
@@ -83,6 +118,9 @@ public void onCallResult(String id, String payload) {
83118
String message = "Field %s violates constraints with value: \"%s\". %s";
84119
communicator.sendCallError(id, "TypeConstraintViolation", String.format(message, ex.getFieldKey(), ex.getFieldValue(), ex.getMessage()));
85120
ex.printStackTrace();
121+
} catch (UnsupportedFeatureException ex) {
122+
communicator.sendCallError(id, "InternalError", "An internal error occurred and the receiver was not able to process the requested Action successfully");
123+
ex.printStackTrace();
86124
} catch (Exception ex) {
87125
communicator.sendCallError(id, "FormationViolation", "Unable to process action");
88126
ex.printStackTrace();
@@ -123,13 +161,19 @@ public void onCall(String id, String action, String payload) {
123161
}
124162

125163
@Override
126-
public void onError(String id, String payload) { }
164+
public void onError(String id, String payload) {
165+
events.handleError(id);
166+
}
127167

128168
@Override
129-
public void onDisconnected() { }
169+
public void onDisconnected() {
170+
events.handleConnectionClosed();
171+
}
130172

131173
@Override
132-
public void onConnected() { }
174+
public void onConnected() {
175+
events.handleConnectionOpened();
176+
}
133177

134178
private CompletableFuture<Confirmation> handleIncomingRequest(Request request) {
135179
CompletableFuture<Confirmation> promise = new CompletableFuture<>();

0 commit comments

Comments
 (0)