- if delay:
- await asyncio.sleep(delay)
-
- if period:
- my_timer = RepeatTimer(
- period, asyncio.create_task, [cp.send_ocpp_command(command_name)]
- )
- my_timer.start()
+ async def send_command(
+ self, command_name: Action, delay: Optional[float], period: Optional[float]
+ ):
+ try:
+ if delay and not self._command_timer:
+ self._command_timer = Timer(
+ delay,
+ False,
+ self._send_command,
+ [command_name],
+ )
+ if period and not self._command_timer:
+ self._command_timer = Timer(
+ period,
+ True,
+ self._send_command,
+ [command_name],
+ )
+ except ConnectionClosed:
+ self.handle_connection_closed()
+
+ def handle_connection_closed(self):
+ logging.info("ChargePoint %s closed connection", self.id)
+ if self._command_timer:
+ self._command_timer.cancel()
+ ChargePoints.remove(self)
+ logging.debug("Connected ChargePoint(s): %d", len(ChargePoints))