| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899 |
- import os
- import ssl
- import smtplib
- import tempfile
- import time
- import urllib.request
- from datetime import datetime, timedelta
- from email.mime.image import MIMEImage
- from email.mime.multipart import MIMEMultipart
- from email.mime.text import MIMEText
- from influxdb import InfluxDBClient
- import requests
- # Configurations
- strFrom = 'pv-tannenstr@web.de'
- strTo = ["tobias.siegel@outlook.com", "wsiegel@web.de"]
- bot_token = '949332240:AAHNsQEmCW4it86Esa7F5o07XxwrotSM7s8'
- chat_id = '-914111351'
- def get_boot_time():
- with open('/proc/uptime', 'r') as f:
- uptime_seconds = int(float(f.readline().split()[0]))
- boot_time = datetime.now() - timedelta(seconds=uptime_seconds) + timedelta(hours=1)
- return boot_time
- def get_kwh(beginning, end):
- # 10 seconds = 10/3600 hours = 1/360 hour.
- # So, the conversion factor for watts to kilowatt-hours for a 10-second interval is:
- # (1/1000) * (1/360) = 1/360000 = 0.000002777777777
- client = InfluxDBClient(host='127.0.0.1', port=8086, database='solar')
- result = client.query("SELECT sum(\"eingespeiste_leistung\") *0.000002777777777 FROM \"Tannenstrasse\" WHERE time >= {}ms and time <= {}ms".format(beginning, end))
- if 'series' in result.raw:
- return round(result.raw['series'][0]['values'][0][1], 1)
- else:
- return "0 - Keine Werte"
- def create_email(kwh):
- msgRoot = MIMEMultipart('related')
- msgRoot['Subject'] = f'{datetime.today().strftime("%Y-%m-%d")} - {kwh}kWh - Solaranlage'
- msgRoot['From'] = strFrom
- msgRoot['To'] = ",".join(strTo)
- msgRoot.preamble = 'This is a multi-part message in MIME format.'
- msgAlternative = MIMEMultipart('alternative')
- msgRoot.attach(msgAlternative)
- msgText = MIMEText('This is the alternative plain text message.')
- msgAlternative.attach(msgText)
- bt = get_boot_time()
- bt_str = bt.strftime("%d.%m.%Y, %H.%M")
- msgText = MIMEText(f'<img src="cid:image1"><br><p>Letzter Start: {bt_str} Uhr</p>', 'html')
- msgAlternative.attach(msgText)
- with open(tmpdir.name + '/leistung.png', 'rb') as fp:
- msgImage = MIMEImage(fp.read())
- msgImage.add_header('Content-ID', '<image1>')
- msgRoot.attach(msgImage)
- return msgRoot
- def send_email(msgRoot):
- context = ssl.create_default_context()
- with smtplib.SMTP('smtp.web.de', 587) as server:
- server.ehlo()
- server.starttls(context=context)
- server.ehlo()
- server.login(strFrom, 'PV-Tannenstr1')
- server.sendmail(strFrom, strTo, msgRoot.as_string())
- def send_telegram(bot_token, chat_id, kwh):
- bot_url = f'https://api.telegram.org/bot{bot_token}/sendPhoto'
- bt = get_boot_time()
- bt_str = bt.strftime("%d.%m.%Y, %H.%M")
- caption = f'{datetime.today().strftime("%Y-%m-%d")} - {kwh}kWh - Solaranlage\nLetzter Start: {bt_str} Uhr'
- with open(tmpdir.name + '/leistung.png', 'rb') as img:
- response = requests.post(
- bot_url,
- data={'chat_id': chat_id, 'caption': caption},
- files={'photo': img}
- )
- return response
- if __name__ == "__main__":
- tmpdir = tempfile.TemporaryDirectory()
- beginning = str((1000 * int(time.time())) - (17 * 3600000))
- end = str(1000 * int(time.time()))
- urllib.request.urlretrieve(f"http://localhost:3000/render/d-solo/-wCxs6mgk/solaranlage?orgId=1&refresh=10s&panelId=2&from={beginning}&to={end}&width=600&height=300&tz=Europe%2FBerlin", f"{tmpdir.name}/leistung.png")
- kwh = get_kwh(beginning, end)
- email_msg = create_email(kwh)
- send_email(email_msg)
- send_telegram(bot_token, chat_id, kwh)
- tmpdir.cleanup()
|