All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.motown.ocpp.websocketjson.request.handler.StartTransactionFutureEventCallback Maven / Gradle / Ivy

/**
 * Copyright (C) 2013 Motown.IO ([email protected])
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package io.motown.ocpp.websocketjson.request.handler;

import com.google.gson.Gson;
import io.motown.domain.api.chargingstation.*;
import io.motown.domain.api.security.AddOnIdentity;
import io.motown.ocpp.viewmodel.domain.AuthorizationResult;
import io.motown.ocpp.viewmodel.domain.DomainService;
import io.motown.ocpp.viewmodel.domain.FutureEventCallback;
import io.motown.ocpp.viewmodel.persistence.entities.Transaction;
import io.motown.ocpp.websocketjson.schema.generated.v15.IdTagInfo__;
import io.motown.ocpp.websocketjson.schema.generated.v15.Starttransaction;
import io.motown.ocpp.websocketjson.schema.generated.v15.StarttransactionResponse;
import io.motown.ocpp.websocketjson.wamp.WampMessage;
import org.atmosphere.websocket.WebSocket;
import org.axonframework.domain.EventMessage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;

/**
 * Future event callback for start transaction. Part of the flow of handling a start transaction is authorizing the
 * identification that started the transaction. This class handles the {@code AuthorizationResult} and uses that
 * to construct the transaction.
 */
public class StartTransactionFutureEventCallback extends FutureEventCallback {

    private static final Logger LOG = LoggerFactory.getLogger(StartTransactionFutureEventCallback.class);

    private WebSocket webSocket;

    private String callId;

    private Gson gson;

    private ChargingStationId chargingStationId;

    private String protocolIdentifier;

    private Starttransaction request;

    private DomainService domainService;

    private AddOnIdentity addOnIdentity;

    public StartTransactionFutureEventCallback(String callId, WebSocket webSocket, Gson gson, ChargingStationId chargingStationId,
                                               String protocolIdentifier, Starttransaction request, DomainService domainService,
                                               AddOnIdentity addOnIdentity) {
        this.webSocket = webSocket;
        this.callId = callId;
        this.gson = gson;
        this.chargingStationId = chargingStationId;
        this.protocolIdentifier = protocolIdentifier;
        this.request = request;
        this.domainService = domainService;
        this.addOnIdentity = addOnIdentity;
    }

    /**
     * Handles the {@code AuthorizationResultEvent} (other events will directly result in 'false' return value). In the
     * flow of handling a start transaction message the first step is to authorize the identification used to start
     * the transaction. This method handles that event and uses the domain service to start the transaction and write
     * a response on the web socket.
     *
     * @param event Authorizaton result event.
     * @return true if the event has been handled, false if the event was the wrong type.
     */
    @Override
    public boolean onEvent(EventMessage event) {
        AuthorizationResultEvent resultEvent;

        if (!(event.getPayload() instanceof AuthorizationResultEvent)) {
            // not the right type of event... not 'handled'
            return false;
        }

        resultEvent = (AuthorizationResultEvent) event.getPayload();

        ReservationId reservationId = null;
        if (request.getReservationId() != null && request.getReservationId() != 0) {
            reservationId = new NumberedReservationId(chargingStationId, protocolIdentifier, request.getReservationId());
        }

        EvseId evseId = new EvseId(request.getConnectorId());
        Transaction transaction = domainService.createTransaction(evseId);
        NumberedTransactionId transactionId = new NumberedTransactionId(chargingStationId, protocolIdentifier, transaction.getId().intValue());

        domainService.startTransactionNoAuthorize(transactionId, evseId, chargingStationId, reservationId, addOnIdentity,
                new TextualToken(request.getIdTag()), request.getMeterStart(), request.getTimestamp());

        IdTagInfo__ idTagInfo = new IdTagInfo__();
        idTagInfo.setStatus(convert(resultEvent.getAuthenticationStatus()));

        StarttransactionResponse response = new StarttransactionResponse();
        response.setTransactionId(transactionId.getNumber());
        response.setIdTagInfo(idTagInfo);

        writeResult(response);

        return true;
    }

    private void writeResult(StarttransactionResponse result) {
        try {
            webSocket.write(new WampMessage(WampMessage.CALL_RESULT, callId, result).toJson(gson));
        } catch (IOException e) {
            LOG.error("IOException while writing to web socket.", e);
        }
    }

    /**
     * Converts a {@code AuthorizationResultStatus} to a {@code IdTagInfo__.Status}. Throws an assertion error is the
     * status is unknown.
     *
     * @param status status to convert.
     * @return converted status.
     */
    private static IdTagInfo__.Status convert(AuthorizationResultStatus status) {
        IdTagInfo__.Status result;

        switch (status) {
            case ACCEPTED:
                result = IdTagInfo__.Status.ACCEPTED;
                break;
            case BLOCKED:
                result = IdTagInfo__.Status.BLOCKED;
                break;
            case EXPIRED:
                result = IdTagInfo__.Status.EXPIRED;
                break;
            case INVALID:
                result = IdTagInfo__.Status.INVALID;
                break;
            default:
                throw new AssertionError("AuthorizationResultStatus has unknown status: " + status);
        }

        return result;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy