Skip to content

Commit

Permalink
few updates, seems to run
Browse files Browse the repository at this point in the history
  • Loading branch information
mkj committed Dec 15, 2012
1 parent 1b9b893 commit 07ce8e6
Show file tree
Hide file tree
Showing 9 changed files with 57 additions and 21 deletions.
1 change: 1 addition & 0 deletions .hgignore
@@ -0,0 +1 @@
venv
13 changes: 11 additions & 2 deletions py/config.py
@@ -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'
2 changes: 1 addition & 1 deletion py/fridge.py
Expand Up @@ -22,7 +22,7 @@ def setup_gpio(self):
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)
Expand Down
7 changes: 6 additions & 1 deletion py/params.py
Expand Up @@ -2,6 +2,7 @@
import collections
import json
import config
from utils import W,L,E,EX

_FIELD_DEFAULTS = {
'fridge_setpoint': 16,
Expand All @@ -27,7 +28,11 @@ def __setattr__(self, k, v):

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:
Expand Down
25 changes: 16 additions & 9 deletions py/sensor_ds18b20.py
@@ -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
Expand All @@ -12,23 +13,29 @@ class DS18B20s(gevent.Greenlet):
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
Expand All @@ -49,8 +56,8 @@ def do(self):

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
Expand Down
14 changes: 14 additions & 0 deletions py/setup_gpio.sh
@@ -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
6 changes: 3 additions & 3 deletions py/tempserver.py
@@ -1,4 +1,4 @@
#!/usr/bin/env python2.7
#!/home/matt/templog/venv/bin/python

import sys
import os
Expand Down Expand Up @@ -39,7 +39,7 @@ def now(self):
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()
Expand All @@ -64,7 +64,7 @@ def add_reading(self, reading):

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',
Expand Down
6 changes: 3 additions & 3 deletions py/test.py
Expand Up @@ -13,19 +13,19 @@ def test_sensors_regex(self):
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):
Expand Down
4 changes: 2 additions & 2 deletions requirements.txt
@@ -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

0 comments on commit 07ce8e6

Please sign in to comment.