changeset 822:b1c92ee20ff4

yard light toggle button Ignore-this: c003731daf656c377440278527522d75 darcs-hash:20111204030307-312f9-b076d447ae7f7d2c1060b89b921e68c245e38e02.gz
author drewp <drewp@bigasterisk.com>
date Sat, 03 Dec 2011 19:03:07 -0800
parents f045826df601
children 0aafd40e4afc
files service/frontDoorArduino/frontDoorArduino.py service/frontDoorArduino/index.html
diffstat 2 files changed, 55 insertions(+), 7 deletions(-) [+]
line wrap: on
line diff
--- a/service/frontDoorArduino/frontDoorArduino.py	Sat Dec 03 19:01:18 2011 -0800
+++ b/service/frontDoorArduino/frontDoorArduino.py	Sat Dec 03 19:03:07 2011 -0800
@@ -6,6 +6,9 @@
 |-------------------|
 22:05 85F in, 71F out
 
+pin 11 senses the door
+pin 12 activates the front yard lights ('out yard')
+
 """
 
 from __future__ import division
@@ -29,10 +32,9 @@
         self.ser = LoggingSerial(port=port)
         self.ser.flush()
 
-        self.ser.write("\xff\x00\x00")
-        self.ser.write("\xff\x03\x00")
-        self.currentText = ""
-        self.currentBrightness = 0
+        self.setLcd("")
+        self.setLcdBrightness(0)
+        self.setYardLight(0)
 
     def ping(self):
         self.getDoor()
@@ -42,6 +44,13 @@
         ret = self.ser.readJson()
         return ret['door']
 
+    def setYardLight(self, level):
+        self.currentYardLight = bool(level)
+        self.ser.write("\xff\x04" + chr(bool(self.currentYardLight)))
+
+    def getYardLight(self):
+        return self.currentYardLight
+
     def getLcd(self):
         return self.currentText
         
@@ -101,6 +110,21 @@
         self.write("ok")
     post = put
 
+
+class YardLight(PrettyErrorHandler, cyclone.web.RequestHandler):
+    def get(self):
+        self.set_header("Content-Type", "application/json")
+        self.write(json.dumps({
+            "yardLight" : self.settings.board.getYardLight()}))
+        
+    def put(self):
+        """text true or false or 0 or 1"""
+        self.settings.board.setYardLight(
+            self.request.body.strip() in ['true', '1'])
+        self.write("ok")
+    post = put
+
+
 class Door(PrettyErrorHandler, cyclone.web.RequestHandler):
     def get(self):
         self.set_header("Content-Type", "text/plain")
@@ -120,6 +144,7 @@
             (r'/door', Door),
             (r'/temperature', Temperature),
             (r'/lcd/backlight', Backlight),
+            (r'/yardLight', YardLight),
         ]
         settings = {"board" : board}
         cyclone.web.Application.__init__(self, handlers, **settings)
@@ -169,7 +194,7 @@
         # todo: need options to preset inputs/outputs at startup
         }
 
-    log.startLogging(sys.stdout)
+    #log.startLogging(sys.stdout)
 
     board = Board(port=config['arduinoPort'])
     
--- a/service/frontDoorArduino/index.html	Sat Dec 03 19:01:18 2011 -0800
+++ b/service/frontDoorArduino/index.html	Sat Dec 03 19:03:07 2011 -0800
@@ -15,8 +15,8 @@
    background: none repeat scroll 0 0 #E1E1DF;
     border: 1px solid #595555;
     float: left;
-    margin: 20px;
-    padding: 20px;
+    margin: 5px;
+    padding: 10px;
 }
 	/* ]]> */
 </style>
@@ -48,6 +48,13 @@
       <button id="getDoor">refresh</button>
       </div>
     </section>
+    <section>
+      <h1>yard light</h1>
+      <div>Current: <span id="yardLight"/>
+      <button id="toggleYardLight">toggle</button>
+      </div>
+    </section>
+    
     <script type="text/javascript">
     // <![CDATA[
     $(function () {
@@ -84,6 +91,22 @@
 	getDoor();
 	$("#getDoor").click(getDoor);
 
+	function refreshYardLight() {
+	$.getJSON("yardLight", function (data) {
+	    $("#yardLight").text(data.yardLight);
+	  });
+	}
+	refreshYardLight();
+	$("#toggleYardLight").click(function () {
+	    $.getJSON("yardLight", function (data) {
+		$.ajax({
+		  type: "PUT",
+   	          url: "yardLight",
+		      data: JSON.stringify(!data.yardLight),
+		      success: refreshYardLight
+		});
+	    });
+	});
     });
 	    // ]]>
 </script>