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