From 1cf14c5755e96486f4cdf89687adb0f4a73a6e60 Mon Sep 17 00:00:00 2001 From: "ry.yamafuji" Date: Sun, 20 Oct 2024 18:57:44 +0900 Subject: [PATCH] =?UTF-8?q?=E6=9C=80=E5=B0=8FMQTT=E7=A2=BA=E8=AA=8D?= =?UTF-8?q?=E7=94=A8=E3=82=BD=E3=83=BC=E3=82=B9UP?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .env.sample | 5 +++++ .gitignore | 2 ++ README.md | 28 +++++++++++++++++++++++++++ client/mqtt_client.py | 34 +++++++++++++++++++++++++++++++++ docker-compose.yaml | 8 ++++++++ mosquitto/config/mosquitto.conf | 4 ++++ requirements.txt | 4 ++++ 7 files changed, 85 insertions(+) create mode 100644 .env.sample create mode 100644 .gitignore create mode 100644 client/mqtt_client.py create mode 100644 docker-compose.yaml create mode 100644 mosquitto/config/mosquitto.conf create mode 100644 requirements.txt diff --git a/.env.sample b/.env.sample new file mode 100644 index 0000000..71b8949 --- /dev/null +++ b/.env.sample @@ -0,0 +1,5 @@ +# MQTT CONFIGURATION +MQTT_HOST=localhost +MQTT_PORT=1883 +MQTT_DEVICE_CODE=test0001 + diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..f78d3c0 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +mqttmock +.env \ No newline at end of file diff --git a/README.md b/README.md index e69de29..fa4f46b 100644 --- a/README.md +++ b/README.md @@ -0,0 +1,28 @@ +# mosquitto-mock-server + +* Mosquittoを構築して、MQTTの動作を検証するためのブランチ +* Broker + * ブローカにはmosquittoを使用する +* Client + * Paho-mqttライブラリを使用して接続する + + +## init + + +### 開発用仮想空間を作成する + +```bash +python -m venv mqttmock +# (windows) +mqttmock\Scripts\activate +# (linux) +source mqttmock/bin/activate +``` + +ライブラリをインストールする + +```bash +pip install -r requirements.txt +``` + diff --git a/client/mqtt_client.py b/client/mqtt_client.py new file mode 100644 index 0000000..2fd94b8 --- /dev/null +++ b/client/mqtt_client.py @@ -0,0 +1,34 @@ +import paho.mqtt.client as mqtt +import os +from dotenv import load_dotenv +import time +import json + + +load_dotenv(dotenv_path=".env.test") + +MQTT_HOST = os.getenv("MQTT_HOST") +MQTT_PORT = int(os.getenv("MQTT_PORT")) +MQTT_DEVICE_CODE = int(os.getenv("MQTT_DEVICE_CODE")) + + +def on_connect_mock(client, userdata, flags, rc, properties): + print(f"Connected with result code {rc}") + + +def test_mqtt_mock(): + + # connect処理 + client = mqtt.Client() + client.on_connect = on_connect_mock + client.connect(MQTT_HOST, MQTT_PORT, 60) + time.sleep(5) + + +def main(): + print("Mqtt Mock Client Start.") + test_mqtt_mock() + + +if __name__ == "__main__": + main() diff --git a/docker-compose.yaml b/docker-compose.yaml new file mode 100644 index 0000000..e6a187b --- /dev/null +++ b/docker-compose.yaml @@ -0,0 +1,8 @@ +version: '3.8' +services: + mosquitto-mock: + image: eclipse-mosquitto:latest + ports: + - "1883:1883" # 標準のMQTTポート + volumes: + - ./mosquitto/config:/mosquitto/config # Mosquittoの設定ファイルが含まれるディレクトリ diff --git a/mosquitto/config/mosquitto.conf b/mosquitto/config/mosquitto.conf new file mode 100644 index 0000000..03a96d5 --- /dev/null +++ b/mosquitto/config/mosquitto.conf @@ -0,0 +1,4 @@ +set_tcp_nodelay true +listener 1883 +allow_anonymous true +max_queued_messages 0 diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..ee206ce --- /dev/null +++ b/requirements.txt @@ -0,0 +1,4 @@ +paho-mqtt +requests +python-dotenv +requests \ No newline at end of file