123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178 |
- <?php
- namespace App\Jobs;
- use App\Model\BigKingDevice;
- use App\Model\DeviceData;
- use App\Service\ClearDataService;
- use Illuminate\Bus\Queueable;
- use Illuminate\Contracts\Queue\ShouldQueue;
- use Illuminate\Foundation\Bus\Dispatchable;
- use Illuminate\Queue\InteractsWithQueue;
- use Illuminate\Queue\SerializesModels;
- use Symfony\Component\Console\Output\OutputInterface;
- class ManDeviceJob implements ShouldQueue
- {
- use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
- protected $data;
- protected $url;
- /**
- * Create a new job instance.
- *
- * @return void
- */
- public function __construct($data)
- {
- $this->data = $data;
- $this->url = config('ip.zs');
- }
- /**
- * Execute the job.
- *
- * @return void
- */
- public function handle()
- {
- // {"data":{"position":{"lngGcj":118.56216535926838,"lngBd":118.56858403462685,"lngWgs":118.557395,"latGcj":28.684134864745076,"locationType":"CELL","time":1695365429,"latWgs":28.687429,"deviceId":"01401422100800000103","cusdeviceNos":["0000130876000007"],"latBd":28.690489277743108},"deviceId":"01401422100800000103","deviceName":"1\u53f7\u70ed\u538b\u673a"},"type":"position"}
- try{
- if(empty($this->data['data'])) return;
- $device = BigKingDevice::select('data')->get()->toArray();
- $device = array_column($device,'data');
- $deviceId = $this->data['data']['deviceId'];
- $deviceName = $this->data['data']['deviceName'];
- $ip = self::getIP();
- $data_type = 3;
- if($this->data['type'] == "dataPoint"){
- $insert = [];
- foreach ($this->data['data']['dataPoints'] as $value){
- $dev_eui = $deviceId . $value['dataPointId'];
- if(! in_array($dev_eui,$device)) continue;
- //发送给大王椰
- $this->sendToDevice(['dev_eui' => $dev_eui, 'value' => $value['value']]);
- $insert[] = [
- // 'data' => json_encode($value),
- 'happening_data' => $value['value'],
- 'dev_eui' => $dev_eui,
- 'device_name' => $deviceName,
- 'source_ip' => $ip,
- 'data_type' => $data_type,
- 'crt_time' => time()
- ];
- }
- DeviceData::insert($insert);
- }
- // elseif ($this->data['type'] == "position"){
- // $dev_eui = $deviceId;
- // if(in_array($dev_eui,$device)) {
- // $insert[] = [
- //// 'data' => json_encode($this->data['data']['position']),
- // 'happening_data' => '',
- // 'dev_eui' => $deviceId,
- // 'device_name' => $deviceName,
- // 'source_ip' => $ip,
- // 'data_type' => $data_type,
- // 'crt_time' => time()
- // ];
- // DeviceData::insert($insert);
- // }
- // }
- // if(is_array($this->data) || is_object($this->data)){
- // $data = json_encode($this->data);
- // }else {
- // $data = $this->data;
- // }
- // file_put_contents('send_man.txt',date('Y-m-d H:i:s').PHP_EOL . $data . PHP_EOL,8);
- }catch (\Exception $exception){
- file_put_contents('send_man_error.txt',date("Y-m-d H:i:s").json_encode($this->data).PHP_EOL.$exception->getMessage().$exception->getLine().$exception->getFile().PHP_EOL,8);
- }
- }
- 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_man_error.txt',date('Y-m-d H:i:s'). PHP_EOL . $response .PHP_EOL.json_encode($post),8);
- }
- }
- public static function getIP(){
- if (getenv('HTTP_CLIENT_IP')) {
- $ip = getenv('HTTP_CLIENT_IP');
- }
- elseif (getenv('HTTP_X_REAL_IP')) {
- $ip = getenv('HTTP_X_REAL_IP');
- } elseif (getenv('HTTP_X_FORWARDED_FOR')) {
- $ip = getenv('HTTP_X_FORWARDED_FOR');
- $ips = explode(',', $ip);
- $ip = $ips[0];
- } elseif (getenv('REMOTE_ADDR')) {
- $ip = getenv('REMOTE_ADDR');
- } else {
- $ip = '0.0.0.0';
- }
- return $ip;
- }
- protected function echoMessage(OutputInterface $output)
- {
- $output->writeln($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;
- }
- }
|