Skip to content

Commit

Permalink
feat: Added optional logger param to ChargePoint class
Browse files Browse the repository at this point in the history
  • Loading branch information
jainmohit2001 committed Oct 1, 2024
1 parent d59ecf1 commit cb6bed5
Showing 1 changed file with 13 additions and 8 deletions.
21 changes: 13 additions & 8 deletions ocpp/charge_point.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ class ChargePoint:
initiated and received by the Central System
"""

def __init__(self, id, connection, response_timeout=30):
def __init__(self, id, connection, response_timeout=30, logger=LOGGER):
"""
Args:
Expand All @@ -206,6 +206,8 @@ def __init__(self, id, connection, response_timeout=30):
connection: Connection to CP.
response_timeout (int): When no response on a request is received
within this interval, a asyncio.TimeoutError is raised.
logger: Optional Logger instance used for logging.
By default, the 'ocpp' logger is used.
"""
self.id = id
Expand Down Expand Up @@ -234,10 +236,13 @@ def __init__(self, id, connection, response_timeout=30):
# for testing purposes to have predictable unique ids.
self._unique_id_generator = uuid.uuid4

# The logger used to log messages
self.logger = logger

async def start(self):
while True:
message = await self._connection.recv()
LOGGER.info("%s: receive message %s", self.id, message)
self.logger.info("%s: receive message %s", self.id, message)

await self.route_message(message)

Expand All @@ -252,7 +257,7 @@ async def route_message(self, raw_msg):
try:
msg = unpack(raw_msg)
except OCPPError as e:
LOGGER.exception(
self.logger.exception(
"Unable to parse message: '%s', it doesn't seem "
"to be valid OCPP: %s",
raw_msg,
Expand All @@ -264,7 +269,7 @@ async def route_message(self, raw_msg):
try:
await self._handle_call(msg)
except OCPPError as error:
LOGGER.exception("Error while handling request '%s'", msg)
self.logger.exception("Error while handling request '%s'", msg)
response = msg.create_call_error(error).to_json()
await self._send(response)

Expand Down Expand Up @@ -315,7 +320,7 @@ async def _handle_call(self, msg):
if inspect.isawaitable(response):
response = await response
except Exception as e:
LOGGER.exception("Error while handling request '%s'", msg)
self.logger.exception("Error while handling request '%s'", msg)
response = msg.create_call_error(e).to_json()
await self._send(response)

Expand Down Expand Up @@ -423,7 +428,7 @@ async def call(
)

if response.message_type_id == MessageType.CallError:
LOGGER.warning("Received a CALLError: %s'", response)
self.logger.warning("Received a CALLError: %s'", response)
if suppress:
return
raise response.to_exception()
Expand Down Expand Up @@ -454,7 +459,7 @@ async def _get_specific_response(self, unique_id, timeout):
if response.unique_id == unique_id:
return response

LOGGER.error("Ignoring response with unknown unique id: %s", response)
self.logger.error("Ignoring response with unknown unique id: %s", response)
timeout_left = wait_until - time.time()

if timeout_left < 0:
Expand All @@ -463,5 +468,5 @@ async def _get_specific_response(self, unique_id, timeout):
return await self._get_specific_response(unique_id, timeout_left)

async def _send(self, message):
LOGGER.info("%s: send %s", self.id, message)
self.logger.info("%s: send %s", self.id, message)
await self._connection.send(message)

0 comments on commit cb6bed5

Please sign in to comment.