Source code for juham.ts.power_record
from juham.base import Object
from juham.base import Base
from influxdb_client_3 import Point
import json
import pdb
[docs]
class PowerRecord(Base):
"""Power utilization record.
This class listens the power utilization message and writes the
state to time series database.
"""
_class_id = None
def __init__(self, name="powerrecord"):
"""Construct power record object with the given name."""
super().__init__(name)
[docs]
def on_connect(self, client, userdata, flags, rc):
"""Standard mqtt connect notification.
This method is called when the client connection with the MQTT
broker is established.
"""
super().on_connect(client, userdata, flags, rc)
self.subscribe(Base.mqtt_root_topic + "/power")
self.debug("Subscribed to f{Base.mqtt_root_topic}/power")
[docs]
def on_message(self, client, userdata, msg):
"""Standard mqtt message notification method.
This method is called upon new arrived message.
"""
m = json.loads(msg.payload.decode())
unit = m["Unit"]
ts = m["Timestamp"]
state = m["State"]
point = (
Point("power")
.tag("unit", unit)
.field("state", state)
.time(self.epoc2utc(ts))
)
self.write(point)
[docs]
@classmethod
def register(cls):
if cls._class_id is None:
Base.register()
cls.initialize_class()