CacheShelfService.php 7.8 KB

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