diff --git a/klimalogger/store/__init__.py b/klimalogger/store/__init__.py index 9822c3b..3ad9816 100644 --- a/klimalogger/store/__init__.py +++ b/klimalogger/store/__init__.py @@ -3,7 +3,6 @@ from injector import Module, provider, singleton from .client import StoreClient -from .queue import QueueStore from ..config import Config log = logging.getLogger(__name__) @@ -22,7 +21,11 @@ class StoreModule(Module): def store_provider(self, config: Config) -> StoreClient: log.info("store provider type: %s", config.store_type) - if config.store_type == 'queue': + if config.store_type == "file": + from .file import FileStore + return FileStore() + elif config.store_type == 'queue': + from .queue import QueueStore return QueueStore(config) else: return influxdb_store_factory(config) diff --git a/klimalogger/store/file.py b/klimalogger/store/file.py new file mode 100644 index 0000000..908fc89 --- /dev/null +++ b/klimalogger/store/file.py @@ -0,0 +1,27 @@ +import logging +import os +from datetime import datetime +from typing import List + +from .client import StoreClient + +log = logging.getLogger(__name__) + + +class FileStore(StoreClient): + target_folder = "/var/lib/klimalogger/data" + + def store(self, data: List[dict]): + for entry in data: + timestamp = datetime.fromisoformat(entry["time"]) + value = entry["fields"]["value"] + tags = entry["tags"] + measurement_type = tags["type"] + unit = tags["unit"] + sensor = tags["sensor"] + folder_name = f"{self.target_folder}/{timestamp:%Y}/{timestamp:%m}/" + file_name = f"{measurement_type}_{timestamp:%Y%m%d}.txt" + + os.makedirs(folder_name, exist_ok=True) + with open(folder_name + '/' + file_name, 'a') as output_file: + output_file.write(f"{timestamp.isoformat()} {value} {unit} {sensor}\n") diff --git a/pyproject.toml b/pyproject.toml index 78a1882..edda95e 100755 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [project] name = "klimalogger" -version = "0.7.11" +version = "0.7.12" authors = [ { name = "Andreas Würl", email = "andi@tryb.de" }, ]