22
33import 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+ */
3141public 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 }
0 commit comments