def main(): evs = EventSubscriber("tcp://127.0.0.1:9001") evs.connect() print ("Connected: ", evs.connected) while True: event = evs.receiveEvent() ### Print Event Info ### print "########################" print event.id + ":" if event.elementtype == event.BUTTON: print " Type: BUTTON" elif event.elementtype == event.SLIDER: print " Type: SLIDER" if event.trigger == event.TRIGGERED: print " Triggered!" elif event.trigger == event.UNTRIGGERED: print " Untriggered!" elif event.trigger == event.INTRIGGER: print " Intrigger." ### Do some switching ### if event.id == "button_red" and event.trigger == event.TRIGGERED: setLolStripe("R") if event.id == "button_green" and event.trigger == event.TRIGGERED: setLolStripe("G") if event.id == "button_blue" and event.trigger == event.TRIGGERED: setLolStripe("B")
class CraftUIMQTTPub: def __init__(self, broker_host="localhost", craftui_url="tcp://127.0.0.1:9001"): self.baseTopic = "craftui/" self.brokerHost = broker_host self.eventSubscriber = EventSubscriber(craftui_url) self.eventSubscriber.connect() def run(self): while True: event = self.eventSubscriber.receiveEvent() # button events if event.elementtype == event.BUTTON: topic = "button/" + event.id if event.trigger == event.TRIGGERED: msg = "DOWN" elif event.trigger == event.UNTRIGGERED: msg = "UP" self.pub(topic, msg) def pub(self, topic, msg): fulltopic = self.baseTopic + topic #FIXME use python-mosquitto os.system("mosquitto_pub -h " + self.brokerHost + " -t " + fulltopic + " -m " + msg ) print(fulltopic + " " + msg)
def main(): ## Parse Command Line Arguments if "-h" in sys.argv or "--help" in sys.argv: printHelp() parser = argparse.ArgumentParser() parser.add_argument("--host", default="127.0.0.1") parser.add_argument("-p", type=int, default=9001) parser.add_argument("--short", default=False, action="store_true") parser.add_argument("--logfile") args = parser.parse_args() printShort = args.short doLogfile = (args.logfile != None) fname = args.logfile ## Connect to CraftUI hostUrl = "tcp://" + args.host + ":" + str(args.p) sys.stdout.write("Connecting.... ") evs = EventSubscriber("tcp://127.0.0.1:9001") evs.connect() if evs.connected: print "done." else: print "ERROR." sys.exit(1) ## Open the logfile in append mode. logfile = None if doLogfile: try: logfile = open(fname, "a") print "Appending to logfile '" + fname + "'" except: print "Error accesing logfile." exit(1) ## Process events while True: event = evs.receiveEvent() if printShort: s = shortEventString(event) else: s = fullEventString(event) print(s) if logfile: logfile.write(s) logfile.write("\n") # Close the file if logfile: logfile.close()
def main(): brokerhost = "localhost" if (len(sys.argv) > 1): brokerhost = sys.argv[1] evs = EventSubscriber("tcp://127.0.0.1:9001") evs.connect() print("Connected: ", evs.connected) ircclient = craftuiirc.CraftUIIRC("irc.servus.at", 6667, "#devlol") ircclient.start() while True: event = evs.receiveEvent() ### Print Event Info ### print "########################" print event.id + ":" if event.elementtype == event.BUTTON: print " Type: BUTTON" elif event.elementtype == event.SLIDER: print " Type: SLIDER" if event.trigger == event.TRIGGERED: print " Triggered!" elif event.trigger == event.UNTRIGGERED: print " Untriggered!" elif event.trigger == event.INTRIGGER: print " Intrigger." ### The LoL strip ### if event.id == "button_red" and event.trigger == event.TRIGGERED: setLolStripe("R", brokerhost) setProjMappingColor("R", brokerhost) if event.id == "button_green" and event.trigger == event.TRIGGERED: setLolStripe("G", brokerhost) setProjMappingColor("G", brokerhost) if event.id == "button_blue" and event.trigger == event.TRIGGERED: setLolStripe("B", brokerhost) setProjMappingColor("B", brokerhost) ### Print to IRC ### if event.id == "button_black" and event.trigger == event.TRIGGERED: ircclient.postLinesRateLimited("hi", 10, ["Someone says Hi at the window!"]) displayHelloOnIRC(brokerhost) if event.id == "buttonHi5" and event.trigger == event.TRIGGERED: ircclient.postLinesRateLimited("hi5", 10, ["Hi 5!"]) displayHi5(brokerhost) #toggleChico(2) ircclient.stop()
def __init__(self, broker_host="localhost", craftui_url="tcp://127.0.0.1:9001"): self.baseTopic = "craftui/" self.brokerHost = broker_host self.eventSubscriber = EventSubscriber(craftui_url) self.eventSubscriber.connect()