changeset 447:8fdf86ea41e7

few updates, seems to run
author Matt Johnston <matt@ucc.asn.au>
date Sat, 15 Dec 2012 23:49:08 +0800
parents d8ff29036740
children fe729664a5e6
files .hgignore py/config.py py/fridge.py py/params.py py/sensor_ds18b20.py py/setup_gpio.sh py/tempserver.py py/test.py requirements.txt
diffstat 9 files changed, 57 insertions(+), 21 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/.hgignore	Sat Dec 15 23:49:08 2012 +0800
@@ -0,0 +1,1 @@
+venv
--- a/py/config.py	Sun Dec 09 20:07:15 2012 +0800
+++ b/py/config.py	Sat Dec 15 23:49:08 2012 +0800
@@ -1,8 +1,17 @@
-FRIDGE_GPIO = '/sys/devices/virtual/gpio/gpio5'
+FRIDGE_GPIO = '/sys/devices/virtual/gpio/gpio17'
 FRIDGE_SLEEP = 60
 
+FRIDGE_DELAY = 600 # 10 mins
+
+FRIDGE_WORT_INVALID_TIME = 300 # 5 mins
+
 SENSOR_SLEEP = 120
 
 UPLOAD_SLEEP = 300
 
-PARAMS_FILE='./tempserver.conf'
+PARAMS_FILE = './tempserver.conf'
+
+SENSOR_BASE_DIR = '/sys/devices/w1_bus_master1'
+
+WORT_NAME = 'aa'
+FRIDGE_NAME = 'bb'
--- a/py/fridge.py	Sun Dec 09 20:07:15 2012 +0800
+++ b/py/fridge.py	Sat Dec 15 23:49:08 2012 +0800
@@ -22,7 +22,7 @@
         with open(dir_fn, 'w') as f:
             f.write('low')
         val_fn = '%s/value' % config.FRIDGE_GPIO
-        self.value_file = f.open(val_fn, 'r+')
+        self.value_file = open(val_fn, 'r+')
 
     def turn(self, value):
         self.value_file.seek(0)
--- a/py/params.py	Sun Dec 09 20:07:15 2012 +0800
+++ b/py/params.py	Sat Dec 15 23:49:08 2012 +0800
@@ -2,6 +2,7 @@
 import collections
 import json
 import config
