Kaynağa Gözat

有人云设备

cqpCow 1 yıl önce
ebeveyn
işleme
4dd2433fcc

+ 1 - 1
app/Http/Controllers/Api/JobController.php

@@ -28,6 +28,6 @@ class JobController extends BaseController
     public function processDataJobAddDeviceMan(Request $request){
         dispatch(new ManDeviceJob($request->all()))->onQueue('man_device');
 
-        return $this->json_return(200,'',['verify' => $request->get('verify')]);
+        echo $request->get('verify');die;
     }
 }

+ 4 - 2
app/Jobs/DesktopDeviceJob.php

@@ -2,6 +2,7 @@
 
 namespace App\Jobs;
 
+use App\Service\ClearDataService;
 use Illuminate\Bus\Queueable;
 use Illuminate\Contracts\Queue\ShouldQueue;
 use Illuminate\Foundation\Bus\Dispatchable;
@@ -60,6 +61,8 @@ class DesktopDeviceJob implements ShouldQueue
 
     public function getDispatchList($data){
         if(empty($data)) return [];
+        list($status,$token) = ClearDataService::getToken();
+        if(! $status) return;
 
         $url = 'http://121.36.142.167:7774/jbl/api/module-data/dispatch_orders/page';
         $post = [
@@ -78,8 +81,7 @@ class DesktopDeviceJob implements ShouldQueue
             "developmentSystemId" => null,
             "debugFlag" => true,
         ];
-        $header = ['Authorization: Bearer eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiIxIiwiYXV0aCI6IlJPTEVfSU5ORVJfVVNFUixST0xFX0FETUlOLFJPTEVfSU5URVJGQUNFIiwidG9rZW5JZCI6IjM1IiwiZXhwIjoxNjk0Njc0MTE0fQ.L3Di3K_cpF0rWSgvzbcLufLm8bkCxd3Y-xudfKzSm4F-qdpDr0hYWWQP5K5BYTNuZnu4tWpGmSW2KRHU0pjt-A','Content-Type:application/json','Site:010800179'];
-
+        $header = ["Authorization: Bearer {$token}","Content-Type:application/json",'Site:91451322MA5P9JNKXA'];
         $json = str_replace('"workflowSearchBean":[]','"workflowSearchBean":{}',json_encode($post));
 
         $curl = curl_init();

+ 129 - 8
app/Jobs/ManDeviceJob.php

@@ -2,6 +2,9 @@
 
 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;
@@ -33,23 +36,141 @@ class ManDeviceJob implements ShouldQueue
      */
     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(is_array($this->data) || is_object($this->data)){
-                $data = json_encode($this->data);
-            }else {
-                $data = $this->data;
+            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);
+                }
             }
-            file_put_contents('send_man.txt',date('Y-m-d H:i:s').PHP_EOL . $data . PHP_EOL,8);
 
-            //输出信息 测试
-            $this->echoMessage(new ConsoleOutput());
+//            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").PHP_EOL.$exception->getMessage().PHP_EOL,8);
+            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 = "http://121.36.142.167:7774/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('Asia/Shanghai'));
+
+// 格式化时间为 "2023-09-21T16:00:00.000Z" 的格式
+        $formattedDateTime = $currentDateTime->format('Y-m-d\TH:i:s.000\Z');
+
+        return $formattedDateTime;
+    }
 }

+ 22 - 3
app/Jobs/ProcessDataJob.php

@@ -55,6 +55,9 @@ class ProcessDataJob implements ShouldQueue
 
     public function sendToDevice($data){
         date_default_timezone_set("PRC");
+        list($status,$token) = ClearDataService::getToken();
+        if(! $status) return;
+
         $url = "http://121.36.142.167:7774/api/module-data/device_machine_record/device_machine_record";
         $post = [
             'bizId' => -1,
@@ -63,13 +66,13 @@ class ProcessDataJob implements ShouldQueue
                 'device_machine_record' => [
                     'machine_code' => $data['dev_eui'],
                     'param_value' => $data['value'],
-                    'record_time' => date("Y-m-d H:i:s")
+                    'record_time' => $this->getNowDay()
                 ]
             ],
             'dynamicFormId' => '477743923368955904',
             'showModelId' => '477745421456904192'
         ];
