CacheShelfService.php 7.6 KB

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