CacheShelfService.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. <?php
  2. namespace App\Services;
  3. use App\Events\BroadcastToStation;
  4. use App\Exceptions\ErrorException;
  5. use App\Station;
  6. use App\StationTaskMaterialBox;
  7. use App\Traits\ServiceAppAop;
  8. use Illuminate\Support\Facades\Cache;
  9. use Illuminate\Support\Facades\Http;
  10. use Illuminate\Database\Eloquent\Builder;
  11. use Illuminate\Database\Eloquent\Collection;
  12. class CacheShelfService
  13. {
  14. use ServiceAppAop;
  15. protected $modelClass = Station::class;
  16. /** @var StationService $stationService */
  17. private $stationService;
  18. /** @var StationTaskMaterialBoxService $stationTaskMaterialBoxService */
  19. private $stationTaskMaterialBoxService;
  20. /** @var ForeignHaiRoboticsService $foreignHaiRoboticsService */
  21. private $foreignHaiRoboticsService;
  22. /** @var StationTaskService $stationTaskService */
  23. private $stationTaskService;
  24. /** @var StationTaskChildService $stationTaskChildService */
  25. private $stationTaskChildService;
  26. /**
  27. * 获取缓存架上子货架当前任务
  28. * @param $id
  29. * @return Builder[]|Collection
  30. */
  31. public function getChildStation($id)
  32. {
  33. return Station::query()->where('parent_id', $id)->with('storage.materialBox')->get();
  34. }
  35. /**
  36. * 拍灯触发任务
  37. * @param $locCode
  38. *
  39. * @return array|bool[]
  40. * @throws \Exception
  41. */
  42. public function lightOffTask($locCode): array
  43. {
  44. $station = Station::query()->with('materialBox')->where('code', $locCode)->first();
  45. if (Cache::has("CACHE_SHELF_OCCUPANCY_{$station->id}")){
  46. //缓存存在 不允许灭灯 灭了再点开
  47. app("CacheShelfService")->lightUp($station->code,'3','0',["title"=>"机器人取箱中,禁止操作"]);
  48. return ['success' => true];
  49. }
  50. //站存在 站为缓存架
  51. if (app("StationService")->isHalfBoxLocation($station)){
  52. $result = app("StorageService")->checkStorage($station);
  53. if ($result===false){//任务存在且失败 红灯
  54. $this->lightUp($station->code,'0','1',["title"=>"上架失败,联系管理员"]);
  55. return ['success' => false,'errMsg' => '上架任务失败'];
  56. };
  57. if ($result===true){//任务存在且成功 绿灯
  58. $this->lightUp($station->code);
  59. return ['success' => true];
  60. }
  61. app("StationService")->locationFreed($station->code);
  62. }
  63. try {
  64. $bool = $this->putBinToStore($station); // 推送任务
  65. if ($bool) {
  66. LogService::log(__CLASS__, 'lightOffTask', 'code' . ' true' . $locCode . json_encode($station));
  67. return ['success' => true];
  68. }else return ['success' => false, 'errMsg' => '机器人推送失败'];
  69. } catch (ErrorException $e) {
  70. LogService::log(__FUNCTION__, '缓存架推送任务失败', json_encode($e->getMessage()));
  71. return ['success' => false, 'errMsg' => $e->getMessage()];
  72. }
  73. }
  74. /**
  75. * 推任务至海柔机器人
  76. * @param $station
  77. * @return bool
  78. * @throws ErrorException
  79. * @throws \Exception
  80. */
  81. public function putBinToStore($station): bool
  82. {
  83. $this->instant($this->foreignHaiRoboticsService, 'ForeignHaiRoboticsService');
  84. $this->instant($this->stationService, 'StationService');
  85. if (!$station->materialBox)return false;
  86. /** @var StationTaskMaterialBox $stationTaskMaterialBox */
  87. $stationTaskMaterialBox = app("StorageService")->createWarehousingTask($this->stationService->getStation_byType('立库')["id"],$station->materialBox->id);
  88. return $this->foreignHaiRoboticsService->putBinToStore_fromCacheShelf($stationTaskMaterialBox, $station);
  89. }
  90. /**
  91. * 站亮灯
  92. *
  93. * @param string $stationCode
  94. * @param string $color explain: 0-red 1-green 2-blue 3-yellow
  95. * @param string $frequency explain: 0-常亮 1-一次 2-两次 3-三次 4-四次 5-五次 (均为/秒)
  96. * @param array $info
  97. * @return bool
  98. */
  99. public function lightUp(string $stationCode, string $color = '1', string $frequency = '0',array $info = []):bool
  100. {
  101. $default = [
  102. "detail01" => '',
  103. "detail02" => "",
  104. "detail03" => "",
  105. "qrCode" => "",
  106. "qty00" => "",
  107. "qty01" => "",
  108. "qty02" => "",
  109. "title" => '',
  110. "uomDesc01" => "",
  111. "uomDesc02" => ""
  112. ];
  113. foreach ($info as $key=>$item)$default[$key] = $item;
  114. $params = [
  115. "areaCode" => "1004",
  116. 'locCode' => $stationCode,
  117. 'PTLAction' => 1,
  118. 'PTLSettings' => ['color' => $color, 'frequency' => $frequency],
  119. "displayInfo" => $default,
  120. ];
  121. $response = Http::post(config('api.haiq.storage.light'), $params);
  122. return $response->status()==200;
  123. }
  124. /**
  125. * 控制格口灭灯
  126. * @param $locCode
  127. * @return mixed
  128. */
  129. public function _stationCacheLightOff($locCode)
  130. {
  131. if (!$locCode) return null;
  132. $params = [
  133. "areaCode" => "1004",
  134. 'locCode' => $locCode,
  135. 'PTLAction' => 0,
  136. ];
  137. $response = Http::post(config('api.haiq.storage.light'), $params);
  138. return json_decode($response->body());
  139. }
  140. /**
  141. * 广播 type success成功 error 异常
  142. * @param $locCode
  143. * @param $PTLAction
  144. * @param string $type
  145. */
  146. public function _stationCacheBroadCast($locCode, $PTLAction, string $type = 'success')
  147. {
  148. if (!$locCode) return;
  149. if ($PTLAction == 0) {
  150. $station = Station::query()->with('parent')->where('code', $locCode)->first();
  151. $json = json_encode([
  152. 'station_id' => $station['parent']['id'],
  153. 'code' => $station['parent']['code'],
  154. 'gird_id' => $station['id'],
  155. 'grid_code' => $station['code'],
  156. 'type' => $type,
  157. 'status' => 0
  158. ]);
  159. broadcast(new BroadcastToStation($station['parent_id'], $json));
  160. }
  161. }
  162. /**
  163. * 取消任务
  164. * @param $stationCode
  165. * @return array
  166. * @throws \Exception
  167. */
  168. public function clearTask($stationCode): array
  169. {
  170. $station = Station::query()->with('storage')->where('code', $stationCode)->first();
  171. if (!$station) return ['success' => false, 'message' => '传入参数异常,找不到对应的缓存架记录'];
  172. $stationTaskMaterialBox = StationTaskMaterialBox::query()->where('material_box_id',$station['material_box_id'])->first();
  173. if($stationTaskMaterialBox ){
  174. if($stationTaskMaterialBox->status == '处理中')
  175. return ['success' => false, 'message' => '当前缓存架任务正在处理中'];
  176. else{
  177. $stationTaskMaterialBox->delete();
  178. }
  179. }
  180. $station->update(['status' => 0,'material_box_id' => null]);
  181. return ['success' => true];
  182. }
  183. }