-
Notifications
You must be signed in to change notification settings - Fork 339
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
Comments
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 |
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). 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. |
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() |
### 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?
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
The text was updated successfully, but these errors were encountered: