CacheShelfService.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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\StationTaskChild;
  9. use App\StationTaskChildren;
  10. use App\StationTaskMaterialBox;
  11. use App\StationType;
  12. use App\Storage;
  13. use App\Traits\ServiceAppAop;
  14. use Illuminate\Http\Request;
  15. use Illuminate\Support\Facades\Http;
  16. use Illuminate\Database\Eloquent\Builder;
  17. use Illuminate\Database\Eloquent\Collection;
  18. class CacheShelfService
  19. {
  20. use ServiceAppAop;
  21. protected $modelClass = Station::class;
  22. /** @var StationService $stationService */
  23. private $stationService;
  24. /** @var StationTaskMaterialBoxService $stationTaskMaterialBoxService */
  25. private $stationTaskMaterialBoxService;
  26. /** @var ForeignHaiRoboticsService $foreignHaiRoboticsService */
  27. private $foreignHaiRoboticsService;
  28. /** @var StationTaskService $stationTaskService */
  29. private $stationTaskService;
  30. /** @var StationTaskChildService $stationTaskChildService */
  31. private $stationTaskChildService;
  32. /**
  33. * 获取缓存架上子货架当前任务
  34. * @param $id
  35. * @return Builder[]|Collection
  36. */
  37. public function getChildStation($id)
  38. {
  39. return Station::query()->where('parent_id', $id)->with('storage.materialBox')->get();
  40. }
  41. /**
  42. * 拍灯触发任务
  43. * @param $locCode
  44. * @param $PTLAction
  45. * @return array|bool[]
  46. * @throws \Exception
  47. */
  48. public function lightOffTask($locCode, $PTLAction): array
  49. {
  50. $station = Station::query()->with('pendingStationTask.stationTaskMaterialBoxes.materialBox')->where('code', $locCode)->first();
  51. if (!app("StorageService")->checkStorage($station)){
  52. $this->stationLightUp($station->code,null,'0','1','上架任务失败');
  53. return ['success' => false,'errMsg' => '上架任务失败'];
  54. };
  55. try {
  56. $bool = $this->putBinToStore($station); // 推送任务
  57. if ($bool) {
  58. LogService::log(__CLASS__, 'lightOffTask', 'code' . ' true' . $locCode . json_encode($station));
  59. return ['success' => true];
  60. } else {
  61. return ['success' => false, 'errMsg' => '机器人推送失败'];
  62. }
  63. } catch (ErrorException $e) {
  64. LogService::log(__FUNCTION__, '缓存架推送任务失败', json_encode($e->getMessage()));
  65. return ['success' => false, 'errMsg' => $e->getMessage()];
  66. }
  67. }
  68. /**
  69. * 推任务至海柔机器人
  70. * @param $station
  71. * @return array
  72. * @throws ErrorException
  73. * @throws \Exception
  74. */
  75. public function putBinToStore($station): array
  76. {
  77. $this->instant($this->foreignHaiRoboticsService, 'ForeignHaiRoboticsService');
  78. $this->instant($this->stationService, 'StationService');
  79. /** @var MaterialBox $materialbox */
  80. $materialBox = $station->storage->materialbox;
  81. $formStation = $this->stationService->getStation_byType('立库'); // 立库
  82. $stationTask = StationTask::query()->create(['station_id' => $formStation['id'], 'status' => '待处理']); // 生成任务
  83. /** @var StationTaskMaterialBox $stationTaskMaterialBox */
  84. $stationTaskMaterialBox = StationTaskMaterialBox::query()->create([
  85. 'station_id' => $formStation['id'],
  86. 'material_box_id' => $materialBox['id'],
  87. 'status' => '待处理',
  88. 'station_task_id' => $stationTask['id'],
  89. 'type' => '放',
  90. ]);
  91. StationTaskChildren::query()->create([
  92. 'station_task_id' => $stationTask['id'],
  93. 'station_taskable_type' => StationTaskMaterialBox::class,
  94. 'station_taskable_id' => $stationTaskMaterialBox['id']
  95. ]);
  96. $bool = $this->foreignHaiRoboticsService->putBinToStore_fromCacheShelf($stationTaskMaterialBox, $station['code']);
  97. return $bool ? ['success' => true] : ['success' => false];
  98. }
  99. /**
  100. * 缓存架和料箱的绑定
  101. * @param $stationCode
  102. * @param $materialBoxCode
  103. * @return array
  104. */
  105. public function bindMaterialBox($stationCode, $materialBoxCode): array
  106. {
  107. $station = Station::query()->with('storage')->where('code', $stationCode)->first();
  108. if (!$station) {
  109. $arr = [];
  110. preg_match('/^HAI([\w]+)/', $stationCode, $arr);
  111. $parentCode = $arr[1] ?? '';
  112. $stationType = StationType::query()->where('name', '缓存架')->first();
  113. $parentStation = Station::query()->firstOrCreate(['code' => $parentCode], ['station_type_id' => $stationType['id']]);
  114. $station = Station::query()->firstOrCreate(['code' => $stationCode, 'parent_id' => $parentStation['id']], ['name' => $stationCode, 'station_type_id' => $stationType['id']]);
  115. }
  116. $materialBox = MaterialBox::query()->firstOrCreate(['code' => $materialBoxCode]);
  117. $storage = $station->storage ?? Storage::query()->firstOrCreate(['station_id' => $station['id']]);
  118. $result = $this->_stationCacheLightOn($station['code'],$materialBoxCode);
  119. if($result['code'] == 200){
  120. $storage->update(['material_box_id' => $materialBox['id'],'status' => 1]);
  121. return ['success' => true];
  122. }
  123. return ['success' => false,'message' => $result['errMsg']];
  124. }
  125. /**
  126. * 控制格口亮灯
  127. * @param $locCode
  128. * @param null $materialCode
  129. * @param string $title
  130. * @param string $color
  131. * @return mixed
  132. */
  133. public function _stationCacheLightOn($locCode, $materialCode = null, $title = 'title', string $color = '1')
  134. {
  135. $params = [
  136. "areaCode" => "1004",
  137. 'locCode' => $locCode,
  138. 'PTLAction' => 1,
  139. 'PTLSettings' => ['color' => $color, 'frequency' => 1],
  140. "displayInfo" => [
  141. "detail01" => $materialCode,
  142. "detail02" => "detail02",
  143. "detail03" => "detail03",
  144. "qrCode" => "qrCode",
  145. "qty00" => "11",
  146. "qty01" => 1,
  147. "qty02" => 2,
  148. "title" => $title,
  149. "uomDesc01" => "uo",
  150. "uomDesc02" => "uo"
  151. ],
  152. ];
  153. $response = Http::post(config('api.haiq.storage.light'), $params);
  154. return json_decode($response->body());
  155. }
  156. /**
  157. * 站亮灯
  158. *
  159. * @param string $stationCode
  160. * @param string|null $materialCode
  161. * @param string $color explain: 0-red 1-green 2-blue 3-yellow
  162. * @param string $frequency explain: 0-常亮 1-一次 2-两次 3-三次 4-四次 5-五次 (均为/秒)
  163. * @param string $title
  164. * @return mixed
  165. */
  166. public function stationLightUp(string $stationCode,?string $materialCode = null, string $color = '1', string $frequency = '0', $title = '')
  167. {
  168. $params = [
  169. "areaCode" => "1004",
  170. 'locCode' => $stationCode,
  171. 'PTLAction' => 1,
  172. 'PTLSettings' => ['color' => $color, 'frequency' => $frequency],
  173. "displayInfo" => [
  174. "detail01" => $materialCode,
  175. "detail02" => "detail02",
  176. "detail03" => "detail03",
  177. "qrCode" => "qrCode",
  178. "qty00" => "11",
  179. "qty01" => 1,
  180. "qty02" => 2,
  181. "title" => $title,
  182. "uomDesc01" => "uo",
  183. "uomDesc02" => "uo"
  184. ],
  185. ];
  186. $response = Http::post(config('api.haiq.storage.light'), $params);
  187. return json_decode($response->body());
  188. }
  189. /**
  190. * 控制格口灭灯
  191. * @param $locCode
  192. * @return mixed
  193. */
  194. public function _stationCacheLightOff($locCode)
  195. {
  196. if (!$locCode) return null;
  197. $params = [
  198. "areaCode" => "1004",
  199. 'locCode' => $locCode,
  200. 'PTLAction' => 0,
  201. ];
  202. $response = Http::post(config('api.haiq.storage.light'), $params);
  203. return json_decode($response->body());
  204. }
  205. /**
  206. * 广播 type success成功 error 异常
  207. * @param $locCode
  208. * @param $PTLAction
  209. * @param string $type
  210. */
  211. public function _stationCacheBroadCast($locCode, $PTLAction, string $type = 'success')
  212. {
  213. if (!$locCode) return;
  214. if ($PTLAction == 0) {
  215. $station = Station::query()->with('parent')->where('code', $locCode)->first();
  216. $json = json_encode([
  217. 'station_id' => $station['parent']['id'],
  218. 'code' => $station['parent']['code'],
  219. 'gird_id' => $station['id'],
  220. 'grid_code' => $station['code'],
  221. 'type' => $type,
  222. 'status' => 0
  223. ]);
  224. broadcast(new BroadcastToStation($station['parent_id'], $json));
  225. }
  226. }
  227. /**
  228. * 根据立库任务完成对storage进行修改
  229. * @param StationTaskMaterialBox $stationTaskMaterial
  230. */
  231. public function putStationTaskMaterialBoxProcess(StationTaskMaterialBox $stationTaskMaterial)
  232. {
  233. $this->instant($this->stationTaskMaterialBoxService, 'StationTaskMaterialBoxService');
  234. $storage = Storage::query()->where('material_box_id', $stationTaskMaterial['material_box_id'])->where('status',1)->first();
  235. if($storage)$storage->update(['status' => 0, 'material_box_id' => null]);
  236. $this->_stationCacheLightOff($stationTaskMaterial->station->code ?? null); //海柔格口灭灯
  237. $this->_stationCacheBroadCast($stationTaskMaterial->station->code ?? null, 0); //通知缓存架任务完成
  238. }
  239. /**
  240. * 取消任务
  241. * @param $stationCode
  242. * @return array
  243. * @throws \Exception
  244. */
  245. public function clearTask($stationCode): array
  246. {
  247. $station = Station::query()->with('storage')->where('code', $stationCode)->first();
  248. if (!$station) return ['success' => false, 'message' => '传入参数异常,找不到对应的缓存架记录'];
  249. $stationTaskMaterialBox = StationTaskMaterialBox::query()->where('material_box_id',$station['storage']['material_box_id'])->first();
  250. if($stationTaskMaterialBox ){
  251. if($stationTaskMaterialBox->status == '处理中')
  252. return ['success' => false, 'message' => '当前缓存架任务正在处理中'];
  253. else{
  254. $stationTaskMaterialBox->delete();
  255. }
  256. }
  257. $station->storage->update(['status' => 0,'material_box_id' => null]);
  258. return ['success' => true];
  259. }
  260. }