-        $header = ['Authorization: Bearer eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiIxIiwiYXV0aCI6IlJPTEVfSU5ORVJfVVNFUixST0xFX0FETUlOLFJPTEVfSU5URVJGQUNFIiwidG9rZW5JZCI6IjM1IiwiZXhwIjoxNjk0Njc0MTE0fQ.L3Di3K_cpF0rWSgvzbcLufLm8bkCxd3Y-xudfKzSm4F-qdpDr0hYWWQP5K5BYTNuZnu4tWpGmSW2KRHU0pjt-A','Content-Type:application/json'];
+        $header = ["Authorization: Bearer {$token}","Content-Type:application/json"];
 
         $curl = curl_init();
         curl_setopt_array($curl, array(
@@ -87,11 +90,27 @@ class ProcessDataJob implements ShouldQueue
         $response = curl_exec($curl);
         curl_close($curl);
 
-        file_put_contents('record2.txt',date('Y-m-d H:i:s'). PHP_EOL . $response .PHP_EOL,8);
+        $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('Asia/Shanghai'));
+
+// 格式化时间为 "2023-09-21T16:00:00.000Z" 的格式
+        $formattedDateTime = $currentDateTime->format('Y-m-d\TH:i:s.000\Z');
+
+        return $formattedDateTime;
+    }
 }

+ 14 - 0
app/Model/BigKingDevice.php

@@ -0,0 +1,14 @@
+<?php
+
+namespace App\Model;
+
+use Illuminate\Database\Eloquent\Model;
+
+class BigKingDevice extends Model
+{
+    protected $table = "big_king_device"; //指定表
+    const CREATED_AT = null;
+    const UPDATED_AT = null;
+    protected $dateFormat = 'U';
+
+}

+ 63 - 13
app/Service/ClearDataService.php

@@ -4,6 +4,8 @@ namespace App\Service;
 
 
 use App\Model\DeviceData;
+use Illuminate\Http\Request;
+use Illuminate\Support\Facades\Redis;
 
 class ClearDataService extends Service
 {
@@ -84,20 +86,24 @@ class ClearDataService extends Service
      * */
 
     public static function saveData($data){
-        //保存数据
-        $model = new DeviceData();
-        $model->data = json_encode($data);//源数据
-        $model->dev_eui = $data['obj']['devEUI'];
-        $model->device_name = $data['obj']['deviceName'];
-        $model->source_ip = self::getIP();
-        if(isset($data['temperature'])){
-            $model->data_type = 1;
-            $model->happening_data = $data['temperature'];
-        }elseif (isset($data['pressure'])){
-            $model->data_type = 2;
-            $model->happening_data = $data['pressure'];
+        try{
+            //保存数据
+            $model = new DeviceData();
+            $model->data = json_encode($data);//源数据
+            $model->dev_eui = $data['obj']['devEUI'];
+            $model->device_name = $data['obj']['deviceName'];
+            $model->source_ip = self::getIP();
+            if(isset($data['temperature'])){
+                $model->data_type = 1;
+                $model->happening_data = $data['temperature'];
+            }elseif (isset($data['pressure'])){
+                $model->data_type = 2;
+                $model->happening_data = $data['pressure'];
+            }
+            $model->save();
+        }catch (\Exception $exception){
+            file_put_contents('record_errors.txt',json_encode($data) . PHP_EOL . $exception->getFile().$exception->getLine().$exception->getCode(),8);
         }
-        $model->save();
     }
 
     public static function clearData($data){
@@ -140,4 +146,48 @@ class ClearDataService extends Service
         }
         return $ip;
     }
+
+    //"status": "error",
+    //        "userType": null,
+    //        "userDto": null,
+    //        "token": null,
+    //        "errorMessage": "用户名或密码不正确",
+    //        "brushFaceFlag": false,
+    //        "brushFaceValidationResult": null
+    public static function getToken(){
+        $token_key = 'big_king_login_token';
+        $token = Redis::get($token_key);
+        if(! $token){
+            $post = array("name" => "admin","password"=>"admin","rememberMe"=>true);
+            $header = ['Content-Type:application/json'];
+            $curl = curl_init();
+            curl_setopt_array($curl, array(
+                CURLOPT_URL => 'http://121.36.142.167:7774/jbl/api/mes/login',
+                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);
+            $result = json_decode($response,true);
+            if(empty($result['token'])) {
+                file_put_contents('big_king_token_error.txt',date('Y-m-d H:i:s'). PHP_EOL . $response .PHP_EOL,8);
+                return [false,''];
+            }else{
+                $token = $result['token'];
+                $expire_time = 1728000; //20天
+                Redis::set($token_key,$token);
+                Redis::expire($token_key, $expire_time);
+                return [true,$token];
+            }
+        }
+
+        return [true,$token];
+    }
 }

+ 1 - 1
config/logging.php

@@ -111,7 +111,7 @@ return [
             'driver' => 'daily',
             'path' => storage_path('logs/request.log'),
             'level' => 'debug',
-            'days' => 31,
+            'days' => 7,
         ],
     ],