CacheShelfService.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. <?php
  2. namespace App\Services;
  3. use App\Events\BroadcastToStation;
  4. use App\Exceptions\ErrorException;
  5. use App\MaterialBox;
  6. use App\Station;
  7. use App\StationTaskMaterialBox;
  8. use App\StationType;
  9. use App\Traits\ServiceAppAop;
  10. use Illuminate\Support\Facades\Http;
  11. use Illuminate\Database\Eloquent\Builder;
  12. use Illuminate\Database\Eloquent\Collection;
  13. class CacheShelfService
  14. {
  15. use ServiceAppAop;
  16. protected $modelClass = Station::class;
  17. /** @var StationTaskMaterialBoxService $stationTaskMaterialBoxService */
  18. private $stationTaskMaterialBoxService;
  19. /** @var StationCacheShelfGridService $stationCacheShelfGridService */
  20. private $stationCacheShelfGridService;
  21. /** @var ForeignHaiRoboticsService $foreignHaiRoboticsService */
  22. private $foreignHaiRoboticsService;
  23. /** @var StationTaskService $stationTaskService */
  24. private $stationTaskService;
  25. /**
  26. * 获取缓存架上子货架当前任务
  27. * @param $id
  28. * @return Builder[]|Collection
  29. */
  30. public function getChildStation($id)
  31. {
  32. return Station::query()->where('parent_id',$id)->with('parent','currentStationTask.stationTaskMaterialBoxes.materialBox')->get();
  33. }
  34. /**
  35. * 拍灯触任务
  36. * @param $locCode
  37. * @param $PTLAction
  38. * @return array|bool[]
  39. */
  40. public function lightOffTask($locCode, $PTLAction): array
  41. {
  42. $this->instant($this->stationCacheShelfGridService, 'StationCacheShelfGridService');
  43. $station = Station::query()->where('code', $locCode)->with('parent','currentStationTask.stationTaskMaterialBoxes.materialBox')->first();
  44. try {
  45. $bool = $this->putBinToStore($station); // 推送任务
  46. if($bool){
  47. $this->_stationCacheBroadCast($locCode,$PTLAction); // 灭灯广播
  48. $this->_stationCacheLightOff($locCode);
  49. return ['success' => true];
  50. }else{
  51. return ['success' => false,'errMsg' => '机器人推送失败'];
  52. }
  53. } catch (ErrorException $e) {
  54. LogService::log(__FUNCTION__,'缓存架推送任务失败',json_encode($e->getMessage()));
  55. return ['success' => false,'errMsg' => $e->getMessage()];
  56. }
  57. }
  58. /**
  59. * 推任务至海柔机器人
  60. * @param $station
  61. * @return bool
  62. * @throws ErrorException
  63. */
  64. public function putBinToStore($station): bool
  65. {
  66. $this->instant($this->stationTaskMaterialBoxService, 'StationTaskMaterialBoxService');
  67. $this->instant($this->stationCacheShelfGridService, 'StationCacheShelfGridService');
  68. $this->instant($this->foreignHaiRoboticsService, 'ForeignHaiRoboticsService');
  69. /** @var StationTaskMaterialBox $stationTaskMaterialBox */
  70. $stationTaskMaterialBox = $station['currentStationTask']['stationTaskMaterialBoxes'];
  71. return $this->foreignHaiRoboticsService->putBinToStore_fromCacheShelf($stationTaskMaterialBox);
  72. }
  73. /**
  74. * 创建站任务和料箱任务
  75. * @param $stationCode
  76. * @param $materialBoxCode
  77. * @return array
  78. */
  79. public function createStationTask($stationCode,$materialBoxCode): array
  80. {
  81. $this->instant($this->stationTaskService, 'StationTaskService');
  82. $this->instant($this->stationTaskMaterialBoxService,'stationTaskMaterialBoxService');
  83. // $station = Station::query()->with('currentPendingTask.stationTaskMaterialBoxes.materialBox')->firstOrCreate(['code' => $stationCode]);
  84. $station = Station::query()->where('code' , $stationCode)->first();
  85. if(!$station){
  86. $arr = [];
  87. preg_match('/^HAI([\w]+)/',$stationCode,$arr);
  88. $parentCode = $arr[1] ?? '';
  89. $stationType = StationType::query()->where('name','缓存架')->first();
  90. $parentStation = Station::query()->firstOrCreate(['code'=>$parentCode],['station_type_id'=>$stationType['id']]);
  91. $station = Station::query()->firstOrCreate(['code' => $stationCode,'parent_id'=>$parentStation['id']],['name'=>$stationCode,'station_type_id' => $stationType['id']]);
  92. }
  93. $materialBox = MaterialBox::query()->firstOrCreate(['code' => $materialBoxCode]);
  94. if($station['currentPendingTask']['stationTaskMaterialBoxes']['materialBox'] ?? false){
  95. return ['success' => false,'message' => '当前已有未完成的站任务'];
  96. }
  97. $stationTask = $this->stationTaskService->create(1); // 生成站任务
  98. $this->stationTaskMaterialBoxService->createByStationMaterialBox($station,$materialBox); // 创建料箱任务
  99. $this->stationTaskService->registerStations($stationTask,[$station['id']]); // 注册站任务站
  100. return['success'=>true];
  101. }
  102. /**
  103. * 控制格口亮灯
  104. * @param $locCode
  105. * @param string $title
  106. * @param null $materialCode
  107. * @return mixed
  108. */
  109. public function _stationCacheLightOn($locCode,$title = 'title',$materialCode = null)
  110. {
  111. $params = [
  112. "areaCode" => "1004",
  113. 'locCode' => $locCode,
  114. 'PTLAction' => 1,
  115. 'PTLSettings' => [
  116. 'color'=> 1,
  117. 'frequency' =>1
  118. ],
  119. "displayInfo" => [
  120. "detail01" => $materialCode,
  121. "detail02" => "detail02",
  122. "detail03" => "detail03",
  123. "qrCode" => "qrCode",
  124. "qty00" => "11",
  125. "qty01" => 1,
  126. "qty02" => 2,
  127. "title" => $title,
  128. "uomDesc01" => "uo",
  129. "uomDesc02" => "uo"
  130. ],
  131. ];
  132. $response = Http::post(config('api.haiq.storage.light'), $params);
  133. return json_decode($response->body());
  134. }
  135. /**
  136. * 控制格口灭灯
  137. * @param $locCode
  138. * @return mixed
  139. */
  140. public function _stationCacheLightOff($locCode){
  141. $params = [
  142. "areaCode" => "1004",
  143. 'locCode' => $locCode,
  144. 'PTLAction' => 0,
  145. ];
  146. $response = Http::post(config('api.haiq.storage.light'), $params);
  147. return json_decode($response->body());
  148. }
  149. /**
  150. * 广播 通知货物被取走
  151. * @param $locCode
  152. * @param $PTLAction
  153. */
  154. public function _stationCacheBroadCast($locCode,$PTLAction)
  155. {
  156. if($PTLAction == 0){
  157. $station = Station::query()->with('parent')->where('code',$locCode)->first();
  158. $json = json_encode( [
  159. 'station_id' => $station['parent']['id'],
  160. 'code' => $station['parent']['code'],
  161. 'gird_id' => $station['id'],
  162. 'grid_code' => $station['code'],
  163. ]);
  164. broadcast(new BroadcastToStation($station['id'],$json));
  165. }
  166. }
  167. }