Mercurial > ~darius > hgwebdir.cgi > iwws
view iwws.py @ 16:5a9b33dcdb2c default tip
Show how many samples as well as when the last sample was.
author | Daniel O'Connor <darius@dons.net.au> |
---|---|
date | Fri, 31 May 2013 13:36:26 +0930 |
parents | a0213f0e707b |
children |
line wrap: on
line source
#!/usr/bin/env python # # Copyright 2011 Daniel O'Connor <darius@dons.net.au> # # Redistribution and use in source and binary forms, with or without modification, are # permitted provided that the following conditions are met: # # 1. Redistributions of source code must retain the above copyright notice, this list of # conditions and the following disclaimer. # # 2. Redistributions in binary form must reproduce the above copyright notice, this list # of conditions and the following disclaimer in the documentation and/or other materials # provided with the distribution. # # THIS SOFTWARE IS PROVIDED BY <COPYRIGHT HOLDER> ''AS IS'' AND ANY EXPRESS OR IMPLIED # WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND # FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> OR # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON # ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF # ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. # # The views and conclusions contained in the software and documentation are those of the # authors and should not be interpreted as representing official policies, either expressed # or implied, of Daniel O'Connor. # import sys sys.path.append('..') import calendar import datetime import pywws.DataStore import flask import logging DEBUG = True DATA_DIR = '/data/weather' # Create application object app = flask.Flask(__name__) app.debug_log_format = '%(asctime)s: %(message)s' app.config.from_object(__name__) app.config.from_envvar('IWWS_SETTINGS', silent=True) if DEBUG: app.logger.setLevel(logging.DEBUG) @app.route('/') def index(): # Could be static really.. return flask.render_template('index.html') @app.route('/getdata.json') def getdata(): app.logger.debug("Getting data") if 'duration' in flask.request.args: duration = float(flask.request.args['duration']) else: duration = 24 * 60 * 60 duration = datetime.timedelta(seconds = duration) store = pywws.DataStore.data_store(DATA_DIR) now = datetime.datetime.now() then = now - duration data = store[then:now] app.logger.debug("done") r = {} for k in store.key_list: r[k] = [] # Raw data has absolute rain counts baserain = None for d in data: if baserain == None: baserain = d['rain'] delta = d['rain'] - baserain # Sanity check if d['rain'] < baserain or delta > 100: delta = 0 d['rain'] = delta lastdata = d['idx'] for k in d.keys(): if k == 'idx': t = int(dt2epoch(d[k])) r[k].append(t) else: r[k].append(d[k]) r['lastdata'] = dt2epoch(lastdata) return flask.jsonify(r) def dt2epoch(dt): return calendar.timegm(dt.utctimetuple()[0:6]) + dt.microsecond / 1e6 if __name__ == "__main__": app.run(host = '0.0.0.0', port = 5001, debug = DEBUG)