From abc2777a6eb9e48b67d31796c2ccb4584f083f06 Mon Sep 17 00:00:00 2001 From: Liyou Zhou Date: Tue, 11 Nov 2014 22:55:54 +0000 Subject: [PATCH] modification made for CafeDemo --- code/OLTwi_server.py | 10 ++--- code/examples/CafeDemo/UI_config.ini | 28 ++++++++++++++ code/examples/CafeDemo/device.py | 55 ++++++++++++++++++++++++++++ code/examples/OLT_cluster_config.ini | 5 +++ 4 files changed, 93 insertions(+), 5 deletions(-) create mode 100644 code/examples/CafeDemo/UI_config.ini create mode 100644 code/examples/CafeDemo/device.py diff --git a/code/OLTwi_server.py b/code/OLTwi_server.py index 7cd9849..92bc06e 100755 --- a/code/OLTwi_server.py +++ b/code/OLTwi_server.py @@ -3,10 +3,10 @@ from flask import Flask, render_template, request, jsonify, Response, abort import calendar, time, xmlrpclib from OLT_config_parser import get_config, get_config_by_id -from flask.ext.cache import Cache +#from flask.ext.cache import Cache app = Flask(__name__) -cache = Cache(app,config={'CACHE_TYPE': 'memcached'}) +#cache = Cache(app,config={'CACHE_TYPE': 'memcached'}) def get_xmlrpc_server(): @@ -27,7 +27,7 @@ def get_cluster_config_obj(): return get_config_by_fn(app.config['cluster_config_fn']) -@cache.memoize(60, unless=(lambda: app.debug)) +#@cache.memoize(60, unless=(lambda: app.debug)) def get_config_by_fn(fn): return get_config(fn) @@ -48,7 +48,7 @@ def xmlrpc_call( elem_args, extra_args, fast_update=False ): func = elem_args['func'] - @cache.memoize(1, unless = (lambda: fast_update) ) +# @cache.memoize(1, unless = (lambda: fast_update) ) def cachable( func, args ): """This function construct allow caching by matching func, args""" f = getattr(xmlrpc_server, func) @@ -130,4 +130,4 @@ def html_gen(): else: cluster_config_fn = './examples/OLT_cluster_config.ini' app.config.update( dict( cluster_config_fn=cluster_config_fn )) app.debug = True - app.run(host='0.0.0.0', port=80) + app.run(host='0.0.0.0', port=5000) diff --git a/code/examples/CafeDemo/UI_config.ini b/code/examples/CafeDemo/UI_config.ini new file mode 100644 index 0000000..a831485 --- /dev/null +++ b/code/examples/CafeDemo/UI_config.ini @@ -0,0 +1,28 @@ +[Controls] + id = 095668e8 + [[Set Temp]] + type = slider + func = set_angle + min = 0 + max = 180 + step = 1 + init_val = 90 + id = eb5d4841 + +[Monitor] + id = 0484edb9 + [[Temperature]] + type = text + func = get_temperature + refresh interval = 1 + id = 08bd6d5f + [[Humidity]] + type = text + func = get_humidity + refresh interval = 1 + id = 1098734e + [[Orientation]] + type = text + func = get_orientation + refresh interval = 1 + id = 2c6ccb77 diff --git a/code/examples/CafeDemo/device.py b/code/examples/CafeDemo/device.py new file mode 100644 index 0000000..29b3d01 --- /dev/null +++ b/code/examples/CafeDemo/device.py @@ -0,0 +1,55 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +import serial, sys, threading + +class Device(): + def __init__(self, serial_port = "COM5"): + self.ser = serial.Serial(serial_port, 9600, timeout=3) + self.lock = threading.Lock() + self.ser_lock = threading.Lock() + self.temperature = -1; + self.humidity = -1; + self.orientation = -1; + def update_thread(): + while(self.ser.isOpen()): + with self.ser_lock: + self.ser.flushInput() + self.ser.readline() + new_line = self.ser.readline() + with self.lock: + self.orientation = ord(new_line[0]) + self.temperature = ord(new_line[1]) + self.humidity = ord(new_line[2]) + thread = threading.Thread( target = update_thread ) + thread.daemon = True + thread.start() + + def __del__(self): + self.ser.close(); + + def set_angle(self, angle): + cmd = "1 %i\n"%( int(float(angle)) ); + print "writing command: " + cmd + sys.stdout.flush() + with self.ser_lock: + self.ser.write( cmd ) + self.ser.flushOutput() + return angle + + def get_temperature(self): + with self.lock: + return self.temperature + + def get_humidity(self): + with self.lock: + return self.humidity + + def get_orientation(self): + with self.lock: + return self.orientation + +if __name__ == "__main__": + r = Device() + print r.get_temperature() + print r.get_humidity() + print r.set_angle(20) diff --git a/code/examples/OLT_cluster_config.ini b/code/examples/OLT_cluster_config.ini index b2322d1..8c139a9 100644 --- a/code/examples/OLT_cluster_config.ini +++ b/code/examples/OLT_cluster_config.ini @@ -19,3 +19,8 @@ ip = lz307pi-urop.eng.cam.ac.uk:2000 config_file = ./examples/OLT_PiCamera/UI_config.ini id = fe058a5c + + [[Pub Demo]] + ip = localhost:9000 + config_file = ./examples/CafeDemo/UI_config.ini + id = fb66d9af