ShellRechargeSolutionsEU / ocpp

Open Charge Point Protocol
GNU General Public License v3.0
197 stars 63 forks source link

Exception processing OCPP request Heartbeat: #39

Open gianmarcoodorizzi opened 2 years ago

gianmarcoodorizzi commented 2 years ago

Hello,

I am new to Scala, so please, excuse me in advance if this sounds like a silly issue. I added this piece of code in the match case of ExampleServerTestApp.scala.

case req: HeartbeatReq => HeartbeatRes(currentTime = ZonedDateTime.now())

And I get this in response:

Received request HeartbeatReq from client ocppws [scala-execution-context-global-88] WARN com.thenewmotion.ocpp.json.api.CentralSystemOcpp1XConnectionComponent$CentralSystemOcpp1XConnection - Exception processing OCPP request Heartbeat: com.thenewmotion.ocpp.json.api.OcppException: com.thenewmotion.ocpp.json.PayloadErrorCode$NotImplemented$@72712127: Request type not implemented at com.thenewmotion.ocpp.json.api.OcppException$.apply(OcppConnectionComponent.scala:61) at com.thenewmotion.ocpp.json.example.ExampleServerTestApp$$anon$1.$anonfun$handleConnection$2(ExampleServerTestApp.scala:57) at com.thenewmotion.ocpp.json.api.RequestHandler$$anon$4.$anonfun$apply$4(RequestHandler.scala:68) at scala.concurrent.Future$.$anonfun$apply$1(Future.scala:659) at scala.util.Success.$anonfun$map$1(Try.scala:255) at scala.util.Success.map(Try.scala:213) at scala.concurrent.Future.$anonfun$map$1(Future.scala:292) at scala.concurrent.impl.Promise.liftedTree1$1(Promise.scala:33) at scala.concurrent.impl.Promise.$anonfun$transform$1(Promise.scala:33) at scala.concurrent.impl.CallbackRunnable.run(Promise.scala:64) at java.util.concurrent.ForkJoinTask$RunnableExecuteAction.exec(ForkJoinTask.java:1402) at java.util.concurrent.ForkJoinTask.doExec(ForkJoinTask.java:289) at java.util.concurrent.ForkJoinPool$WorkQueue.runTask(ForkJoinPool.java:1056) at java.util.concurrent.ForkJoinPool.runWorker(ForkJoinPool.java:1692) at java.util.concurrent.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:175) to a HeartBeat.Req [2,"1383653547","Heartbeat",{}]

Could you please tell me what is wrong with my code?

Thank you in advance.

azolotko commented 2 years ago

@gianmarcoodorizzi Did you add it before or after case _?

gianmarcoodorizzi commented 2 years ago

Hello, I added before it! Here is the complete code:

package com.thenewmotion.ocpp
package json
package example

import java.time.ZonedDateTime
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.duration._
import scala.io.Source
import messages.v1x._
import api._
import com.thenewmotion.ocpp.json.v1x.v16.HeartbeatReq

object ExampleServerTestApp extends App {

val server = new Ocpp1XJsonServer(2345, Version.V16) {

override def handleConnection(chargePointIdentity: String, conn: Ocpp1XJsonServer.OutgoingEndpoint): CentralSystemRequestHandler = {

      conn.onClose.foreach(_ => println(s"Disconnected client $chargePointIdentity"))

      {
        (req: CentralSystemReq) =>

          println(s"Received request $req from client $chargePointIdentity")

          req match {
            case req: BootNotificationReq =>
              // let's send a GetConfiguration to see what this charge point is up to
              conn.send(GetConfigurationReq(Nil)) foreach { res =>
                println(s"Charge point $chargePointIdentity responded to GetConfiguration: $res")
              }

              BootNotificationRes(
                status = RegistrationStatus.Accepted,
                currentTime = ZonedDateTime.now(),
                interval = 5.minutes
              )

            case req: HeartbeatReq =>
              HeartbeatRes(currentTime = ZonedDateTime.now())

            case _ =>
              throw OcppException(PayloadErrorCode.NotImplemented, "Request type not implemented")
          }
      }
    }
  }

  server.start()

  println("Example server listening at port 2345. Press ENTER to exit.")
  Source.stdin.bufferedReader.readLine()

  server.stop()
}

Thank you

azolotko commented 2 years ago

@gianmarcoodorizzi The problem is in import com.thenewmotion.ocpp.json.v1x.v16.HeartbeatReq. Just remove it and it should be fine because there's import messages.v1x._ already.

gianmarcoodorizzi commented 2 years ago

@azolotko thanks, I added that (well, IntelliJ IDEA added it) because without it it would not even compile. Indeeed I get the error:

ExampleServerTestApp.scala:38:23 not found: type HeartbeatReq case req: HeartbeatReq =>

azolotko commented 2 years ago

@gianmarcoodorizzi Ok, now I see where the problem comes from. HeartbeatReq is defined as object. This means that if you want to match it, you should either do

case req: HeartbeatReq.type =>
  // ...

or

case HeartbeatReq =>
  // ...

The comment about import com.thenewmotion.ocpp.json.v1x.v16.HeartbeatReq still applies: it's unnecessary.

gianmarcoodorizzi commented 2 years ago

Thank you very much!