Search code examples
javascriptnode.jsiotocpp

How to start remote transection in OCPP server using node js


We have created OCPP server using node js with help of ocpp-rpc and we got web socket connection from OCPP server here is the my sample code

const { RPCServer, createRPCError } = require('ocpp-rpc');

const server = new RPCServer({
  protocols: ['ocpp1.6'], // server accepts ocpp1.6 subprotocol
  strictMode: true,       // enable strict validation of requests & responses
 });

server.auth((accept, reject, handshake) => {
// accept the incoming client
accept({
    // anything passed to accept() will be attached as a 'session' property of the client.
    sessionId: 'XYZ123'
});
});

server.on('client', async (client) => {
console.log(`${client.session.sessionId} connected!`); // `XYZ123 connected!`

// create a specific handler for handling BootNotification requests
client.handle('BootNotification', ({params}) => {
    console.log(`Server got BootNotification from ${client.identity}:`, params);

    // respond to accept the client
    return {
        status: "Accepted",
        interval: 300,
        currentTime: new Date().toISOString()
    };
});

// create a specific handler for handling Heartbeat requests
client.handle('Heartbeat', ({params}) => {
    console.log(`Server got Heartbeat from ${client.identity}:`, params);

    // respond with the server's current time.
    return {
        currentTime: new Date().toISOString()
    };
});

// create a specific handler for handling StatusNotification requests
client.handle('StatusNotification', ({params}) => {
    console.log(`Server got StatusNotification from ${client.identity}:`, params);
    return {};
});

// create a wildcard handler to handle any RPC method
client.handle(({method, params}) => {
    // This handler will be called if the incoming method cannot be handled elsewhere.
    console.log(`Server got ${method} from ${client.identity}:`, params);

    // throw an RPC error to inform the server that we don't understand the request.
    throw createRPCError("NotImplemented");
});
});

await server.listen(3000);

now My question is how to start remote transaction from mobile app ? should i need to call my own OCPP server ? and how i will get data from the charging station point. as per our need we are adding node js OCPP server link to the ABB charging station config. please help if there any public repo or any blog or any sample code is available. Thanks in advance


Solution

  • To make an OCPP call to a charging station using ocpp-rpc, first you'll need a reference to the charging station's client connection. Adapting your example, here's one way you could set this up based on the charging station's OCPP identity:

    const allClients = new Map();
    
    server.on('client', async (client) => {
        console.log(`${client.identity} connected!`);
    
        allClients.set(client.identity, client); // store client reference
    });
    

    Next, you need a way to trigger and make your call. Let's assume for a moment that you are using a http framework like express to create a REST API for this purpose. You could route a request like so:

    app.post('/charging-station/:identity/start-transaction', async (req, res, next) => {
        try {
    
            const client = allClients.get(req.params.identity);
    
            if (!client) {
                throw Error("Client not found");
            }
    
            const response = await client.call('RemoteStartTransaction', {
                connectorId: 1, // start on connector 1
                idTag: 'XXXXXXXX', // using an idTag with identity 'XXXXXXXX'
            });
    
            if (response.status === 'Accepted') {
                console.log('Remote start worked!');
            } else {
                console.log('Remote start rejected.');
            }
    
        } catch (err) {
            next(err);
        }
    });
    

    Obviously if you're not using express, you'll need to adapt this to work for your situation.

    Hope that gives you a good starting point to go from.