2021-11-14 00:55:43 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
from abc import ABC, abstractmethod
|
|
|
|
from typing import Any, Dict
|
|
|
|
|
2023-12-10 22:50:34 +00:00
|
|
|
from ..protocol import ChargeController
|
|
|
|
|
2021-11-14 00:55:43 +00:00
|
|
|
|
|
|
|
class BaseConsumer(ABC):
|
|
|
|
settings: Dict[str, Any]
|
2023-12-10 22:50:34 +00:00
|
|
|
controller: ChargeController | None = None
|
2021-11-14 00:55:43 +00:00
|
|
|
|
|
|
|
@abstractmethod
|
|
|
|
def __init__(self, settings: Dict[str, Any]) -> None:
|
|
|
|
self.config(settings)
|
|
|
|
|
|
|
|
@abstractmethod
|
|
|
|
def write(self, data: Dict[str, Any]):
|
|
|
|
"""
|
|
|
|
Process and send data to wherever it is going.
|
|
|
|
Avoid blocking.
|
|
|
|
"""
|
|
|
|
pass
|
|
|
|
|
|
|
|
@abstractmethod
|
|
|
|
def poll(self):
|
|
|
|
"""
|
|
|
|
This function will be ran whenever there is down time.
|
|
|
|
If your consumer needs to do something periodically, do so here.
|
|
|
|
This function should not block.
|
|
|
|
"""
|
|
|
|
pass
|
|
|
|
|
|
|
|
def exit(self):
|
|
|
|
"""
|
|
|
|
Called on exit, clean up your handles here
|
|
|
|
"""
|
|
|
|
pass
|
|
|
|
|
|
|
|
def config(self, settings: Dict[str, Any]):
|
|
|
|
self.settings = settings
|
|
|
|
|
|
|
|
def __enter__(self):
|
|
|
|
return self
|
|
|
|
|
|
|
|
def __exit__(self, etype, value, traceback):
|
|
|
|
self.exit()
|