-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmower_weather.py
executable file
·237 lines (190 loc) · 7.99 KB
/
mower_weather.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
#!/usr/bin/env python3
# ---------------------------------------------------------------------------
# Mähroboter parken, wenn der Deutsche Wetterdienst (DWD) eine Warnmeldung
# für die Region ausgibt.
#
# Mario Zimmermann <[email protected]>
# 2024-06-03
# ---------------------------------------------------------------------------
import os
from configparser import ConfigParser
import requests
import re
import json
import datetime
import time
import smtplib
from email.mime.text import MIMEText
# ---------------------------------------------------------------------------
# OpenData Hilfe:
# https://www.dwd.de/DE/leistungen/opendata/hilfe.html
#
# DWD Produktübersicht:
# https://www.dwd.de/DE/leistungen/opendata/help/warnungen/dwd_warnings_products_overview_de_pdf.pdf?__blob=publicationFile&v=7
#
# DWD WarnCell-IDs:
# https://www.dwd.de/DE/leistungen/opendata/help/warnungen/cap_warncellids_csv.csv?__blob=publicationFile&v=6
#
# Hochaktueller Landreis-Status (JSON):
# https://www.dwd.de/DWD/warnungen/warnapp/json/warnings.json
#
# Husqvarna API:
# https://developer.husqvarnagroup.cloud/apis/automower-connect-api?tab=openapi
# ---------------------------------------------------------------------------
# ---------------------------------------------------------------------------
abspath = os.path.abspath(__file__)
dname = os.path.dirname(abspath)
os.chdir(dname)
# Configfile einlesen
config_object = ConfigParser()
config_object.read("./mower_weather.ini")
HUSQVARNA_API = config_object["HUSQVARNA_API"]
MISC = config_object["MISC"]
DWD = config_object["DWD"]
MOWER = config_object["MOWER"]
SMTP = config_object["SMTP"]
EMAIL = config_object["EMAIL"]
# ---------------------------------------------------------------------------
def parkMower(duration_minutes):
# Authentifizierung
response = requests.post(
HUSQVARNA_API["auth_url"],
headers = {
"Content-Type": "application/x-www-form-urlencoded"
},
data = {
"grant_type": "client_credentials",
"client_id": HUSQVARNA_API["client_id"],
"client_secret": HUSQVARNA_API["client_secret"]
}
)
data = json.loads(response.text)
access_token = data["access_token"]
# Mower parken
response = requests.post(
f"{HUSQVARNA_API['base_url']}/mowers/{MOWER['mower_id']}/actions",
headers = {
"Content-Type": "application/vnd.api+json",
"Authorization-Provider": "husqvarna",
"Authorization": f"Bearer {access_token}",
"X-Api-Key": HUSQVARNA_API["client_id"]
},
json = {
"data": {
"type": "Park",
"attributes": {
"duration": duration_minutes
}
}
}
)
# print(response.text)
# ---------------------------------------------------------------------------
# ---------------------------------------------------------------------------
def send_email(email_address: str, subject: str, body: str):
server = smtplib.SMTP(SMTP["server"])
server.starttls()
server.login(SMTP["user"], SMTP["password"])
msg = MIMEText(body, _charset='utf-8')
msg["From"] = EMAIL["from"]
msg["Subject"] = subject
server.sendmail(EMAIL["from"], email_address, msg.as_string())
server.quit()
# ---------------------------------------------------------------------------
# ---------------------------------------------------------------------------
# Aktueller Zeitstempel
now = datetime.datetime.now()
now_timestamp = int(now.timestamp())
# ---------------------------------------------------------------------------
# Zeitstempel einlesen, bis zu dem der Mower geparkt ist. Wenn der noch in der
# Zukunft liegt, gibt es nichts zu tun
park_timestamp = 0
if os.path.isfile(MISC["statusfile"]):
with open(MISC["statusfile"], 'r') as statusfile:
park_timestamp = int(statusfile.readline())
if park_timestamp > now_timestamp:
exit()
# ---------------------------------------------------------------------------
# Wetter-Warnungen des DWD abfragen.
# Da der Service manchmal mit einem Fehler antwortet, max. 10 Versuche
try_count = 0
success = False
while not success and try_count < 10:
response = requests.get(DWD["url"])
text = response.text
text = re.sub("warnWetter.loadWarnings\(", "", text)
text = re.sub("\);", "", text)
try:
data = json.loads(text)
success = True
except json.decoder.JSONDecodeError as e:
try_count += 1
time.sleep(1)
if not success:
exit()
# ---------------------------------------------------------------------------
# Alle vorkommenden DWD-Events sammeln und in der Events-Datei ablegen
events = {}
# bisherige Einträge einlesen
if os.path.isfile(DWD["events_file"]):
with open(DWD["events_file"], 'r') as file:
file_events = file.readlines()
for event in file_events:
events[event.rstrip()] = 1
event_count = len(events)
for cellId in data["warnings"]:
for warning in data["warnings"][cellId]:
events[warning["event"]] = 1
if len(events) > event_count:
# Neue Events gefunden
with open(DWD["events_file"], 'w') as file:
for event in events:
file.write(f'{event}\n')
# ---------------------------------------------------------------------------
# ---------------------------------------------------------------------------
# Daten für die gewünschten Regionen verarbeiten
if data["warnings"]:
for cell_info in DWD["cell_ids"].split(","):
cell_id,location = cell_info.split(";")
if cell_id in data["warnings"].keys():
cell_data = data["warnings"][cell_id]
with open(MISC["logfile"], 'a') as logfile:
logfile.write(f'{now.strftime("%Y-%m-%d %H:%M:%S")}\n')
logfile.write(f'cell-id: {cell_id}\n')
logfile.write(f'location: {location}\n')
for cd in cell_data:
logfile.write(f'{cd["event"]}\n')
logfile.write(f'type: {cd["type"]}\n')
logfile.write(f'level: {cd["level"]}\n')
if cd["start"]:
s_dt = int(int(cd["start"]) / 1000)
start_dt = datetime.datetime.fromtimestamp(s_dt)
start_dt = f"{start_dt:%Y-%m-%d %H:%M:%S}"
else:
start_dt = ""
if cd["end"]:
e_dt = int(int(cd["end"]) / 1000)
end_dt = datetime.datetime.fromtimestamp(e_dt)
end_dt = f"{end_dt:%Y-%m-%d %H:%M:%S}"
else:
end_dt = ""
logfile.write(f'start_dt: {start_dt}\n')
logfile.write(f'end_dt: {end_dt}\n')
for park_event in DWD["park_events"].split(","):
if park_event in cd["event"]:
park_duration = int((e_dt - now_timestamp) / 60)
with open(MISC["statusfile"], 'w') as statusfile:
statusfile.write(f'{e_dt}\n')
parkMower(park_duration)
logfile.write(f"*** MOWER GEPARKT BIS {end_dt}\n")
if EMAIL["use_mail"] == "1":
# Send email
mail_text = cd["description"]
mail_text += "\n\n"
mail_text += f"{cd['event']}\n"
mail_text += f"Ort: {location}\n"
mail_text += f"Beginn: {start_dt}\n"
mail_text += f"Ende: {end_dt}\n\n"
mail_text += f"Mähroboter geparkt bis {end_dt}\n"
send_email(EMAIL["to"], cd["headline"], mail_text)
logfile.write('------\n')