123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142 |
- <?php
- require('../vendor/autoload.php');
- use \PhpMqtt\Client\MqttClient;
- use \PhpMqtt\Client\ConnectionSettings;
- use think\facade\Cache;
- define('HOST', '127.0.0.1');
- define('PORT', '6379');
- define('PASSWORD', '123456');
- define('DATABASE', 2);
- use \think\facade\Db;
- function app_redis()
- {
- static $redis = null;
- static $conn = false;
- if (!$conn) {
- connect:
- $redis = new Redis();
- try {
-
- $conn = $redis->connect(HOST, PORT);
- $conn = $redis->auth(PASSWORD);
- $conn = $redis->select(DATABASE);
-
- return ($conn === true) ? $redis : false;
- } catch (Exception $e) {
- return false;
- }
- } else {
-
-
- if (php_sapi_name() === 'cli') {
- try {
-
-
-
-
-
- @$redis->ping();
- if (!$redis->ping()) {
- goto connect;
- }
- } catch (Exception $e) {
-
- echo $e->getMessage();
- echo 'Redis 连接失败 重新连接:' . PHP_EOL;
-
- goto connect;
- }
- }
- return $redis;
- }
- }
- function sendConfig($topic,$config)
- {
- $server = '61.175.203.188';
- $port = 10002;
- $clientId = 'mqtt_rlsta_door_config';
- $username = 'rl0606';
- $password = "rlian2023";
- $clean_session = false;
- $connectionSettings = new ConnectionSettings();
- $connectionSettings = $connectionSettings
- ->setUsername($username)
- ->setPassword($password)
- ->setKeepAliveInterval(60)
-
-
-
-
- ;
- $mqtt = new MqttClient($server, $port, $clientId);
- $mqtt->connect($connectionSettings, $clean_session);
- echo 'connect OK'.PHP_EOL;
- echo 'topic:'.$topic.PHP_EOL;
- echo 'config:'.$config.PHP_EOL;
- $res=$mqtt->publish(
- $topic,
- $config,
- 1
- );
- echo 'publish end'.PHP_EOL;
- $mqtt->loop(true,true);
- $mqtt->disconnect();
- return $res;
- }
- function updateSendResult($msgid){
-
- $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= app_redis()->rpop("rlsta_door_black_list");
- if(!$jsonData){
- sleep(3);
- continue;
- }
- $data=json_decode($jsonData,true);
- var_dump($data);
- $topic="rlsta/door/bklist/set/".$data['device_id'];
-
-
-
-
-
-
-
-
- $config=$data['config'];
-
-
- $config_json=json_encode($config);
-
-
-
-
- sendConfig($topic,$config_json);
-
- }
|