CacheShelfService.php 6.9 KB

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