DeviceHC.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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 DeviceHC extends Command
  8. {
  9. /**
  10. * The name and signature of the console command.
  11. *
  12. * @var string
  13. */
  14. protected $signature = 'command:deviceHc';
  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->handleReminders();
  39. }catch (\Exception $exception){
  40. Log::error("恒昌设备异常", ['msg' => $exception->getMessage()]);
  41. }
  42. }
  43. public function handleReminders()
  44. {
  45. $data = [
  46. // --- 2号机 ---
  47. [
  48. 'machine_code' => '000013087600010036093380',
  49. 'param_value' => rand(779, 952), // 865 ± 10% (778.5 - 951.5)
  50. ],
  51. [
  52. 'machine_code' => '000013087600010036093381',
  53. 'param_value' => rand(264, 357), // 310 ± 15% (263.5 - 356.5)
  54. ],
  55. [
  56. 'machine_code' => '000013087600010036093382',
  57. 'param_value' => rand(779, 952),
  58. ],
  59. [
  60. 'machine_code' => '000013087600010036093383',
  61. 'param_value' => rand(264, 357),
  62. ],
  63. [
  64. 'machine_code' => '000013087600010036093384',
  65. 'param_value' => rand(779, 952),
  66. ],
  67. [
  68. 'machine_code' => '000013087600010036093385',
  69. 'param_value' => rand(264, 357),
  70. ],
  71. [
  72. 'machine_code' => '000013087600010036093386',
  73. 'param_value' => rand(779, 952),
  74. ],
  75. [
  76. 'machine_code' => '000013087600010036093387',
  77. 'param_value' => rand(264, 357),
  78. ],
  79. [
  80. 'machine_code' => '000013087600010036093388',
  81. 'param_value' => rand(779, 952),
  82. ],
  83. [
  84. 'machine_code' => '000013087600010036093389',
  85. 'param_value' => rand(264, 357),
  86. ],
  87. [
  88. 'machine_code' => '000013087600010036093390',
  89. 'param_value' => number_format(40 / (rand(0, 1) ? 3.5 : 4), 2), // 淬火速度
  90. ],
  91. [
  92. 'machine_code' => '000013087600010036093391',
  93. 'param_value' => number_format(40 / (rand(0, 1) ? 3.5 : 4), 2), // 回火速度
  94. ],
  95. // --- 3号机 ---
  96. [
  97. 'machine_code' => '000013087600010136093380',
  98. 'param_value' => rand(704, 1056), // 880 ± 20%
  99. ],
  100. [
  101. 'machine_code' => '000013087600010136093381',
  102. 'param_value' => rand(252, 308),
  103. ],
  104. [
  105. 'machine_code' => '000013087600010136093382',
  106. 'param_value' => rand(712, 1068), // 890 ± 20%
  107. ],
  108. [
  109. 'machine_code' => '000013087600010136093383',
  110. 'param_value' => rand(712, 1068),
  111. ],
  112. [
  113. 'machine_code' => '000013087600010136093384',
  114. 'param_value' => rand(712, 1068),
  115. ],
  116. [
  117. 'machine_code' => '000013087600010136093385',
  118. 'param_value' => rand(704, 1056), // 880 ± 20%
  119. ],
  120. [
  121. 'machine_code' => '000013087600010136093386',
  122. 'param_value' => number_format(35 / (rand(0, 1) ? 4.5 : 4), 2), // 渗碳速度
  123. ],
  124. [
  125. 'machine_code' => '000013087600010136093387',
  126. 'param_value' => number_format(35 / (rand(0, 1) ? 4.5 : 4), 2), // 回火速度
  127. ],
  128. ];
  129. foreach ($data as $key => $value){
  130. $data[$key]['time'] = $this->getNowDay();
  131. $data[$key]['param_value'] = floatval($value['param_value']);
  132. }
  133. $this->sendToDeviceBatch($data);
  134. }
  135. public function sendToDeviceBatch($data){
  136. list($status,$token) = ClearDataService::getTokenHc();
  137. if(! $status) return [false, $token];
  138. $url = config('ip.zslf') . "api/module-data/hc_device_machine_record/hc_device_machine_record/diy/create_data";
  139. $post = [
  140. 'data' => [
  141. 'device_machine_record' => $data
  142. ]
  143. ];
  144. $header = ["Authorization: Bearer {$token}","Content-Type:application/json","Site:HCLT"];
  145. $curl = curl_init();
  146. curl_setopt_array($curl, array(
  147. CURLOPT_URL => $url,
  148. CURLOPT_RETURNTRANSFER => true,
  149. CURLOPT_ENCODING => '',
  150. CURLOPT_MAXREDIRS => 10,
  151. CURLOPT_TIMEOUT => 15,
  152. CURLOPT_FOLLOWLOCATION => true,
  153. CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  154. CURLOPT_CUSTOMREQUEST => 'POST',
  155. CURLOPT_POSTFIELDS => json_encode($post),
  156. CURLOPT_SSL_VERIFYPEER => false,
  157. CURLOPT_HTTPHEADER => $header,
  158. ));
  159. $response = curl_exec($curl);
  160. if ($response === false) {
  161. // 获取错误号
  162. $errorNumber = curl_errno($curl);
  163. // 获取错误信息
  164. $errorMessage = curl_error($curl);
  165. $message = "cURL Error #{$errorNumber}: {$errorMessage}";
  166. Log::channel('apiLog')->info('hc写入数据', ["param" => $message]);
  167. }
  168. curl_close($curl);
  169. $res = json_decode($response,true);
  170. if(! isset($res['status'])){//报错了
  171. Log::channel('apiLog')->info('hc写入数据返回错误', ["param" => $response]);
  172. }
  173. return [true, ''];
  174. }
  175. function getNowDay(){
  176. // 获取当前时间
  177. $currentDateTime = new \DateTime();
  178. // 设置时区为 PRC
  179. $currentDateTime->setTimezone(new \DateTimeZone('UTC'));
  180. // 格式化时间为 "2023-09-21T16:00:00.000Z" 的格式
  181. $formattedDateTime = $currentDateTime->format('Y-m-d\TH:i:s.000\Z');
  182. return $formattedDateTime;
  183. }
  184. }