view iwws.py @ 13:a0213f0e707b

- Update for v12.10 - Plot raw data to show more up to date data - Average/peak winds so they don't spam the display. - Display the time of the most recent data.
author Daniel O'Connor <darius@dons.net.au>
date Thu, 10 Jan 2013 16:38:36 +1030
parents e5ba2e76d1b0
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)