cqp 2 hafta önce
ebeveyn
işleme
0b5d960545
1 değiştirilmiş dosya ile 178 ekleme ve 0 silme
  1. 178 0
      app/Console/Commands/DeviceJLWM.php

+ 178 - 0
app/Console/Commands/DeviceJLWM.php

@@ -0,0 +1,178 @@
+<?php
+
+namespace App\Console\Commands;
+
+use App\Jobs\ManDeviceJobHc;
+use App\Service\ClearDataService;
+use Illuminate\Console\Command;
+use Illuminate\Support\Facades\Log;
+
+class DeviceJLWM extends Command
+{
+    /**
+     * The name and signature of the console command.
+     *
+     * @var string
+     */
+    protected $signature = 'command:deviceJLWM';
+
+    /**
+     * The console command description.
+     *
+     * @var string
+     */
+    protected $description = 'Command description';
+
+    /**
+     * Create a new command instance.
+     *
+     * @return void
+     */
+    public function __construct()
+    {
+        parent::__construct();
+    }
+
+    /**
+     * Execute the console command.
+     *
+     * @return mixed
+     */
+    public function handle()
+    {
+        try {
+            $this->generateMachineMockData();
+        }catch (\Exception $exception){
+            Log::error("恒昌设备异常", ['msg' => $exception->getMessage()]);
+        }
+    }
+
+    public function sendToDeviceBatch($data){
+        list($status,$token) = ClearDataService::getTokenJLWM();
+        if(! $status) return [false, $token];
+
+        $url = $this->url . "api/module-data/jlwm_device_machine_record/jlwm_device_machine_record/diy/create_data";
+        $post = [
+            'data' => [
+                'device_machine_record' => $data
+            ]
+        ];
+        $header = ["Authorization: Bearer {$token}","Content-Type:application/json","Site:JLWM"];
+
+        $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('jlwm写入数据', ["param" => $message]);
+        }
+        curl_close($curl);
+
+        $res = json_decode($response,true);
+        if(! isset($res['status'])){//报错了
+            Log::channel('apiLog')->info('jlwm写入数据返回错误', ["param" => $response]);
+        }
+
+        return [true, ''];
+    }
+
+    public function generateMachineMockData() {
+        $machines = [
+            "家丽屋美四边锯" => ["0140142511130000594535177367", "0140142511130000594535177368", "0140142511130000594535177369"],
+            "双边锁孔机1号" => ["0140142511130000614735177367", "0140142511130000614735177368", "0140142511130000614735177369"],
+            "门板吸塑机1号" => ["0140142511130000694435177367", "0140142511130000694435177368", "0140142511130000694435177369"],
+            "双头追踪切割机1号" => ["0140142511130000730335177367", "0140142511130000730335177368", "0140142511130000730335177369"],
+            "门套洗型机1号" => ["0140142511130000718335177367", "0140142511130000718335177368", "0140142511130000718335177369"],
+            "L型侧拼机1号" => ["0140142511130000818335177367", "0140142511130000818335177368", "0140142511130000818335177369"],
+            "雕刻机1号" => ["0140142511130000579835177367", "0140142511130000579835177368", "0140142511130000579835177369"],
+            "(龙门)上板机1号" => ["0140142511130000650735177367", "0140142511130000650735177368", "0140142511130000650735177369"],
+            "开料机1号" => ["0140142511130000685035177367", "0140142511130000685035177368", "0140142511130000685035177369"],
+            "双边锁孔机2号" => ["0140142511130000714735177367", "0140142511130000714735177368", "0140142511130000714735177369"],
+            "双边锁孔机3号" => ["0140142511130000724735177367", "0140142511130000724735177368", "0140142511130000724735177369"],
+            "门板吸塑机2号" => ["0140142511130000604435177367", "0140142511130000604435177368", "0140142511130000604435177369"],
+            "雕刻机2号" => ["0140142511130000578935177367", "0140142511130000578935177368", "0140142511130000578935177369"],
+            "雕刻机3号" => ["0140142511130000978935177367", "0140142511130000978935177368", "0140142511130000978935177369"],
+            "门套包覆机2号" => ["0140142511130000685635177367", "0140142511130000685635177368", "0140142511130000685635177369"],
+            "门套包覆机3号" => ["0140142511130000179135177367", "0140142511130000179135177368", "0140142511130000179135177369"],
+            "门套包覆机4号" => ["0140142511130000532735177367", "0140142511130000532735177368", "0140142511130000532735177369"],
+            "门套包覆机5号" => ["0140142511130000525835177367", "0140142511130000525835177368", "0140142511130000525835177369"],
+            "封边机2号" => ["0140142511130000649735177367", "0140142511130000649735177368", "0140142511130000649735177369"],
+            "门套包覆机1号" => ["0140142511130000673036193247", "0140142511130000673036193329", "0140142511130000673036229962"],
+            "封边机1号" => ["0140142511130000649735314979", "0140142511130000649735314986", "0140142511130000649735314996"]
+        ];
+
+        $results = [];
+        $todayStr = date('Ymd');
+
+        // 计算从凌晨 00:00 到现在经过的总分钟数
+        $passedMinutes = (int)date('H') * 60 + (int)date('i');
+
+        foreach ($machines as $name => $codes) {
+            // 1. 模拟 80% 的设备活跃度 (这里不使用固定种子,让每次请求活跃机器略有不同)
+            // 如果希望某台机器今天一整天都不活跃,可以把这行移到种子设定之后
+            if (rand(1, 100) > 80) continue;
+
+            foreach ($codes as $code) {
+                // 2. 每个动作 80% 概率有数据
+                if (rand(1, 100) > 80) continue;
+
+                // --- 核心逻辑:锁定种子 ---
+                mt_srand(crc32($todayStr . $code));
+
+                // A. 锁定今天开机时的“初始值” (例如 500 到 2000 之间)
+                $baseValue = mt_rand(10, 100);
+
+                // B. 锁定这台机器今天的“生产速度” (例如每分钟产出 1 到 5 次)
+                // 关键:因为种子固定,这一天内 $speed 永远不变
+                $speed = mt_rand(1, 5);
+
+                // C. 计算当前累计值 = 初始值 + 经过时间 * 速度
+                $accumulated = $baseValue + ($passedMinutes * $speed);
+
+                // D. (可选) 加入极小的随机扰动,让数据看起来更“活”一点点
+                mt_srand(); // 恢复真随机
+                $accumulated += rand(0, 2);
+
+                $results[] = [
+                    "machine_code" => $code,
+                    "param_value" => floatval($accumulated),
+                    "time" => $this->getNowDay(),
+                ];
+            }
+        }
+
+        // 重置随机种子,确保不影响系统其他逻辑
+        mt_srand();
+
+        $this->sendToDeviceBatch($results);
+    }
+
+    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;
+    }
+}