CacheShelfService.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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->createByStationAndMaterialBox($station,$materialBox); // 创建料箱任务
  97. $this->stationTaskService->registerStations($stationTask,[$station['id']]); // 注册站任务站
  98. $stationTaskMaterialBox->update(['station_task_id'=>$stationTask['id']]);
  99. $params = [[
  100. 'station_task_id'=>$stationTask->first()['id'],
  101. 'station_taskable_type'=>StationTaskMaterialBox::class,
  102. 'station_taskable_id'=>$stationTaskMaterialBox['id']
  103. ]];
  104. $this->stationTaskChildService->insert($params); // 任务任务注册
  105. $body = $this->_stationCacheLightOn($stationCode,$materialBoxCode);
  106. if($body->code == 200)['success'=>true];
  107. return ['success' => false,'message' => '机器人亮灯异常'];
  108. }
  109. /**
  110. * 控制格口亮灯
  111. * @param $locCode
  112. * @param string $title
  113. * @param null $materialCode
  114. * @return mixed
  115. */
  116. public function _stationCacheLightOn($locCode,$materialCode = null,$title = 'title')
  117. {
  118. $params = [
  119. "areaCode" => "1004",
  120. 'locCode' => $locCode,
  121. 'PTLAction' => 1,
  122. 'PTLSettings' => [
  123. 'color'=> 1,
  124. 'frequency' =>1
  125. ],
  126. "displayInfo" => [
  127. "detail01" => $materialCode,
  128. "detail02" => "detail02",
  129. "detail03" => "detail03",
  130. "qrCode" => "qrCode",
  131. "qty00" => "11",
  132. "qty01" => 1,
  133. "qty02" => 2,
  134. "title" => $title,
  135. "uomDesc01" => "uo",
  136. "uomDesc02" => "uo"
  137. ],
  138. ];
  139. $response = Http::post(config('api.haiq.storage.light'), $params);
  140. return json_decode($response->body());
  141. }
  142. /**
  143. * 控制格口灭灯
  144. * @param $locCode
  145. * @return mixed
  146. */
  147. public function _stationCacheLightOff($locCode){
  148. $params = [
  149. "areaCode" => "1004",
  150. 'locCode' => $locCode,
  151. 'PTLAction' => 0,
  152. ];
  153. $response = Http::post(config('api.haiq.storage.light'), $params);
  154. return json_decode($response->body());
  155. }
  156. /**
  157. * 广播 通知货物被取走
  158. * @param $locCode
  159. * @param $PTLAction
  160. */
  161. public function _stationCacheBroadCast($locCode,$PTLAction)
  162. {
  163. if($PTLAction == 0){
  164. $station = Station::query()->with('parent')->where('code',$locCode)->first();
  165. $json = json_encode( [
  166. 'station_id' => $station['parent']['id'],
  167. 'code' => $station['parent']['code'],
  168. 'gird_id' => $station['id'],
  169. 'grid_code' => $station['code'],
  170. ]);
  171. broadcast(new BroadcastToStation($station['parent_id'],$json));
  172. }
  173. }
  174. }