Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: if connecting to discovergy fails (timeout, no internet) "0W" we…
…re reported and no error logged
- Loading branch information
Showing
3 changed files
with
148 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
#!/usr/bin/python3 | ||
import json | ||
import sys | ||
import urllib.request | ||
import urllib.parse | ||
import base64 | ||
from datetime import datetime | ||
|
||
RAMDISK_PATH = "/var/www/html/openWB/ramdisk/" | ||
|
||
|
||
def log(msg: str): | ||
with open("/var/log/openWB.log", "a") as fd: | ||
fd.write("%s: Discovergy: %s\n" % (datetime.now().strftime("%Y-%m-%d %H:%M:%S"), msg)) | ||
|
||
|
||
def write_to_ramdisk_file(filename: str, content: str): | ||
with open(RAMDISK_PATH + filename, 'w') as f: | ||
f.write(content) | ||
|
||
|
||
def write_float_to_ramdisk_file(filename: str, content: float): | ||
write_to_ramdisk_file(filename, str(round(content))) | ||
|
||
|
||
def get_last_reading(user: str, password: str, meter_id: str): | ||
try: | ||
return try_get_last_reading(user, password, meter_id) | ||
except Exception as e: | ||
log("Getting last readings failed: " + str(e)) | ||
raise e | ||
|
||
|
||
def try_get_last_reading(user: str, password: str, meter_id: str): | ||
request = urllib.request.Request( | ||
"https://api.discovergy.com/public/v1/last_reading?" + urllib.parse.urlencode({"meterId": meter_id})) | ||
request.add_header("Authorization", | ||
"Basic " + base64.b64encode(bytes("%s:%s" % (user, password), "utf-8")).decode("utf-8")) | ||
return json.loads(str(urllib.request.urlopen(request, timeout=3).read().decode("utf-8"))) | ||
|
||
|
||
def write_readings_to_ramdisk(discovergy: dict): | ||
values = discovergy["values"] | ||
write_float_to_ramdisk_file("wattbezug", values["power"] / 1000) | ||
write_float_to_ramdisk_file("einspeisungkwh", values["energyOut"] / 10000000) | ||
write_float_to_ramdisk_file("bezugkwh", values["energy"] / 10000000) | ||
|
||
for phase in range(1, 4): | ||
str_phase = str(phase) | ||
voltage = values["voltage" + str_phase] / 1000 | ||
power = values["power" + str_phase] / 1000 | ||
current = power / (voltage if voltage > 150 else 230) | ||
write_float_to_ramdisk_file("evuv" + str_phase, voltage) | ||
write_float_to_ramdisk_file("bezugw" + str_phase, power) | ||
write_float_to_ramdisk_file("bezuga" + str_phase, current) | ||
|
||
|
||
def update(user: str, password: str, meter_id: str): | ||
write_readings_to_ramdisk(get_last_reading(user, password, meter_id)) | ||
|
||
|
||
if __name__ == '__main__': | ||
update(sys.argv[1], sys.argv[2], sys.argv[3]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import json | ||
import unittest | ||
from unittest import TestCase | ||
from unittest.mock import patch, MagicMock | ||
|
||
import discovergy | ||
|
||
|
||
class TestDiscovergy(unittest.TestCase): | ||
SAMPLE_JSON = """{ | ||
"time":1622132613773, | ||
"values":{ | ||
"energyOut":25545649812000, | ||
"energy2":12593551340000, | ||
"energy1":2210138000, | ||
"voltage1":233400, | ||
"voltage2":234600, | ||
"voltage3":100000, | ||
"energyOut1":0, | ||
"energyOut2":0, | ||
"power":1234567, | ||
"power1":1167000, | ||
"power2":2650980, | ||
"power3":-230000, | ||
"energy":12595761479000 | ||
} | ||
}""" | ||
|
||
@patch('discovergy.write_to_ramdisk_file') | ||
@patch('discovergy.try_get_last_reading') | ||
@patch('discovergy.log') | ||
def test_update(self: TestCase, | ||
_: MagicMock, | ||
get_last_reading_mock: MagicMock, | ||
write_to_ramdisk_file_mock: MagicMock): | ||
# setup | ||
get_last_reading_mock.return_value = json.loads(TestDiscovergy.SAMPLE_JSON) | ||
|
||
# exeuction | ||
discovergy.update("someUser", "somePassword", "someMeterId") | ||
|
||
# evaluation | ||
get_last_reading_mock.assert_called_once_with("someUser", "somePassword", "someMeterId") | ||
|
||
write_to_ramdisk_file_mock.assert_any_call("bezuga1", "5") | ||
write_to_ramdisk_file_mock.assert_any_call("bezuga2", "11") | ||
write_to_ramdisk_file_mock.assert_any_call("bezuga3", "-1") | ||
write_to_ramdisk_file_mock.assert_any_call("bezugkwh", "1259576") | ||
write_to_ramdisk_file_mock.assert_any_call("bezugw1", "1167") | ||
write_to_ramdisk_file_mock.assert_any_call("bezugw2", "2651") | ||
write_to_ramdisk_file_mock.assert_any_call("bezugw3", "-230") | ||
write_to_ramdisk_file_mock.assert_any_call("einspeisungkwh", "2554565") | ||
write_to_ramdisk_file_mock.assert_any_call("evuv1", "233") | ||
write_to_ramdisk_file_mock.assert_any_call("evuv2", "235") | ||
write_to_ramdisk_file_mock.assert_any_call("evuv3", "100") | ||
write_to_ramdisk_file_mock.assert_any_call("wattbezug", "1235") | ||
|
||
@patch('discovergy.write_to_ramdisk_file') | ||
@patch('discovergy.try_get_last_reading') | ||
@patch('discovergy.log') | ||
def test_update_handles_fetch_error(self: TestCase, | ||
log_mock: MagicMock, | ||
get_last_reading_mock: MagicMock, | ||
write_to_ramdisk_file_mock: MagicMock): | ||
# setup | ||
get_last_reading_mock.side_effect = Exception("some error message") | ||
|
||
# execution | ||
try: | ||
discovergy.update("someUser", "somePassword", "someMeterId") | ||
except: | ||
pass | ||
else: | ||
self.fail("expected exception not raised") | ||
|
||
# evaluation | ||
write_to_ramdisk_file_mock.assert_not_called() | ||
log_mock.assert_called_once_with("Getting last readings failed: some error message") | ||
|
||
|
||
if __name__ == '__main__': | ||
unittest.main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,52 +1,5 @@ | ||
#!/bin/bash | ||
output=$(curl --connect-timeout 3 -s -u $discovergyuser:"$discovergypass" "https://api.discovergy.com/public/v1/last_reading?meterId=$discovergyevuid") | ||
|
||
einspeisungwh=$(echo $output | jq .values.energyOut) | ||
einspeisungwh=$(( einspeisungwh / 10000000 )) | ||
echo $einspeisungwh > /var/www/html/openWB/ramdisk/einspeisungkwh | ||
|
||
bezugwh=$(echo $output | jq .values.energy) | ||
bezugwh=$(( bezugwh / 10000000 )) | ||
echo $bezugwh > /var/www/html/openWB/ramdisk/bezugkwh | ||
|
||
vl1=$(echo $output | jq .values.voltage1) | ||
vl1=$(( vl1 / 1000 )) | ||
echo $vl1 > /var/www/html/openWB/ramdisk/evuv1 | ||
vl2=$(echo $output | jq .values.voltage2) | ||
vl2=$(( vl2 / 1000 )) | ||
echo $vl2 > /var/www/html/openWB/ramdisk/evuv2 | ||
vl3=$(echo $output | jq .values.voltage3) | ||
vl3=$(( vl3 / 1000 )) | ||
echo $vl3 > /var/www/html/openWB/ramdisk/evuv3 | ||
watt=$(echo $output | jq .values.power) | ||
watt=$(( watt / 1000 )) | ||
echo $watt > /var/www/html/openWB/ramdisk/wattbezug | ||
wattl1=$(echo $output | jq .values.power1) | ||
wattl1=$(( wattl1 / 1000 )) | ||
echo $wattl1 > /var/www/html/openWB/ramdisk/bezugw1 | ||
wattl2=$(echo $output | jq .values.power2) | ||
wattl2=$(( wattl2 / 1000 )) | ||
echo $wattl2 > /var/www/html/openWB/ramdisk/bezugw2 | ||
wattl3=$(echo $output | jq .values.power3) | ||
wattl3=$(( wattl3 / 1000 )) | ||
echo $wattl3 > /var/www/html/openWB/ramdisk/bezugw3 | ||
if (( vl1 > 150 )); then | ||
al1=$(( wattl1 / vl1 )) | ||
else | ||
al1=$(( wattl1 / 230 )) | ||
fi | ||
echo $al1 > /var/www/html/openWB/ramdisk/bezuga1 | ||
if (( vl2 > 150 )); then | ||
al2=$(( wattl2 / vl2 )) | ||
else | ||
al2=$(( wattl2 / 230 )) | ||
fi | ||
echo $al2 > /var/www/html/openWB/ramdisk/bezuga2 | ||
if (( vl3 > 150 )); then | ||
al3=$(( wattl3 / vl3 )) | ||
else | ||
al3=$(( wattl3 / 230 )) | ||
fi | ||
echo $al3 > /var/www/html/openWB/ramdisk/bezuga3 | ||
echo $watt | ||
|
||
SCRIPT_DIR=$(cd $(dirname "${BASH_SOURCE[0]}") && pwd) | ||
python "$SCRIPT_DIR/discovergy.py" "$discovergyuser" "$discovergypass" "$discovergyevuid" | ||
cat /var/www/html/openWB/ramdisk/wattbezug |