mikuso / ocpp-rpc

A Node.js client & server implementation of the WAMP-like RPC-over-websocket system defined in the OCPP-J protocols.
MIT License
98 stars 29 forks source link

Fail to run README samples #52

Closed fulup-bzh closed 1 year ago

fulup-bzh commented 1 year ago

I'm trying to run bare server+client copied from the readme and miserably fail.

I probably mess-up somewhere removing the await that are refused by nodejs when not running a module. Here after the message from the server that show BootNotification is received and my client modified code.

Thank you for your support

server log

XYZ123 connected!
Server got BootNotification from EXAMPLE: { chargePointVendor: 'ocpp-rpc', chargePointModel: 'ocpp-rpc' }

client code

cli.connect()
    .catch(x => { console.log('fail to connect OCPP server'); process.exit(1) })
    .then(x => {
        console.log('connected to ocpp server')

        // send a BootNotification request and await the response
        const bootResponse = cli.call('BootNotification', {
            chargePointVendor: "ocpp-rpc",
            chargePointModel: "ocpp-rpc",
        })
            .catch(x => { console.log('fail BootNotification call'); process.exit(1) })
            .then(x => {
                console.log('Status ocpp:', bootResponse)

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

                    // send a Heartbeat request and await the response
                    const heartbeatResponse = cli.call('Heartbeat', {})
                        .catch(x => { console.log('fail Heartbeat call'); process.exit(1) })
                        .then(x => {

                            // read the current server time from the response
                            console.log('Server time is:', heartbeatResponse.currentTime);

                            // send a StatusNotification request for the controller
                            cli.call('StatusNotification', {
                                connectorId: 0,
                                errorCode: "NoError",
                                status: "Available",
                            });
                        })
                }
            })
    })
fulup-bzh commented 1 year ago

Some more insight. TCPdump trace between client and server.

13:12:58.163261 IP6 ::1.33314 > ::1.3000: Flags [F.], seq 13, ack 4, win 512, options [nop,nop,TS val 3070434695 ecr 3070427457], length 0
13:12:58.164498 IP6 ::1.3000 > ::1.33314: Flags [F.], seq 4, ack 14, win 512, options [nop,nop,TS val 3070434696 ecr 3070434695], length 0
13:12:58.164559 IP6 ::1.33314 > ::1.3000: Flags [.], ack 5, win 512, options [nop,nop,TS val 3070434696 ecr 3070434696], length 0
13:12:58.968464 IP6 ::1.40834 > ::1.3000: Flags [S], seq 1430421052, win 65476, options [mss 65476,sackOK,TS val 3070435500 ecr 0,nop,wscale 7], length 0
13:12:58.968482 IP6 ::1.3000 > ::1.40834: Flags [S.], seq 2887968591, ack 1430421053, win 65464, options [mss 65476,sackOK,TS val 3070435500 ecr 3070435500,nop,wscale 7], length 0
13:12:58.968490 IP6 ::1.40834 > ::1.3000: Flags [.], ack 1, win 512, options [nop,nop,TS val 3070435500 ecr 3070435500], length 0
13:12:58.968958 IP6 ::1.40834 > ::1.3000: Flags [P.], seq 1:300, ack 1, win 512, options [nop,nop,TS val 3070435501 ecr 3070435500], length 299
13:12:58.968967 IP6 ::1.3000 > ::1.40834: Flags [.], ack 300, win 510, options [nop,nop,TS val 3070435501 ecr 3070435501], length 0
13:12:58.969668 IP6 ::1.3000 > ::1.40834: Flags [P.], seq 1:195, ack 300, win 512, options [nop,nop,TS val 3070435502 ecr 3070435501], length 194
13:12:58.969676 IP6 ::1.40834 > ::1.3000: Flags [.], ack 195, win 511, options [nop,nop,TS val 3070435502 ecr 3070435502], length 0
13:12:58.972808 IP6 ::1.40834 > ::1.3000: Flags [P.], seq 300:430, ack 195, win 512, options [nop,nop,TS val 3070435505 ecr 3070435502], length 130
13:12:58.972820 IP6 ::1.3000 > ::1.40834: Flags [.], ack 430, win 511, options [nop,nop,TS val 3070435505 ecr 3070435505], length 0
13:12:58.973409 IP6 ::1.3000 > ::1.40834: Flags [P.], seq 195:317, ack 430, win 512, options [nop,nop,TS val 3070435505 ecr 3070435505], length 122
13:12:58.973419 IP6 ::1.40834 > ::1.3000: Flags [.], ack 317, win 512, options [nop,nop,TS val 3070435505 ecr 3070435505], length 0
mikuso commented 1 year ago

That's quite a promise chain you've got going there.

I think the problem you're having is that you're inspecting the Promise object itself, rather than the value that is being promised.

Try making these changes (where I've commented *CHANGE*):

cli.connect()
    .catch(x => { console.log('fail to connect OCPP server'); process.exit(1) })
    .then(x => {
        console.log('connected to ocpp server')

        // send a BootNotification request and await the response
        const bootResponse = cli.call('BootNotification', {
            chargePointVendor: "ocpp-rpc",
            chargePointModel: "ocpp-rpc",
        })
            .catch(x => { console.log('fail BootNotification call'); process.exit(1) })
            .then(x => {
                console.log('Status ocpp:', x) // *CHANGE* <-- Inspect x instead of the bootResponse Promise.

                // check that the server accepted the client
                if (x.status === 'Accepted') { // *CHANGE* <-- Compare x.status instead of the bootResponse.x which is undefined.

                    // send a Heartbeat request and await the response
                    const heartbeatResponse = cli.call('Heartbeat', {})
                        .catch(x => { console.log('fail Heartbeat call'); process.exit(1) })
                        .then(x => {

                            // read the current server time from the response
                            console.log('Server time is:', x.currentTime); // *CHANGE* <-- Inspect x.currentTime instead of the heartbeatResponse.x which is an undefined property of a Promise.

                            // send a StatusNotification request for the controller
                            cli.call('StatusNotification', {
                                connectorId: 0,
                                errorCode: "NoError",
                                status: "Available",
                            });
                        })
                }
            })
    })
fulup-bzh commented 1 year ago

Thank you for your proposition, it works :)

I improve a little bit your proposition my removing constant. It would be nice to have an example directory that run out of the box client+server sample. For long promises, it is not that I 'love' promissed but 'await' impose restriction that in the context of a simple standalone test.js file I was not able to handle.

cli.connect()
    .catch(x => { console.log('fail to connect OCPP server'); process.exit(1) })
    .then(x => {
        console.log('connected to ocpp server')

        // send a BootNotification request and await the response
        cli.call('BootNotification', {
            chargePointVendor: "ocpp-rpc",
            chargePointModel: "ocpp-rpc",
        })
            .catch(x => { console.log('fail BootNotification call'); process.exit(1) })
            .then(bootResponse => {
                console.log('Status ocpp:', bootResponse)

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

                    // send a Heartbeat request and await the response
                    cli.call('Heartbeat', {})
                        .catch(x => { console.log('fail Heartbeat call'); process.exit(1) })
                        .then(heartbeatResponse => {

                            // read the current server time from the response
                            console.log('Server time is:', heartbeatResponse.currentTime);

                            // send a StatusNotification request for the controller
                            cli.call('StatusNotification', {
                                connectorId: 0,
                                errorCode: "NoError",
                                status: "Available",
                            });
                        })
                }
            })
    })
mikuso commented 1 year ago

Thanks for the feedback.

I will consider adding some examples to a folder that are easier to run out of the box.