LIVESTOCK_REPEAT_BRAZIL.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. <?php
  2. require('../vendor/autoload.php');
  3. use \PhpMqtt\Client\MqttClient;
  4. use \PhpMqtt\Client\ConnectionSettings;
  5. use think\facade\Cache;
  6. date_default_timezone_set("America/Bahia");
  7. define('HOST', '127.0.0.1');
  8. define('PORT', '6379');
  9. define('PASSWORD', '123456');
  10. define('DATABASE', 0);
  11. function rlog(...$args)
  12. {
  13. if (empty($args[0])) {
  14. return;
  15. }
  16. static $LOG_CONSOLE = false; //是否输出到控制台
  17. static $LOG_NAME = "livestock_repeat_brazil.log"; //值为空时 不写入文件
  18. static $LOG_SIZE = 64 * 1024 * 1024; //文件最大尺寸
  19. static $LOG_CACHE = false; //是否缓存日志内容 用于批量写入文件
  20. static $CACHE_DURATION = 10; //缓存最大时间 秒
  21. static $CACHE_SIZE = 1024; //缓存大小
  22. static $cacheStartTime = 0;
  23. static $cacheBuf = '';
  24. static $LOG_TIMES = 10; //调用这个函数最大次数 超过次数后判断下文件大小
  25. static $logCount = 0;
  26. $buf = '';
  27. if (count($args) == 1 && $args[0] == "\n") { //只有换行时 不写入时间戳了
  28. $buf = "\n";
  29. } else {
  30. $pid = ''; //进程id
  31. if (function_exists('posix_getpid')) {
  32. $pid = ' ' . posix_getpid() . ' ';
  33. }
  34. $fileLine = ''; //文件名:行号
  35. {
  36. $debug = debug_backtrace();
  37. $fileLine = ($pid == '' ? ' ' : '') . basename($debug[0]['file']) . ':' . $debug[0]['line'] . ' ';
  38. }
  39. $buf = date("y-m-d H:i:s") . "{$pid}{$fileLine}" . implode(' ', $args) . "\n";
  40. }
  41. $logCount++;
  42. if (!empty($LOG_NAME)) {
  43. if ($LOG_CACHE) {
  44. $cacheBuf .= $buf;
  45. //超过缓存尺寸 或者 超过缓存时长 写缓存到文件
  46. if (strlen($cacheBuf) > $CACHE_SIZE || time() - $cacheStartTime > $CACHE_DURATION) {
  47. $cacheStartTime = time();
  48. goto write;
  49. } else {
  50. goto skipWrite;
  51. }
  52. } else {
  53. $cacheBuf = $buf;
  54. }
  55. write: {
  56. //超过尺寸后 删除旧文件 把新文件重命名为旧文件 多进程同时操作 不加锁问题不大
  57. if ($logCount > $LOG_TIMES && filesize($LOG_NAME) > $LOG_SIZE) {
  58. $oldLogName = $LOG_NAME . '.old';
  59. if (file_exists($oldLogName)) {
  60. if (!unlink($oldLogName)) {
  61. echo "unlink err\n";
  62. }
  63. }
  64. if (!rename($LOG_NAME, $oldLogName)) {
  65. echo "rename err\n";
  66. }
  67. $logCount = 0;
  68. }
  69. if (!file_put_contents($LOG_NAME, $cacheBuf, FILE_APPEND)) {
  70. echo "file_put_contents err\n";
  71. }
  72. $cacheBuf = '';
  73. }
  74. skipWrite: {
  75. }
  76. }
  77. if ($LOG_CONSOLE) {
  78. echo $buf;
  79. }
  80. }
  81. function loop()
  82. {
  83. $server = '43.157.183.167';
  84. $port = 1883;
  85. $clientId = 'mqtt_bx_repart_livestock_cli';
  86. $username = 'rl517';
  87. $password = "rlian2022";
  88. $clean_session = false;
  89. $connectionSettings = new ConnectionSettings();
  90. $connectionSettings = $connectionSettings
  91. ->setUsername($username)
  92. ->setPassword($password)
  93. ->setKeepAliveInterval(60)
  94. // Last Will 设置
  95. // ->setLastWillTopic('emqx/test/last-will')
  96. // ->setLastWillMessage('client disconnect')
  97. // ->setLastWillQualityOfService(1)
  98. ;
  99. //include "RLog.php";
  100. // $mqtt = new MqttClient($server, $port, $clientId, MqttClient::MQTT_3_1, null, new RLog());
  101. $mqtt = new MqttClient($server, $port, $clientId);
  102. $mqtt->connect($connectionSettings, $clean_session);
  103. rlog('INFO', "connect OK");
  104. /*
  105. 消息方向 设备->服务器
  106. 设备主动上报当前设备公共信息参数:ScBusTem/DevRegularInfo
  107. 服务器获取设备系统信息后设备上传信息,即GetDevSysMsg的回应 ScBusTem/GetUpDevSysMsg
  108. 服务器设置设备重量信息信息 ScBusTem/RCInfoMsg
  109. */
  110. // $mqtt->subscribe('ScBusTem/GetDevSysMsg/*', function ($topic, $message) {
  111. // rlog("INFO", 'recv', $topic, $message);
  112. // getDevSysMsg($topic, $message);
  113. // }, 0);
  114. //终端上报系统信息数据
  115. $mqtt->subscribe('earings/+/reportData', function ($topic, $message) use($mqtt) {
  116. rlog("reportData", 'recv', $topic, $message);
  117. $topicArr=explode('/',$topic);
  118. $deviceId=$topicArr[1];
  119. $device_arr=[
  120. '869761079960164-999274877903265',
  121. '869761079959216-999274877904165',
  122. '869761079718869-999274877904300',
  123. '869761079720154-999274877903457',
  124. '869761079714413-999274877902831',
  125. '869761079979404-999274877904081',
  126. '866216066888053-999274877905803',
  127. '869761079982671-999274877904303',
  128. '869761079957889-999274877904193',
  129. '869761079711112-999274877904283',
  130. '869761079959604-999274877904128',
  131. '869761079694268-999274877903963',
  132. '869761079959026-999274877904093',
  133. '869761079962137-999274877903395',
  134. '869761079721764-999274877904203',
  135. '869761079979529-999274877904129',
  136. '869761079695976-999274877904164',
  137. '869761079709769-999274877902136',
  138. '869761079984131-999274877903943',
  139. '869761079695968-999274877903109',
  140. '869761079960693-999274877903377',
  141. ];
  142. if(in_array($deviceId,$device_arr)){
  143. mqttRepeat($topic, $message);
  144. }else{
  145. rlog($deviceId.'not in array');
  146. }
  147. }, 1);
  148. // $mqtt->subscribe('earings/+/cloudResp', function ($topic, $message) use($mqtt) {
  149. // rlog("cloudResp", 'recv', $topic, $message);
  150. // mqttRepeat($topic, $message);
  151. // }, 1);
  152. // $mqtt->subscribe('earings/+/cloudControl', function ($topic, $message) use($mqtt) {
  153. // rlog("cloudControl", 'recv', $topic, $message);
  154. // mqttRepeat($topic, $message);
  155. // }, 1);
  156. // $mqtt->subscribe('$SYS/brokers/+/clients/+/connected', function ($topic, $message) use($mqtt) {
  157. // rlog("connected", 'recv', $topic, $message);
  158. // $data=json_decode($message,true);
  159. // $data['deviceId']=$data['clientid'];
  160. // $data['data_type']='connected';
  161. // }, 1);
  162. // $mqtt->subscribe('$SYS/brokers/+/clients/+/disconnected', function ($topic, $message) use($mqtt) {
  163. // rlog("connected", 'recv', $topic, $message);
  164. // $data=json_decode($message,true);
  165. // $data['deviceId']=$data['clientid'];
  166. // $data['data_type']='disconnected';
  167. // }, 1);
  168. // 上线,: $SYS/brokers/+/clients/+/connected
  169. // 下线,: $SYS/brokers/+/clients/+/disconnected
  170. $mqtt->loop(true);
  171. }
  172. function mqttRepeat($topic,$message){
  173. $server = '3.80.119.202';
  174. $port = 1883;
  175. $clientId = 'repeat_mqtt_livestock_baxi';
  176. $username = 'rl241107';
  177. $password = "rlian2024";
  178. $clean_session = false;
  179. $connectionSettings = new ConnectionSettings();
  180. $connectionSettings = $connectionSettings
  181. ->setUsername($username)
  182. ->setPassword($password)
  183. ->setKeepAliveInterval(60)
  184. // Last Will 设置
  185. // ->setLastWillTopic('emqx/test/last-will')
  186. // ->setLastWillMessage('client disconnect')
  187. // ->setLastWillQualityOfService(1)
  188. ;
  189. $mqtt2 = new MqttClient($server, $port, $clientId);
  190. $mqtt2->connect($connectionSettings, $clean_session);
  191. rlog('connect OK');
  192. rlog('topic:'.$topic);
  193. rlog('message:'.$message);
  194. $res=$mqtt2->publish(
  195. $topic,
  196. $message,
  197. 0
  198. );
  199. rlog('publish end');
  200. $mqtt2->loop(true,true);
  201. $mqtt2->disconnect();
  202. return $res;
  203. }
  204. while (1) {
  205. try {
  206. rlog('INFO', 'connect start');
  207. loop();
  208. } catch (\Exception $ex) {
  209. rlog("ERR", $ex->getTraceAsString());
  210. rlog("ERR", $ex->getMessage());
  211. }
  212. sleep(3);
  213. }
  214. // $text='{"idESim":460046697314223,"stepCount":0,"EnvironmentTemperature":"27.0","earTemperature":"22.2","latitude":0,"longitude":0,"charging":1,"lastCharge":1704328944,"battery-level":99,"measurementTimestamp":1691173083,"agnss-dtime":16170,"agnss-inserttime":28050,"gnss-locatetime":39600,"gnss-satnum":1,"gnss-cn":28,"csq":"0-0","edrxrdp":",,","deviceId":"869154043484299-999202300000012","data_type":"reportData"}';
  215. // $text='{"clean_start":false,"clientid":"866216066939047-999274877906912","connack":0,"connected_at":1716169665,"expiry_interval":86400,"ipaddress":"39.144.129.107","keepalive":120,"proto_name":"MQTT","proto_ver":4,"sockport":1883,"ts":1716169665478,"username":"rl517","deviceId":"866216066939047-999274877906912","data_type":"connected"}';
  216. // app_redis()->lpush("mqtt_data_livestock",$text);