Jiffy/mqtt_run.py

47 lines
1.0 KiB
Python
Raw Normal View History

2019-04-21 19:21:01 +00:00
#!/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))
2019-04-21 19:38:43 +00:00
client.subscribe('display/nec/#')
2019-04-21 19:21:01 +00:00
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))
2019-04-21 19:38:43 +00:00
if msg.topic == 'display/nec/image':
2019-04-21 19:21:01 +00:00
logging.debug('image message received')
2019-04-21 19:51:43 +00:00
logging.debug('Image url is: {}'.format(lastmsg))
2019-04-21 19:21:01 +00:00
send_jiffy(client, lastmsg)
client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message
client.connect("mqtt.hacklab")
client.loop_forever()