DeviceJLWM.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\Jobs\ManDeviceJobHc;
  4. use App\Service\ClearDataService;
  5. use Illuminate\Console\Command;
  6. use Illuminate\Support\Facades\Log;
  7. class DeviceJLWM extends Command
  8. {
  9. /**
  10. * The name and signature of the console command.
  11. *
  12. * @var string
  13. */
  14. protected $signature = 'command:deviceJLWM';
  15. /**
  16. * The console command description.
  17. *
  18. * @var string
  19. */
  20. protected $description = 'Command description';
  21. /**
  22. * Create a new command instance.
  23. *
  24. * @return void
  25. */
  26. public function __construct()
  27. {
  28. parent::__construct();
  29. }
  30. /**
  31. * Execute the console command.
  32. *
  33. * @return mixed
  34. */
  35. public function handle()
  36. {
  37. try {
  38. $this->generateMachineMockData();
  39. }catch (\Exception $exception){
  40. Log::error("恒昌设备异常", ['msg' => $exception->getMessage()]);
  41. }
  42. }
  43. public function sendToDeviceBatch($data){
  44. list($status,$token) = ClearDataService::getTokenJLWM();
  45. if(! $status) return [false, $token];
  46. $url = $this->url . "api/module-data/jlwm_device_machine_record/jlwm_device_machine_record/diy/create_data";
  47. $post = [
  48. 'data' => [
  49. 'device_machine_record' => $data
  50. ]
  51. ];
  52. $header = ["Authorization: Bearer {$token}","Content-Type:application/json","Site:JLWM"];
  53. $curl = curl_init();
  54. curl_setopt_array($curl, array(
  55. CURLOPT_URL => $url,
  56. CURLOPT_RETURNTRANSFER => true,
  57. CURLOPT_ENCODING => '',
  58. CURLOPT_MAXREDIRS => 10,
  59. CURLOPT_TIMEOUT => 15,
  60. CURLOPT_FOLLOWLOCATION => true,
  61. CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  62. CURLOPT_CUSTOMREQUEST => 'POST',
  63. CURLOPT_POSTFIELDS => json_encode($post),
  64. CURLOPT_SSL_VERIFYPEER => false,
  65. CURLOPT_HTTPHEADER => $header,
  66. ));
  67. $response = curl_exec($curl);
  68. if ($response === false) {
  69. // 获取错误号
  70. $errorNumber = curl_errno($curl);
  71. // 获取错误信息
  72. $errorMessage = curl_error($curl);
  73. $message = "cURL Error #{$errorNumber}: {$errorMessage}";
  74. Log::channel('apiLog')->info('jlwm写入数据', ["param" => $message]);
  75. }
  76. curl_close($curl);
  77. $res = json_decode($response,true);
  78. if(! isset($res['status'])){//报错了
  79. Log::channel('apiLog')->info('jlwm写入数据返回错误', ["param" => $response]);
  80. }
  81. return [true, ''];
  82. }
  83. public function generateMachineMockData() {
  84. $machines = [
  85. "家丽屋美四边锯" => ["0140142511130000594535177367", "0140142511130000594535177368", "0140142511130000594535177369"],
  86. "双边锁孔机1号" => ["0140142511130000614735177367", "0140142511130000614735177368", "0140142511130000614735177369"],
  87. "门板吸塑机1号" => ["0140142511130000694435177367", "0140142511130000694435177368", "0140142511130000694435177369"],
  88. "双头追踪切割机1号" => ["0140142511130000730335177367", "0140142511130000730335177368", "0140142511130000730335177369"],
  89. "门套洗型机1号" => ["0140142511130000718335177367", "0140142511130000718335177368", "0140142511130000718335177369"],
  90. "L型侧拼机1号" => ["0140142511130000818335177367", "0140142511130000818335177368", "0140142511130000818335177369"],
  91. "雕刻机1号" => ["0140142511130000579835177367", "0140142511130000579835177368", "0140142511130000579835177369"],
  92. "(龙门)上板机1号" => ["0140142511130000650735177367", "0140142511130000650735177368", "0140142511130000650735177369"],
  93. "开料机1号" => ["0140142511130000685035177367", "0140142511130000685035177368", "0140142511130000685035177369"],
  94. "双边锁孔机2号" => ["0140142511130000714735177367", "0140142511130000714735177368", "0140142511130000714735177369"],
  95. "双边锁孔机3号" => ["0140142511130000724735177367", "0140142511130000724735177368", "0140142511130000724735177369"],
  96. "门板吸塑机2号" => ["0140142511130000604435177367", "0140142511130000604435177368", "0140142511130000604435177369"],
  97. "雕刻机2号" => ["0140142511130000578935177367", "0140142511130000578935177368", "0140142511130000578935177369"],
  98. "雕刻机3号" => ["0140142511130000978935177367", "0140142511130000978935177368", "0140142511130000978935177369"],
  99. "门套包覆机2号" => ["0140142511130000685635177367", "0140142511130000685635177368", "0140142511130000685635177369"],
  100. "门套包覆机3号" => ["0140142511130000179135177367", "0140142511130000179135177368", "0140142511130000179135177369"],
  101. "门套包覆机4号" => ["0140142511130000532735177367", "0140142511130000532735177368", "0140142511130000532735177369"],
  102. "门套包覆机5号" => ["0140142511130000525835177367", "0140142511130000525835177368", "0140142511130000525835177369"],
  103. "封边机2号" => ["0140142511130000649735177367", "0140142511130000649735177368", "0140142511130000649735177369"],
  104. "门套包覆机1号" => ["0140142511130000673036193247", "0140142511130000673036193329", "0140142511130000673036229962"],
  105. "封边机1号" => ["0140142511130000649735314979", "0140142511130000649735314986", "0140142511130000649735314996"]
  106. ];
  107. $results = [];
  108. $todayStr = date('Ymd');
  109. // 计算从凌晨 00:00 到现在经过的总分钟数
  110. $passedMinutes = (int)date('H') * 60 + (int)date('i');
  111. foreach ($machines as $name => $codes) {
  112. // 1. 模拟 80% 的设备活跃度 (这里不使用固定种子,让每次请求活跃机器略有不同)
  113. // 如果希望某台机器今天一整天都不活跃,可以把这行移到种子设定之后
  114. if (rand(1, 100) > 80) continue;
  115. foreach ($codes as $code) {
  116. // 2. 每个动作 80% 概率有数据
  117. if (rand(1, 100) > 80) continue;
  118. // --- 核心逻辑:锁定种子 ---
  119. mt_srand(crc32($todayStr . $code));
  120. // A. 锁定今天开机时的“初始值” (例如 500 到 2000 之间)
  121. $baseValue = mt_rand(10, 100);
  122. // B. 锁定这台机器今天的“生产速度” (例如每分钟产出 1 到 5 次)
  123. // 关键:因为种子固定,这一天内 $speed 永远不变
  124. $speed = mt_rand(1, 5);
  125. // C. 计算当前累计值 = 初始值 + 经过时间 * 速度
  126. $accumulated = $baseValue + ($passedMinutes * $speed);
  127. // D. (可选) 加入极小的随机扰动,让数据看起来更“活”一点点
  128. mt_srand(); // 恢复真随机
  129. $accumulated += rand(0, 2);
  130. $results[] = [
  131. "machine_code" => $code,
  132. "param_value" => floatval($accumulated),
  133. "time" => $this->getNowDay(),
  134. ];
  135. }
  136. }
  137. // 重置随机种子,确保不影响系统其他逻辑
  138. mt_srand();
  139. $this->sendToDeviceBatch($results);
  140. }
  141. function getNowDay(){
  142. // 获取当前时间
  143. $currentDateTime = new \DateTime();
  144. // 设置时区为 PRC
  145. $currentDateTime->setTimezone(new \DateTimeZone('UTC'));
  146. // 格式化时间为 "2023-09-21T16:00:00.000Z" 的格式
  147. $formattedDateTime = $currentDateTime->format('Y-m-d\TH:i:s.000\Z');
  148. return $formattedDateTime;
  149. }
  150. }