Skip to content

Commit

Permalink
removed unneeded library, fixed null and non-null value's issue, crea…
Browse files Browse the repository at this point in the history
…tes a data.json file to be sent to the db
  • Loading branch information
mdrocan committed Sep 10, 2023
1 parent 733f32e commit c1e7fbe
Showing 1 changed file with 28 additions and 13 deletions.
41 changes: 28 additions & 13 deletions fronius.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,19 @@
Importing data from the Fronius device.
"""
import json # pylint: disable=import-error
import time # pylint: disable=import-error
import requests # pylint: disable=import-error
from requests.exceptions import HTTPError # pylint: disable=import-error
from numerize import numerize # pylint: disable=import-error

with open("config.json", "r", encoding="utf8") as config_file:
config = json.load(config_file)

URL = config["fronius"]["url"]
MAX_C = config["fronius"]["max_capacity"]
converted_MAX_C = int(MAX_C)

t = time.localtime()
current_time = time.strftime("%H:%M:%S", t)


def check_values():
Expand All @@ -22,28 +26,39 @@ def check_values():
response.raise_for_status()
jsondata = response.json()

print("Timestamp:", jsondata["Head"]["Timestamp"])
print("Time:", current_time)
print("Response timestamp:", jsondata["Head"]["Timestamp"])

currently = jsondata["Body"]["Data"]["Site"]["P_PV"]
if currently is None:
currently = 0
print("Currently:", currently, "W")
print("From max capacity:", "0%")
if currently == "None":
currently = "0"
print("Currently producing:", currently, "W")
print("Setup utilization:", "0%")
else:
percentage = currently / MAX_C * 100
print("From max capacity:", round(percentage, 1), "%")
print("Currently producing:", currently, "W")
percentage = currently / converted_MAX_C * 100
print("Setup utilization:", round(percentage), "%")

today = numerize.numerize(jsondata["Body"]["Data"]["Site"]["E_Day"], 2)
today = jsondata["Body"]["Data"]["Site"]["E_Day"]
print("Today:", today, "Wh")

year = numerize.numerize(jsondata["Body"]["Data"]["Site"]["E_Year"], 2)
year = jsondata["Body"]["Data"]["Site"]["E_Year"]
print("Year:", year, "Wh")

total_produce = numerize.numerize(
jsondata["Body"]["Data"]["Site"]["E_Total"], 2
) # noqa: E501
total_produce = jsondata["Body"]["Data"]["Site"]["E_Total"] # noqa: E501
print("Total:", total_produce, "Wh")

data = {
"currently": currently,
"percentage": percentage,
"today": today,
"year": year,
"total_produce": total_produce,
}

with open("data.json", "w", encoding="utf8") as outfile:
json.dump(data, outfile, indent=4)

except HTTPError as http_err:
print(f"HTTP error occurred: {http_err}")
except Exception as err: # pylint: disable=broad-except
Expand Down

0 comments on commit c1e7fbe

Please sign in to comment.