-
Notifications
You must be signed in to change notification settings - Fork 2
/
giveweather.py
198 lines (169 loc) · 7.11 KB
/
giveweather.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
#! /usr/bin/python
'''
This process waits for an HTTP request from someone wanting the
weather. It reads the data from a MySQL database on my NAS and formats it
into either JSON for a process interface or a very simple HTML page for
debugging or just checking around from time to time.
'''
import json
from datetime import datetime, timedelta
from houseutils import lprint, getHouseValues, timer, checkTimer, midnight
from datetime import datetime, timedelta
import sys
import MySQLdb as mdb
import argparse
import cherrypy
def collectWeather():
global Weather
dbconn = mdb.connect(host=dbHost, user=dbUser, passwd=dbPassword, db=dbName)
c = dbconn.cursor()
c.execute("select reading from ftemperature where utime = "
"(select max(utime) from ftemperature);")
Weather["currentOutsideTemp"] = c.fetchone()[0]
c.execute("select reading from rtemperature where utime = "
"(select max(utime) from rtemperature);")
Weather["roofTemperature"] = c.fetchone()[0]
c.execute("select speed from wind where utime = "
"(select max(utime) from wind);")
Weather["windSpeed"] = c.fetchone()[0]
c.execute("select directionc from wind where utime = "
"(select max(utime) from wind);")
Weather["windDirectionC"] = c.fetchone()[0]
c.execute("select directiond from wind where utime = "
"(select max(utime) from wind);")
Weather["windDirectionD"] = c.fetchone()[0]
c.execute \
("select reading from humidity where utime = "
"(select max(utime) from humidity);")
Weather["humidity"] = c.fetchone()[0]
c.execute("select reading from barometer where utime = "
"(select max(utime) from barometer);")
Weather["currentBarometric"] = c.fetchone()[0]
# Get the cumulative readings
#c.execute("select barometer from daily where utime = %s;",
# (midnight(1),))
c.execute("select reading from barometer "
"where rdate > date_format(now(), '%Y-%m-%d 00:00:00')"
"order by rdate asc limit 1;")
Weather["midnightBarometric"] = c.fetchone()[0]
# now get the rest of the daily items
c.execute("SELECT max(speed) FROM wind "
"where rdate > date_sub(now(), interval 24 hour) "
"ORDER BY `rdate` asc limit 1"
)
Weather["maxWindSpeedToday"] = c.fetchone()[0]
c.execute("SELECT max(reading) FROM rtemperature "
"where rdate > date_sub(now(), interval 24 hour) "
"ORDER BY `rdate` asc limit 1"
)
Weather["maxTempToday"] = c.fetchone()[0]
c.execute("SELECT min(reading) FROM rtemperature "
"where rdate > date_sub(now(), interval 24 hour) "
"ORDER BY `rdate` asc limit 1"
)
Weather["minTempToday"] = c.fetchone()[0]
c.execute("SELECT reading, rdate FROM `raincounter` "
"where rdate > date_sub(now(), interval 24 hour) "
"ORDER BY `rdate` asc limit 1"
)
startCount = c.fetchone()[0]
c.execute("select reading from raincounter where rdate = "
"(select max(rdate) from raincounter);")
endCount = c.fetchone()[0]
Weather["rainToday"] = str((float(endCount) - float(startCount)) * 0.01)
dbconn.close()
def showData():
global Weather
returnString = ""
# fill in the Weather dictionary
collectWeather()
# now compose the screen
returnString += "Temperature on fence: " + str(Weather["currentOutsideTemp"]) + \
"<br />"
returnString += "Temperature on roof: " + str(Weather["roofTemperature"]) + \
"<br />"
returnString += "Wind Speed: " + str(Weather["windSpeed"]) + "<br />"
returnString += "Wind Direction: " + Weather["windDirectionC"] + \
" (" + Weather["windDirectionD"] + ")" + "<br />"
returnString += "Humidity: " + str(Weather["humidity"]) + "%" + "<br />"
returnString += "Barometer: " + str(Weather["currentBarometric"]) + \
" mbar" + "<br />"
returnString += "<br />"
returnString += "Midnight Barometer Reading: " + str(Weather["midnightBarometric"]) + \
" mbar" + "<br />"
returnString += "High Temperature Today: " + str(Weather["maxTempToday"]) + \
"<br />"
returnString += "Low Temperature Today: " + str(Weather["minTempToday"]) + \
"<br />"
returnString += "Highest Wind Speed Today: " + str(Weather["maxWindSpeedToday"]) + \
"<br />"
returnString += "Rainfall Today: " + Weather["rainToday"] + \
" inch" + "<br />"
return(returnString)
def keepAlive():
# this is only to log that the process is still alive
lprint(" keep alive")
# This is the process interface, it consists of a status report for humans
# and a json output for other things.
class WeatherSC(object):
@cherrypy.expose
@cherrypy.tools.json_out() # This allows a dictionary input to go out as JSON
def status(self):
collectWeather()
return Weather
@cherrypy.expose
def index(self):
status = "<strong>Desert Home Weather</strong><br /><br />"
status += showData()
return status
####################### Actually Starts Here ################################
debug = False
Weather ={
"currentBarometric": "default",
"midnightBarometric":"default",
"rainToday": "default",
"currentOutsideTemp": "default",
"roofTemperature": "default",
"humidity": "default",
"windSpeed" : "default",
"windDirectionC" : "default",
"windDirectionD" : "default",
"maxWindSpeedToday" : "default",
"maxTempToday" : "default",
"minTempToday" : "default"
}
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("-d", "--debug",
action = "store_true",
help='debug flag')
args = parser.parse_args()
if args.debug:
print "Running with debug on"
debug = True
# the database where I'm storing stuff
hv = getHouseValues()
dbName = hv["weatherDatabase"]
dbHost = hv["weatherHost"]
dbPassword = hv["weatherPassword"]
dbUser = hv["weatherUser"]
# Get the ip address and port number you want to use
# from the houserc file
ipAddress=getHouseValues()["giveweather"]["ipAddress"]
port = getHouseValues()["giveweather"]["port"]
# periodically put a message into the log to indicate that
# this is still alive
keepAliveTimer = timer(keepAlive, minutes=4)
cherrypy.config.update({'server.socket_host' : ipAddress.encode('ascii','ignore'),
'server.socket_port': port,
'engine.autoreload.on': False,
})
# Subscribe to the 'main' channel in cherrypy with my timer
cherrypy.engine.subscribe("main", checkTimer.tick)
lprint ("Hanging on the wait for HTTP message")
# Now just hang on the HTTP server looking for something to
# come in. The cherrypy dispatcher will update the things that
# are subscribed which will update the timers so the light
# status gets recorded.
cherrypy.quickstart(WeatherSC())
sys.exit("Told to shut down");