annotate iwws.py @ 7:24e9b717722d

- Update every 10 minutes. - Redraw the graph on orientation change.
author Daniel O'Connor <darius@dons.net.au>
date Tue, 16 Aug 2011 22:34:30 +0930
parents 2d9ee2b3ae82
children e5ba2e76d1b0
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
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
37 import DataStore
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")
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
61 store = DataStore.hourly_store(DATA_DIR)
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
62 now = datetime.datetime.now()
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
63 then = now - datetime.timedelta(1)
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
64 data = store[then:now]
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
65 app.logger.debug("done")
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
66 r = {}
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
67 for k in store.key_list:
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
68 r[k] = []
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
69
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
70 for d in data:
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
71 for k in store.key_list:
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
72 if k == 'idx':
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
73 t = int(dt2epoch(d[k]))
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
74 r[k].append(t)
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
75 else:
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
76 r[k].append(d[k])
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
77 return flask.jsonify(r)
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
78
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
79 def dt2epoch(dt):
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
80 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
81
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
82
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
83 if __name__ == "__main__":
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
84 app.run(host = '0.0.0.0', port = 5001, debug = DEBUG)