| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309 |
- <?php
- namespace App\Services;
- use App\Exceptions\ErrorException;
- use App\MaterialBox;
- use App\StationTaskMaterialBox;
- use Illuminate\Support\Collection;
- use Illuminate\Support\Facades\Http;
- use App\Traits\ServiceAppAop;
- class ForeignHaiRoboticsService
- {
- use ServiceAppAop;
- // protected $modelClass=ForeignHaiRobotics::class;
- /** @var $stationTaskMaterialBoxService StationTaskMaterialBoxService */
- private $stationTaskMaterialBoxService;
- /** @var $stationTaskCommoditiesService StationTaskCommodityService */
- private $stationTaskCommoditiesService;
- /** @var $materialBoxService MaterialBoxService */
- private $materialBoxService;
- /** @var $stationTaskService StationTaskService */
- private $stationTaskService;
- /** @var $stationService StationService */
- private $stationService;
- public function __construct()
- {
- $this->stationTaskMaterialBoxService=null;
- $this->materialBoxService=null;
- $this->stationService=null;
- }
- /**
- * @param string $modeName '输送线入立架'|'立架出至输送线'|'移动立架内位置'|'缓存架入立架'|'立架出至缓存架'
- * @param string $fromLocation
- * @param string $toLocation
- * @param Collection $taskMaterialBoxes
- * @param string $groupId
- * @param int $priority
- * @param int $isSequenced
- * @return array
- */
- private function makeJson_move(
- Collection $taskMaterialBoxes,
- string $modeName,
- string $fromLocation='',
- string $toLocation='',
- $groupId=''
- , $priority=10
- , $isSequenced=1
- ): array
- {
- $timestampSuffix = microtime(true);
- $taskMode=(function()use($modeName){
- switch ($modeName){
- case '输送线入立架': return 1;
- case '立架出至输送线': return 2;
- case '移动立架内位置': return 3;
- case '缓存架入立架':
- case '立架出至缓存架': return 6;
- default: throw new \Exception('发至海柔的移料箱请求,模式不存在');
- }
- })();
- $bins=$taskMaterialBoxes->map(function (StationTaskMaterialBox $taskMaterialBox)use($timestampSuffix,$fromLocation,$toLocation){
- return [
- "taskCode" =>$taskMaterialBox['id'],
- "binCode" => $taskMaterialBox['materialBox']['code'],
- "fromLocCode" => $fromLocation??'',
- "toLocCode" => $toLocation??'',
- ];
- });
- return [[
- "taskMode" =>$taskMode,
- "bins"=>$bins,
- "groupCode"=>$groupId.$timestampSuffix,
- "priority"=>$priority,
- "sequenceFlag"=>$isSequenced,
- ]];
- }
- public function fetchGroup($toLocation, Collection $taskMaterialBoxes, $groupIdPrefix=''): bool
- {
- LogService::log(__METHOD__,'runMany','波次任务分配6.r5f0:');
- LogService::log(__METHOD__,'runMany','波次任务分配6.r5f1:'.json_encode($toLocation).json_encode($taskMaterialBoxes).json_encode($groupIdPrefix));
- $dataToPost=$this->makeJson_move(
- $taskMaterialBoxes,
- '立架出至输送线',
- '',
- $toLocation??'',
- $groupIdPrefix
- );
- LogService::log(__METHOD__,'runMany','波次任务分配6.r5f2:'.json_encode($dataToPost));
- return $this->controlHaiRobot($dataToPost);
- }
- public function moveBin(){
- }
- public function markBinProcessed(
- $workStation,
- $binCode,
- $success,
- $created_at,
- $exception,
- $is_in_plan
- ): bool
- {
- $this->instant($this->stationService,'StationService');
- $this->instant($this->materialBoxService,'MaterialBoxService');
- $this->instant($this->stationTaskMaterialBoxService,'StationTaskMaterialBoxService');
- $this->instant($this->stationTaskCommoditiesService,'StationTaskCommodityService');
- try{
- if($failed
- =!$success)
- throw new ErrorException('海柔任务失败:'.$exception);
- if($NotInPlan
- =!$is_in_plan)
- throw new ErrorException('海柔认为是计划外的料箱:'.$exception);
- $materialBox=
- $this->materialBoxService->get(['code'=>$binCode])->first();
- $stationTaskMaterialBox
- =(function()use($materialBox){
- return $stationTaskMaterialBox=
- $this->stationTaskMaterialBoxService
- ->get(
- [
- 'material_box_id'=>$materialBox['id'],
- 'status'=>['处理中','待处理','异常','处理队列'],
- ]
- )->first();
- })();
- if(!$stationTaskMaterialBox){
- throw new ErrorException('该料箱没有安排在处理队列中');
- }
- $this->putBinToStore($stationTaskMaterialBox)
- ?true
- :(function(){throw new ErrorException('呼叫机器人回收U型线料箱失败');})();
- $this->stationTaskMaterialBoxService
- ->markProcessed($stationTaskMaterialBox);
- $stationTaskMaterialBox_next=
- $this->stationTaskMaterialBoxService
- ->processNextQueued($stationTaskMaterialBox); //找到队列中下一个料箱,并标记为处理中
- $this->stationTaskCommoditiesService
- ->markProcessed($stationTaskMaterialBox['stationTaskCommodities']);
- if($stationTaskMaterialBox_next)
- $this->stationTaskCommoditiesService
- ->markProcessing($stationTaskMaterialBox_next['stationTaskCommodities']);//因为上边商品任务被标记完成了,所以这里要将队列中找出正在处理的料箱对应的标记为“处理中”
- if(!$stationTaskMaterialBox_next){
- $this->instant($this->stationTaskService,'StationTaskService');
- $this->stationTaskService->markProcessed($stationTaskMaterialBox['stationTask']);
- }
- $this->stationService->broadcastBinMonitor($stationTaskMaterialBox['station_id'],$stationTaskMaterialBox['stationTask']);
- return true;
- }catch (\Exception $e){
- $stationTaskMaterialBox = $stationTaskMaterialBox??$materialBox??null;
- if($stationTaskMaterialBox && get_class($stationTaskMaterialBox)==MaterialBox::class){
- $stationTaskMaterialBox = StationTaskMaterialBox::query()
- ->where('material_box_id',$stationTaskMaterialBox['id'])
- ->where('status','<>','完成')
- ->where('created_at','>',now()->format('Y-m-d'))
- ->first();
- }
- if($stationTaskMaterialBox)
- $this->stationTaskMaterialBoxService
- ->excepted($stationTaskMaterialBox);
- return $e->getMessage();
- }
- }
- public function putBinToStore(?StationTaskMaterialBox $stationTaskMaterialBox): bool
- {
- $this->instant($this->stationService,'StationService');
- $dataToPost=$this->makeJson_move(
- collect([$stationTaskMaterialBox]),
- '缓存架入立架',
- 'BIN-IN1',//TODO:这里应该是动态取得,参考出立架getULineExit()方法,不然不能从站获得对应的出口,而且要改Station的child为children
- '',
- $stationTaskMaterialBox['stationTaskBatch']['id']
- );
- return $this->controlHaiRobot($dataToPost);
- }
- public function taskUpdate(
- // $groupCode,
- $stationTaskMaterialBox_id, //实际对应传入的字段 taskCode, 这里用料箱任务号做taskCode
- $updateEventType, //0:task_begin(取货)1:task_end(放货)
- $status, //0:任务成功1:任务失败
- $binCode
- ):bool{
- $this->instant($this->stationTaskMaterialBoxService,'StationTaskMaterialBoxService');
- try{
- if(($failed
- =$status)==1){
- throw new ErrorException('海柔任务失败');
- }
- if($料箱不匹配=
- !$stationTaskMaterialBox
- =(function()use($stationTaskMaterialBox_id,$binCode){
- $stationTaskMaterialBox=StationTaskMaterialBox::query()->find($id=$stationTaskMaterialBox_id);
- if($stationTaskMaterialBox['materialBox']['code']==$binCode)return $stationTaskMaterialBox;
- return null;
- })()){
- throw new ErrorException('发回的料箱和任务号(ID)不匹配');
- }
- ($标记已放置在库外=
- function()use($updateEventType,$stationTaskMaterialBox){
- if(($isPut
- =$updateEventType)==0){
- $this->stationTaskMaterialBoxService->markHasTaken($stationTaskMaterialBox);
- }
- })();
- ($标记已入立架=
- function()use($updateEventType,$stationTaskMaterialBox){
- if(($isGet
- =$updateEventType)==1){
- $this->stationTaskMaterialBoxService->markPutStored($stationTaskMaterialBox);
- }
- })();
- }catch (\Exception $e){
- $this->excepted($stationTaskMaterialBox_id, $binCode, $e->getMessage());
- return false;
- }
- return true;
- }
- // public function markHasPut($taskCode,$binCode):bool{
- // try{
- // //标记料箱进入位置
- //// $taskMaterialBoxesService->markDone();//
- // }catch (\Exception $e){
- // switch ($e->getCode()){
- // case 'taskBinNotMatch';
- // case 'taskGetFailed';
- // }
- // }
- //
- // }
- public function excepted($taskCode='',$binCode='', $msg=''):bool{
- try{
- throw new ErrorException(
- "taskCode任务号:$taskCode , binCode箱号:$binCode 海柔运行报错: $msg"
- );
- }catch (\Exception $e){
- return true;
- }
- }
- /**
- * @param array $dataToPost
- * @return bool
- */
- public function controlHaiRobot(array $dataToPost): bool
- {
- LogService::log(__METHOD__,'runMany','波次任务分配6.r5f2c1:'.json_encode($dataToPost));
- try{
- LogService::log(__METHOD__,'runMany','波次任务分配6.r5f2c1.51:');
- $a = json_encode($dataToPost);
- $response = Http::post(config('api.haiq.storage.moveBin'), $dataToPost);
- if(isset($response->json()['code'])&&$response->json()['code']==500)
- throw new ErrorException('机器人500错误:'.json_encode($response->json()));
- $b=json_encode($response->json());
- LogService::log(__METHOD__,'runMany','波次任务分配6.r5f2c1.52:');
- LogService::log(__METHOD__,'runMany','波次任务分配6.r5f2c1.53:'.json_encode($response->json()));
- }catch (\Exception $e){
- LogService::log(__METHOD__,'runMany','波次任务分配6.r5f2c1.54:');
- throw new ErrorException('海柔机器人任务执行失败:'.json_encode($dataToPost).$e->getMessage());
- }
- LogService::log(__METHOD__,'runMany','波次任务分配6.r5f2c2:'.json_encode($dataToPost));
- $errMsg = (function () use ($response) {
- if ($response->ok()) return '';
- $errMsg = '错误: ';
- if (!$response) {
- return $errMsg . '没有返回内容,检查连接或目标服务器';
- }
- switch (((string)$response["code"])[0]) {
- case 5:
- $errMsg .= '目标服务器代码错误,请联系对方';
- break;
- case 4:
- $errMsg .= '权限不足以请求资源,请检查对方服务器规范';
- break;
- default:
- $errMsg .= '出现未知请求错误';
- break;
- }
- $responseDetails = ' code:' . $response["code"]
- . ' header:' . $response->body()
- . ' response:' . json_encode($response->headers());
- return $errMsg . $responseDetails;
- })();
- LogService::log(__METHOD__,'runMany','波次任务分配6.r5f2c3:'.json_encode($errMsg));
- LogService::log(__METHOD__, __FUNCTION__,
- $errMsg ?? ''
- . '请求:' . json_encode($dataToPost)
- . '调用堆栈:' . json_encode(array_slice(debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS), 0, 3))
- );
- return $isSuccess = !$errMsg;
- }
- }
|