From 58916d7f87f08af9b9dded6d01b73e695ab6141f Mon Sep 17 00:00:00 2001 From: MinCheTsai Date: Thu, 13 Oct 2022 19:15:49 +0800 Subject: [PATCH] Add forwarder --- docker-compose.yml | 4 ++++ forwarder/Dockerfile | 12 ++++++++++++ forwarder/app.py | 38 ++++++++++++++++++++++++++++++++++++++ forwarder/requirements.txt | 2 ++ 4 files changed, 56 insertions(+) create mode 100644 forwarder/Dockerfile create mode 100644 forwarder/app.py create mode 100644 forwarder/requirements.txt diff --git a/docker-compose.yml b/docker-compose.yml index 1011918..11595e7 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -61,6 +61,10 @@ services: volumes: - ./configuration/mosquitto/mosquitto.conf:/mosquitto/config/mosquitto.conf + forwarder: + build: ./forwarder + restart: unless-stopped + volumes: postgresqldata: redisdata: diff --git a/forwarder/Dockerfile b/forwarder/Dockerfile new file mode 100644 index 0000000..f5682fe --- /dev/null +++ b/forwarder/Dockerfile @@ -0,0 +1,12 @@ +FROM python:3.7-slim-buster +COPY requirements.txt ./ + +RUN apt-get update && apt-get install -y cmake +RUN pip install -r ./requirements.txt + +COPY app.py ./ + +CMD ["python", "app.py"] + +RUN adduser --system forwarder +USER forwarder \ No newline at end of file diff --git a/forwarder/app.py b/forwarder/app.py new file mode 100644 index 0000000..b969489 --- /dev/null +++ b/forwarder/app.py @@ -0,0 +1,38 @@ +import os +# import awsiot.greengrasscoreipc +# import awsiot.greengrasscoreipc.client as client +# import awsiot.greengrasscoreipc.model as model +import json +import paho.mqtt.client as mqtt + +# ipc_client = awsiot.greengrasscoreipc.connect() + +print(os.environ) + +def on_connect(client, userdata, flags, rc): + print("Connected with result code "+str(rc)) + client.subscribe("application/+/device/+/event/up") + +def on_message(client, userdata, msg): + payload = json.loads(str(msg.payload.decode("utf-8"))) + print(payload['object']) + message = { + payload: payload['object'] + } + print(message) + # publish_operation = ipc_client.new_publish_to_iot_core() + # publish_operation.activate( + # request = model.PublishToIoTCoreRequest( + # topic_name = 'MCT/PY/FD', + # qos = model.QOS.AT_MOST_ONCE, + # payload = json.dumps(message).encode() + # ) + # ) + +client = mqtt.Client() +client.on_connect = on_connect +client.on_message = on_message + +client.connect("mosquitto", 1883, 60) + +client.loop_forever() \ No newline at end of file diff --git a/forwarder/requirements.txt b/forwarder/requirements.txt new file mode 100644 index 0000000..669b8f2 --- /dev/null +++ b/forwarder/requirements.txt @@ -0,0 +1,2 @@ +awsiotsdk==1.7.1 +paho-mqtt==1.6.1 \ No newline at end of file