LIVESTOCK_MQTT_PUBLISH.php 3.6 KB

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