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