where('parent_id', $id)->with('storage.materialBox')->get(); } /** * 拍灯触发任务 * @param $locCode * @param $PTLAction * @return array|bool[] * @throws \Exception */ public function lightOffTask($locCode, $PTLAction): array { $station = Station::query()->with(['pendingStationTask.stationTaskMaterialBoxes.materialBox','storage'=>function($query){ $query->whereNotNull("material_box_id")->orderByDesc("updated_at"); }])->where('code', $locCode)->first(); //站存在 站为缓存架2 站为蓝灯状态 if (app("StationService")->isHalfBoxLocation($station)){ if (!Storage::query()->where("station_id",$station->id)->whereNotNull("material_box_id")->first())return ['success' => false, 'errMsg' => "任务执行中,不允许灭灯"]; if (!app("StorageService")->checkStorage($station)){ $this->lightUp($station->code,'0','1',["title"=>"上架失败,联系管理员"]); return ['success' => false,'errMsg' => '上架任务失败']; }; } try { $bool = $this->putBinToStore($station); // 推送任务 if ($bool) { LogService::log(__CLASS__, 'lightOffTask', 'code' . ' true' . $locCode . json_encode($station)); return ['success' => true]; } else { return ['success' => false, 'errMsg' => '机器人推送失败']; } } catch (ErrorException $e) { LogService::log(__FUNCTION__, '缓存架推送任务失败', json_encode($e->getMessage())); return ['success' => false, 'errMsg' => $e->getMessage()]; } } /** * 推任务至海柔机器人 * @param $station * @return array * @throws ErrorException * @throws \Exception */ public function putBinToStore($station): array { $this->instant($this->foreignHaiRoboticsService, 'ForeignHaiRoboticsService'); $this->instant($this->stationService, 'StationService'); /** @var MaterialBox $materialbox */ $materialBox = $station->storage->materialbox; $formStation = $this->stationService->getStation_byType('立库'); // 立库 $stationTask = StationTask::query()->create(['station_id' => $formStation['id'], 'status' => '待处理']); // 生成任务 /** @var StationTaskMaterialBox $stationTaskMaterialBox */ $stationTaskMaterialBox = StationTaskMaterialBox::query()->create([ 'station_id' => $formStation['id'], 'material_box_id' => $materialBox['id'], 'status' => '待处理', 'station_task_id' => $stationTask['id'], 'type' => '放', ]); StationTaskChildren::query()->create([ 'station_task_id' => $stationTask['id'], 'station_taskable_type' => StationTaskMaterialBox::class, 'station_taskable_id' => $stationTaskMaterialBox['id'] ]); $bool = $this->foreignHaiRoboticsService->putBinToStore_fromCacheShelf($stationTaskMaterialBox, $station); return $bool ? ['success' => true] : ['success' => false]; } /** * 缓存架和料箱的绑定 * @param $stationCode * @param $materialBoxCode * @return array */ public function bindMaterialBox($stationCode, $materialBoxCode): array { $station = Station::query()->with('storage')->where('code', $stationCode)->first(); if (!$station) { $arr = []; preg_match('/^HAI([\w]+)/', $stationCode, $arr); $parentCode = $arr[1] ?? ''; $stationType = StationType::query()->where('name', '缓存架')->first(); $parentStation = Station::query()->firstOrCreate(['code' => $parentCode], ['station_type_id' => $stationType['id']]); $station = Station::query()->firstOrCreate(['code' => $stationCode, 'parent_id' => $parentStation['id']], ['name' => $stationCode, 'station_type_id' => $stationType['id']]); } $materialBox = MaterialBox::query()->firstOrCreate(['code' => $materialBoxCode]); $storage = $station->storage ?? Storage::query()->firstOrCreate(['station_id' => $station['id']]); $result = $this->_stationCacheLightOn($station['code'],$materialBoxCode); if($result['code'] == 200){ $storage->update(['material_box_id' => $materialBox['id'],'status' => 1]); return ['success' => true]; } return ['success' => false,'message' => $result['errMsg']]; } /** * 控制格口亮灯 * @param $locCode * @param null $materialCode * @param string $title * @param string $color * @return mixed */ public function _stationCacheLightOn($locCode, $materialCode = null, $title = 'title', string $color = '1') { $params = [ "areaCode" => "1004", 'locCode' => $locCode, 'PTLAction' => 1, 'PTLSettings' => ['color' => $color, 'frequency' => 1], "displayInfo" => [ "detail01" => $materialCode, "detail02" => "detail02", "detail03" => "detail03", "qrCode" => "qrCode", "qty00" => "11", "qty01" => 1, "qty02" => 2, "title" => $title, "uomDesc01" => "uo", "uomDesc02" => "uo" ], ]; $response = Http::post(config('api.haiq.storage.light'), $params); return json_decode($response->body()); } /** * 站亮灯 * * @param string $stationCode * @param string|null $materialCode * @param string $color explain: 0-red 1-green 2-blue 3-yellow * @param string $frequency explain: 0-常亮 1-一次 2-两次 3-三次 4-四次 5-五次 (均为/秒) * @param string $title * @return mixed */ public function stationLightUp(string $stationCode,?string $materialCode = null, string $color = '1', string $frequency = '0', $title = '') { $params = [ "areaCode" => "1004", 'locCode' => $stationCode, 'PTLAction' => 1, 'PTLSettings' => ['color' => $color, 'frequency' => $frequency], "displayInfo" => [ "detail01" => $materialCode, "detail02" => "detail02", "detail03" => "detail03", "qrCode" => "qrCode", "qty00" => "11", "qty01" => 1, "qty02" => 2, "title" => $title, "uomDesc01" => "uo", "uomDesc02" => "uo" ], ]; $response = Http::post(config('api.haiq.storage.light'), $params); return json_decode($response->body()); } /** * 站亮灯 * * @param string $stationCode * @param string $color explain: 0-red 1-green 2-blue 3-yellow * @param string $frequency explain: 0-常亮 1-一次 2-两次 3-三次 4-四次 5-五次 (均为/秒) * @param array $info * @return bool */ public function lightUp(string $stationCode, string $color = '1', string $frequency = '0',array $info = []):bool { $default = [ "detail01" => '', "detail02" => "", "detail03" => "", "qrCode" => "", "qty00" => "", "qty01" => 0, "qty02" => 0, "title" => '', "uomDesc01" => "", "uomDesc02" => "" ]; foreach ($info as $key=>$item)$default[$key] = $item; $params = [ "areaCode" => "1004", 'locCode' => $stationCode, 'PTLAction' => 1, 'PTLSettings' => ['color' => $color, 'frequency' => $frequency], "displayInfo" => $default, ]; $response = Http::post(config('api.haiq.storage.light'), $params); return $response->status()==200; } /** * 控制格口灭灯 * @param $locCode * @return mixed */ public function _stationCacheLightOff($locCode) { if (!$locCode) return null; $params = [ "areaCode" => "1004", 'locCode' => $locCode, 'PTLAction' => 0, ]; $response = Http::post(config('api.haiq.storage.light'), $params); return json_decode($response->body()); } /** * 广播 type success成功 error 异常 * @param $locCode * @param $PTLAction * @param string $type */ public function _stationCacheBroadCast($locCode, $PTLAction, string $type = 'success') { if (!$locCode) return; if ($PTLAction == 0) { $station = Station::query()->with('parent')->where('code', $locCode)->first(); $json = json_encode([ 'station_id' => $station['parent']['id'], 'code' => $station['parent']['code'], 'gird_id' => $station['id'], 'grid_code' => $station['code'], 'type' => $type, 'status' => 0 ]); broadcast(new BroadcastToStation($station['parent_id'], $json)); } } /** * 根据立库任务完成对storage进行修改 * @param StationTaskMaterialBox $stationTaskMaterial */ public function putStationTaskMaterialBoxProcess(StationTaskMaterialBox $stationTaskMaterial) { $this->instant($this->stationTaskMaterialBoxService, 'StationTaskMaterialBoxService'); $storage = Storage::query()->where('material_box_id', $stationTaskMaterial['material_box_id'])->where('status',1)->first(); if($storage)$storage->update(['status' => 0, 'material_box_id' => null]); $this->_stationCacheLightOff($stationTaskMaterial->station->code ?? null); //海柔格口灭灯 $this->_stationCacheBroadCast($stationTaskMaterial->station->code ?? null, 0); //通知缓存架任务完成 } /** * 取消任务 * @param $stationCode * @return array * @throws \Exception */ public function clearTask($stationCode): array { $station = Station::query()->with('storage')->where('code', $stationCode)->first(); if (!$station) return ['success' => false, 'message' => '传入参数异常,找不到对应的缓存架记录']; $stationTaskMaterialBox = StationTaskMaterialBox::query()->where('material_box_id',$station['storage']['material_box_id'])->first(); if($stationTaskMaterialBox ){ if($stationTaskMaterialBox->status == '处理中') return ['success' => false, 'message' => '当前缓存架任务正在处理中']; else{ $stationTaskMaterialBox->delete(); } } $station->storage->update(['status' => 0,'material_box_id' => null]); return ['success' => true]; } }