+from utils import W,L,E,EX
 
 _FIELD_DEFAULTS = {
     'fridge_setpoint': 16,
@@ -27,7 +28,11 @@
 
     def load(self, f = None):
         if not f:
-            f = file(config.PARAMS_FILE, 'r')
+            try:
+                f = file(config.PARAMS_FILE, 'r')
+            except IOError, e:
+                W("Missing parameter file, using defaults. %s", e)
+                return
         u = json.load(f)
         for k in u:
             if k not in self:
--- a/py/sensor_ds18b20.py	Sun Dec 09 20:07:15 2012 +0800
+++ b/py/sensor_ds18b20.py	Sat Dec 15 23:49:08 2012 +0800
@@ -1,6 +1,7 @@
 #!/usr/bin/env python2.7
 
 import gevent
+import gevent.threadpool
 import config
 import re
 from utils import L,W,E,EX
@@ -12,23 +13,29 @@
     def __init__(self, server):
         gevent.Greenlet.__init__(self)
         self.server = server
-        # XXX set up paths
-        # XXX set up drain etc
+        self.readthread = gevent.threadpool.ThreadPool(1)
+        self.master_dir = config.SENSOR_BASE_DIR
 
     def _run(self):
         while True:
             self.do()
             gevent.sleep(config.SENSOR_SLEEP)
 
-    def sensor_path(self, s):
-        return os.path.join(self.master_dir, s)
+    def read_wait(self, f):
+        # handles a blocking file read with a gevent threadpool. A
+        # real python thread performs the read while other gevent
+        # greenlets keep running.
+        # the ds18b20 takes ~750ms to read, which is noticable
+        # interactively.
+        return self.readthread.apply(lambda: f.read)
 
-    def do_sensor_name(self, s, contents = None):
+    def do_sensor(self, s, contents = None):
+        """ contents can be set by the caller for testing """
         try:
             if contents is None:
-                fn = os.path.join(self.sensor_path(s), 'w1_slave')
+                fn = os.path.join(self.master_dir, s, 'w1_slave')
                 f = open(fn, 'r')
-                contents = f.read()
+                contents = self.read_wait(f)
             match = self.THERM_RE.match(contents)
             if match is None:
                 return None
@@ -49,8 +56,8 @@
 
     def sensor_names(self):
         """ Returns a sequence of sensorname """
-        return [d for d in os.listdir(self.master_dir) if
-            os.stat(sensor_path(d)).st_mode & stat.S_ISDIR]
+        slaves_path = os.path.join(self.master_dir, "w1_master_slaves")
+        return open(slaves_path, 'r').split()
 
     def wort_name(self):
         return config.WORT_NAME
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/py/setup_gpio.sh	Sat Dec 15 23:49:08 2012 +0800
@@ -0,0 +1,14 @@
+#!/bin/sh
+
+# this must run as root
+
+PIN=17
+GROUP=fridgeio
+
+echo $PIN > /sys/class/gpio/export
+
+for f in direction value; do
+    fn=/sys/devices/virtual/gpio/gpio$PIN/$f
+    chgrp $GROUP $fn
+    chmod g+rw $fn
+done
--- a/py/tempserver.py	Sun Dec 09 20:07:15 2012 +0800
+++ b/py/tempserver.py	Sat Dec 15 23:49:08 2012 +0800
@@ -1,4 +1,4 @@
-#!/usr/bin/env python2.7
+#!/home/matt/templog/venv/bin/python
 
 import sys
 import os
@@ -39,7 +39,7 @@
         return utils.monotonic_time()
 
     def set_sensors(self, sensors):
-        if self.hasattr(self, 'sensors'):
+        if hasattr(self, 'sensors'):
             self.sensors.kill()
         self.sensors = sensors
         self.wort_name = sensors.wort_name()
@@ -64,7 +64,7 @@
 
     def current_temps(self):
         """ returns (wort_temp, fridge_temp) tuple """
-        return current
+        return self.current
 
 def setup_logging():
     logging.basicConfig(format='%(asctime)s %(message)s', 
--- a/py/test.py	Sun Dec 09 20:07:15 2012 +0800
+++ b/py/test.py	Sat Dec 15 23:49:08 2012 +0800
@@ -13,19 +13,19 @@
         f1 = """6e 01 4b 46 7f ff 02 10 71 : crc=71 YES
 6e 01 4b 46 7f ff 02 10 71 t=22875
 """
-        val = self.sensors.do_sensor_name('blank', f1)
+        val = self.sensors.do_sensor('blank', f1)
         self.assertEqual(val, 22.875)
 
         f2 = """6e 01 4b 46 7f ff 02 10 71 : crc=71 NO
 6e 01 4b 46 7f ff 02 10 71 t=22875
 """
-        val = self.sensors.do_sensor_name('blank', f2)
+        val = self.sensors.do_sensor('blank', f2)
         self.assertEqual(val, None)
 
         f3 = """6e 01 4b 46 7f ff 02 10 71 : crc=71 YES
 6e 01 4b 46 7f ff 02 10 71 t=-1
 """
-        val = self.sensors.do_sensor_name('blank', f3)
+        val = self.sensors.do_sensor('blank', f3)
         self.assertEqual(val, -0.001)
 
 class TestParams(unittest.TestCase):
--- a/requirements.txt	Sun Dec 09 20:07:15 2012 +0800
+++ b/requirements.txt	Sat Dec 15 23:49:08 2012 +0800
@@ -1,5 +1,5 @@
-RPi.GPIO==0.4.1a
 argparse==1.2.1
-gevent==0.13.8
+gevent==1.0rc2
 greenlet==0.4.0
+smbus==1.1
 wsgiref==0.1.2