@@ -9,6 +9,7 @@ import javax.net.ssl.SSLContext
99import scala .language .higherKinds
1010import scala .concurrent .{Await , ExecutionContext , Future , Promise }
1111import scala .concurrent .duration .DurationInt
12+ import scala .util .{Failure , Success }
1213import com .thenewmotion .ocpp .{Version , Version1X , VersionFamily }
1314import com .thenewmotion .ocpp .json .api ._
1415import com .thenewmotion .ocpp .messages .{ReqRes , Request , Response }
@@ -17,6 +18,7 @@ import com.thenewmotion.ocpp.messages.v20._
1718import com .typesafe .scalalogging .Logger
1819import org .slf4j .LoggerFactory
1920
21+
2022trait OcppTest [VFam <: VersionFamily ] {
2123
2224 // these type variables are filled in differently for OCPP 1.X and OCPP 2.0
@@ -240,6 +242,32 @@ trait DocileConnection[
240242 ocppClient = Some (connection)
241243 }
242244
245+ def sendRequestAndManageResponse [REQ <: OutgoingReqBound ](req : REQ )(
246+ implicit reqRes : OutgoingReqRes [REQ , _ <: IncomingResBound ],
247+ executionContext : ExecutionContext
248+ ): Unit =
249+ ocppClient match {
250+ case None =>
251+ throw ExpectationFailed (" Trying to send an OCPP message while not connected" )
252+ case Some (client) =>
253+ outgoingLogger.info(s " $req" )
254+ client.send(req)(reqRes) onComplete {
255+ case Success (res) =>
256+ incomingLogger.info(s " $res" )
257+ receivedMsgManager.enqueue(
258+ GenericIncomingMessage [OutgoingReqBound , IncomingResBound , OutgoingReqRes , IncomingReqBound , OutgoingResBound , IncomingReqRes ](res)
259+ )
260+ case Failure (OcppException (ocppError)) =>
261+ incomingLogger.info(s " $ocppError" )
262+ receivedMsgManager.enqueue(
263+ GenericIncomingMessage [OutgoingReqBound , IncomingResBound , OutgoingReqRes , IncomingReqBound , OutgoingResBound , IncomingReqRes ](ocppError)
264+ )
265+ case Failure (e) =>
266+ connectionLogger.error(s " Failed to get response to outgoing OCPP request $req: ${e.getMessage}\n\t ${e.getStackTrace.mkString(" \n\t " )}" )
267+ throw ExecutionError (e)
268+ }
269+ }
270+
243271 /** Template method to be implemented by version-specific extending classes to establish a connection for that
244272 * version of OCPP */
245273 protected def createClient (
0 commit comments