123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298 |
- <?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 = "parea_inoutres_push.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 http($url, $params, $header = [], $method = 'GET', $timeout = 10)
- {
-
-
-
-
-
-
- rlog("[HTTP] url:$url,method:$method" . ",header:" . json_encode($header));
- if (strtoupper($method) == 'POST') {
- rlog("[POST] send params " . (!is_array($params) ? $params : json_encode($params, JSON_UNESCAPED_UNICODE)));
- } else {
- rlog("[GET] send " . json_encode($params));
- }
- $header[] = "Expect: ";
- $opts = array(
- CURLOPT_TIMEOUT => $timeout,
- CURLOPT_RETURNTRANSFER => 1,
- CURLOPT_SSL_VERIFYPEER => false,
- CURLOPT_SSL_VERIFYHOST => false,
- CURLOPT_HTTPHEADER => $header
- );
-
- switch (strtoupper($method)) {
- case 'GET':
- $opts[CURLOPT_URL] = $url . (empty($params) ? '' : ('?' . http_build_query($params)));
- break;
- case 'POST':
-
- $opts[CURLOPT_URL] = $url;
- $opts[CURLOPT_POST] = 1;
- $opts[CURLOPT_POSTFIELDS] = json_encode($params);
- break;
- default:
- rlog("[ERR] method " . $method);
- return false;
- }
- global $ch;
- if (empty($ch)) {
- $ch = curl_init();
- }
- if (empty($ch)) {
- rlog("[ERR] curl_init");
- return false;
- }
- $csa = curl_setopt_array($ch, $opts);
- if (empty($csa)) {
- rlog("[ERR] curl_setopt_array");
- return false;
- }
- $data = curl_exec($ch);
- if ($data === false) {
- rlog("[ERR] curl_exec errno:" . curl_errno($ch) . " " . curl_error($ch));
- return false;
- }
-
- $data = decodeUnicode($data);
- rlog("[HTTP] recv " . $data);
-
- return $data;
- }
- function decodeUnicode($str)
- {
- return preg_replace_callback('/\\\\u([0-9a-f]{4})/i', function ($matches) {
- return iconv("UCS-2BE", "UTF-8", pack("H*", $matches[1]));
- }, $str);
- }
-
- while (true) {
- try {
-
- if (app_redis() === false) {
- echo 'redis连接错误!' . PHP_EOL;
- continue;
- }
- $data = app_redis()->lpop('parea_inoutres_push');
- if(!$data){
-
- echo "redis无数据!",PHP_EOL;
- sleep(1);
- continue;
- }
- echo ('取出数据' . $data." TYPE:". gettype($data) . PHP_EOL);
- $url_data = json_decode($data, true);
- $url="http://localhost:8115/api/areaReport";
-
-
- http($url, $url_data, [], 'POST');
- if($url_data["first_time"]){
- $camData = [
- "station" => $url_data["mac"],
- "list" => [
- [
- "label" =>$url_data["id"],
- "first_time" => $url_data["first_time"],
- "time" => $url_data["time"]
- ]
- ]
- ];
-
- $resp = http("http://localhost:8115/api/areaLabelReport", $camData, [], 'POST');
-
- }
- rlog("PUSH INFO","推送依赖数据", $data);
- }catch (\Exception $ex) {
- rlog("INFO", 'pushhttp',"推送 异常".$ex->getMessage());
- }
-
- }
|