BikeController.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. <?php
  2. namespace App\Http\Controllers\App\Open;
  3. use App\Handlers\Aes128Handler;
  4. use App\Handlers\BikeControl;
  5. use App\Handlers\BikeStatusInfoSyncHandler;
  6. use App\Http\Resources\App\BikeResource;
  7. use App\Models\Bike;
  8. use App\Models\BoxBinding;
  9. use App\Models\LocationsLog;
  10. use Illuminate\Http\Request;
  11. use App\Http\Controllers\Controller;
  12. use Illuminate\Support\Facades\DB;
  13. use Illuminate\Support\Facades\Log;
  14. use Illuminate\Support\Facades\Validator;
  15. class BikeController extends Controller
  16. {
  17. public function test()
  18. {
  19. return $this->ok('aa');
  20. }
  21. /**
  22. * 绑定车辆
  23. * */
  24. public function addBike(Request $request)
  25. {
  26. // 验证参数
  27. /* $validator = Validator::make($request->all(), [
  28. 'bike_no' => 'bail|required|unique:bikes',
  29. 'box_no' => 'bail|required|unique:bikes',
  30. ], [
  31. 'bike_no.required' => '车辆编号不能为空',
  32. 'bike_no.unique' => '车辆编号不可重复',
  33. 'box_no.required' => '控制器号不能为空',
  34. 'box_no.unique' => '控制器号不可重复',
  35. ]);
  36. if ($validator->fails()) {
  37. $res = $validator->getMessageBag();
  38. $str = '';
  39. foreach ($res->all() as $v) {
  40. $str .= $v . ',';
  41. }
  42. return $this->error(rtrim($str, ','));
  43. }*/
  44. $bike_no = $request->get('bike_no') ?? '';
  45. $box_no = $request->get('box_no') ?? '';
  46. if (empty($bike_no) || empty($box_no)) return $this->error('参数错误');
  47. // 判断中控是否存在
  48. $box = BoxBinding::query()->where('box_no', $box_no)->first();
  49. if (empty($box)) return $this->error('找不到此设备信息,请联系管理员');
  50. if ($box->is_binding == BoxBinding::BINDING_YES) return $this->error('此设备已经绑定过');
  51. // 蓝牙信息'TBIT_WA205-7HBLE';
  52. $blu_key = config('systemConfig.blu_key');
  53. try {
  54. $blu_ase_key = Aes128Handler::genKey($blu_key, $box_no);
  55. } catch (\Exception $e) {
  56. return $this->error($e->getMessage());
  57. }
  58. // 车辆信息
  59. $bike = Bike::query()->where('bike_no', $bike_no)->first();
  60. if (empty($bike)) return $this->error('找不到车辆信息');
  61. // $update = [
  62. // 'box_no' => $box_no,
  63. // 'blu_key' => $blu_key,
  64. // 'blu_ase_key' => $blu_ase_key
  65. // ];
  66. try {
  67. DB::beginTransaction();
  68. // 更新绑定表
  69. $box->is_binding = BoxBinding::BINDING_YES;
  70. $box->save();
  71. // 更新车辆信息
  72. // $bike->update($update);
  73. $bike->box_no = $box_no;
  74. $bike->blu_key = $blu_key;
  75. $bike->blu_ase_key = $blu_ase_key;
  76. $bike->save();
  77. DB::commit();
  78. return $this->ok('绑定成功');
  79. } catch (\Exception $e) {
  80. DB::rollBack();
  81. Log::error($e->getMessage());
  82. return $this->error('绑定失败,请联系管理员');
  83. }
  84. }
  85. /**
  86. * 解绑车辆
  87. * */
  88. public function unbindingBike(Request $request)
  89. {
  90. // $bike_no = $request->get('bike_no') ?? '';
  91. $box_no = $request->get('box_no') ?? '';
  92. if (empty($box_no)) return $this->error('参数错误');
  93. // 判断中控是否存在
  94. $box = BoxBinding::query()->where('box_no', $box_no)->first();
  95. if (empty($box)) return $this->error('找不到此设备信息,请联系管理员');
  96. // if($box->is_binding == BoxBinding::BINDING_NO) return $this->error('此设备未绑定过,不需要解绑');
  97. // 车辆信息
  98. $bike = Bike::query()->where('box_no', $box_no)->first();
  99. // if(empty($bike)) return $this->error('找不到车辆信息');
  100. try {
  101. DB::beginTransaction();
  102. // 更新绑定表
  103. $box->is_binding = BoxBinding::BINDING_NO;
  104. $box->save();
  105. // 更新车辆信息
  106. // $bike->update($update);
  107. if (!empty($bike)) {
  108. $bike->box_no = '';
  109. $bike->blu_key = '';
  110. $bike->blu_ase_key = '';
  111. $bike->save();
  112. }
  113. DB::commit();
  114. return $this->ok('解绑成功');
  115. } catch (\Exception $e) {
  116. DB::rollBack();
  117. Log::error($e->getMessage());
  118. return $this->error('解绑失败,请联系管理员');
  119. }
  120. }
  121. /**
  122. * 获取绑定后信息
  123. * */
  124. public function bikeInfoByBikeNo(Request $request)
  125. {
  126. $bike_no = $request->get('bike_no') ?? '';
  127. if (empty($bike_no)) return $this->error('参数错误');
  128. $bike = Bike::query()->where('bike_no', $bike_no)->first(['is_link', 'battery_power', 'bike_no', 'box_no'])->toArray();
  129. if (empty($bike['box_no'])) {
  130. return $this->error('车辆未绑定,请先绑定');
  131. } else {
  132. return $this->ok($bike);
  133. }
  134. }
  135. public function bikeInfo(Request $request)
  136. {
  137. $box_no = $request->get('box_no') ?? '';
  138. if (empty($box_no)) return $this->error('参数错误');
  139. $bike = Bike::query()->where('box_no', $box_no)->first(['is_link', 'battery_power', 'bike_no', 'box_no'])->toArray();
  140. if (empty($bike)) {
  141. return $this->error('找不到车辆信息');
  142. } else {
  143. return $this->ok($bike);
  144. }
  145. }
  146. /**
  147. * 获取蓝牙密钥
  148. * */
  149. public function getKey(Request $request)
  150. {
  151. $box_no = $request->get('box_no');
  152. $bike = Bike::where('box_no', $box_no)->first();
  153. $key = $bike->blu_ase_key ?? '';
  154. return $this->ok(['key' => $key]);
  155. }
  156. /**
  157. * 响铃
  158. * */
  159. public function bikeBell(Request $request)
  160. {
  161. $bike_no = $request->get('bike_no') ?? '';
  162. if (empty($bike_no)) return $this->error('参数错误');
  163. $bike = Bike::query()->where('bike_no', $bike_no)->first();
  164. if (empty($bike)) return $this->error('找不到车辆信息');
  165. // $box_no = "003448483"; // 测试写死
  166. $box_no = $bike->box_no;
  167. $bool = BikeControl::bellBike($box_no);
  168. if ($bool) {
  169. return $this->ok('操作成功,请寻找响铃车辆');
  170. } else {
  171. return $this->error('操作失败,请联系管理员');
  172. }
  173. }
  174. /**
  175. * 开电车锁
  176. * */
  177. public function bikeOpen(Request $request)
  178. {
  179. $bike_no = $request->get('bike_no') ?? '';
  180. if (empty($bike_no)) return $this->error('参数错误');
  181. $bike = Bike::query()->where('bike_no', $bike_no)->first();
  182. if (empty($bike)) return $this->error('找不到车辆信息');
  183. // $box_no = "003448483"; // 测试写死
  184. $box_no = $bike->box_no;
  185. $bool = BikeControl::openLock($box_no);
  186. (new BikeStatusInfoSyncHandler())->toBikeRideStatus(BikeStatusInfoSyncHandler::ROLE_BIND, $bike_no, ['bike_id' => $bike->id, 'area_id' => $bike->put_area_id]);
  187. if ($bool) {
  188. return $this->ok('操作成功,请寻找响铃车辆');
  189. } else {
  190. return $this->error('操作失败,请联系管理员');
  191. }
  192. }
  193. /**
  194. * 关电车锁
  195. * */
  196. public function bikeClose(Request $request)
  197. {
  198. $bike_no = $request->get('bike_no') ?? '';
  199. if (empty($bike_no)) return $this->error('参数错误');
  200. $bike = Bike::query()->where('bike_no', $bike_no)->first();
  201. if (empty($bike)) return $this->error('找不到车辆信息');
  202. // $box_no = "003448483"; // 测试写死
  203. $box_no = $bike->box_no;
  204. $bool = BikeControl::closeLock($box_no);
  205. $lastLocation = LocationsLog::getNewestLocationByBikeNo($bike_no);
  206. // 修改redis
  207. (new BikeStatusInfoSyncHandler())->toBikeWaitRideStatus($bike->bike_no, $lastLocation['lng'] ?? 0, $lastLocation['lat'] ?? 0, $bike->put_status);
  208. if ($bool) {
  209. return $this->ok('操作成功');
  210. } else {
  211. return $this->error('操作失败,请联系管理员');
  212. }
  213. }
  214. /**
  215. * 开电池锁
  216. * */
  217. public function bikeOpenBattery(Request $request)
  218. {
  219. $bike_no = $request->get('bike_no') ?? '';
  220. if (empty($bike_no)) return $this->error('参数错误');
  221. $bike = Bike::query()->where('bike_no', $bike_no)->first();
  222. if (empty($bike)) return $this->error('找不到车辆信息');
  223. // $box_no = "003448483"; // 测试写死
  224. $box_no = $bike->box_no;
  225. $bool = BikeControl::openBatteryLock($box_no);
  226. if ($bool) {
  227. return $this->ok('操作成功');
  228. } else {
  229. return $this->error('操作失败,请联系管理员');
  230. }
  231. }
  232. /**
  233. * 关电池锁
  234. * */
  235. public function bikeCloseBattery(Request $request)
  236. {
  237. $bike_no = $request->get('bike_no') ?? '';
  238. if (empty($bike_no)) return $this->error('参数错误');
  239. $bike = Bike::query()->where('bike_no', $bike_no)->first();
  240. if (empty($bike)) return $this->error('找不到车辆信息');
  241. // $box_no = "003448483"; // 测试写死
  242. $box_no = $bike->box_no;
  243. $bool = BikeControl::closeBatteryLock($box_no);
  244. if ($bool) {
  245. return $this->ok('操作成功');
  246. } else {
  247. return $this->error('操作失败,请联系管理员');
  248. }
  249. }
  250. /**
  251. * 重启中控
  252. * */
  253. public function rebootBox(Request $request)
  254. {
  255. $bike_no = $request->get('bike_no') ?? '';
  256. if (empty($bike_no)) return $this->error('参数错误');
  257. $bike = Bike::query()->where('bike_no', $bike_no)->first();
  258. if (empty($bike)) return $this->error('找不到车辆');
  259. // $box_no = "003448483"; // 测试写死
  260. $box_no = $bike->box_no;
  261. $bool = BikeControl::rebootBox($box_no);
  262. if ($bool) {
  263. return $this->ok('重启中控成功');
  264. } else {
  265. return $this->error('重启中控失败,请联系管理员');
  266. }
  267. }
  268. /**
  269. * 立即定位
  270. * */
  271. public function newBikeLocation(Request $request)
  272. {
  273. $bike_no = $request->get('bike_no') ?? '';
  274. if (empty($bike_no)) return $this->error('参数错误');
  275. $bike = Bike::query()->where('bike_no', $bike_no)->first();
  276. if (empty($bike)) return $this->error('找不到车辆');
  277. // $box_no = "003448483"; // 测试写死
  278. $box_no = $bike->box_no;
  279. $bool = BikeControl::nowBikeLocation($box_no);
  280. if ($bool) {
  281. return $this->ok('立即定位车辆成功');
  282. } else {
  283. return $this->error('立即定位车辆失败,请联系管理员');
  284. }
  285. }
  286. /**
  287. * 立即更新电池信息
  288. * */
  289. public function newBikeBatteryMSG(Request $request)
  290. {
  291. $bike_no = $request->get('bike_no') ?? '';
  292. if (empty($bike_no)) return $this->error('参数错误');
  293. $bike = Bike::query()->where('bike_no', $bike_no)->first();
  294. if (empty($bike)) return $this->error('找不到车辆');
  295. // $box_no = "003448483"; // 测试写死
  296. $box_no = $bike->box_no;
  297. $bool = BikeControl::nowBikeBatteryMSG($box_no);
  298. if ($bool) {
  299. return $this->ok('立即更新电池信息成功');
  300. } else {
  301. return $this->error('立即更新电池信息失败,请联系管理员');
  302. }
  303. }
  304. }