diff --git a/.gitignore b/.gitignore index a7b1556..469047b 100644 --- a/.gitignore +++ b/.gitignore @@ -1,9 +1,10 @@ __pycache__ *.egg-info +*.log .coverage .mypy_cache +.pypirc .pytest_cache .ruff_cache -.pypirc .venv dist diff --git a/src/pywaveshare/boards/sim868/__init__.py b/src/pywaveshare/boards/sim868/__init__.py index c2e874c..554443e 100644 --- a/src/pywaveshare/boards/sim868/__init__.py +++ b/src/pywaveshare/boards/sim868/__init__.py @@ -320,7 +320,7 @@ def __processData(self): rawData = match[0][1].split(",") self.__GPRSIPaddress = rawData[2].replace('"', "") - if self.__GPRSIPaddress != "0.0.0.0": # nosec + if self.__GPRSIPaddress != "0.0.0.0": # nosec self.__GPRSready = True else: self.__GPRSready = False diff --git a/src/pywaveshare/boards/sim868/client.py b/src/pywaveshare/boards/sim868/client.py index c1fda1e..125ed32 100644 --- a/src/pywaveshare/boards/sim868/client.py +++ b/src/pywaveshare/boards/sim868/client.py @@ -2,6 +2,8 @@ class Client: + logger = config.get_logger("client") + def __init__(self, config: config.Config) -> None: self.config = config self.worker = worker.Worker(self.config) diff --git a/src/pywaveshare/boards/sim868/config.py b/src/pywaveshare/boards/sim868/config.py index d193063..9c2ff08 100644 --- a/src/pywaveshare/boards/sim868/config.py +++ b/src/pywaveshare/boards/sim868/config.py @@ -1,3 +1,72 @@ +import json +import logging +import logging.config +import time + +LOGGING_CONFIG = { + "version": 1, + "formatters": { + "txt": { + "format": "[%(asctime)s.%(msecs)03d][%(levelname)s]: %(message)s", + "datefmt": "%Y-%m-%dT%H:%M:%S", + }, + "jsonl": { + "format": json.dumps( + { + "timestamp": "%(asctime)s.%(msecs)03d", + "process": "%(process)d", + "thread": "%(thread)d", + "logger": "%(name)s", + "level": "%(levelname)s", + "source": "%(pathname)s:%(lineno)d", + "message": "%(message)s", + } + ), + "datefmt": "%Y-%m-%dT%H:%M:%S", + }, + }, + "filters": {}, + "handlers": { + "file": { + "class": "logging.FileHandler", + "filename": "event.log", + "formatter": "jsonl", + "level": "DEBUG", + "filters": [], + }, + "stream": { + "class": "logging.StreamHandler", + "formatter": "txt", + "level": "DEBUG", + "filters": [], + }, + }, + "loggers": { + "client": { + "handlers": ["file", "stream"], + "level": "DEBUG", + "propagate": True, + }, + "worker": { + "handlers": ["file", "stream"], + "level": "DEBUG", + "propagate": True, + }, + "protocol": { + "handlers": ["file", "stream"], + "level": "DEBUG", + "propagate": True, + }, + }, +} +logging.Formatter.converter = time.gmtime +logging.config.dictConfig(LOGGING_CONFIG) + + +def get_logger(id: str) -> logging.Logger: + return logging.getLogger(id) + + class Config: def __init__(self, serial_port: str, baud_rate: int, encoding: str) -> None: self.serial_port = serial_port diff --git a/src/pywaveshare/boards/sim868/itc.py b/src/pywaveshare/boards/sim868/itc.py index ed21aed..3428dd4 100644 --- a/src/pywaveshare/boards/sim868/itc.py +++ b/src/pywaveshare/boards/sim868/itc.py @@ -1,3 +1,5 @@ +from __future__ import annotations + import queue import threading diff --git a/src/pywaveshare/boards/sim868/protocols.py b/src/pywaveshare/boards/sim868/protocols.py index 3990691..725465c 100644 --- a/src/pywaveshare/boards/sim868/protocols.py +++ b/src/pywaveshare/boards/sim868/protocols.py @@ -1,5 +1,8 @@ import abc +import typing class SupportedProtocol(abc.ABC): - pass + NAME: typing.Optional[str] = None + + RESPONSE_PATTERN = r"" diff --git a/src/pywaveshare/boards/sim868/worker.py b/src/pywaveshare/boards/sim868/worker.py index b0e5c6e..6becadd 100644 --- a/src/pywaveshare/boards/sim868/worker.py +++ b/src/pywaveshare/boards/sim868/worker.py @@ -4,6 +4,8 @@ class Worker(threading.Thread): + logger = config.get_logger("worker") + def __init__(self, config: config.Config) -> None: super().__init__()