Giter VIP home page Giter VIP logo

Comments (2)

mikuso avatar mikuso commented on July 22, 2024

Sorry I've not been able to reproduce the error. Perhaps you can share your code?

I have just tested the following using Node.js v18.9.0

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

async function main() {

    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);


    const cli = new RPCClient({
        endpoint: 'ws://localhost:3000', // the OCPP endpoint URL
        identity: 'EXAMPLE',             // the OCPP identity
        protocols: ['ocpp1.6'],          // client understands ocpp1.6 subprotocol
        strictMode: true,                // enable strict validation of requests & responses
    });

    // connect to the OCPP server
    await cli.connect();

    // send a BootNotification request and await the response
    const bootResponse = await cli.call('BootNotification', {
        chargePointVendor: "ocpp-rpc",
        chargePointModel: "ocpp-rpc",
    });

    // check that the server accepted the client
    if (bootResponse.status === 'Accepted') {

        // send a Heartbeat request and await the response
        const heartbeatResponse = await cli.call('Heartbeat', {});
        // read the current server time from the response
        console.log('Server time is:', heartbeatResponse.currentTime);

        // send a StatusNotification request for the controller
        await cli.call('StatusNotification', {
            connectorId: 0,
            errorCode: "NoError",
            status: "Available",
        });
    }
}
main().catch(console.error);

from ocpp-rpc.

mikuso avatar mikuso commented on July 22, 2024

Closing due to inactivity.

from ocpp-rpc.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.