| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229 |
- <?php
- namespace App\Services;
- use App\Events\BroadcastToStation;
- use App\Exceptions\ErrorException;
- use App\MaterialBox;
- use App\Station;
- use App\StationTask;
- use App\StationTaskChildren;
- use App\StationTaskMaterialBox;
- use App\Traits\ServiceAppAop;
- use Illuminate\Support\Facades\Http;
- use Illuminate\Database\Eloquent\Builder;
- use Illuminate\Database\Eloquent\Collection;
- class CacheShelfService
- {
- use ServiceAppAop;
- 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
- * @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 (!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
- {
- return ['success' => false,'message' => "接口废弃"];
- }
- /**
- * 站亮灯
- *
- * @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));
- }
- }
- /**
- * 取消任务
- * @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];
- }
- }
|