CacheShelfService.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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\StationTask;
  8. use App\StationTaskChildren;
  9. use App\StationTaskMaterialBox;
  10. use App\Traits\ServiceAppAop;
  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;
  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. * @param $PTLAction
  41. * @return array|bool[]
  42. * @throws \Exception
  43. */
  44. public function lightOffTask($locCode, $PTLAction): array
  45. {
  46. $station = Station::query()->with(['pendingStationTask.stationTaskMaterialBoxes.materialBox','storage'=>function($query){
  47. $query->whereNotNull("material_box_id")->orderByDesc("updated_at");
  48. }])->where('code', $locCode)->first();
  49. //站存在 站为缓存架2 站为蓝灯状态
  50. if (app("StationService")->isHalfBoxLocation($station)){
  51. if (!app("StorageService")->checkStorage($station)){
  52. $this->lightUp($station->code,'0','1',["title"=>"上架失败,联系管理员"]);
  53. return ['success' => false,'errMsg' => '上架任务失败'];
  54. };
  55. }
  56. try {
  57. $bool = $this->putBinToStore($station); // 推送任务
  58. if ($bool) {
  59. LogService::log(__CLASS__, 'lightOffTask', 'code' . ' true' . $locCode . json_encode($station));
  60. return ['success' => true];
  61. } else {
  62. return ['success' => false, 'errMsg' => '机器人推送失败'];
  63. }
  64. } catch (ErrorException $e) {
  65. LogService::log(__FUNCTION__, '缓存架推送任务失败', json_encode($e->getMessage()));
  66. return ['success' => false, 'errMsg' => $e->getMessage()];
  67. }
  68. }
  69. /**
  70. * 推任务至海柔机器人
  71. * @param $station
  72. * @return array
  73. * @throws ErrorException
  74. * @throws \Exception
  75. */
  76. public function putBinToStore($station): array
  77. {
  78. $this->instant($this->foreignHaiRoboticsService, 'ForeignHaiRoboticsService');
  79. $this->instant($this->stationService, 'StationService');
  80. /** @var MaterialBox $materialbox */
  81. $materialBox = $station->storage->materialbox;
  82. $formStation = $this->stationService->getStation_byType('立库'); // 立库
  83. $stationTask = StationTask::query()->create(['station_id' => $formStation['id'], 'status' => '待处理']); // 生成任务
  84. /** @var StationTaskMaterialBox $stationTaskMaterialBox */
  85. $stationTaskMaterialBox = StationTaskMaterialBox::query()->create([
  86. 'station_id' => $formStation['id'],
  87. 'material_box_id' => $materialBox['id'],
  88. 'status' => '待处理',
  89. 'station_task_id' => $stationTask['id'],
  90. 'type' => '放',
  91. ]);
  92. StationTaskChildren::query()->create([
  93. 'station_task_id' => $stationTask['id'],
  94. 'station_taskable_type' => StationTaskMaterialBox::class,
  95. 'station_taskable_id' => $stationTaskMaterialBox['id']
  96. ]);
  97. $bool = $this->foreignHaiRoboticsService->putBinToStore_fromCacheShelf($stationTaskMaterialBox, $station);
  98. return $bool ? ['success' => true] : ['success' => false];
  99. }
  100. /**
  101. * 缓存架和料箱的绑定
  102. * @param $stationCode
  103. * @param $materialBoxCode
  104. * @return array
  105. */
  106. public function bindMaterialBox($stationCode, $materialBoxCode): array
  107. {
  108. return ['success' => false,'message' => "接口废弃"];
  109. }
  110. /**
  111. * 站亮灯
  112. *
  113. * @param string $stationCode
  114. * @param string $color explain: 0-red 1-green 2-blue 3-yellow
  115. * @param string $frequency explain: 0-常亮 1-一次 2-两次 3-三次 4-四次 5-五次 (均为/秒)
  116. * @param array $info
  117. * @return bool
  118. */
  119. public function lightUp(string $stationCode, string $color = '1', string $frequency = '0',array $info = []):bool
  120. {
  121. $default = [
  122. "detail01" => '',
  123. "detail02" => "",
  124. "detail03" => "",
  125. "qrCode" => "",
  126. "qty00" => "",
  127. "qty01" => 0,
  128. "qty02" => 0,
  129. "title" => '',
  130. "uomDesc01" => "",
  131. "uomDesc02" => ""
  132. ];
  133. foreach ($info as $key=>$item)$default[$key] = $item;
  134. $params = [
  135. "areaCode" => "1004",
  136. 'locCode' => $stationCode,
  137. 'PTLAction' => 1,
  138. 'PTLSettings' => ['color' => $color, 'frequency' => $frequency],
  139. "displayInfo" => $default,
  140. ];
  141. $response = Http::post(config('api.haiq.storage.light'), $params);
  142. return $response->status()==200;
  143. }
  144. /**
  145. * 控制格口灭灯
  146. * @param $locCode
  147. * @return mixed
  148. */
  149. public function _stationCacheLightOff($locCode)
  150. {
  151. if (!$locCode) return null;
  152. $params = [
  153. "areaCode" => "1004",
  154. 'locCode' => $locCode,
  155. 'PTLAction' => 0,
  156. ];
  157. $response = Http::post(config('api.haiq.storage.light'), $params);
  158. return json_decode($response->body());
  159. }
  160. /**
  161. * 广播 type success成功 error 异常
  162. * @param $locCode
  163. * @param $PTLAction
  164. * @param string $type
  165. */
  166. public function _stationCacheBroadCast($locCode, $PTLAction, string $type = 'success')
  167. {
  168. if (!$locCode) return;
  169. if ($PTLAction == 0) {
  170. $station = Station::query()->with('parent')->where('code', $locCode)->first();
  171. $json = json_encode([
  172. 'station_id' => $station['parent']['id'],
  173. 'code' => $station['parent']['code'],
  174. 'gird_id' => $station['id'],
  175. 'grid_code' => $station['code'],
  176. 'type' => $type,
  177. 'status' => 0
  178. ]);
  179. broadcast(new BroadcastToStation($station['parent_id'], $json));
  180. }
  181. }
  182. /**
  183. * 取消任务
  184. * @param $stationCode
  185. * @return array
  186. * @throws \Exception
  187. */
  188. public function clearTask($stationCode): array
  189. {
  190. $station = Station::query()->with('storage')->where('code', $stationCode)->first();
  191. if (!$station) return ['success' => false, 'message' => '传入参数异常,找不到对应的缓存架记录'];
  192. $stationTaskMaterialBox = StationTaskMaterialBox::query()->where('material_box_id',$station['material_box_id'])->first();
  193. if($stationTaskMaterialBox ){
  194. if($stationTaskMaterialBox->status == '处理中')
  195. return ['success' => false, 'message' => '当前缓存架任务正在处理中'];
  196. else{
  197. $stationTaskMaterialBox->delete();
  198. }
  199. }
  200. $station->update(['status' => 0,'material_box_id' => null]);
  201. return ['success' => true];
  202. }
  203. }