From 5e52d880436f7b22c1554121fc1b84c18fb78878 Mon Sep 17 00:00:00 2001 From: slaupster Date: Tue, 15 Sep 2015 10:47:52 +0100 Subject: [PATCH] Use the device cli rather than the app cli plus use a command callback instead of an event callback --- client.py | 27 ++++++++++----------------- 1 file changed, 10 insertions(+), 17 deletions(-) diff --git a/client.py b/client.py index bd5fc73..cdba701 100644 --- a/client.py +++ b/client.py @@ -1,40 +1,33 @@ import RPi.GPIO as GPIO import time import os, json -import ibmiotf.application +import ibmiotf.device import uuid GPIO.setmode(GPIO.BCM) GPIO.setup(18, GPIO.IN, pull_up_down=GPIO.PUD_UP) GPIO.setup(17, GPIO.OUT) -client = None - def myCommandCallback(cmd): - if cmd.event == "light": - command = cmd.payload["d"]["command"] - print command - if command == "on": + print "Received command %s %s" % (cmd.command, cmd.data) + if cmd.command == "light": + if cmd.data["command"] == "on": GPIO.output(17, True) - elif command == "off": + elif cmd.data["command"] == "off": GPIO.output(17, False) try: - options = ibmiotf.application.ParseConfigFile("/home/pi/device.cfg") - options["deviceId"] = options["id"] - options["id"] = "aaa" + options["id"] - client = ibmiotf.application.Client(options) + options = ibmiotf.device.ParseConfigFile("/home/me/device.cfg") + client = ibmiotf.device.Client(options) client.connect() - client.deviceEventCallback = myCommandCallback - client.subscribeToDeviceEvents(event="light") - + client.commandCallback = myCommandCallback + while True: GPIO.wait_for_edge(18, GPIO.FALLING) print "Button Pushed" myData = {'buttonPushed' : True} - client.publishEvent("raspberrypi", options["deviceId"], "input", "json", myData) + client.publishEvent("raspberrypievt", "json", myData) time.sleep(0.2) except ibmiotf.ConnectionException as e: print e -