cqp пре 6 месеци
родитељ
комит
aea6efd37c

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

@@ -7,9 +7,11 @@ use App\Jobs\DesktopDeviceJob;
 use App\Jobs\LabelDealCsJob;
 use App\Jobs\LabelDealJob;
 use App\Jobs\ManDeviceJob;
+use App\Jobs\ManDeviceJobLf;
 use App\Jobs\ProcessDataJob;
 use App\Model\BigKingCbj;
 use Illuminate\Http\Request;
+use Illuminate\Support\Facades\Log;
 use Illuminate\Support\Facades\Redis;
 
 
@@ -28,13 +30,22 @@ class JobController extends BaseController
         echo 'ok';
     }
 
-    //有人云
+    //有人云(大王椰)
     public function processDataJobAddDeviceMan(Request $request){
         dispatch(new ManDeviceJob($request->all()))->onQueue('man_device');
 
         echo $request->get('verify');die;
     }
 
+    //有人云(朗峰)
+    public function processDataJobAddDeviceManLf(Request $request){
+//        Log::channel('apiLog')->info('lf源数据', ["param" => $request->toArray()]);
+
+        dispatch(new ManDeviceJobLf($request->all()))->onQueue('man_device_lf');
+
+        echo $request->get('verify');die;
+    }
+
     public function delKey(Request $request){return;
         $token_key = 'big_king_login_token';
 //        $token = Redis::get($token_key);

+ 16 - 19
app/Jobs/ManDeviceJob.php

@@ -48,10 +48,6 @@ class ManDeviceJob implements ShouldQueue
             $deviceName = $this->data['data']['deviceName'];
             $ip = self::getIP();
             $data_type = 3;
-            $date = date('Ymd',time());
-
-            //工序-----------------------------
-            $model = new DeviceData(['channel' => $date]);
             if($this->data['type'] == "dataPoint"){
                 $insert = [];
                 foreach ($this->data['data']['dataPoints'] as $value){
@@ -72,22 +68,23 @@ class ManDeviceJob implements ShouldQueue
                         'crt_time' => time()
                     ];
                 }
-                $model->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()
-                    ];
-                    $model->insert($insert);
-                }
+                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);

+ 163 - 0
app/Jobs/ManDeviceJobLf.php

@@ -0,0 +1,163 @@
+<?php
+
+namespace App\Jobs;
+
+use App\Model\DeviceDataLf;
+use App\Model\LfDevice;
+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 ManDeviceJobLf 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.zslf');
+    }
+
+    /**
+     * Execute the job.
+     *
+     * @return void
+     */
+    public function handle()
+    {
+        try{
+            if(empty($this->data['data'])) return;
+
+            $device = LfDevice::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[] = [
+                        'happening_data' => $value['value'],
+                        'dev_eui' => $dev_eui,
+                        'device_name' => $deviceName,
+                        'source_ip' => $ip,
+                        'data_type' => $data_type,
+                        'crt_time' => time()
+                    ];
+                }
+                DeviceDataLf::insert($insert);
+            }
+        }catch (\Exception $exception){
+            file_put_contents('send_man_error_lf.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::getTokenLf();
+        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_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}";
+            file_put_contents('record_man_lf_error.txt',date('Y-m-d H:i:s'). PHP_EOL . $message .PHP_EOL,8);
+        }
+        curl_close($curl);
+
+        $res = json_decode($response,true);
+        if(isset($res['code'])){//报错了
+            file_put_contents('record_man_lf_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;
+    }
+}

+ 1 - 37
app/Model/DeviceData.php

@@ -3,48 +3,12 @@
 namespace App\Model;
 
 use Illuminate\Database\Eloquent\Model;
