Jiffy/mqtt_run.py
2019-04-21 20:51:43 +01:00

47 lines
1.0 KiB
Python

#!/usr/bin/env python
import logging
import paho.mqtt.client as mqtt
from Jiffy import Jiffy
logging.basicConfig(level=logging.DEBUG)
jiffy = Jiffy(
host='dashpi-g1',
api_url_v1_0='api/v1',
api_url_v1_2='api/v1.2',
credentials=(
'admin',
'hacklab'
)
)
def send_jiffy(client, url):
jiffy.display_media_from_url(url, 10)
def on_connect(client, userdata, flags, rc):
logging.debug("connected with result " + str(rc))
client.subscribe('display/nec/#')
def on_message(client, userdata, msg):
try:
lastmsg = msg.payload.decode("utf-8")
#logging.debug(lastmsg)
except Exception as e:
lastmsg = ''
logging.debug("Error {}".format(e))
if msg.topic == 'display/nec/image':
logging.debug('image message received')
logging.debug('Image url is: {}'.format(lastmsg))
send_jiffy(client, lastmsg)
client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message
client.connect("mqtt.hacklab")
client.loop_forever()