view weatherlog/types.py @ 5:885bff085edf

Add remote logger class for eventual HTTP writer.
author Paul Fisher <paul@pfish.zone>
date Sat, 28 Sep 2019 19:28:22 -0400
parents weatherlog/reader.py@d961c8a93d6b
children c01f9929ae38
line wrap: on
line source

"""Basic datatypes for the weather station.
"""

import datetime

import attr
import pytz


def _utc_now() -> datetime.datetime:
    """utcnow, but timezone-aware."""
    return datetime.datetime.utcnow().replace(tzinfo=pytz.UTC)


@attr.s(frozen=True, slots=True)
class Reading(object):
    """A single reading from a temperature/humidity sensor."""

    # The timestamp of the reading.
    sample_time = attr.ib(type=datetime.datetime)

    # The temperature, in degrees Celsius.
    temp_c = attr.ib(type=float)

    # The relative humidity, in percent.
    rh_pct = attr.ib(type=float)

    @classmethod
    def from_now(cls, **kwargs) -> 'Reading':
        """Creates a new reading taken at the current time."""
        return cls(sample_time=_utc_now(), **kwargs)

    def as_dict(self):
        return attr.asdict(self, recurse=False)