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):
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'
Letzter Start: {bt_str} Uhr
', 'html') msgAlternative.attach(msgText) with open(tmpdir.name + '/leistung.png', 'rb') as fp: msgImage = MIMEImage(fp.read()) msgImage.add_header('Content-ID', '