solar_edge/solar_edge.py

74 lines
2.0 KiB
Python
Raw Normal View History

2019-04-15 16:15:57 -06:00
#!/usr/bin/env python3
2020-10-28 11:45:03 -06:00
import sys
2019-04-15 16:15:57 -06:00
import urllib.request
import json
import os
2019-04-15 17:26:14 -06:00
import shutil
2019-04-15 16:15:57 -06:00
from PIL import ImageFont
from PIL import ImageDraw
from PIL import Image
from dotenv import load_dotenv
from paramiko import SSHClient
from scp import SCPClient
2020-10-28 09:04:44 -06:00
#### HEAR BE DRAGONS
# set python environment
if getattr(sys, 'frozen', False):
bundle_dir = sys._MEIPASS
else:
# we are running in a normal Python environment
bundle_dir = os.path.dirname(os.path.abspath(__file__))
# load environmental variables
load_dotenv(bundle_dir + "/.env")
2019-04-15 16:15:57 -06:00
## get JSON from solar edge server
infile = os.getenv('INFILE')
outfile = os.getenv('OUTFILE')
font_face = os.getenv('FONT_FACE')
ssh_host = os.getenv('SSH_HOST')
2020-10-28 09:04:44 -06:00
ssh_port = os.getenv('SSH_PORT')
2019-04-15 16:15:57 -06:00
ssh_user = os.getenv('SSH_USER')
ssh_folder = os.getenv('SSH_FOLDER')
2020-10-28 11:45:03 -06:00
line1 = int(os.getenv('LINE1'))
line2 = int(os.getenv('LINE2'))
xpos = int(os.getenv('MARGIN'))
fill_color = str(os.getenv('COLOR'))
2019-04-15 16:15:57 -06:00
2020-10-28 11:45:03 -06:00
co2 = 0
trees = 0
url = []
while os.getenv('URL'+str(len(url)+1)):
req = urllib.request.Request(os.getenv('URL'+str(len(url)+1)))
url.append(os.getenv('URL'+str(len(url)+1)))
# parsing response
r = urllib.request.urlopen(req).read()
cont = json.loads(r.decode('utf-8'))
co2 += round(cont['envBenefits']['gasEmissionSaved']['co2'])
trees += round(cont['envBenefits']['treesPlanted'])
2019-04-15 16:15:57 -06:00
## create image for website
2020-10-28 09:04:44 -06:00
image = Image.open(bundle_dir + "/" + infile)
font = ImageFont.truetype(bundle_dir + "/" + font_face, 19)
2019-04-15 16:15:57 -06:00
draw = ImageDraw.Draw(image)
2022-05-05 13:45:34 -06:00
draw.text((xpos, line1), "{:,} lbs".format(co2) , font=font, fill=fill_color)
draw.text((xpos, line2), "{:,}".format(trees), font=font, fill=fill_color)
2019-04-15 16:15:57 -06:00
image.save(outfile)
2019-04-15 17:26:14 -06:00
## upload image to website or copy to folder
if ssh_host:
ssh = SSHClient()
ssh.load_system_host_keys()
2020-10-28 09:04:44 -06:00
ssh.connect(hostname=ssh_host, port=ssh_port, username=ssh_user)
2019-04-15 17:26:14 -06:00
scp = SCPClient(ssh.get_transport())
scp.put(outfile, ssh_folder)
scp.close()
ssh.close()
else:
shutil.copy2(outfile, ssh_folder)
2019-04-15 16:15:57 -06:00
os.remove(outfile)