annotate iwws.py @ 15:ebf411c89a3d

Fix bar width now we get data more frequently.
author Daniel O'Connor <darius@dons.net.au>
date Fri, 31 May 2013 13:35:56 +0930
parents a0213f0e707b
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1 #!/usr/bin/env python
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
2
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
3 #
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
4 # Copyright 2011 Daniel O'Connor <darius@dons.net.au>
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
5 #
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
6 # Redistribution and use in source and binary forms, with or without modification, are
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
7 # permitted provided that the following conditions are met:
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
8 #
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
9 # 1. Redistributions of source code must retain the above copyright notice, this list of
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
10 # conditions and the following disclaimer.
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
11 #
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
12 # 2. Redistributions in binary form must reproduce the above copyright notice, this list
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
13 # of conditions and the following disclaimer in the documentation and/or other materials
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
14 # provided with the distribution.
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
15 #
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
16 # THIS SOFTWARE IS PROVIDED BY <COPYRIGHT HOLDER> ''AS IS'' AND ANY EXPRESS OR IMPLIED
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
17 # WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
18 # FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> OR
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
19 # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
20 # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
21 # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
22 # ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
23 # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
24 # ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
25 #
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
26 # The views and conclusions contained in the software and documentation are those of the
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
27 # authors and should not be interpreted as representing official policies, either expressed
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
28 # or implied, of Daniel O'Connor.
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
29 #
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
30
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
31 import sys
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
32
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
33 sys.path.append('..')
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
34
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
35 import calendar
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
36 import datetime
13
a0213f0e707b - Update for v12.10
Daniel O'Connor <darius@dons.net.au>
parents: 11
diff changeset
37 import pywws.DataStore
0
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
38 import flask
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
39 import logging
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
40
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
41 DEBUG = True
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
42 DATA_DIR = '/data/weather'
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
43
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
44 # Create application object
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
45 app = flask.Flask(__name__)
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
46 app.debug_log_format = '%(asctime)s: %(message)s'
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
47 app.config.from_object(__name__)
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
48 app.config.from_envvar('IWWS_SETTINGS', silent=True)
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
49
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
50 if DEBUG:
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
51 app.logger.setLevel(logging.DEBUG)
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
52
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
53 @app.route('/')
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
54 def index():
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
55 # Could be static really..
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
56 return flask.render_template('index.html')
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
57
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
58 @app.route('/getdata.json')
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
59 def getdata():
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
60 app.logger.debug("Getting data")
13
a0213f0e707b - Update for v12.10
Daniel O'Connor <darius@dons.net.au>
parents: 11
diff changeset
61 if 'duration' in flask.request.args:
a0213f0e707b - Update for v12.10
Daniel O'Connor <darius@dons.net.au>
parents: 11
diff changeset
62 duration = float(flask.request.args['duration'])
a0213f0e707b - Update for v12.10
Daniel O'Connor <darius@dons.net.au>
parents: 11
diff changeset
63 else:
a0213f0e707b - Update for v12.10
Daniel O'Connor <darius@dons.net.au>
parents: 11
diff changeset
64 duration = 24 * 60 * 60
a0213f0e707b - Update for v12.10
Daniel O'Connor <darius@dons.net.au>
parents: 11
diff changeset
65 duration = datetime.timedelta(seconds = duration)
a0213f0e707b - Update for v12.10
Daniel O'Connor <darius@dons.net.au>
parents: 11
diff changeset
66 store = pywws.DataStore.data_store(DATA_DIR)
0
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
67 now = datetime.datetime.now()
13
a0213f0e707b - Update for v12.10
Daniel O'Connor <darius@dons.net.au>
parents: 11
diff changeset
68 then = now - duration
0
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
69 data = store[then:now]
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
70 app.logger.debug("done")
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
71 r = {}
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
72 for k in store.key_list:
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
73 r[k] = []
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
74
13
a0213f0e707b - Update for v12.10
Daniel O'Connor <darius@dons.net.au>
parents: 11
diff changeset
75 # Raw data has absolute rain counts
a0213f0e707b - Update for v12.10
Daniel O'Connor <darius@dons.net.au>
parents: 11
diff changeset
76 baserain = None
0
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
77 for d in data:
13
a0213f0e707b - Update for v12.10
Daniel O'Connor <darius@dons.net.au>
parents: 11
diff changeset
78 if baserain == None:
a0213f0e707b - Update for v12.10
Daniel O'Connor <darius@dons.net.au>
parents: 11
diff changeset
79 baserain = d['rain']
a0213f0e707b - Update for v12.10
Daniel O'Connor <darius@dons.net.au>
parents: 11
diff changeset
80 delta = d['rain'] - baserain
a0213f0e707b - Update for v12.10
Daniel O'Connor <darius@dons.net.au>
parents: 11
diff changeset
81 # Sanity check
a0213f0e707b - Update for v12.10
Daniel O'Connor <darius@dons.net.au>
parents: 11
diff changeset
82 if d['rain'] < baserain or delta > 100:
a0213f0e707b - Update for v12.10
Daniel O'Connor <darius@dons.net.au>
parents: 11
diff changeset
83 delta = 0
a0213f0e707b - Update for v12.10
Daniel O'Connor <darius@dons.net.au>
parents: 11
diff changeset
84 d['rain'] = delta
a0213f0e707b - Update for v12.10
Daniel O'Connor <darius@dons.net.au>
parents: 11
diff changeset
85
a0213f0e707b - Update for v12.10
Daniel O'Connor <darius@dons.net.au>
parents: 11
diff changeset
86 lastdata = d['idx']
11
e5ba2e76d1b0 Only look at keys which exist. Newer pywws has illuminance which
Daniel O'Connor <darius@dons.net.au>
parents: 0
diff changeset
87 for k in d.keys():
0
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
88 if k == 'idx':
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
89 t = int(dt2epoch(d[k]))
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
90 r[k].append(t)
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
91 else:
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
92 r[k].append(d[k])
13
a0213f0e707b - Update for v12.10
Daniel O'Connor <darius@dons.net.au>
parents: 11
diff changeset
93 r['lastdata'] = dt2epoch(lastdata)
0
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
94 return flask.jsonify(r)
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
95
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
96 def dt2epoch(dt):
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
97 return calendar.timegm(dt.utctimetuple()[0:6]) + dt.microsecond / 1e6
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
98
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
99
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
100 if __name__ == "__main__":
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
101 app.run(host = '0.0.0.0', port = 5001, debug = DEBUG)