123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189 |
- <?php
- require('../vendor/autoload.php');
- use \PhpMqtt\Client\MqttClient;
- use \PhpMqtt\Client\ConnectionSettings;
- use think\facade\Cache;
- date_default_timezone_set("PRC");
- define('HOST', '127.0.0.1');
- define('PORT', '6379');
- define('PASSWORD', 'R!478gH*%23nPn');
- define('DATABASE', 2);
- 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 {
-
-
-
-
- echo 'Redis 连接状态' . $redis->ping() . PHP_EOL;
- @$redis->ping();
- if (!$redis->ping()) {
- goto connect;
- }
- } catch (Exception $e) {
-
- echo $e->getMessage();
- echo 'Redis 连接失败 重新连接:' . PHP_EOL;
-
- goto connect;
- }
- }
- return $redis;
- }
- }
- function rlog(...$args)
- {
- if (empty($args[0])) {
- return;
- }
- static $LOG_CONSOLE = false;
- static $LOG_NAME = "indoor_mqttpub_wait2send.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 loop()
- {
- $server = 'develop.rltest.cn';
- $port = 1883;
- $clientId = 'mqtt_indoor_mqttpub_wait2send_'.rand(10000,99999);
- $username = 'rl517';
- $password = "rlian2022";
- $clean_session = true;
- $connectionSettings = new ConnectionSettings();
- $connectionSettings = $connectionSettings
- ->setUsername($username)
- ->setPassword($password)
- ->setKeepAliveInterval(60)
- ;
- rlog('INFO', "connect OK");
- while (1) {
-
- $configCnt = app_redis()->lpop("indoor_mqttpub_wait2send");
- if (!$configCnt) {
-
- sleep(1);
- continue;
- }
- $mqtt = new MqttClient($server, $port, $clientId);
- $mqtt->connect($connectionSettings, $clean_session);
- $cnt = json_decode($configCnt, true);
- if ($cnt["rfidsConfig"]) {
-
- $rfidsConfig = explode(",",$cnt["rfidsConfig"]);
- $cnt["rfidsConfig"] = $rfidsConfig;
- }else{
- $cnt["rfidsConfig"] == 0;
- }
- $sendCnt = json_encode($cnt);
- $mqtt->publish('RlianStation/G31W/'.$cnt["mac"].'/SetConfig', $sendCnt, MqttClient::QOS_AT_MOST_ONCE);
- $mqtt->loop(true, true);
- $mqtt->disconnect();
- var_dump("out!!");
- rlog('PUB_CONFIG_CNT', $configCnt);
- }
- }
- try {
- rlog('INFO', 'connect start');
- loop();
- } catch (\Exception $ex) {
- rlog("ERR", $ex->getTraceAsString());
- rlog("ERR", $ex->getMessage());
- }
|