handleReminders(); }catch (\Exception $exception){ Log::error("恒昌设备异常", ['msg' => $exception->getMessage()]); } } public function handleReminders() { $data = [ // --- 2号机 --- [ 'machine_code' => '000013087600010036093380', 'param_value' => rand(779, 952), // 865 ± 10% (778.5 - 951.5) ], [ 'machine_code' => '000013087600010036093381', 'param_value' => rand(264, 357), // 310 ± 15% (263.5 - 356.5) ], [ 'machine_code' => '000013087600010036093382', 'param_value' => rand(779, 952), ], [ 'machine_code' => '000013087600010036093383', 'param_value' => rand(264, 357), ], [ 'machine_code' => '000013087600010036093384', 'param_value' => rand(779, 952), ], [ 'machine_code' => '000013087600010036093385', 'param_value' => rand(264, 357), ], [ 'machine_code' => '000013087600010036093386', 'param_value' => rand(779, 952), ], [ 'machine_code' => '000013087600010036093387', 'param_value' => rand(264, 357), ], [ 'machine_code' => '000013087600010036093388', 'param_value' => rand(779, 952), ], [ 'machine_code' => '000013087600010036093389', 'param_value' => rand(264, 357), ], [ 'machine_code' => '000013087600010036093390', 'param_value' => number_format(40 / (rand(0, 1) ? 3.5 : 4), 2), // 淬火速度 ], [ 'machine_code' => '000013087600010036093391', 'param_value' => number_format(40 / (rand(0, 1) ? 3.5 : 4), 2), // 回火速度 ], // --- 3号机 --- [ 'machine_code' => '000013087600010136093380', 'param_value' => rand(704, 1056), // 880 ± 20% ], [ 'machine_code' => '000013087600010136093381', 'param_value' => rand(180, 200), // 180 - 200 区间 ], [ 'machine_code' => '000013087600010136093382', 'param_value' => rand(712, 1068), // 890 ± 20% ], [ 'machine_code' => '000013087600010136093383', 'param_value' => rand(712, 1068), ], [ 'machine_code' => '000013087600010136093384', 'param_value' => rand(712, 1068), ], [ 'machine_code' => '000013087600010136093385', 'param_value' => rand(704, 1056), // 880 ± 20% ], [ 'machine_code' => '000013087600010136093386', 'param_value' => number_format(35 / (rand(0, 1) ? 4.5 : 4), 2), // 渗碳速度 ], [ 'machine_code' => '000013087600010136093387', 'param_value' => number_format(35 / (rand(0, 1) ? 4.5 : 4), 2), // 回火速度 ], ]; foreach ($data as $key => $value){ $data[$key]['time'] = $this->getNowDay(); $data[$key]['param_value'] = floatval($value['param_value']); } $this->sendToDeviceBatch($data); } public function sendToDeviceBatch($data){ list($status,$token) = ClearDataService::getTokenHc(); if(! $status) return [false, $token]; $url = config('ip.zslf') . "api/module-data/hc_device_machine_record/hc_device_machine_record/diy/create_data"; $post = [ 'data' => [ 'device_machine_record' => $data ] ]; $header = ["Authorization: Bearer {$token}","Content-Type:application/json","Site:HCLT"]; $curl = curl_init(); curl_setopt_array($curl, array( CURLOPT_URL => $url, CURLOPT_RETURNTRANSFER => true, CURLOPT_ENCODING => '', CURLOPT_MAXREDIRS => 10, CURLOPT_TIMEOUT => 15, CURLOPT_FOLLOWLOCATION => true, CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, CURLOPT_CUSTOMREQUEST => 'POST', CURLOPT_POSTFIELDS => json_encode($post), CURLOPT_SSL_VERIFYPEER => false, CURLOPT_HTTPHEADER => $header, )); $response = curl_exec($curl); if ($response === false) { // 获取错误号 $errorNumber = curl_errno($curl); // 获取错误信息 $errorMessage = curl_error($curl); $message = "cURL Error #{$errorNumber}: {$errorMessage}"; Log::channel('apiLog')->info('hc写入数据', ["param" => $message]); } curl_close($curl); $res = json_decode($response,true); if(! isset($res['status'])){//报错了 Log::channel('apiLog')->info('hc写入数据返回错误', ["param" => $response]); } return [true, '']; } function getNowDay(){ // 获取当前时间 $currentDateTime = new \DateTime(); // 设置时区为 PRC $currentDateTime->setTimezone(new \DateTimeZone('UTC')); // 格式化时间为 "2023-09-21T16:00:00.000Z" 的格式 $formattedDateTime = $currentDateTime->format('Y-m-d\TH:i:s.000\Z'); return $formattedDateTime; } }