Replies: 2 comments 1 reply
-
I am not sure how to share the code. That's why I just copied it in now import logging
import websockets
import asyncio
import threading
from MyChargePoint import MyChargePoint
class Server(threading.Thread):
def __init__(self,serverAddress,port):
threading.Thread.__init__(self)
self.ws = None
self.clients = []
self._stop_event = threading.Event()
self.server_address = serverAddress
self.port = port
def stop(self):
if len(self.clients) != 0:
# testcase with one client --> [0]
client = self.clients[0]
client.isRunning = False
self.clients.remove(client)
self.ws.close()
def run(self):
asyncio.run(self.init_ws())
# init websocket
async def init_ws(self):
self.ws = await websockets.serve(
self.on_connect,self.server_address, self.port, subprotocols=["ocpp1.6"])
logging.info("Server Started listening to new connections...")
await self.ws.wait_closed()
# handle connect requests
async def on_connect(self,websocket,path):
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 = path.strip("/")
logging.info("connected to Wallbox: %s",charge_point_id)
cp = MyChargePoint(charge_point_id, websocket)
self.clients.append(cp)
await cp.start()
# functions to send msg. to the client
def send_message(self,id,command):
asyncio.run(self.clients[0].setChargingProfil()) |
Beta Was this translation helpful? Give feedback.
1 reply
-
I think the |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hello together
I am currently trying to implement an Ocpp Central System in Python. I was already able to connect and communicate with an wallbox using the library.
However, I have the problem that I do not know how to shut down the server properly. Does anyone have an example?
I am dealing with websockets in python for the first time and can't find a way that works with the ocpp library. Additionally it should work on windows as well.
Beta Was this translation helpful? Give feedback.
All reactions