| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241 |
- <?php
- namespace App\Jobs;
- 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 Illuminate\Support\Facades\Log;
- 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.zslfip');
- }
- // public function handle()
- // {
- // try{
- // if(empty($this->data['data'])) return;
- //
- // $deviceId = $this->data['data']['deviceId'];
- // if($this->data['type'] == "dataPoint"){
- // foreach ($this->data['data']['dataPoints'] as $value){
- // $dev_eui = $deviceId . $value['dataPointId'];
- //
- // if(empty($value['value'])) continue;
- //
- // //发送给朗峰
- // $this->sendToDevice(['dev_eui' => $dev_eui, 'value' => $value['value']]);
- // }
- // }
- // }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);
- // $this->delete();
- // }
- // }
- // public function sendToDevice($data){
- // list($status,$token) = ClearDataService::getTokenLf();
- // if(! $status) return;
- //
- // $url = $this->url . "api/module-data/device_machine_record/device_machine_record/diy/create_single_data";
- // $post = [
- // 'bizId' => -1,
- // 'bizTypeEk' => 'LOWCODE',
- // 'data' => [
- // 'device_machine_record' => [
- // 'machine_code' => $data['dev_eui'],
- // 'param_value' => floatval($data['value']),
- // 'record_time' => $this->getNowDay()
- // ]
- // ],
- // 'dynamicFormId' => '477743923368955904',
- // 'showModelId' => '477745421456904192'
- // ];
- // $header = ["Authorization: Bearer {$token}","Content-Type:application/json","Site:LFMY"];
- //
- // $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}";
- // 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['status'])){//报错了
- // file_put_contents('record_man_lf_error.txt',date('Y-m-d H:i:s'). PHP_EOL . $response .PHP_EOL.json_encode($post),8);
- // }
- // }
- /**
- * Execute the job.
- *
- * @return void
- */
- public function handle()
- {
- try {
- if (empty($this->data['data'])) return;
- $deviceId = $this->data['data']['deviceId'];
- $dataPoints = $this->data['data']['dataPoints'] ?? [];
- if ($this->data['type'] == "dataPoint") {
- $redisKey = 'buffer:langfeng_data';
- $batchSize = 500;
- // 1. 整理数据并推入 Redis 列表
- $records = [];
- foreach ($dataPoints as $value) {
- if (empty($value['value'])) continue;
- $records[] = json_encode([
- 'machine_code' => $deviceId . $value['dataPointId'],
- 'param_value' => floatval($value['value']),
- 'time' => $this->getNowDay()
- ]);
- }
- if (!empty($records)) {
- // 批量推入 Redis
- \Illuminate\Support\Facades\Redis::rpush($redisKey, ...$records);
- // 【核心修改点】:每次写入数据都重置 Key 的过期时间为 30 分钟 (1800秒)
- // 只要持续有数据进来,这个 Key 就一直有效
- // 如果超过 30 分钟没新数据进来(设备关机/下班),Redis 自动删除该 Key
- \Illuminate\Support\Facades\Redis::expire($redisKey, 1800);
- }
- // 2. 检查 Redis 里的总条数
- $currentCount = \Illuminate\Support\Facades\Redis::llen($redisKey);
- // 3. 达到 500 条,执行批量发送
- if ($currentCount >= $batchSize) {
- $this->processBatchSend($redisKey, $batchSize);
- }
- }
- } catch (\Exception $exception) {
- Log::channel('apiLog')->info('lf开始位置', ["param" => $exception->getMessage()]);
- $this->delete();
- }
- }
- /**
- * 批量处理发送逻辑
- */
- protected function processBatchSend($redisKey, $batchSize)
- {
- // 原子化弹出 500 条数据
- $rawRecords = \Illuminate\Support\Facades\Redis::pipeline(function ($pipe) use ($redisKey, $batchSize) {
- for ($i = 0; $i < $batchSize; $i++) {
- $pipe->lpop($redisKey);
- }
- });
- $batchData = array_map(fn($item) => json_decode($item, true), array_filter($rawRecords));
- if (empty($batchData)) return;
- list($status,$msg) = $this->sendToDeviceBatch($batchData);
- if(! $status) echo $msg;
- }
- public function sendToDeviceBatch($data){
- list($status,$token) = ClearDataService::getTokenLf();
- if(! $status) return [false, $token];
- $url = $this->url . "api/module-data/hc_device_machine_record/hc_device_machine_record/diy/create_data";
- $post = [
- 'data' => [
- 'device_machine_record' => $data
- ]
- ];
- $header = ["Authorization: Bearer {$token}","Content-Type:application/json","Site:LFMY"];
- $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('lf写入数据', ["param" => $message]);
- }
- curl_close($curl);
- $res = json_decode($response,true);
- if(! isset($res['status'])){//报错了
- Log::channel('apiLog')->info('lf写入数据返回错误', ["param" => $response]);
- }
- return [true, ''];
- }
- 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;
- }
- }
|