changeset 489:46e327c00246

gets current params
author Matt Johnston <matt@ucc.asn.au>
date Tue, 11 Feb 2014 22:10:32 +0800
parents 4792e9910cde
children f2e990b99637
files web/atomicfile.py web/log.py web/secure.py web/templog.py web/views/set.tpl web/watch.py
diffstat 6 files changed, 78 insertions(+), 13 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/web/atomicfile.py	Tue Feb 11 22:10:32 2014 +0800
@@ -0,0 +1,46 @@
+import os
+import time
+import fcntl
+import sys
+
+class AtomicFile(object):
+	DELAY = 0.5
+	def __init__(self, name):
+		self.name = name
+
+	def write(self, data, timeout = 5):
+		try:
+			end = time.time() + timeout
+			with open(self.name, "r+") as f:
+				while timeout == 0 or time.time() < end:
+					try:
+						fcntl.lockf(f, fcntl.LOCK_EX | fcntl.LOCK_NB)
+					except IOError:
+						time.sleep(DELAY)
+						continue
+
+					f.write(data)
+					return True
+
+		except IOError, e:
+			print>>sys.stderr, e
+
+		return False
+
+	def read(self, timeout = 5):
+		try:
+			end = time.time() + timeout
+			with open(self.name, "r") as f:
+				while timeout == 0 or time.time() < end:
+					try:
+						fcntl.lockf(f, fcntl.LOCK_SH | fcntl.LOCK_NB)
+					except IOError:
+						time.sleep(DELAY)
+						continue
+
+					return f.read()
+
+		except IOError, e:
+			print>>sys.stderr, e
+
+		return None
--- a/web/log.py	Sun Feb 09 11:41:13 2014 +0800
+++ b/web/log.py	Tue Feb 11 22:10:32 2014 +0800
@@ -18,6 +18,7 @@
 from colorsys import hls_to_rgb
 
 import config
+import atomicfile
 
 def sensor_rrd_path(s):
     return '%s/sensor_%s.rrd' % (config.DATA_PATH, str(s))
@@ -161,7 +162,7 @@
 #            '--right-axis-label', 'Temperature'
             ]
 
-	print>>sys.stderr, ' '.join("'%s'" % s for s in args)
+	#print>>sys.stderr, ' '.join("'%s'" % s for s in args)
     rrdtool.graph(*args)
     #return tempf
     return tempf.read()
@@ -222,6 +223,18 @@
     tick_secs = int(entries['tick_secs'])
     return val_ticks + float(val_rem) * tick_secs / tick_wake
 
+def write_current_params(current_params):
+    out = {}
+    out['params'] = current_params
+    out['time'] = time.time()
+    atomicfile.AtomicFile("%s/current_params.txt" % config.DATA_PATH).write(
+        json.dumps(out, sort_keys=True, indent=4)+'\n')
+
+def read_current_params():
+    p = atomicfile.AtomicFile("%s/current_params.txt" % config.DATA_PATH).read()
+    dat = json.loads(p)
+    return dat['params']
+
 def parse(params):
 
     start_time = time.time()
@@ -243,8 +256,11 @@
             measurements.setdefault(s, []).append((real_t, v))
 
     # one-off measurements here
+    current_params = params['current_params']
     measurements['fridge_on'] = [ (time.time(), params['fridge_on']) ]
-    measurements['fridge_setpoint'] = [ (time.time(), params['fridge_setpoint']) ]
+    measurements['fridge_setpoint'] = [ (time.time(), current_params['fridge_setpoint']) ]
+
+    write_current_params(current_params)
 
     for s, vs in measurements.iteritems():
         sensor_update(s, vs)
@@ -266,8 +282,11 @@
         }
 
     r = []
+
+    vals = read_current_params()
+
     for k, v in _FIELD_DEFAULTS.iteritems():
-        n = {'name': k, 'value': v}
+        n = {'name': k, 'value': vals[k]}
         if type(v) is bool:
             kind = 'yesno'
         else:
--- a/web/secure.py	Sun Feb 09 11:41:13 2014 +0800
+++ b/web/secure.py	Tue Feb 11 22:10:32 2014 +0800
@@ -15,7 +15,8 @@
 HASH=hashlib.sha1
 
 def get_user_hash():
-    if bottle.request.environ.get('SSL_CLIENT_VERIFY', '') != 'GENEROUS':
+    verify = bottle.request.environ.get('SSL_CLIENT_VERIFY', '')
+    if not (verify == 'GENEROUS' or verify == 'SUCCESS'):
         return 'FAILVERIFY'
     blob = bottle.request.environ.get('SSL_CLIENT_CERT')
     if not blob:
--- a/web/templog.py	Sun Feb 09 11:41:13 2014 +0800
+++ b/web/templog.py	Tue Feb 11 22:10:32 2014 +0800
@@ -18,6 +18,7 @@
 import config
 import log
 import secure
+import atomicfile
 
 DATE_FORMAT = '%Y%m%d-%H.%M'
 ZOOM_SCALE = 2.0
--- a/web/views/set.tpl	Sun Feb 09 11:41:13 2014 +0800
+++ b/web/views/set.tpl	Tue Feb 11 22:10:32 2014 +0800
@@ -257,11 +257,13 @@
             setter.edit(param, Number(this.value));
         });
 
-        $(".button_up", el).on("vmousedown", function() {
+        $(".button_up", el).on("vmousedown", function(e) {
+            e.preventDefault();
             setter.adjust(param, 1);
             this.blur()
         });
-        $(".button_down", el).on("vmousedown", function() {
+        $(".button_down", el).on("vmousedown", function(e) {
+            e.preventDefault();
             setter.adjust(param, -1);
             this.blur()
         });
@@ -274,12 +276,12 @@
         var button_yes = $(".button_yes", el);
         var button_no = $(".button_no", el);
 
-        button_yes.on("vmousedown", function() {
+        button_yes.on("vmousedown", function(e) {
             setter.edit(param, true);
             this.blur()
         })
 
-        button_no.on("vmousedown", function() {
+        button_no.on("vmousedown", function(e) {
             setter.edit(param, false);
             this.blur()
         })
--- a/web/watch.py	Sun Feb 09 11:41:13 2014 +0800
+++ b/web/watch.py	Tue Feb 11 22:10:32 2014 +0800
@@ -27,8 +27,6 @@
 		pattern = os.path.basename(g)
 		dirpatterns.setdefault(d, []).append(pattern)
 
-	print(dirpatterns)
-
 	watchpatterns = {}
 	for d, patterns in dirpatterns.items():
 
@@ -46,12 +44,10 @@
 		if event.name is None:
 			return
 
-		print("%s %s " % (event.name, event.maskname))
 		patterns = watchpatterns[event.wd]
 		for p in patterns:
-			print(p)
 			if fnmatch.fnmatch(event.name, p):
-				print("matched %s" % p)
+				print("matched %s %s" % (event.name, p, event.maskname))
 				os.utime(touchf, None)
 
 	n = pyinotify.Notifier(watcher, triggered)