-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebserverinteraction.py
More file actions
107 lines (72 loc) · 3.72 KB
/
Copy pathwebserverinteraction.py
File metadata and controls
107 lines (72 loc) · 3.72 KB
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
#!/usr/bin/env python3
# WxStation web server interaction (data upload)
import requests, datetime, logging
import smtplib
from email.mime.text import MIMEText
Logger = logging.getLogger(__name__)
def send_email(subject, body, sender, recipients, password):
msg = MIMEText(body)
msg['Subject'] = subject
msg['From'] = sender
msg['To'] = ', '.join(recipients)
with smtplib.SMTP_SSL('smtp.gmail.com', 465) as smtp_server:
smtp_server.login(sender, password)
smtp_server.sendmail(sender, recipients, msg.as_string())
Logger.debug("Message sent!")
def postlightningstrike(dist,dtg,password,url): #TODO: send lightning updates to server
myobj = {'credential': password,
'date':dtg,
'distance':str(round(dist,0))} #number lightning strikes in period
ext = "/strikereport"
try:
req = requests.post(url + ext, data = myobj, timeout = 10)
if req.text == "SUCCESS":
return True
else:
Logger.error(f"[!] Lightning POST returned non success code: {req.text}")
except Exception as e:
Logger.warning(f"[!] Unable to connect to {url+ext}")
Logger.debug("Traceback: " + repr(e))
return False
def postGPSpositionchange(lat,lon,password,url): #TODO: send GPS position updates to server
myobj = {'credential': password,
'latitude':str(round(lat,2)),
'longitude':str(round(lon,2))} #number lightning strikes in period
ext = "/updateGPS"
try:
req = requests.post(url + ext, data = myobj, timeout = 10)
if req.text == "SUCCESS":
return True
else:
Logger.error(f"[!] GPS POST returned non success code: {req.text}")
except Exception as e:
Logger.warning(f"[!] Unable to connect to {url+ext}")
Logger.debug("Traceback: " + repr(e))
return False
def postregularupdate(cdtgstr, T, q, P, rainRate, wspd, wgust, wdir, numStrikes, solarVal, password, url, emailaccount, emailpassword):
myobj = {'credential': password,
'date':cdtgstr,
'ta':str(round(T,1)), #temperature (C)
'rh':str(round(q,1)), #relative humidity (%)
'pres':str(round(P,1)), #pressure (mbar or hPa)
'wspd':str(round(wspd,1)), #wind speed (mph)
'wgust':str(round(wgust,1)), #wind gust (mph)
'wdir':str(wdir), #wind direction (rel to N)
'precip':str(round(rainRate,1)), #precipitation rate (mm/hr)
'solar':str(solarVal), #downwelling shortwave radiation at surface (J/m^2)
'strikes':str(numStrikes)} #number lightning strikes per hour
ext = "/addnewob"
try:
Tf = 9*T/5 + 32
emailbody = f"{cdtgstr} Observation:\nTemperature- {Tf:5.1f} degF\nHumidity- {q:5.1f}%\nPressure- {P:7.1f} mb\nWind- {wspd:4.1f} gust {wgust:4.1f} mph brg {wdir:03.0f}T\nLightning- {numStrikes:4.1f} strikes/hr\nRainfall- {rainRate:4.1f} mm/hr"
logging.debug(f"Sending email to account {emailaccount}, password {emailpassword}, text:\n{emailbody}")
send_email(f"WxUpdate {cdtgstr}", emailbody, emailaccount, [emailaccount], emailpassword)
req = requests.post(url + ext, data = myobj, timeout = 10)
if req.text == "SUCCESS":
return True
else:
Logger.error(f"[!] WxObs POST returned non success code: {req.text}")
except Exception as e:
Logger.warning(f"[!] Unable to transmit data to URL {url+ext} or email {emailaccount}")
Logger.debug("Traceback: " + repr(e))
return False