@@ -41,9 +41,9 @@ of this software and associated documentation files (the "Software"), to deal
4141public abstract class Communicator {
4242 private static final Logger logger = LoggerFactory .getLogger (Communicator .class );
4343
44+ private final ArrayDeque <Object > transactionQueue ;
4445 private RetryRunner retryRunner ;
4546 protected Radio radio ;
46- private ArrayDeque <Object > transactionQueue ;
4747 private CommunicatorEvents events ;
4848 private boolean failedFlag ;
4949
@@ -97,6 +97,27 @@ public abstract class Communicator {
9797 protected abstract Object makeCallError (
9898 String uniqueId , String action , String errorCode , String errorDescription );
9999
100+ /**
101+ * Create a call result error envelope to transmit.
102+ *
103+ * @param uniqueId the id the receiver expects.
104+ * @param errorCode an OCPP error code.
105+ * @param errorDescription an associated error description.
106+ * @return a fully packed message ready to send.
107+ */
108+ protected abstract Object makeCallResultError (
109+ String uniqueId , String action , String errorCode , String errorDescription );
110+
111+ /**
112+ * Create a send envelope to transmit to the server.
113+ *
114+ * @param uniqueId the id of the message.
115+ * @param action action name of the feature.
116+ * @param payload packed payload.
117+ * @return a fully packed message ready to send.
118+ */
119+ protected abstract Object makeSend (String uniqueId , String action , Object payload );
120+
100121 /**
101122 * Identify an incoming call and parse it into one of the following: {@link CallMessage} a
102123 * request. {@link CallResultMessage} a response.
@@ -177,7 +198,7 @@ public synchronized void sendCall(String uniqueId, String action, Request reques
177198 }
178199 } else if (request .transactionRelated ()
179200 && transactionQueue != null
180- && transactionQueue .size () > 0 ) {
201+ && ! transactionQueue .isEmpty () ) {
181202 transactionQueue .add (call );
182203 processTransactionQueue ();
183204 } else {
@@ -216,7 +237,7 @@ public void sendCallResult(String uniqueId, String action, Confirmation confirma
216237 events .onError (
217238 uniqueId ,
218239 "ConfirmationCompletedHandlerFailed" ,
219- "The confirmation completed callback handler failed with exception " + e . toString () ,
240+ "The confirmation completed callback handler failed with exception " + e ,
220241 confirmation );
221242 }
222243 }
@@ -240,7 +261,7 @@ public void sendCallResult(String uniqueId, String action, Confirmation confirma
240261 public void sendCallError (
241262 String uniqueId , String action , String errorCode , String errorDescription ) {
242263 logger .error (
243- "An error occurred. Sending this information: uniqueId {}: action: {}, errorCore : {}, errorDescription: {}" ,
264+ "An error occurred. Sending this information: uniqueId {}: action: {}, errorCode : {}, errorDescription: {}" ,
244265 uniqueId ,
245266 action ,
246267 errorCode ,
@@ -257,6 +278,65 @@ public void sendCallError(
257278 }
258279 }
259280
281+ /**
282+ * Send a call result error. If offline, the message is thrown away.
283+ *
284+ * @param uniqueId the id the receiver expects a response to.
285+ * @param errorCode an OCPP error Code
286+ * @param errorDescription a associated error description.
287+ */
288+ public void sendCallResultError (
289+ String uniqueId , String action , String errorCode , String errorDescription ) {
290+ logger .error (
291+ "An error occurred while processing a call result. Sending this information: "
292+ + "uniqueId {}: action: {}, errorCode: {}, errorDescription: {}" ,
293+ uniqueId ,
294+ action ,
295+ errorCode ,
296+ errorDescription );
297+ try {
298+ radio .send (makeCallResultError (uniqueId , action , errorCode , errorDescription ));
299+ } catch (NotConnectedException ex ) {
300+ logger .warn ("sendCallResultError() failed" , ex );
301+ events .onError (
302+ uniqueId ,
303+ "Not connected" ,
304+ "The call result error couldn't be sent due to the lack of connection" ,
305+ errorCode );
306+ }
307+ }
308+
309+ /**
310+ * Send a {@link Request} which has no confirmation.
311+ *
312+ * @param uniqueId the id of the {@link Request}.
313+ * @param action action name of the {@link eu.chargetime.ocpp.feature.Feature}.
314+ * @param request the outgoing {@link Request}
315+ */
316+ public synchronized void send (String uniqueId , String action , Request request ) {
317+ Object call = makeSend (uniqueId , action , packPayload (request ));
318+
319+ try {
320+ if (radio .isClosed ()) {
321+ logger .warn ("Not connected: can't send request: {}" , request );
322+ events .onError (
323+ uniqueId ,
324+ "Not connected" ,
325+ "The request can't be sent due to the lack of connection" ,
326+ request );
327+ } else {
328+ radio .send (call );
329+ }
330+ } catch (NotConnectedException ex ) {
331+ logger .warn ("sendCall() failed: not connected" );
332+ events .onError (
333+ uniqueId ,
334+ "Not connected" ,
335+ "The request can't be sent due to the lack of connection" ,
336+ request );
337+ }
338+ }
339+
260340 /** Close down the connection. Uses the {@link Transmitter}. */
261341 public void disconnect () {
262342 radio .disconnect ();
@@ -289,6 +369,10 @@ public void receivedMessage(Object input) {
289369 Message message = parse (input );
290370 if (message instanceof CallResultMessage ) {
291371 events .onCallResult (message .getId (), message .getAction (), message .getPayload ());
372+ } else if (message instanceof CallResultErrorMessage ) {
373+ CallResultErrorMessage call = (CallResultErrorMessage ) message ;
374+ events .onCallResultError (
375+ call .getId (), call .getErrorCode (), call .getErrorDescription (), call .getRawPayload ());
292376 } else if (message instanceof CallErrorMessage ) {
293377 failedFlag = true ;
294378 CallErrorMessage call = (CallErrorMessage ) message ;
@@ -297,6 +381,9 @@ public void receivedMessage(Object input) {
297381 } else if (message instanceof CallMessage ) {
298382 CallMessage call = (CallMessage ) message ;
299383 events .onCall (call .getId (), call .getAction (), call .getPayload ());
384+ } else if (message instanceof SendMessage ) {
385+ SendMessage send = (SendMessage ) message ;
386+ events .onSend (send .getId (), send .getAction (), send .getPayload ());
300387 }
301388 }
302389
@@ -318,7 +405,7 @@ private Object getRetryMessage() {
318405 }
319406
320407 /**
321- * Check if a error message was received.
408+ * Check if an error message was received.
322409 *
323410 * @return whether a fail flag has been raised.
324411 */
0 commit comments