changeset 251:b6079cb0c665

merge
author Matt Johnston <matt@ucc.asn.au>
date Wed, 27 May 2015 23:46:06 +0800
parents c9b20d3d393a (diff) 141948a400a6 (current diff)
children 0a1b642e3086
files
diffstat 4 files changed, 51 insertions(+), 27 deletions(-) [+]
line wrap: on
line diff
--- a/py/gpio_rpi.py	Wed May 27 23:45:03 2015 +0800
+++ b/py/gpio_rpi.py	Wed May 27 23:46:06 2015 +0800
@@ -7,15 +7,43 @@
 __all__ = ["Gpio"]
 
 class Gpio(object):
-	def __init__(self, pin, name):
-		self.pin = pin
-		self.name = name
-		GPIO.setmode(GPIO.BOARD)
-		GPIO.setup(self.pin, GPIO.OUT)
+    SYS_GPIO_BASE = '/sys/class/gpio/gpio'
+    def __init__(self, pin, name):
+        self.pin = pin
+        self.name = name
+
+        dir_fn = '%s%d/direction' % (self.SYS_GPIO_BASE, pin)
+        with open(dir_fn, 'w') as f:
+            # make sure it doesn't start "on"
+            f.write('low')
+        val_fn = '%s%d/value' % (self.SYS_GPIO_BASE, pin)
+        self.value_file = open(val_fn, 'r+')
+
+    def turn(self, value):
+        self.value_file.seek(0)
+        self.value_file.write('1' if value else '0')
+        self.value_file.flush()
 
-	def turn(self, value):
-		self.state = bool(value)
-		GPIO.output(self.pin, self.state)
+    def get_state(self):
+        self.value_file.seek(0)
+        buf = self.value_file.read().strip()
+        if buf == '0':
+            return False
+        if buf != '1':
+            E("Bad value read from gpio '%s': '%s'" 
+                % (self.value_file.name, buf))
+        return True
+
 
-	def get_state(self):
-		return GPIO.input(self.pin)
+def main():
+    g = Gpio(17, 'f')
+    g.turn(1)
+
+    print(g.get_state())
+
+    g.turn(0)
+
+    print(g.get_state())
+
+if __name__ == '__main__':
+    main()
--- a/py/sensor_ds18b20.py	Wed May 27 23:45:03 2015 +0800
+++ b/py/sensor_ds18b20.py	Wed May 27 23:46:06 2015 +0800
@@ -8,7 +8,7 @@
 import config
 from utils import D,L,W,E,EX
 
-class DS18B20s(object):
+class SensorDS18B20(object):
 
     THERM_RE = re.compile('.* YES\n.*t=(.*)\n', re.MULTILINE)
 
@@ -37,6 +37,7 @@
             yield from self.do()
             yield from self.server.sleep(config.SENSOR_SLEEP)
 
+
     @asyncio.coroutine
     def read_wait(self, f):
         # handles a blocking file read with a threadpool. A
@@ -45,7 +46,7 @@
         # the ds18b20 takes ~750ms to read, which is noticable
         # interactively.
         loop = asyncio.get_event_loop()
-        yield from loop.run_in_executor(self.readthread, f.read)
+        return loop.run_in_executor(None, f.read)
 
     @asyncio.coroutine
     def do_sensor(self, s, contents = None):
@@ -53,8 +54,8 @@
         try:
             if contents is None:
                 fn = os.path.join(self.master_dir, s, 'w1_slave')
-                f = open(fn, 'r')
-                contents = yield from self.read_wait(f)
+                with open(fn, 'r') as f:
+                    contents = yield from self.read_wait(f)
 
             match = self.THERM_RE.match(contents)
             if match is None:
@@ -71,7 +72,8 @@
 
     def do_internal(self):
         try:
-            return int(open(config.INTERNAL_TEMPERATURE, 'r').read()) / 1000.0
+            with open(config.INTERNAL_TEMPERATURE, 'r') as f:
+                return int(f.read()) / 1000.0
         except Exception as e:
             EX("Problem reading internal sensor: %s" % str(e))
             return None
@@ -80,7 +82,8 @@
     def sensor_names(self):
         """ Returns a sequence of sensorname """
         slaves_path = os.path.join(self.master_dir, "w1_master_slaves")
-        contents = open(slaves_path, 'r').read()
+        with open(slaves_path, 'r') as f:
+            contents = f.read()
         if 'not found' in contents:
             E("No W1 sensors found")
             return []
--- a/py/setup_gpio.sh	Wed May 27 23:45:03 2015 +0800
+++ b/py/setup_gpio.sh	Wed May 27 23:46:06 2015 +0800
@@ -2,15 +2,8 @@
 
 # this must run as root
 
-PINS="17 7 24 25"
-GROUP=fridgeio
+PINS="17"
 
 for PIN in $PINS; do
     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
 done
--- a/py/uploader.py	Wed May 27 23:45:03 2015 +0800
+++ b/py/uploader.py	Wed May 27 23:46:06 2015 +0800
@@ -48,9 +48,9 @@
         if self.server.test_mode():
             D("Would upload %s to %s" % (js, config.UPDATE_URL))
             return
-        js_enc = binascii.b2a_base64(zlib.compress(js.encode()))
-        mac = hmac.new(config.HMAC_KEY.encode(), js_enc, hashlib.sha1).hexdigest()
-        send_data = {'data': js_enc, 'hmac': mac}
+        js_enc = binascii.b2a_base64(zlib.compress(js.encode())).strip()
+        mac = hmac.new(config.HMAC_KEY.encode(), js_enc, hashlib.sha256).hexdigest()
+        send_data = {'data': js_enc.decode(), 'hmac': mac}
         r = yield from asyncio.wait_for(aiohttp.request('post', config.UPDATE_URL, data=send_data), 60)
         result = yield from asyncio.wait_for(r.text(), 60)
         if result != 'OK':