tongshanglei 1 år sedan
förälder
incheckning
e2290adca4
1 ändrade filer med 32 tillägg och 31 borttagningar
  1. 32 31
      task_script/LIVESTOCK_MQTT_PUBLISH.php

+ 32 - 31
task_script/LIVESTOCK_MQTT_PUBLISH.php

@@ -60,25 +60,7 @@ function app_redis()
 
 function sendConfig($topic,$config)
 {
-    
-}
-function updateSendResult($msgid){
-    $conn = new mysqli('rm-bp1h3uqkzy66ckt8yro.mysql.rds.aliyuncs.com', 'dev', '711e7D69f9d0c3f1', 'smart_livestock');
-    // $conn = new mysqli('127.0.0.1', 'root', 'root', 'smart_livestock');
-    if ($conn -> connect_errno) {
-        printf("Connect failed: %s\n", $conn->connect_error);
-        exit();
-    }
-    $sql = "UPDATE send_config_log SET result='1' WHERE id=".$msgid;
-    if ($conn->query($sql) === TRUE) {
-        echo "send_config_log update success";
-    } else {
-        echo "更新失败: " . $conn->error;
-    }
-
-}
-
-$server   = '116.62.220.88';
+    $server   = '116.62.220.88';
     $port     = 1883;
     $clientId = 'mqtt_livestock_config_cli';
     $username = 'rl517';
@@ -100,10 +82,36 @@ $server   = '116.62.220.88';
 
     $mqtt->connect($connectionSettings, $clean_session);
     echo 'connect OK'.PHP_EOL;
- 
-$redis=app_redis();
+    echo 'topic:'.$topic.PHP_EOL;
+    echo 'config:'.$config.PHP_EOL;
+    $res=$mqtt->publish(
+        $topic,
+        $config,
+        0
+    );
+    $matt->loop(true,true);
+    $mqtt->disconnect();
+    return $res;
+}
+function updateSendResult($msgid){
+    $conn = new mysqli('rm-bp1h3uqkzy66ckt8yro.mysql.rds.aliyuncs.com', 'dev', '711e7D69f9d0c3f1', 'smart_livestock');
+    // $conn = new mysqli('127.0.0.1', 'root', 'root', 'smart_livestock');
+    if ($conn -> connect_errno) {
+        printf("Connect failed: %s\n", $conn->connect_error);
+        exit();
+    }
+    $sql = "UPDATE send_config_log SET result='1' WHERE id=".$msgid;
+    if ($conn->query($sql) === TRUE) {
+        echo "send_config_log update success";
+    } else {
+        echo "更新失败: " . $conn->error;
+    }
+
+}
+
+
 while (1) {
-    $jsonData= $redis->rpop("device_mqtt_config_list");
+    $jsonData= app_redis()->rpop("device_mqtt_config_list");
     if(!$jsonData){
         sleep(3);
         continue;
@@ -124,15 +132,8 @@ while (1) {
     }
    
     $config_json=json_encode($config);
-    var_dump($config_json);
-    echo 'topic:'.$topic.PHP_EOL;
-    echo 'config:'.$config_json.PHP_EOL;
-    // $res=sendConfig($topic,$config_json);
-    $res=$mqtt->publish(
-        $topic,
-        $config_json,
-        1
-    );
+    // var_dump($config_json);
+    $res=sendConfig($topic,$config_json);
     updateSendResult($data['msgid']);
     // if($res){
         // Db::table('send_config_log')->where('id',$data['msgid'])->update(['result'=>'1']);