domotruc/mqttgen
Composer 安装命令:
composer require domotruc/mqttgen
包简介
MQTT message flow generator
README 文档
README
MqttGen is an MQTT message generator.
MqttGen can be used to create MQTT message flow to test applications using the MQTT protocol. It was created to test the jMQTT Jeedom plugin.
MqttGen comes with two tools:
- mqttgen which is a kind of MQTT simulator allowing to send and receive messages. Behaviour is defined in a JSON input file.
- mqttplay which allows to replay a MQTT flow previously recorded thanks to an MQTT client such as mosquitto_sub.
Installation
The recommended way to install MqttGen is through Composer.
composer require domotruc/mqttgen
Usage of mqttgen
As a standalone application
vendor/bin/mqttgen your_json_input_file.json
To run the provided example, execute the following from the directory containing the composer.json file:
vendor/bin/mqttgen vendor/domotruc/mqttgen/topics.json
As a library
Following file is assumed to be in the same directory as your composer.json file.
<?php require __DIR__ . '/vendor/autoload.php'; try { $filename = 'vendor/domotruc/mqttgen/topics.json'; $mqttgen = new MqttGen\MqttGen($filename); do { $mqttgen->nextMessage(); sleep(2); } while (true); } catch (\Exception $e) { print($e->getMessage() . PHP_EOL); }
JSON input file
The library is provided with an example of JSON input file topics.json that gives and explains all the options.
Once the libray is installed using composer (see above), file topics.json can be found in the vendor/domotruc/mqttgen directory.
This file generates the following MQTT flows:
2018-06-24 08:38:33.171 boiler/status "online"
2018-06-24 08:38:35.173 boiler/brand "undisclosed brand"
2018-06-24 08:38:37.172 boiler/uptime 94002
2018-06-24 08:38:39.173 boiler/date {"time":{"value":"08:38:39"},"date":{"value":"24.06.2018"}}
2018-06-24 08:38:41.175 boiler/ping "ping"
2018-06-24 08:38:43.176 boiler/burner "off"
2018-06-24 08:38:45.174 boiler/temp 89.5
2018-06-24 08:38:47.175 boiler/ext_temp 20.157077
2018-06-24 08:38:49.176 boiler/hw/temp 50.5
2018-06-24 08:38:51.175 boiler/info {"device":"ESP32"}
2018-06-24 08:38:53.179 boiler/temperatures {"device":"tronic","sensorType":"Temperature","values":[9.710066,84.988007,22.03299]}
2018-06-24 08:38:55.179 boiler/power 1.01
2018-06-24 08:38:57.180 boiler/lux 1114.44
It is also possible to interact with mqttgen. Given the topics.json example file, the following command:
mosquitto_pub -t 'boiler/hw/setpoint/get' -m ''
makes mqttgen send the following message:
2018-06-24 08:47:46.127 boiler/hw/setpoint 50
Then:
mosquitto_pub -t 'boiler/hw/setpoint/set' -m '65'
updates the internal mqttgen setpoint value. Sending again the get message:
mosquitto_pub -t 'boiler/hw/setpoint/get' -m ''
makes mqttgen send the following message:
2018-06-24 08:47:46.127 boiler/hw/setpoint 65
Usage of mqttplay
mqttplay allows to replay an MQTT flow previously recorded thanks to the following command:
mosquitto_sub -t "#" -v| xargs -d$'\n' -L1 bash -c 'date "+%T.%3N $0"' | tee flow.txt
which gives a file such as:
15:27:10.358 N/pvinverter/20/Ac/L1/Voltage {"value": 240.59999999999999}
15:27:10.386 N/pvinverter/20/Ac/L1/Power {"value": 1821.8742186612658}
15:27:10.415 N/pvinverter/20/Ac/L1/Energy/Forward {"value": 4272.6587533761876}
15:27:10.496 N/pvinverter/20/Ac/L1/Current {"value": 3.9399999999999999}
As a standalone application
To run the provided example, execute the following from the directory containing the composer.json file:
vendor/bin/mqttplay vendor/domotruc/mqttgen/flow.txt
As a library
Following file is assumed to be in the same directory as your composer.json file.
<?php use MqttPlay\MqttPlay; require __DIR__ . '/vendor/autoload.php'; try { $filename = 'vendor/domotruc/mqttgen/flow.txt'; $mqttPlay = new MqttPlay($filename, true, ' ', 'localhost', 1883, 1); while (($msg = $mqttPlay->nextMessage()) != null) { print($msg[MqttPlay::S_TIME ] . " " . $msg[MqttPlay::S_TOPIC] . " " . $msg[MqttPlay::S_PAYLOAD] . PHP_EOL); } } catch (\Exception $e) { print($e->getMessage() . PHP_EOL); }
The MQTT flow can also be passed directly as an array, which gives:
<?php use MqttPlay\MqttPlay; require __DIR__ . '/vendor/autoload.php'; $mqtt_flow = array( array('15:27:10.358', 'N/pvinverter/20/Ac/L1/Voltage', '{"value": 240.59999999999999}'), array('15:27:10.386', 'N/pvinverter/20/Ac/L1/Power', '{"value": 1821.8742186612658}'), array('15:27:10.415', 'N/pvinverter/20/Ac/L1/Energy/Forward', '{"value": 4272.6587533761876}'), array('15:27:10.496', 'N/pvinverter/20/Ac/L1/Current', '{"value": 3.9399999999999999}') ); try { $mqttPlay = new MqttPlay($mqtt_flow, true, ' ', 'localhost', 1883, 1); while (($msg = $mqttPlay->nextMessage()) != null) { print($msg[MqttPlay::S_TIME ] . " " . $msg[MqttPlay::S_TOPIC] . " " . $msg[MqttPlay::S_PAYLOAD] . PHP_EOL); } } catch (\Exception $e) { print($e->getMessage() . PHP_EOL); }
domotruc/mqttgen 适用场景与选型建议
domotruc/mqttgen 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 43 次下载、GitHub Stars 达 0, 最近一次更新时间为 2018 年 06 月 24 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 domotruc/mqttgen 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 domotruc/mqttgen 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 43
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: AGPL-3.0
- 更新时间: 2018-06-24