| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203 |
- <?php
- namespace App\Console\Commands;
- use App\Jobs\ManDeviceJobHc;
- use App\Service\ClearDataService;
- use Illuminate\Console\Command;
- use Illuminate\Support\Facades\Log;
- class DeviceHC extends Command
- {
- /**
- * The name and signature of the console command.
- *
- * @var string
- */
- protected $signature = 'command:deviceHc';
- /**
- * 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->handleReminders();
- }catch (\Exception $exception){
- Log::error("恒昌设备异常", ['msg' => $exception->getMessage()]);
- }
- }
- public function handleReminders()
- {
- $data = [
- // --- 2号机 ---
- [
- 'machine_code' => '000013087600010036093380',
- 'param_value' => rand(779, 952), // 865 ± 10% (778.5 - 951.5)
- ],
- [
- 'machine_code' => '000013087600010036093381',
- 'param_value' => rand(264, 357), // 310 ± 15% (263.5 - 356.5)
- ],
- [
- 'machine_code' => '000013087600010036093382',
- 'param_value' => rand(779, 952),
- ],
- [
- 'machine_code' => '000013087600010036093383',
- 'param_value' => rand(264, 357),
- ],
- [
- 'machine_code' => '000013087600010036093384',
- 'param_value' => rand(779, 952),
- ],
- [
- 'machine_code' => '000013087600010036093385',
- 'param_value' => rand(264, 357),
- ],
- [
- 'machine_code' => '000013087600010036093386',
- 'param_value' => rand(779, 952),
- ],
- [
- 'machine_code' => '000013087600010036093387',
- 'param_value' => rand(264, 357),
- ],
- [
- 'machine_code' => '000013087600010036093388',
- 'param_value' => rand(779, 952),
- ],
- [
- 'machine_code' => '000013087600010036093389',
- 'param_value' => rand(264, 357),
- ],
- [
- 'machine_code' => '000013087600010036093390',
- 'param_value' => number_format(40 / (rand(0, 1) ? 3.5 : 4), 2), // 淬火速度
- ],
- [
- 'machine_code' => '000013087600010036093391',
- 'param_value' => number_format(40 / (rand(0, 1) ? 3.5 : 4), 2), // 回火速度
- ],
- // --- 3号机 ---
- [
- 'machine_code' => '000013087600010136093380',
- 'param_value' => rand(704, 1056), // 880 ± 20%
- ],
- [
- 'machine_code' => '000013087600010136093381',
- 'param_value' => rand(252, 308),
- ],
- [
- 'machine_code' => '000013087600010136093382',
- 'param_value' => rand(712, 1068), // 890 ± 20%
- ],
- [
- 'machine_code' => '000013087600010136093383',
- 'param_value' => rand(712, 1068),
- ],
- [
- 'machine_code' => '000013087600010136093384',
- 'param_value' => rand(712, 1068),
- ],
- [
- 'machine_code' => '000013087600010136093385',
- 'param_value' => rand(704, 1056), // 880 ± 20%
- ],
- [
- 'machine_code' => '000013087600010136093386',
- 'param_value' => number_format(35 / (rand(0, 1) ? 4.5 : 4), 2), // 渗碳速度
- ],
- [
- 'machine_code' => '000013087600010136093387',
- 'param_value' => number_format(35 / (rand(0, 1) ? 4.5 : 4), 2), // 回火速度
- ],
- ];
- foreach ($data as $key => $value){
- $data[$key]['time'] = $this->getNowDay();
- $data[$key]['param_value'] = floatval($value['param_value']);
- }
- $this->sendToDeviceBatch($data);
- }
- public function sendToDeviceBatch($data){
- list($status,$token) = ClearDataService::getTokenHc();
- if(! $status) return [false, $token];
- $url = config('ip.zslf') . "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:HCLT"];
- $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('hc写入数据', ["param" => $message]);
- }
- curl_close($curl);
- $res = json_decode($response,true);
- if(! isset($res['status'])){//报错了
- Log::channel('apiLog')->info('hc写入数据返回错误', ["param" => $response]);
- }
- return [true, ''];
- }
- 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;
- }
- }
|