123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356 |
- <?php
- namespace App\Http\Controllers\App\Open;
- use App\Handlers\Aes128Handler;
- use App\Handlers\BaseBikeControl;
- use App\Handlers\BikeStatusInfoSyncHandler;
- use App\Http\Resources\App\BikeResource;
- use App\Jobs\CloseBikeJob;
- use App\Models\AdminMerchant;
- use App\Models\Bike;
- use App\Models\BoxBinding;
- use App\Models\LocationsLog;
- use Carbon\Carbon;
- use Illuminate\Http\Request;
- use App\Http\Controllers\Controller;
- use Illuminate\Support\Facades\DB;
- use Illuminate\Support\Facades\Log;
- use Illuminate\Support\Facades\Validator;
- class BikeController extends Controller
- {
- /**
- * 绑定车辆
- * */
- public function addBike(Request $request)
- {
- // 验证参数
- $bike_no = $request->get('bike_no') ?? '';
- $box_no = $request->get('box_no') ?? '';
- $type = $request->get('type', 1);
- if (empty($bike_no) || empty($box_no)) return $this->error('参数错误');
- if (strlen($bike_no) !== 11) {
- return $this->error('车牌编号为11位');
- }
- if (strlen($box_no) != 15) {
- return $this->error('中控编号为15位');
- }
- // 判断中控是否存在 ->where(AdminMerchant::getMerchantWhere())
- $box = BoxBinding::query()->where('box_no', $box_no)->first();
- if (empty($box)) return $this->error('找不到此设备信息,请联系管理员');
- if ($box->is_binding == BoxBinding::BINDING_YES) return $this->error('此设备已经绑定过');
- // if ((int)$box->manufacturer === BoxBinding::TBT) {
- // // 蓝牙信息'TBIT_WA205-7HBLE';
- // $blu_key = config('systemConfig.blu_key');
- // try {
- // $blu_ase_key = Aes128Handler::genKey($blu_key, $box_no);
- // } catch (\Exception $e) {
- // return $this->error($e->getMessage());
- // }
- // }
- $blu_key = $blu_ase_key = '';
- switch ((int)$box->manufacturer) {
- case BoxBinding::TBT:
- $blu_key = config('systemConfig.blu_key');
- try {
- $blu_ase_key = Aes128Handler::genKey($blu_key, $box_no);
- } catch (\Exception $e) {
- return $this->error($e->getMessage());
- }
- break;
- case BoxBinding::WKM:
- $blu_key = $box->bluetooth_token;
- $blu_ase_key = $box->bluetooth_token;
- break;
- }
- // 车辆信息 ->where(AdminMerchant::getMerchantWhere())
- $bike = Bike::query()->where('bike_no', $bike_no)->first();
- if (empty($bike)) return $this->error('找不到车辆信息');
- try {
- DB::beginTransaction();
- // 更新绑定表
- $box->is_binding = BoxBinding::BINDING_YES;
- $box->save();
- // 更新车辆信息
- // $bike->update($update);
- $bike->box_no = $box_no;
- $bike->blu_key = $blu_key;
- $bike->blu_ase_key = $blu_ase_key;
- $bike->save();
- DB::commit();
- return $this->ok('绑定成功');
- } catch (\Exception $e) {
- DB::rollBack();
- Log::error($e->getMessage());
- return $this->error('绑定失败,请联系管理员');
- }
- }
- /**
- * 解绑车辆
- * */
- public function unbindingBike(Request $request)
- {
- // $bike_no = $request->get('bike_no') ?? '';
- $box_no = $request->get('box_no') ?? '';
- if (empty($box_no)) return $this->error('参数错误');
- // 判断中控是否存在 ->where(AdminMerchant::getMerchantWhere())
- $box = BoxBinding::query()->where('box_no', $box_no)->first();
- if (empty($box)) return $this->error('找不到此设备信息,请联系管理员');
- // if($box->is_binding == BoxBinding::BINDING_NO) return $this->error('此设备未绑定过,不需要解绑');
- // 车辆信息 ->where(AdminMerchant::getMerchantWhere())
- $bike = Bike::query()->where('box_no', $box_no)->first();
- // if(empty($bike)) return $this->error('找不到车辆信息');
- try {
- DB::beginTransaction();
- // 更新绑定表
- $box->is_binding = BoxBinding::BINDING_NO;
- $box->save();
- // 更新车辆信息
- // $bike->update($update);
- if (!empty($bike)) {
- $bike->box_no = '';
- $bike->blu_key = '';
- $bike->blu_ase_key = '';
- $bike->save();
- }
- DB::commit();
- return $this->ok('解绑成功');
- } catch (\Exception $e) {
- DB::rollBack();
- Log::error($e->getMessage());
- return $this->error('解绑失败,请联系管理员');
- }
- }
- /**
- * 获取绑定后信息
- * */
- public function bikeInfoByBikeNo(Request $request)
- {
- $bike_no = $request->get('bike_no') ?? '';
- if (empty($bike_no)) return $this->error('参数错误');
- $bike = Bike::query()->where('bike_no', $bike_no)->first(['is_link', 'battery_power', 'bike_no', 'box_no'])->toArray();
- if (empty($bike['box_no'])) {
- return $this->error('车辆未绑定,请先绑定');
- } else {
- return $this->ok($bike);
- }
- }
- public function bikeInfo(Request $request)
- {
- $box_no = $request->get('box_no') ?? '';
- if (empty($box_no)) return $this->error('参数错误');
- $bike = Bike::query()->where('box_no', $box_no)->first(['is_link', 'battery_power', 'bike_no', 'box_no'])->toArray();
- if (empty($bike)) {
- return $this->error('找不到车辆信息');
- } else {
- return $this->ok($bike);
- }
- }
- /**
- * 获取蓝牙密钥
- * */
- public function getKey(Request $request)
- {
- $box_no = $request->get('box_no');
- $bike = Bike::query()->where('box_no', $box_no)->first();
- $key = $bike->blu_ase_key ?? '';
- return $this->ok(['key' => $key]);
- }
- /**
- * 响铃
- * */
- public function bikeBell(Request $request)
- {
- $bike_no = $request->get('bike_no') ?? '';
- if (empty($bike_no)) return $this->error('参数错误');
- $bike = Bike::query()->where('bike_no', $bike_no)->first();
- if (empty($bike)) return $this->error('找不到车辆信息');
- // $box_no = "003448483"; // 测试写死
- $box_no = $bike->box_no;
- $bool = (new BaseBikeControl($box_no))::bellBike();
- if ($bool) {
- return $this->ok('操作成功,请寻找响铃车辆');
- } else {
- return $this->error('操作失败,请联系管理员');
- }
- }
- /**
- * 开电车锁
- * */
- public function bikeOpen(Request $request)
- {
- $bike_no = $request->get('bike_no') ?? '';
- if (empty($bike_no)) return $this->error('参数错误');
- $bike = Bike::query()->where('bike_no', $bike_no)->first();
- if (empty($bike)) return $this->error('找不到车辆信息');
- // $box_no = "003448483"; // 测试写死
- $box_no = $bike->box_no;
- $bool = (new BaseBikeControl($box_no))::openLock();
- (new BikeStatusInfoSyncHandler())->toBikeRideStatus(BikeStatusInfoSyncHandler::ROLE_BIND, $bike_no, ['bike_id' => $bike->id, 'area_id' => $bike->put_area_id]);
- CloseBikeJob::dispatch($bike);
- if ($bool) {
- return $this->ok('操作成功,请寻找响铃车辆');
- } else {
- return $this->error('操作失败,请联系管理员');
- }
- }
- /**
- * 关电车锁
- * */
- public function bikeClose(Request $request)
- {
- $bike_no = $request->get('bike_no') ?? '';
- if (empty($bike_no)) return $this->error('参数错误');
- $bike = Bike::query()->where('bike_no', $bike_no)->first();
- if (empty($bike)) return $this->error('找不到车辆信息');
- // $box_no = "003448483"; // 测试写死
- $box_no = $bike->box_no;
- $bool = (new BaseBikeControl($box_no))::closeLock();
- $lastLocation = LocationsLog::getNewestLocationByBikeNo($bike_no);
- // 修改redis
- (new BikeStatusInfoSyncHandler())->toBikeWaitRideStatus($bike->bike_no, $lastLocation['lng'] ?? 0, $lastLocation['lat'] ?? 0, $bike->put_status);
- if ($bool) {
- return $this->ok('操作成功');
- } else {
- return $this->error('操作失败,请联系管理员');
- }
- }
- /**
- * 开电池锁
- * */
- public function bikeOpenBattery(Request $request)
- {
- $bike_no = $request->get('bike_no') ?? '';
- if (empty($bike_no)) return $this->error('参数错误');
- $bike = Bike::query()->where('bike_no', $bike_no)->first();
- if (empty($bike)) return $this->error('找不到车辆信息');
- // $box_no = "003448483"; // 测试写死
- $box_no = $bike->box_no;
- $bool = (new BaseBikeControl($box_no))::openBatteryLock();
- if ($bool) {
- return $this->ok('操作成功');
- } else {
- return $this->error('操作失败,请联系管理员');
- }
- }
- /**
- * 关电池锁
- * */
- public function bikeCloseBattery(Request $request)
- {
- $bike_no = $request->get('bike_no') ?? '';
- if (empty($bike_no)) return $this->error('参数错误');
- $bike = Bike::query()->where('bike_no', $bike_no)->first();
- if (empty($bike)) return $this->error('找不到车辆信息');
- // $box_no = "003448483"; // 测试写死
- $box_no = $bike->box_no;
- $bool = (new BaseBikeControl($box_no))::closeBatteryLock();
- if ($bool) {
- return $this->ok('操作成功');
- } else {
- return $this->error('操作失败,请联系管理员');
- }
- }
- /**
- * 重启中控
- * */
- public function rebootBox(Request $request)
- {
- $bike_no = $request->get('bike_no') ?? '';
- if (empty($bike_no)) return $this->error('参数错误');
- $bike = Bike::query()->where('bike_no', $bike_no)->first();
- if (empty($bike)) return $this->error('找不到车辆');
- // $box_no = "003448483"; // 测试写死
- $box_no = $bike->box_no;
- $bool = (new BaseBikeControl($box_no))::rebootBox();
- if ($bool) {
- return $this->ok('重启中控成功');
- } else {
- return $this->error('重启中控失败,请联系管理员');
- }
- }
- /**
- * 立即定位
- * */
- public function newBikeLocation(Request $request)
- {
- $bike_no = $request->get('bike_no') ?? '';
- if (empty($bike_no)) return $this->error('参数错误');
- $bike = Bike::query()->where('bike_no', $bike_no)->first();
- if (empty($bike)) return $this->error('找不到车辆');
- // $box_no = "003448483"; // 测试写死
- $box_no = $bike->box_no;
- $bool = (new BaseBikeControl($box_no))::nowBikeLocation();
- if ($bool) {
- return $this->ok('立即定位车辆成功');
- } else {
- return $this->error('立即定位车辆失败,请联系管理员');
- }
- }
- /**
- * 立即更新电池信息
- * */
- public function newBikeBatteryMSG(Request $request)
- {
- $bike_no = $request->get('bike_no') ?? '';
- if (empty($bike_no)) return $this->error('参数错误');
- $bike = Bike::query()->where('bike_no', $bike_no)->first();
- if (empty($bike)) return $this->error('找不到车辆');
- // $box_no = "003448483"; // 测试写死
- $box_no = $bike->box_no;
- $bool = (new BaseBikeControl($box_no))::nowBikeBatteryMSG();
- if ($bool) {
- return $this->ok('立即更新电池信息成功');
- } else {
- return $this->error('立即更新电池信息失败,请联系管理员');
- }
- }
- }
|