Core API - motown-io/motown GitHub Wiki

Functional Overview

The Motown Core is the heart of the system which keeps track of the state of the aggregates. As the Core has been built on the Axon Framework, the Core-API enables the communication with the Core through Commands and Events. This API will primarily be used by the (new to develop) add-ons in the Motown ecosystem.

The available commands to send, and events to listen for, are located in the package io.motown.domain.api.chargingstation located in the core-api project.

Sending Commands to the Core

In order to send commands to the Core, an Axon CommandBus and CommandGatewayFactory are necessary.

Sample configuration:

    <axon:command-bus id="commandBus">
        <axon:handlerInterceptors>
            <bean class="io.motown.domain.utils.axon.CorrelationCommandHandlerInterceptor"/>
            <bean class="org.axonframework.commandhandling.interceptors.LoggingInterceptor"/>
        </axon:handlerInterceptors>
    </axon:command-bus>

    <bean id="domainCommandGateway" class="org.axonframework.commandhandling.gateway.CommandGatewayFactoryBean">
        <property name="commandBus" ref="commandBus"/>
        <!-- Interface which defines the specific commands you want to be able to send from the add on -->
        <property name="gatewayInterface" value="io.motown.ocpp.viewmodel.domain.DomainCommandGateway"/>
    </bean>

Next the defined CommandGatewayFactory can be used to send the commands. For example, to request a charging station to unlock an EVSE:

commandGateway.send(new RequestUnlockEvseCommand(new ChargingStationId("CS-001"), new EvseId(1), identityContext), new CorrelationToken());

More information regarding commands, command buses and command gateways can be found in Axon's documentation.

Listening to Events that are being published

Listening for relevant events allows add-ons to keep track of any state changes in the aggregates, and update their own persistency layer.

You can listen for events by adding an Axon @EventHandler annotation on the method that will handle the event.

Sample code snippet of an event handling method:

    @EventHandler
    public void handle(ChargingStationCreatedEvent event) {
        LOG.debug("ChargingStationCreatedEvent creates [{}] in operator api repo", event.getChargingStationId());
        ChargingStation station = new ChargingStation(event.getChargingStationId().getId());
        repository.createOrUpdate(station);
    }

For the @EventHandler annotation to work, it requires the following entry to be present in the Spring context xml:

    <axon:annotation-config/>
⚠️ **GitHub.com Fallback** ⚠️