最小MQTT確認用ソースUP

This commit is contained in:
ry.yamafuji 2024-10-20 18:57:44 +09:00
parent b319d0a913
commit 1cf14c5755
7 changed files with 85 additions and 0 deletions

5
.env.sample Normal file
View File

@ -0,0 +1,5 @@
# MQTT CONFIGURATION
MQTT_HOST=localhost
MQTT_PORT=1883
MQTT_DEVICE_CODE=test0001

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
mqttmock
.env

View File

@ -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
```

34
client/mqtt_client.py Normal file
View File

@ -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()

8
docker-compose.yaml Normal file
View File

@ -0,0 +1,8 @@
version: '3.8'
services:
mosquitto-mock:
image: eclipse-mosquitto:latest
ports:
- "1883:1883" # 標準のMQTTポート
volumes:
- ./mosquitto/config:/mosquitto/config # Mosquittoの設定ファイルが含まれるディレクトリ

View File

@ -0,0 +1,4 @@
set_tcp_nodelay true
listener 1883
allow_anonymous true
max_queued_messages 0

4
requirements.txt Normal file
View File

@ -0,0 +1,4 @@
paho-mqtt
requests
python-dotenv
requests