ts-ocpp icon indicating copy to clipboard operation
ts-ocpp copied to clipboard

No BootNotification in examples

Open iwo-strzebonski opened this issue 2 years ago • 3 comments

As the title says - there's no BootNotification example for the server nor the client, and the BootNotification is most important message, as it starts whole communication between the server and the charger.

iwo-strzebonski avatar Jul 25 '22 09:07 iwo-strzebonski

Have you figured it out by any chance?

cisc0disco avatar Feb 07 '24 13:02 cisc0disco

@cisc0disco I think I did?

I'll check in the evening

iwo-strzebonski avatar Feb 13 '24 13:02 iwo-strzebonski

@cisc0disco sorry for my late response haha

This is my BootNotification handler:

import { BootNotificationRequest } from '@voltbras/ts-ocpp/dist/messages/json/request'
import { BootNotificationResponse } from '@voltbras/ts-ocpp/dist/messages/json/response'

export default async function bootNotificationHandler (
  chargePointId: string,
  data: BootNotificationRequest
): Promise<BootNotificationResponse> => {
  const response: BootNotificationResponse = {
    status: 'Accepted',
    currentTime: new Date().toISOString(),
    interval: 30
  }

  try {
    const charger = await Charger.init(chargePointId, data)

    if (charger instanceof Error) {
      response.status = 'Rejected'
      return response
    }
  } catch (err) {
    console.error('Error on bootNotificationHandler', err)
    response.status = 'Rejected'
    return response
  }

  return response
}

The Charger.init() method is used to initialize a charger in the database - it can be safely removed.

And this is my IncomingMessage handler:

import { OCPPRequestError, RequestMetadata } from '@voltbras/ts-ocpp'
import { ChargePointAction, ChargePointRequest } from '@voltbras/ts-ocpp/dist/messages/cp'
import { CentralSystemAction } from '@voltbras/ts-ocpp/dist/messages/cs'
import { OCPPVersionV16 } from '@voltbras/ts-ocpp/dist/types'

export default async function handleResponseActions (
  req: ChargePointRequest<OCPPVersionV16, ChargePointAction>,
  metadata: RequestMetadata
): Promise<{ [key: string]: unknown } | OCPPRequestError | Error> => {
  const { action, ocppVersion } = req
  const { chargePointId } = metadata
  let response

  const charger = await Charger.find(chargePointId)

  if (charger === null && action !== 'BootNotification') {
    setTimeout(() => forceBootNotification(ocppVersion, chargePointId), 1000)

    return new OCPPRequestError(`Charger ${chargePointId} not initialized. Please try again in a few seconds.`)
  }

  await Charger.changeState(chargePointId, 'Available')

  switch (req.action) {
    case 'BootNotification':
      response = await cp.bootNotificationHandler(chargePointId, req)
      break

    // other actions go here...

    default:
      return new OCPPRequestError(`Unknown action: ${req.action}`)
  }

  if (response instanceof Error) {
    console.error(`Error on action ${action}`, response)
    return response
  }

  return {
    action,
    ocppVersion,
    ...response
  }
}

iwo-strzebonski avatar Feb 23 '24 09:02 iwo-strzebonski