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
|
|
37 import DataStore
|
|
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")
|
|
61 store = DataStore.hourly_store(DATA_DIR)
|
|
62 now = datetime.datetime.now()
|
|
63 then = now - datetime.timedelta(1)
|
|
64 data = store[then:now]
|
|
65 app.logger.debug("done")
|
|
66 r = {}
|
|
67 for k in store.key_list:
|
|
68 r[k] = []
|
|
69
|
|
70 for d in data:
|
|
71 for k in store.key_list:
|
|
72 if k == 'idx':
|
|
73 t = int(dt2epoch(d[k]))
|
|
74 r[k].append(t)
|
|
75 else:
|
|
76 r[k].append(d[k])
|
|
77 return flask.jsonify(r)
|
|
78
|
|
79 def dt2epoch(dt):
|
|
80 return calendar.timegm(dt.utctimetuple()[0:6]) + dt.microsecond / 1e6
|
|
81
|
|
82
|
|
83 if __name__ == "__main__":
|
|
84 app.run(host = '0.0.0.0', port = 5001, debug = DEBUG)
|