123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218 |
- <?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_3213';
- $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);
- rlog("[MqttClient] connect OK" );
- rlog("[MqttClient] topic: " .$topic);
- rlog("[MqttClient] config: " .$config);
- $res=$mqtt->publish(
- $topic,
- $config,
- 1
- );
- rlog("[MqttClient] publish endK" );
- $mqtt->loop(true,true);
- $mqtt->disconnect();
- return $res;
- }
- function rlog(...$args)
- {
- if (empty($args[0])) {
- return;
- }
- static $LOG_CONSOLE = false;
- static $LOG_NAME = "rlsta_door_publish.log";
- static $LOG_SIZE = 64 * 1024 * 1024;
- static $LOG_CACHE = false;
- static $CACHE_DURATION = 10;
- static $CACHE_SIZE = 1024;
- static $cacheStartTime = 0;
- static $cacheBuf = '';
- static $LOG_TIMES = 10;
- static $logCount = 0;
- $buf = '';
- if (count($args) == 1 && $args[0] == "\n") {
- $buf = "\n";
- } else {
- $pid = '';
- if (function_exists('posix_getpid')) {
- $pid = ' ' . posix_getpid() . ' ';
- }
- $fileLine = '';
- {
- $debug = debug_backtrace();
- $fileLine = ($pid == '' ? ' ' : '') . basename($debug[0]['file']) . ':' . $debug[0]['line'] . ' ';
- }
- $buf = date("y-m-d H:i:s") . "{$pid}{$fileLine}" . implode(' ', $args) . "\n";
- }
- $logCount++;
- if (!empty($LOG_NAME)) {
- if ($LOG_CACHE) {
- $cacheBuf .= $buf;
-
- if (strlen($cacheBuf) > $CACHE_SIZE || time() - $cacheStartTime > $CACHE_DURATION) {
- $cacheStartTime = time();
- goto write;
- } else {
- goto skipWrite;
- }
- } else {
- $cacheBuf = $buf;
- }
- write: {
-
- if ($logCount > $LOG_TIMES && filesize($LOG_NAME) > $LOG_SIZE) {
- $oldLogName = $LOG_NAME . '.old';
- if (file_exists($oldLogName)) {
- if (!unlink($oldLogName)) {
- echo "unlink err\n";
- }
- }
- if (!rename($LOG_NAME, $oldLogName)) {
- echo "rename err\n";
- }
- $logCount = 0;
- }
- if (!file_put_contents($LOG_NAME, $cacheBuf, FILE_APPEND)) {
- echo "file_put_contents err\n";
- }
- $cacheBuf = '';
- }
- skipWrite: {
- }
- }
- if ($LOG_CONSOLE) {
- echo $buf;
- }
- }
- 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);
- rlog("[Redis] rpop: " . json_encode($data));
- $topic="rlsta/door/bklist/set/".$data['device_id'];
-
-
-
-
-
-
-
-
- $config=$data['config'];
- rlog("[Config] : " . json_encode($config));
-
- foreach($config['bkList']['list'] as &$val){
- $val = str_pad($val, 8, "0", STR_PAD_LEFT);
- }
- $config_json=json_encode($config);
-
- rlog("[json_config] : " . $config_json);
- sendConfig($topic,$config_json);
-
- }
|