prettify bluemaestro

This commit is contained in:
Dima Gerasimov 2020-03-06 21:04:22 +00:00
parent e720828645
commit 4e93beee27
3 changed files with 31 additions and 35 deletions

View file

@ -1,43 +1,38 @@
#!/usr/bin/python3 #!/usr/bin/python3
""" """
Bluemaestro temperature/humidity/pressure monitor [[https://bluemaestro.com/products/product-details/bluetooth-environmental-monitor-and-logger][Bluemaestro]] temperature/humidity/pressure monitor
""" """
# TODO link?
import logging # TODO eh, most of it belongs to DAL
import sqlite3 import sqlite3
from datetime import datetime from datetime import datetime
from itertools import chain, islice from itertools import chain, islice
from pathlib import Path from pathlib import Path
from typing import Any, Dict, Iterable, NamedTuple, Set from typing import Any, Dict, Iterable, NamedTuple, Set
from ..common import mcachew from ..common import mcachew, LazyLogger, get_files
# TODO move to common??
from kython import dictify
# TODO vendorize in my. pkg? It's quite handy...
from kython.klogging import LazyLogger
from mycfg import paths
# TODO reuse common
logger = LazyLogger('bluemaestro', level=logging.DEBUG)
def get_backup_files(): import mycfg
# TODO reuse common
return list(sorted(chain.from_iterable(d.glob('*.db') for d in paths.bluemaestro.export_paths)))
class Point(NamedTuple): logger = LazyLogger('bluemaestro', level='debug')
def _get_exports():
return get_files(mycfg.bluemaestro.export_path, glob='*.db')
class Measurement(NamedTuple):
dt: datetime dt: datetime
temp: float temp: float
@mcachew(cache_path=paths.bluemaestro.cache) @mcachew(cache_path=mycfg.bluemaestro.cache_path)
def iter_points(dbs) -> Iterable[Point]: def _iter_measurements(dbs) -> Iterable[Measurement]:
# I guess we can affort keeping them in sorted order # I guess we can affort keeping them in sorted order
points: Set[Point] = set() points: Set[Measurement] = set()
# TODO do some sanity check?? # TODO do some sanity check??
for f in dbs: for f in dbs:
# err = f'{f}: mismatch: {v} vs {value}' # err = f'{f}: mismatch: {v} vs {value}'
@ -53,7 +48,7 @@ def iter_points(dbs) -> Iterable[Point]:
# TODO is that utc??? # TODO is that utc???
tss = tss.replace('Juli', 'Jul').replace('Aug.', 'Aug') tss = tss.replace('Juli', 'Jul').replace('Aug.', 'Aug')
dt = datetime.strptime(tss, '%Y-%b-%d %H:%M') dt = datetime.strptime(tss, '%Y-%b-%d %H:%M')
p = Point( p = Measurement(
dt=dt, dt=dt,
temp=temp, temp=temp,
# TODO use pressure and humidity as well # TODO use pressure and humidity as well
@ -61,6 +56,7 @@ def iter_points(dbs) -> Iterable[Point]:
if p in points: if p in points:
continue continue
points.add(p) points.add(p)
# TODO make properly iterative?
for p in sorted(points, key=lambda p: p.dt): for p in sorted(points, key=lambda p: p.dt):
yield p yield p
@ -76,28 +72,23 @@ def iter_points(dbs) -> Iterable[Point]:
# TODO does it even have to be a dict? # TODO does it even have to be a dict?
# @dictify(key=lambda p: p.dt) # @dictify(key=lambda p: p.dt)
def get_temperature(backups=get_backup_files()): # TODO misleading name def measurements(exports=_get_exports()):
return list(iter_points(backups)) yield from _iter_measurements(exports)
def get_dataframe(): def dataframe():
""" """
%matplotlib gtk %matplotlib gtk
from my.bluemaestro import get_dataframe from my.bluemaestro import get_dataframe
get_dataframe().plot() get_dataframe().plot()
""" """
import pandas as pd # type: ignore import pandas as pd # type: ignore
return pd.DataFrame(p._asdict() for p in get_temperature()).set_index('dt') return pd.DataFrame(p._asdict() for p in measurements()).set_index('dt')
def test():
print(get_temperature(get_backup_files()[-1:]))
def main(): def main():
ll = list(iter_points(get_backup_files())) ll = list(measurements(_get_exports()))
print(len(ll)) print(len(ll))
# print(get_temperature(get_backup_files()[-1:]))
# print(type(t))
if __name__ == '__main__': if __name__ == '__main__':

View file

@ -1,13 +1,12 @@
#!/usr/bin/python3 #!/usr/bin/python3
import logging import logging
from datetime import timedelta, datetime from datetime import timedelta, datetime
from kython import setup_logzero
from my.bluemaestro import get_temperature, logger from my.bluemaestro import measurements, logger
# TODO move this to backup checker? # TODO move this to backup checker?
def main(): def main():
temps = get_temperature() temps = list(measurements())
latest = temps[:-2] latest = temps[:-2]
prev, _ = latest[-2] prev, _ = latest[-2]

6
tests/bluemaestro.py Normal file
View file

@ -0,0 +1,6 @@
#!/usr/bin/env python3
from my.bluemaestro import measurements, _get_exports
def test():
print(list(measurements(_get_exports()[-1:])))