| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221 |
- <?php
- namespace App\Services;
- use App\Components\ErrorPush;
- use App\Events\BroadcastToStation;
- use App\Exceptions\ErrorException;
- use App\Jobs\CacheShelfTaskJob;
- use App\Station;
- use App\StationTaskMaterialBox;
- use App\Traits\ServiceAppAop;
- use Illuminate\Support\Facades\Cache;
- use Illuminate\Support\Facades\Http;
- use Illuminate\Database\Eloquent\Builder;
- use Illuminate\Database\Eloquent\Collection;
- class CacheShelfService
- {
- use ServiceAppAop,ErrorPush;
- protected $modelClass = Station::class;
- /** @var StationService $stationService */
- private $stationService;
- /** @var StationTaskMaterialBoxService $stationTaskMaterialBoxService */
- private $stationTaskMaterialBoxService;
- /** @var ForeignHaiRoboticsService $foreignHaiRoboticsService */
- private $foreignHaiRoboticsService;
- /** @var StationTaskService $stationTaskService */
- private $stationTaskService;
- /** @var StationTaskChildService $stationTaskChildService */
- private $stationTaskChildService;
- /**
- * 获取缓存架上子货架当前任务
- * @param $id
- * @return Builder[]|Collection
- */
- public function getChildStation($id)
- {
- return Station::query()->where('parent_id', $id)->with('storage.materialBox')->get();
- }
- /**
- * 拍灯触发任务
- * @param $locCode
- *
- * @return array|bool[]
- * @throws \Exception
- */
- public function lightOffTask($locCode): array
- {
- $station = Station::query()->with('materialBox')->where('code', $locCode)->first();
- if (Cache::has("CACHE_SHELF_OCCUPANCY_{$station->id}")){
- //缓存存在 不允许灭灯 灭了再点开
- app("CacheShelfService")->lightUp($station->code,'3','0',["title"=>"机器人取箱中,禁止操作"]);
- return ['success' => true];
- }
- //站存在 站为缓存架
- if (app("StationService")->isHalfBoxLocation($station)){
- $result = app("StorageService")->checkStorage($station);
- if ($result===false){//任务存在且失败 红灯
- $this->lightUp($station->code,'0','1',["title"=>"上架失败,联系管理员"]);
- return ['success' => false,'errMsg' => '上架任务失败'];
- };
- if ($result===true){//任务存在且成功 绿灯
- $this->lightUp($station->code,'1','0',['title'=>"操作完成,等待入库"]);
- return ['success' => true];
- }
- }
- 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 bool
- * @throws ErrorException
- * @throws \Exception
- */
- public function putBinToStore($station): bool
- {
- $this->instant($this->foreignHaiRoboticsService, 'ForeignHaiRoboticsService');
- $this->instant($this->stationService, 'StationService');
- if (!$station->materialBox)return false;
- /** @var StationTaskMaterialBox $stationTaskMaterialBox */
- $stationTaskMaterialBox = app("StorageService")->createWarehousingTask($this->stationService->getStation_byType('立库')["id"],$station->materialBox->id);
- $this->lightUp($station->code,'3','0',['title'=>"等待机器人拿走,请勿操作"]);
- Cache::forever("CACHE_SHELF_OCCUPANCY_{$station->id}",true);
- return $this->foreignHaiRoboticsService->putBinToStore_fromCacheShelf($stationTaskMaterialBox, $station);
- }
- /**
- * 站亮灯
- *
- * @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" => "",
- "qty02" => "",
- "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));
- }
- }
- /**
- * 取消任务
- * @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['material_box_id'])->first();
- if($stationTaskMaterialBox ){
- if($stationTaskMaterialBox->status == '处理中')
- return ['success' => false, 'message' => '当前缓存架任务正在处理中'];
- else{
- $stationTaskMaterialBox->delete();
- }
- }
- $station->update(['status' => 0,'material_box_id' => null]);
- return ['success' => true];
- }
- /**
- * 料箱被取走
- *
- * @param Station|\stdClass $station
- */
- public function boxHasBeenTaken($station)
- {
- app("StationService")->locationFreed($station->code); //释放库位,解除绑定料箱
- $available=Cache::get("CACHE_SHELF_AVAILABLE",0)+1;//获取可用缓存架数量 plus当前
- Cache::forever("CACHE_SHELF_AVAILABLE",$available);
- $this->_stationCacheLightOff($station->code);
- CacheShelfTaskJob::dispatch("CACHE_SHELF_AVAILABLE",$available)->delay(now()->addSeconds(config("haiRou.cacheShelf.outBinAwait",3)));
- }
- }
|