class Receiver(): def __init__(self): info('receiver', 'init') self.queue = Queue() self.mqtt = MQTT(os.environ['KIT_CHANNEL'], client_id='receiver-mqtt', queue=self.queue) self.running = False # Start the signer. def start(self, pipe): info('receiver', 'start') self.mqtt.connect(os.environ['KIT_MQTT_HOST'], int(os.environ['KIT_MQTT_PORT']), os.environ['KIT_DEVICE_ID'], os.environ['KIT_DEVICE_KEY']) while not self.mqtt.connected: info('receiver', 'waiting for connection') time.sleep(1) info('receiver', 'connected') self.pipe = pipe self.running = True while self.running: # Check the queue for a received message. msg = self.queue.get() # If for this device then pass to controller. if msg.is_valid() and msg.for_device(): info('receiver', 'queue: ' + str(msg)) self.pipe.send(msg) # Stop the signer. def stop(self): info('receiver', 'stop') self.running = False try: self.mqtt.disconnect() #self.pipe.close() except Exception as ex: error('receiver', 'stop error: ' + str(ex))
@app.route("/", methods=["POST"]) def setLight(): data = request.get_json() red = int(data["red"]) blue = int(data["blue"]) green = int(data["green"]) setLightValues(red, gree, blue) return str(red) + ", " + str(green) + ", " + str(blue) def setLightValues(red, green, blue): led.setRed(red) led.setGreen(green) led.setBlue(blue) mqtt.publish_rgb_state(red, green, blue) mqtt = MQTT(setLightValues) if __name__ == "__main__": mqttBroker = os.environ.get("MQTT_BROKER") mqttUser = os.environ.get("MQTT_USER") mqttPass = os.environ.get("MQTT_PASSWORD") if (mqttBroker == None): print("No MQTT broker set, starting in HTTP mode") app.run(host='0.0.0.0') else: mqtt.connect(mqttBroker, mqttUser, mqttPass)