Skip to content

Commit 3998455

Browse files
committed
Started adding java docs.
1 parent 233c077 commit 3998455

File tree

3 files changed

+180
-5
lines changed

3 files changed

+180
-5
lines changed

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

Lines changed: 70 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@
88
import java.util.ArrayList;
99
import java.util.HashMap;
1010
import java.util.concurrent.CompletableFuture;
11-
12-
/**
11+
/*
1312
ChargeTime.eu - Java-OCA-OCPP
1413
Copyright (C) 2015-2016 Thomas Volden <[email protected]>
1514
@@ -35,24 +34,51 @@ of this software and associated documentation files (the "Software"), to deal
3534
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
3635
SOFTWARE.
3736
*/
37+
38+
/**
39+
* Abstract class.
40+
* Handles basic client logic:
41+
* Hold s list of supported features.
42+
* Keeps track of outgoing request.
43+
* Calls back when a confirmation is received.
44+
* <p>
45+
* Must be overloaded in order to support specific protocols and formats.
46+
*/
3847
public abstract class Client
3948
{
4049
private HashMap<String, CompletableFuture<Confirmation>> promises;
4150
private ArrayList<Feature> featureList;
4251
private Session session;
4352

53+
/**
54+
* Constructor
55+
*
56+
* @param session Inject session object
57+
* @see Session
58+
*/
4459
public Client(Session session) {
4560
this.promises = new HashMap<>();
4661
this.featureList = new ArrayList<>();
4762
this.session = session;
4863
}
4964

65+
/**
66+
* Add {@link Profile} to support a group of features.
67+
*
68+
* @param profile supported feature {@link Profile}
69+
* @see Profile
70+
*/
5071
public void addFeatureProfile(Profile profile) {
5172
Feature[] features = profile.getFeatureList();
5273
for (Feature feature: features)
5374
featureList.add(feature);
5475
}
5576

77+
/**
78+
* Connect to server
79+
*
80+
* @param uri url and port of the server
81+
*/
5682
public void connect(String uri)
5783
{
5884
session.open(uri, new SessionEvents() {
@@ -79,6 +105,9 @@ public Confirmation handleRequest(Request request) {
79105
});
80106
}
81107

108+
/**
109+
* Disconnect from server
110+
*/
82111
public void disconnect()
83112
{
84113
try {
@@ -88,6 +117,18 @@ public void disconnect()
88117
}
89118
}
90119

120+
/**
121+
* Search for supported features added with the addProfile.
122+
* If no supported feature is found, null is returned
123+
*
124+
* Can take multiple inputs:
125+
* {@link String}, search for the action name of the feature.
126+
* {@link Request}/{@link Confirmation}, search for a feature that matches.
127+
* Anything else will return null.
128+
*
129+
* @param needle Object supports {@link String}, {@link Request} or {@link Confirmation}
130+
* @return Instance of the supported Feature
131+
*/
91132
private Feature findFeature(Object needle) {
92133
Feature output = null;
93134

@@ -101,6 +142,18 @@ private Feature findFeature(Object needle) {
101142
return output;
102143
}
103144

145+
/**
146+
* Tries to match {@link Feature} with an {@link Object}.
147+
* Different outcome based on the type:
148+
* {@link String}, matches the action name of the feature.
149+
* {@link Request}, matches the request type of the feature.
150+
* {@link Confirmation}, matches the confirmation type of the feature.
151+
* Other wise returns false.
152+
*
153+
* @param feature to match
154+
* @param object to match with, supports {@link String}, {@link Request} or {@link Confirmation}
155+
* @return true if the {@link Feature} matches the {@link Object}
156+
*/
104157
private boolean featureContains(Feature feature, Object object) {
105158
boolean contains = false;
106159
contains |= object instanceof String && feature.getAction().equals(object);
@@ -109,6 +162,15 @@ private boolean featureContains(Feature feature, Object object) {
109162
return contains;
110163
}
111164

165+
/**
166+
* Send a {@link Request} to the server.
167+
* Can only send {@link Request} that the client supports, see {@link #addFeatureProfile(Profile)}
168+
*
169+
* @param request outgoing request
170+
* @return call back object, will be fulfilled with confirmation when received
171+
* @throws UnsupportedFeatureException trying to send a request from an unsupported feature
172+
* @see CompletableFuture
173+
*/
112174
public CompletableFuture<Confirmation> send(Request request) throws UnsupportedFeatureException {
113175
Feature feature = findFeature(request);
114176
if (feature == null)
@@ -119,6 +181,12 @@ public CompletableFuture<Confirmation> send(Request request) throws UnsupportedF
119181
return promise;
120182
}
121183

184+
/**
185+
* Creates call back {@link CompletableFuture} for later use
186+
*
187+
* @param uniqueId identification for the {@link Request}
188+
* @return call back {@link CompletableFuture}
189+
*/
122190
private CompletableFuture<Confirmation> createPromise(String uniqueId) {
123191
CompletableFuture<Confirmation> promise = new CompletableFuture<>();
124192
promises.put(uniqueId, promise);

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

Lines changed: 94 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import eu.chargetime.ocpp.model.*;
44

5-
/**
5+
/*
66
ChargeTime.eu - Java-OCA-OCPP
77
Copyright (C) 2015-2016 Thomas Volden <[email protected]>
88
@@ -28,22 +28,91 @@ of this software and associated documentation files (the "Software"), to deal
2828
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2929
SOFTWARE.
3030
*/
31+
32+
/**
33+
* Abstract class.
34+
* Handles basic communication:
35+
* Pack and send messages.
36+
* Receive and unpack messages.
37+
* <p>
38+
* Requires a {@link Transmitter} to send and receive messages.
39+
* Must be overloaded to implement a specific format.
40+
*/
3141
public abstract class Communicator {
3242
protected Transmitter transmitter;
3343

44+
/**
45+
* Convert a formatted string into a {@link Request}/{@link Confirmation}.
46+
* This is useful for call results, where the confirmation type isn't given.
47+
*
48+
* @param payload the raw formatted payload.
49+
* @param type the expected return type.
50+
* @return the unpacked payload.
51+
* @throws Exception error occurred while converting.
52+
*/
3453
public abstract <T> T unpackPayload(String payload, Class<T> type) throws Exception;
54+
55+
/**
56+
* Convert a {@link Request}/{@link Confirmation} into a formatted string.
57+
*
58+
* @param payload the payload model.
59+
* @return the payload in the form of a formatted string.
60+
*/
3561
public abstract String packPayload(Object payload);
62+
63+
/**
64+
* Create a call result envelope to transmit to the server.
65+
*
66+
* @param uniqueId the id the server expects.
67+
* @param payload packed payload.
68+
* @return a fully packed message ready to send.
69+
*/
3670
protected abstract String makeCallResult(String uniqueId, String payload);
71+
72+
/**
73+
* Create a call envelope to transmit to the server.
74+
*
75+
* @param uniqueId the id the server must reply with.
76+
* @param action action name of the feature.
77+
* @param payload packed payload.
78+
* @return a fully packed message ready to send.
79+
*/
3780
protected abstract String makeCall(String uniqueId, String action, String payload);
38-
protected abstract String makeCallError(String uniqueId, String errorCode, String errorDescription);
3981

82+
/**
83+
* Create a call error envelope to transmit to the server.
84+
*
85+
* @param uniqueId the id the server expects.
86+
* @param errorCode an OCPP error code.
87+
* @param errorDescription an associated error description.
88+
* @return a fully packed message ready to send.
89+
*/
90+
protected abstract String makeCallError(String uniqueId, String errorCode, String errorDescription);
4091

92+
/**
93+
* 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.
96+
*
97+
* @param message raw message from server
98+
* @return CallMessage or {@link CallResultMessage}
99+
*/
41100
protected abstract Message parse(String message);
42101

102+
/**
103+
* Constructore
104+
*
105+
* @param transmitter Injected {@link Transmitter}
106+
*/
43107
public Communicator(Transmitter transmitter) {
44108
this.transmitter = transmitter;
45109
}
46110

111+
/**
112+
* Use the injected {@link Transmitter} to connect to server.
113+
* @param uri the url and port of the server.
114+
* @param events handler for call back events.
115+
*/
47116
public void connect(String uri, CommunicatorEvents events) {
48117
transmitter.connect(uri, new TransmitterEvents() {
49118
@Override
@@ -70,18 +139,41 @@ public void disconnected() {
70139
});
71140
}
72141

142+
/**
143+
* Send a new {@link Request} to the server.
144+
*
145+
* @param uniqueId the id the server should use to reply.
146+
* @param action action name of the {@link eu.chargetime.ocpp.feature.Feature}.
147+
* @param request the outgoing {@link Request}
148+
*/
73149
public void sendCall(String uniqueId, String action, Request request) {
74150
transmitter.send(makeCall(uniqueId, action, packPayload(request)));
75151
}
76152

153+
/**
154+
* Send a {@link Confirmation} reply to a server {@link Request}.
155+
*
156+
* @param uniqueId the id the server expects.
157+
* @param confirmation the outgoing {@link Confirmation}
158+
*/
77159
public void sendCallResult(String uniqueId, Confirmation confirmation) {
78160
transmitter.send(makeCallResult(uniqueId, packPayload(confirmation)));
79161
}
80162

163+
/**
164+
* Send an error to the server.
165+
*
166+
* @param uniqueId the id the server expects a response to.
167+
* @param errorCode an OCPP error Code
168+
* @param errorDescription a associated error description.
169+
*/
81170
public void sendCallError(String uniqueId, String errorCode, String errorDescription) {
82171
transmitter.send(makeCallError(uniqueId, errorCode, errorDescription));
83172
}
84173

174+
/**
175+
* Disconnect from the server. Uses the {@link Transmitter}.
176+
*/
85177
public void disconnect() {
86178
transmitter.disconnect();
87179
}

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

Lines changed: 16 additions & 1 deletion
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,10 +26,25 @@ 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+
* Call back handler for communicator events.
32+
*/
2933
public interface CommunicatorEvents {
34+
/**
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.
37+
*
38+
* @param id unique id used to identify the original request.
39+
* @param payload raw payload.
40+
*/
3041
void onCallResult(String id, String payload);
42+
3143
void onCall(String id, String action, String payload);
44+
3245
void onError(String id, String payload);
46+
3347
void onDisconnected();
48+
3449
void onConnected();
3550
}

0 commit comments

Comments
 (0)