From c66cc445466955bd01be797c4e3ed1242b433aa1 Mon Sep 17 00:00:00 2001 From: Cameron Sharp Date: Sun, 21 Apr 2019 20:21:01 +0100 Subject: [PATCH] Update Dockerfile, and add mqtt client --- Dockerfile | 4 ++++ cli_run.py | 1 + mqtt_run.py | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 51 insertions(+) create mode 100644 mqtt_run.py diff --git a/Dockerfile b/Dockerfile index a8a798e..5578777 100644 --- a/Dockerfile +++ b/Dockerfile @@ -8,3 +8,7 @@ WORKDIR /usr/src/app COPY requirements.txt ./ RUN pip install -r requirements.txt + +COPY . ./ + +CMD ["python", "mqtt_run.py"] \ No newline at end of file diff --git a/cli_run.py b/cli_run.py index ddf57a7..a34b78b 100644 --- a/cli_run.py +++ b/cli_run.py @@ -1,3 +1,4 @@ +#!/usr/bin/env python import sys from Jiffy import Jiffy diff --git a/mqtt_run.py b/mqtt_run.py new file mode 100644 index 0000000..820da2b --- /dev/null +++ b/mqtt_run.py @@ -0,0 +1,46 @@ +#!/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('test/jiffy/#') + +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 == 'test/jiffy/image': + logging.debug('image message received') + logging.debug('Image url is: ', 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()