-use Illuminate\Support\Facades\DB;
-use Illuminate\Support\Facades\Schema;
 
 class DeviceData extends Model
 {
-    protected $table = "device_data"; //表
+    protected $table = "device_data"; //指定
     const CREATED_AT = 'crt_time';
     const UPDATED_AT = 'upd_time';
     protected $dateFormat = 'U';
-    protected $guarded = [];
 
-    public function __construct(array $attributes = [])
-    {
-        parent::__construct($attributes);
-        if (isset($attributes['channel']) && ! empty($attributes['channel'])) {
-            $channel = $attributes['channel'];
-            $year_month = substr($channel,0,7);
-            $year_month = str_replace('-','_',$year_month);
-            $this->setTableById($year_month);
-        }
-    }
-
-    public function setTableById($channel)
-    {
-        if (! empty($channel)) {
-            $tb = $this->table.'_' . (string)$channel;
-            $this->createTable($tb);
-            $this->setTable($tb);
-        }
-
-    }
-
-    //创建表
-    private function createTable($table){
-        if(! empty($table) && ! Schema::hasTable($table)){
-            //执行建表语句
-            DB::statement('create table '. $table .' like device_data_for_create');
-        }
-    }
-
-    public function is_table_isset(){
-        if(Schema::hasTable($this->table)) return true;
-        return false;
-    }
 }

+ 13 - 0
app/Model/DeviceDataLf.php

@@ -0,0 +1,13 @@
+<?php
+
+namespace App\Model;
+
+use Illuminate\Database\Eloquent\Model;
+
+class DeviceDataLf extends Model
+{
+    protected $table = "device_data_lf"; //表
+    const CREATED_AT = 'crt_time';
+    const UPDATED_AT = 'upd_time';
+    protected $dateFormat = 'U';
+}

+ 14 - 0
app/Model/LfDevice.php

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

+ 48 - 0
app/Service/ClearDataService.php

@@ -257,4 +257,52 @@ class ClearDataService extends Service
 
         return [true,$token];
     }
+
+    public static function getTokenLf(){
+        $token_key = 'lf_device_token';
+        $token = Redis::get($token_key);
+        if(! $token){
+            $url = config('ip.zslf');
+            $post = array("name" => "admin","password"=>"gzy#12345","rememberMe"=>true);
+            $header = ['Content-Type:application/json'];
+            $curl = curl_init();
+            curl_setopt_array($curl, array(
+                CURLOPT_URL => $url . '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_SSL_VERIFYPEER => false,
+                CURLOPT_POSTFIELDS => json_encode($post),
+                CURLOPT_HTTPHEADER =>  $header,
+            ));
+            $response = curl_exec($curl);
+            if ($response === false) {
+                // 获取错误号
+                $errorNumber = curl_errno($curl);
+                // 获取错误信息
+                $errorMessage = curl_error($curl);
+                $message = "cURL Error #{$errorNumber}: {$errorMessage}";
+                file_put_contents('lf_device_error.txt',date('Y-m-d H:i:s'). PHP_EOL . $message .PHP_EOL,8);
+            }
+
+            curl_close($curl);
+            $result = json_decode($response,true);
+            if(empty($result['token'])) {
+                file_put_contents('lf_device_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 - 0
config/ip.php

@@ -3,4 +3,5 @@
 return [
     'cs' => 'http://121.36.142.167:7774/', //测试
     'zs' => 'http://122.112.250.253:7774/', //正式
+    'zslf' => 'https://gzy.qingyaokeji.com/', //正式朗峰
 ];

+ 1 - 0
routes/api.php

@@ -42,6 +42,7 @@ Route::any('login', 'Api\LoginController@login');
 Route::any('job', 'Api\JobController@processDataJobAdd');
 Route::any('job_device', 'Api\JobController@processDataJobAddDevice');
 Route::any('man_device', 'Api\JobController@processDataJobAddDeviceMan');
+Route::any('man_device_lf', 'Api\JobController@processDataJobAddDeviceManLf');
 //通道门传输的数据保存单号
 Route::any('setOrderNumber', 'Api\InOutOptionController@setOrderNumber');
 //获取单号