data = $data; $this->url = config('ip.zs'); } /** * Execute the job. * * @return void */ public function handle() { //标记 file_put_contents('record.txt',json_encode($this->data) . PHP_EOL,8); //保存数据到自己服务器 ClearDataService::saveData($this->data); //处理数据 $return = ClearDataService::clearData($this->data); $this->sendToDevice($return); //传递数据给下一个队列 file_put_contents('record2.txt',json_encode($return) . PHP_EOL,8); //输出信息 $this->echoMessage(new ConsoleOutput()); } public function sendToDevice($data){ date_default_timezone_set("PRC"); list($status,$token) = ClearDataService::getToken(); if(! $status) return; $url = $this->url . "api/module-data/device_machine_record/device_machine_record"; $post = [ 'bizId' => -1, 'bizTypeEk' => 'LOWCODE', 'data' => [ 'device_machine_record' => [ 'machine_code' => $data['dev_eui'], 'param_value' => $data['value'], 'record_time' => $this->getNowDay() ] ], 'dynamicFormId' => '477743923368955904', 'showModelId' => '477745421456904192' ]; $header = ["Authorization: Bearer {$token}","Content-Type:application/json"]; $curl = curl_init(); curl_setopt_array($curl, array( CURLOPT_URL => $url, CURLOPT_RETURNTRANSFER => true, CURLOPT_ENCODING => '', CURLOPT_MAXREDIRS => 10, CURLOPT_TIMEOUT => 0, CURLOPT_FOLLOWLOCATION => true, CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, CURLOPT_CUSTOMREQUEST => 'POST', CURLOPT_POSTFIELDS => json_encode($post), CURLOPT_HTTPHEADER => $header, )); $response = curl_exec($curl); curl_close($curl); $res = json_decode($response,true); if(isset($res['code'])){//报错了 file_put_contents('record_chuangan_error.txt',date('Y-m-d H:i:s'). PHP_EOL . $response .PHP_EOL.json_encode($post),8); } } protected function echoMessage(OutputInterface $output) { $output->writeln(json_encode($this->data)); } 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; } }