steckie initial port to pip based service
This commit is contained in:
77
steckie/steckie.py
Executable file
77
steckie/steckie.py
Executable file
@@ -0,0 +1,77 @@
|
||||
#!/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 urllib.request
|
||||
from datetime import datetime
|
||||
from deichapp import Deichapp
|
||||
from deichflux import Deichflux
|
||||
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 = Deichflux(self.cfg["db"])
|
||||
|
||||
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):
|
||||
logging.warning(f'query {name} -> {url}')
|
||||
|
||||
try:
|
||||
with urllib.request.urlopen(f'{url}/cm?cmnd=Status%208') as raw:
|
||||
resp = json.loads(raw.read().decode("utf-8"))
|
||||
logging.warning(resp)
|
||||
except Exception as e:
|
||||
logging.warning(f'no response from {name} -> {e}')
|
||||
return
|
||||
|
||||
point = [{
|
||||
"measurement": self.cfg["measurement"],
|
||||
"tags": {
|
||||
"name": name,
|
||||
},
|
||||
"time": pytz.UTC.localize(datetime.utcnow()).isoformat(),
|
||||
"fields": {
|
||||
"voltage": float(resp['StatusSNS']['ENERGY']['Voltage']),
|
||||
"current": float(resp['StatusSNS']['ENERGY']['Current']),
|
||||
"pwr": float(resp['StatusSNS']['ENERGY']['ApparentPower']),
|
||||
"pwr_r": float(resp['StatusSNS']['ENERGY']['ReactivePower']),
|
||||
"factor": float(resp['StatusSNS']['ENERGY']['Factor']),
|
||||
"e_daily": float(resp['StatusSNS']['ENERGY']['Today']),
|
||||
}
|
||||
}]
|
||||
|
||||
self.db.write(point)
|
||||
Reference in New Issue
Block a user