130 lines
4.7 KiB
Python
Executable File
130 lines
4.7 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
# -*- coding: utf-8 -*-
|
|
|
|
# Copyright (C) 2019 Hauke Petersen <devel@haukepetersen.de>
|
|
#
|
|
# This program is free software: you can redistribute it and/or modify
|
|
# it under the terms of the GNU General Public License as published by
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
# (at your option) any later version.
|
|
#
|
|
# This program is distributed in the hope that it will be useful,
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
# GNU General Public License for more details.
|
|
#
|
|
# You should have received a copy of the GNU General Public License
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
import sys
|
|
import json
|
|
import pytz
|
|
import logging
|
|
import dateutil
|
|
import urllib
|
|
from datetime import datetime
|
|
from deichapp import Deichapp
|
|
from deichfluxx import Deichfluxx
|
|
from apscheduler.schedulers.blocking import BlockingScheduler
|
|
|
|
CONFIG_DEFAULT = {
|
|
"interval": 5.0,
|
|
}
|
|
|
|
|
|
class Steckie(Deichapp):
|
|
def __init__(self, cfg_file):
|
|
super().__init__(cfg_file, CONFIG_DEFAULT)
|
|
|
|
self.scheduler = BlockingScheduler()
|
|
self.db = Deichfluxx.init(self.cfg["db"])
|
|
|
|
logging.warning("Steckie V0.72 - loaded...")
|
|
|
|
self.counters = {}
|
|
for name in self.cfg["devs"]:
|
|
self.counters[name] = {
|
|
"last": None,
|
|
"year": 0.0,
|
|
"month": 0.0,
|
|
"day": 0.0,
|
|
}
|
|
|
|
# read latest known values from database
|
|
query = f'SELECT e_total_kwh, e_year_kwh, e_month_kwh, e_day_kwh ' \
|
|
f'FROM "{self.cfg["measurement"]}" WHERE "name"=\'{name}\' ' \
|
|
f'ORDER BY DESC LIMIT 1'
|
|
res = self.db.query(query, None)
|
|
for p in res.get_points():
|
|
self.counters[name]["last"] = dateutil.parser.isoparse(p["time"])
|
|
total = p["e_total_kwh"]
|
|
self.counters[name]["year"] = total - p["e_year_kwh"]
|
|
self.counters[name]["month"] = total - p["e_month_kwh"]
|
|
self.counters[name]["day"] = total - p["e_day_kwh"]
|
|
logging.warning(f'Found {name}: current total is {total}kWh')
|
|
|
|
def run(self):
|
|
for name, url in self.cfg["devs"].items():
|
|
self.scheduler.add_job(self.query,
|
|
"interval",
|
|
seconds=int(self.cfg["interval"]),
|
|
args=(name, url))
|
|
self.scheduler.start()
|
|
|
|
def query(self, name, url):
|
|
cnt = self.counters[name]
|
|
now = pytz.UTC.localize(datetime.utcnow())
|
|
|
|
try:
|
|
with urllib.request.urlopen(f'{url}/cm?cmnd=Status%208',
|
|
timeout=self.cfg["timeout"]) as raw:
|
|
resp = json.loads(raw.read().decode("utf-8"))
|
|
except Exception as e:
|
|
logging.warning(f'{name} - no response: {e}')
|
|
return
|
|
|
|
total = float(resp["StatusSNS"]["ENERGY"]["Total"])
|
|
|
|
# initialize values if this is the first time we read them
|
|
if cnt["last"] is None:
|
|
cnt["last"] = now
|
|
for f in ("year", "month", "day"):
|
|
cnt[f] = total
|
|
|
|
# reset values if a when new time frame starts
|
|
now_local = now.astimezone(pytz.timezone("Europe/Berlin"))
|
|
last_local = cnt["last"].astimezone(pytz.timezone("Europe/Berlin"))
|
|
if now_local.date().year != last_local.date().year:
|
|
cnt["year"] = total
|
|
cnt["month"] = total
|
|
cnt["day"] = total
|
|
elif now_local.date().month != last_local.date().month:
|
|
cnt["month"] = total
|
|
cnt["day"] = total
|
|
elif now_local.date().day != last_local.date().day:
|
|
cnt["day"] = total
|
|
cnt["last"] = now
|
|
|
|
# write data to DB
|
|
point = {
|
|
"measurement": self.cfg["measurement"],
|
|
"tags": {
|
|
"name": name,
|
|
},
|
|
"time": now.isoformat(),
|
|
"fields": {
|
|
"power_w": float(resp["StatusSNS"]["ENERGY"]["Power"]),
|
|
"voltage_v": float(resp["StatusSNS"]["ENERGY"]["Voltage"]),
|
|
"current_a": float(resp["StatusSNS"]["ENERGY"]["Current"]),
|
|
"factor": float(resp["StatusSNS"]["ENERGY"]["Factor"]),
|
|
"e_total_kwh": total,
|
|
"e_year_kwh": total - cnt["year"],
|
|
"e_month_kwh": total - cnt["month"],
|
|
"e_day_kwh": total - cnt["day"],
|
|
}
|
|
}
|
|
|
|
logging.warning(f'{name} - power:{point["fields"]["power_w"]:.1f}W '
|
|
f'total:{point["fields"]["e_total_kwh"]}kWh')
|
|
self.db.write(point)
|