サブスクライブのmockを作成する
This commit is contained in:
parent
a748baa646
commit
ec70067900
18
README.md
18
README.md
@ -26,9 +26,25 @@ source mqttmock/bin/activate
|
||||
pip install -r requirements.txt
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
```
|
||||
brokerに送信する
|
||||
|
||||
|
||||
```sh
|
||||
apt install mosquitto-clients
|
||||
|
||||
mosquitto_pub -h localhost -p 1883 -t "device" -m '{"msg": "test""}'
|
||||
```
|
||||
|
||||
サブスクライブを体験する
|
||||
|
||||
```sh
|
||||
mosquitto_pub -h localhost -p 1883 -t "device/{MQTT_DEVICE_CODE}/command" -m '{"msg": "test""}'
|
||||
```
|
||||
|
||||
MQTT_DEVICE_CODEが`test0001`の場合
|
||||
|
||||
```sh
|
||||
mosquitto_pub -h localhost -p 1883 -t "device/test0001/command" -m '{"msg": "test""}'
|
||||
```
|
||||
|
||||
@ -13,14 +13,37 @@ MQTT_DEVICE_CODE = os.getenv("MQTT_DEVICE_CODE")
|
||||
|
||||
|
||||
def on_connect_mock(client, userdata, flags, rc):
|
||||
print(f"Connected with result code rc={rc}")
|
||||
print(f"on_connect: rc={rc}")
|
||||
|
||||
|
||||
def on_disconnect_mock(client, userdata, rc):
|
||||
print(f"on_disconnect: rc={rc}")
|
||||
if rc != 0:
|
||||
print("Unexpected disconnection.")
|
||||
|
||||
|
||||
def on_publish_mock(client, userdata, mid):
|
||||
print("on_publish: {0}".format(mid))
|
||||
|
||||
|
||||
def on_subscribe_mock(mqttc, obj, mid, granted_qos):
|
||||
print("on_subscribe: "+str(mid)+" "+str(granted_qos))
|
||||
|
||||
|
||||
def on_message_mock(client, userdata, msg):
|
||||
print(f"on_message")
|
||||
print("Received message '" + str(msg.payload) +
|
||||
"' on topic '" + msg.topic + "' with QoS " + str(msg.qos))
|
||||
|
||||
|
||||
def test_mqtt_mock():
|
||||
|
||||
# connect処理
|
||||
client = mqtt.Client()
|
||||
client.on_connect = on_connect_mock
|
||||
client.on_disconnect = on_disconnect_mock
|
||||
client.on_publish = on_publish_mock
|
||||
client.on_subscribe = on_subscribe_mock
|
||||
client.on_message = on_message_mock
|
||||
client.connect(MQTT_HOST, MQTT_PORT, 60)
|
||||
topic = f"device/{MQTT_DEVICE_CODE}/command"
|
||||
client.subscribe(topic, qos=1)
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user