BikeController.php 12 KB

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