Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Getting TypeError: on_connect() missing 1 required positional argument: 'path'. V1.6 ocpp #708

Open
josalvmo opened this issue Feb 10, 2025 · 4 comments
Labels
bug Something isn't working

Comments

@josalvmo
Copy link

hi, I am getting this error when testing example code located on this repo for V1.6 (ocpp/examples
/v16/)

Any ideas how to fix?

INFO:websockets.server:server listening on 0.0.0.0:9000
INFO:root:Server Started listening to new connections...
INFO:websockets.server:connection open
ERROR:websockets.server:connection handler failed
Traceback (most recent call last):
File ".venv\Lib\site-packages\websockets\asyncio\server.py", line 374, in conn_handler
await self.handler(connection)
^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: on_connect() missing 1 required positional argument: 'path'
Traceback (most recent call last):
File "C:\Program Files\Python311\Lib\asyncio\runners.py", line 118, in run
return self._loop.run_until_complete(task)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Program Files\Python311\Lib\asyncio\base_events.py", line 653, in run_until_complete
return future.result()
^^^^^^^^^^^^^^^
File "c:\Repos\raspberry-pi\ocpp\ocpp_v16\ocpp_server1\central_system.py", line 71, in main
await server.wait_closed()
File ".venv\Lib\site-packages\websockets\asyncio\server.py", line 492, in wait_closed
await asyncio.shield(self.closed_waiter)
asyncio.exceptions.CancelledError

@josalvmo josalvmo added the bug Something isn't working label Feb 10, 2025
@a-alak
Copy link
Contributor

a-alak commented Feb 10, 2025

Can you show the code you wrote?

@josalvmo
Copy link
Author

josalvmo commented Feb 10, 2025

Can you show the code you wrote?

Is the same exact code of examples folder.

https://github.com/mobilityhouse/ocpp/tree/master/examples/v16

Ran central_system.py and charge_point.py on same system

Only had to change BootNotificationPayload to BootNotification on charge_point.py

@a-alak
Copy link
Contributor

a-alak commented Feb 11, 2025

The path argument to the connection handler is deprecated in the websockets library, so if you are using a later version, you should get the path as an attribute from the connection instead (see here).
This is a version that will work with later versions of websockets lib:

async def on_connect(websocket):
    """For every new charge point that connects, create a ChargePoint
    instance and start listening for messages.
    """
    try:
        requested_protocols = websocket.request_headers["Sec-WebSocket-Protocol"]
    except KeyError:
        logging.error("Client hasn't requested any Subprotocol. Closing Connection")
        return await websocket.close()
    if websocket.subprotocol:
        logging.info("Protocols Matched: %s", websocket.subprotocol)
    else:
        # In the websockets lib if no subprotocols are supported by the
        # client and the server, it proceeds without a subprotocol,
        # so we have to manually close the connection.
        logging.warning(
            "Protocols Mismatched | Expected Subprotocols: %s,"
            " but client supports  %s | Closing connection",
            websocket.available_subprotocols,
            requested_protocols,
        )
        return await websocket.close()

    charge_point_id = websocket.path.strip("/")
    cp = ChargePoint(charge_point_id, websocket)

    await cp.start()

There has actually been a lot of changes in the websockets library, so there might be other changes on the connection object. On my implementation I have not upgraded websockets yet, because I haven't had time to look through the changes.

@josalvmo
Copy link
Author

josalvmo commented Feb 11, 2025

Thanks!! I had to do another adjustments when checking headers.

Here working code:

async def on_connect(websocket: ServerConnection):
    """For every new charge point that connects, create a ChargePoint
    instance and start listening for messages.
    """
    try:
        requested_protocols = websocket.request.headers["Sec-WebSocket-Protocol"]
    except KeyError:
        logging.error("Client hasn't requested any Subprotocol. Closing Connection")
        return await websocket.close()
    
    if websocket.subprotocol:
        logging.info("Protocols Matched: %s", websocket.subprotocol)
    else:
        # In the websockets lib if no subprotocols are supported by the
        # client and the server, it proceeds without a subprotocol,
        # so we have to manually close the connection.
        logging.warning(
            "Protocols Mismatched | Expected Subprotocols: %s,"
            " but client supports  %s | Closing connection",
            websocket.available_subprotocols,
            requested_protocols,
        )
        return await websocket.close()

    charge_point_id = websocket.request.path.strip("/")
    cp = ChargePoint(charge_point_id, websocket)

    await cp.start()

proelke pushed a commit that referenced this issue Feb 13, 2025
### Changes included in this PR 

Updates to the example usage of the library (doc update).

### Current behavior

#708 and #709 describe how the current examples in the documentation
fail due to 1) they still use the "Payload" suffix on payload data
classes, this is not part of the latest version of this library 2)
breaking changes from the WebSockets library, primarily regarding the
path parameter passed to the connection handler and where to find the
request headers.

### New behavior

This PR updates the example code both for v16 and v201, so that the
payload dataclass is correct and is compatible with the latest
websockets library.

### Impact

Only changes to documentation. However the documentation currently
wouldn't support older versions of websockets library.

### Checklist

1. [x] Does your submission pass the existing tests?
2. [ ] Are there new tests that cover these additions/changes? 
3. [x] Have you linted your code locally before submission?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Development

No branches or pull requests

2 participants