LIVESTOCK_MQTT_PUBLISH.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <?php
  2. require('../vendor/autoload.php');
  3. use \PhpMqtt\Client\MqttClient;
  4. use \PhpMqtt\Client\ConnectionSettings;
  5. use think\facade\Cache;
  6. date_default_timezone_set("PRC");
  7. define('HOST', 'r-bp1eebab79320044pd.redis.rds.aliyuncs.com');
  8. define('PORT', '6379');
  9. define('PASSWORD', '7e2b5c91e438be3c!');
  10. define('DATABASE', 4);
  11. function app_redis()
  12. {
  13. static $redis = null;
  14. static $conn = false;
  15. if (!$conn) {
  16. connect: //定义标签
  17. $redis = new Redis();
  18. try {
  19. //建立的Redis短连接,在请求结束后不会自动关闭,相当于持久连接.
  20. $conn = $redis->connect(HOST, PORT);
  21. $conn = $redis->auth(PASSWORD);
  22. $conn = $redis->select(DATABASE);
  23. // 连接成功,返回$redis对象,连接失败,返回false.
  24. return ($conn === true) ? $redis : false;
  25. } catch (Exception $e) {
  26. return false;
  27. }
  28. } else {
  29. // 这里假设PHP-FPM在处理一个请求的时间内,Redis连接都是可用的.
  30. // 所以只在PHP-CLI下检查Redis连接的状态,进行断线重连.
  31. if (php_sapi_name() === 'cli') {
  32. try {
  33. // ping用于检查当前连接的状态,成功时返回+PONG,失败时抛出一个RedisException对象.
  34. // ping失败时警告:
  35. // Warning: Redis::ping(): connect() failed: Connection refused
  36. // var_dump('AAAAAAAAA', $redis);
  37. echo 'Redis 连接状态' . $redis->ping() . PHP_EOL;
  38. @$redis->ping();
  39. if (!$redis->ping()) {
  40. goto connect; //跳转到标签出继续执行连接操作
  41. }
  42. } catch (Exception $e) {
  43. // 信息如 Connection lost 或 Redis server went away
  44. echo $e->getMessage();
  45. echo 'Redis 连接失败 重新连接:' . PHP_EOL;
  46. // 断线重连
  47. goto connect;
  48. }
  49. }
  50. return $redis;
  51. }
  52. }
  53. function sendConfig($topic,$config)
  54. {
  55. $server = '116.62.220.88';
  56. $port = 1883;
  57. $clientId = 'mqtt_livestock_config_cli';
  58. $username = 'rl517';
  59. $password = "rlian2022";
  60. $clean_session = false;
  61. $connectionSettings = new ConnectionSettings();
  62. $connectionSettings = $connectionSettings
  63. ->setUsername($username)
  64. ->setPassword($password)
  65. ->setKeepAliveInterval(60)
  66. // Last Will 设置
  67. // ->setLastWillTopic('emqx/test/last-will')
  68. // ->setLastWillMessage('client disconnect')
  69. // ->setLastWillQualityOfService(1)
  70. ;
  71. $mqtt = new MqttClient($server, $port, $clientId);
  72. $mqtt->connect($connectionSettings, $clean_session);
  73. echo 'connect OK'.PHP_EOL;
  74. $res=$mqtt->publish(
  75. $topic,
  76. $config,
  77. 0
  78. );
  79. $mqtt->disconnect();
  80. return $res;
  81. }
  82. $redis=app_redis();
  83. while (1) {
  84. $jsonData= $redis->rpop("device_mqtt_config_list");
  85. if(!$jsonData){
  86. sleep(3);
  87. continue;
  88. }
  89. $data=json_decode($jsonData,true);
  90. $topic="earings/".$data['device_id']."/coludControl";
  91. $config=[
  92. $data['type']=>[
  93. "config"=>$data['config']
  94. ]
  95. ];
  96. $config_json=json_encode($config);
  97. var_dump($config_json);
  98. $res=sendConfig($topic,$config_json);
  99. var_dump($res);
  100. }