Add debug logging to NP-02B PDU
Change-Id: I1b4c1c6f1685c908a58b60bc08da268b1fce68fe
Reviewed-on: https://fuchsia-review.googlesource.com/c/antlion/+/1041521
Reviewed-by: Anthonio Saw <anthonio@google.com>
Commit-Queue: Auto-Submit <auto-submit@fuchsia-infra.iam.gserviceaccount.com>
Fuchsia-Auto-Submit: Sam Balana <sbalana@google.com>
diff --git a/packages/antlion/controllers/pdu_lib/synaccess/np02b.py b/packages/antlion/controllers/pdu_lib/synaccess/np02b.py
index 000d2f2..7f5779f 100644
--- a/packages/antlion/controllers/pdu_lib/synaccess/np02b.py
+++ b/packages/antlion/controllers/pdu_lib/synaccess/np02b.py
@@ -16,6 +16,7 @@
from __future__ import annotations
+import logging
import urllib.parse
import urllib.request
from dataclasses import dataclass
@@ -25,6 +26,7 @@
from mobly import signals
from antlion.controllers import pdu
+from mobly.logger import PrefixLoggerAdapter
class PduDevice(pdu.PduDevice):
@@ -100,23 +102,33 @@
class Client:
def __init__(self, host: str, user: str, password: str) -> None:
- self.host = host
+ self._url = f"http://{host}/cmd.cgi"
- password_manager = urllib.request.HTTPPasswordMgrWithDefaultRealm()
- password_manager.add_password(None, host, user, password)
- auth_handler = urllib.request.HTTPBasicAuthHandler(password_manager)
+ auth_handler = urllib.request.HTTPBasicAuthHandler()
+ auth_handler.add_password("NP-02B", self._url, user, password)
self._opener = urllib.request.build_opener(auth_handler)
+ self.log = PrefixLoggerAdapter(
+ logging.getLogger(),
+ {
+ PrefixLoggerAdapter.EXTRA_KEY_LOG_PREFIX: f"[pdu | {host}]"
+ },
+ )
+
def request(self, command: Command) -> Response:
cmd = command.code()
args = command.args()
if args:
cmd += f' {" ".join(args)}'
- cmd = urllib.parse.quote_plus(cmd)
- with self._opener.open(f"http://{self.host}/cmd.cgi?{cmd}") as res:
+ url = f"{self._url}?{urllib.parse.quote_plus(cmd)}"
+ self.log.debug(f"Sending request {url}")
+
+ with self._opener.open(url) as res:
body = res.read().decode("utf-8")
+ self.log.debug(f"Received response: {body}")
+
# Syntax for the response should be in the form:
# "<StatusCode>[,<PowerStatus>]"
# For example, StatusCommand returns "$A5,01" when Port 1 is ON and