123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395 |
- <?php
- namespace App\Http\Controllers\Admin;
- use App\Filters\BoxBindingFilter;
- use App\Filters\WarningLogsFilter;
- use App\Handlers\BikeControl;
- use App\Http\Requests\BoxBindingRequest;
- use App\Http\Requests\BoxSettingRequest;
- use App\Http\Requests\RemarkRequest;
- use App\Http\Resources\BoxBindingResource;
- use App\Http\Resources\WarningLogResource;
- use App\Imports\BikesImport;
- use App\Imports\BoxBindingImport;
- use App\Models\AdminMerchant;
- use App\Models\Bike;
- use App\Models\BoxBinding;
- use App\Models\WarningLog;
- use App\Utils\Admin;
- use Illuminate\Http\Request;
- use App\Http\Controllers\Controller;
- use Illuminate\Support\Facades\DB;
- use Illuminate\Support\Facades\Log;
- use Maatwebsite\Excel\Facades\Excel;
- /**
- * Class BoxBindingController
- * @package App\Http\Controllers\Admin
- */
- class BoxBindingController extends Controller
- {
- /**
- * index 中控列表
- *
- * @param BoxBindingFilter $filter
- * @param Request $request
- * @return \Illuminate\Http\JsonResponse
- * @author Fx
- *
- */
- public function index(BoxBindingFilter $filter, Request $request)
- {
- //
- $admin_id = Admin::user()->id;
- $box = BoxBinding::query()->filter($filter)
- ->where(AdminMerchant::getMerchantWhere())->orderByDesc('id');
- $box = $request->get('all') ? $box->get() : $box->paginate();
- return $this->ok(BoxBindingResource::collection($box));
- }
- /**
- * Show the form for creating a new resource.
- *
- * @return \Illuminate\Http\Response
- */
- public function create()
- {
- //
- }
- /**
- * store 添加中控
- *
- * @param Request $request
- * @param BoxBinding $box
- * @return \Illuminate\Http\JsonResponse
- * @author Fx
- *
- */
- public function store(BoxBindingRequest $request, BoxBinding $box)
- {
- $inputs = $request->validated();
- //
- $inputs['merchant_id'] = AdminMerchant::putMerchantId();
- $box = $box->updateOrCreate(['box_no' => $inputs['box_no']], $inputs);
- return $this->ok(BoxBindingResource::make($box));
- }
- /**
- * Display the specified resource.
- *
- * @param int $id
- * @return \Illuminate\Http\Response
- */
- public function show($id)
- {
- //
- }
- /**
- * Show the form for editing the specified resource.
- *
- * @param int $id
- * @return \Illuminate\Http\Response
- */
- public function edit($id)
- {
- //
- }
- /**
- * update 更新中控
- *
- * @param Request $request
- * @param $id
- * @return void
- * @author Fx
- *
- */
- public function update(Request $request, $id)
- {
- //
- }
- public function updateRemark(RemarkRequest $request, $id)
- {
- //
- $box = BoxBinding::find($id);
- $inputs = $request->validated();
- // Log::info($id);
- $box->update($inputs);
- return $this->ok(BoxBindingResource::make($box));
- }
- /**
- * Remove the specified resource from storage.
- *
- * @param int $id
- * @return \Illuminate\Http\Response
- */
- public function destroy($id)
- {
- //
- }
- /**
- * import 导入excle
- *
- * @param Request $request
- * @return \Illuminate\Http\JsonResponse
- * @author Fx
- *
- */
- public function import(Request $request)
- {
- try {
- Excel::import(new BoxBindingImport, $request->file('boxs'));
- return $this->ok('导入成功');
- } catch (\Exception $e) {
- Log::error($e->getMessage());
- return $this->error('导入失败');
- }
- }
- /**
- * download 下载模板
- *
- * @return \Symfony\Component\HttpFoundation\BinaryFileResponse
- * @author Fx
- *
- */
- public function download()
- {
- return response()->download(public_path('example_box.xlsx'));
- }
- /**
- * unbindBox 解绑
- *
- * @param $id
- * @return \Illuminate\Http\JsonResponse
- * @author Fx
- *
- */
- public function unbindBox($id)
- {
- $box = BoxBinding::find($id);
- if (empty($box)) return $this->error('找不到该中控信息');
- $box_no = $box->box_no;
- $bike = Bike::query()->where('box_no', $box_no)->first();
- try {
- DB::beginTransaction();
- // 更新绑定表
- $box->is_binding = BoxBinding::BINDING_NO;
- $box->save();
- // 更新车辆信息
- if (!empty($bike)) {
- $bike->box_no = '';
- $bike->blu_key = '';
- $bike->blu_ase_key = '';
- $bike->last_location = json_encode(['lng' => 116.397546, 'lat' => 39.909153]);
- $bike->save();
- }
- DB::commit();
- return $this->ok('解绑成功');
- } catch (\Exception $e) {
- DB::rollBack();
- Log::error($e->getMessage());
- return $this->error('解绑失败,请联系管理员');
- }
- }
- /**
- * 单个配置中控 setting
- *
- * @param BoxSettingRequest $boxSettingRequest
- * @param $id
- * @return \Illuminate\Http\JsonResponse
- * @author Fx
- *
- */
- public function setting(BoxSettingRequest $boxSettingRequest, $id)
- {
- $inputs = $boxSettingRequest->validated();
- unset($inputs['box_no']);
- unset($inputs['box_ids']);
- $boxBinding = BoxBinding::query()->find($id);
- $res = $boxBinding->update($inputs);
- // $res = $boxBinding->query()->save($inputs);
- if ($res) {
- $setting = [
- // 心跳间隔时间(秒)
- 'PULSE=' . $inputs['pulse'],
- // 骑行定位包间隔(秒)
- 'FREQ=' . $inputs['freq'],
- // 骑行时静止多少分钟报锁车(分)
- 'VIBFILTERREMINDT=' . $inputs['vibfilterremindt'],
- // 配置服务器地址
- 'SERVER=' . $inputs['server'],
- // 蓝牙秘钥
- // 'DFTBLEENCKEY' => 'TBIT_WA205-7HBLE',
- // 蓝牙功能开关
- // 'BLEKG='.$inputs['blekg'],
- //车的最高速度
- // 'MAXECUSPEED=' . $inputs['maxecuspeed'],
- //车速度的百分比
- 'MAXSPEEDPERCENT=' . BikeControl::maxecuspeedToMaxspeedpercent($inputs['maxecuspeed'])
- ];
- // 发指令
- // $data = BikeControl::setBoxSetting($boxBinding->box_no, $setting, true);
- if (strlen($boxBinding->box_no) < 10) {
- $data = BikeControl::setBoxSetting($boxBinding->box_no, $setting, true);
- } else {
- $server = explode(':', $inputs['server']);
- $setting = [
- 'auto_lock_time' => $inputs['vibfilterremindt'],
- 'motion_position_reporting_interval' => $inputs['freq'],
- 'static_position_reporting_interval' => ceil($inputs['pulse'] / 60),
- 'ip' => $server[0],
- 'port' => $server[1],
- ];
- // if (!filter_var($server[0], FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
- // return $this->error('ip错误');
- // }
- // 限速
- \App\Handlers\Weikemu\BikeControl::limitSpeed($boxBinding->box_no, \App\Handlers\Weikemu\BikeControl::maxecuspeedToMaxspeedpercent($inputs['maxecuspeed']));
- usleep(5000);
- $data = [];
- // $data = \App\Handlers\Weikemu\BikeControl::setBoxSetting($boxBinding->box_no, $setting);
- // sleep(1);
- // $data = \App\Handlers\Weikemu\BikeControl::queryConfig($boxBinding->box_no);
- // $data = object_array($data);
- // return $this->ok($data);
- }
- return $this->ok($data);
- } else {
- return $this->error('操作失败');
- }
- }
- /**
- * 批量配置中控 settingMul
- *
- * @param BoxSettingRequest $boxSettingRequest
- * @return \Illuminate\Http\JsonResponse
- * @author Fx
- *
- */
- public function settingMul(BoxSettingRequest $boxSettingRequest)
- {
- $inputs = $boxSettingRequest->validated();
- $ids = $inputs['box_ids'];
- unset($inputs['box_ids']);
- unset($inputs['box_no']);
- $res = BoxBinding::query()->whereIn('id', $ids)->update($inputs);
- // 发指令
- if ($res) {
- $setting = [
- // 心跳间隔时间(秒)
- 'PULSE=' . $inputs['pulse'],
- // 骑行定位包间隔(秒)
- 'FREQ=' . $inputs['freq'],
- // 骑行时静止多少分钟报锁车(分)
- 'VIBFILTERREMINDT=' . $inputs['vibfilterremindt'],
- // 配置服务器地址
- 'SERVER=' . $inputs['server'],
- // 蓝牙秘钥
- // 'DFTBLEENCKEY' => 'TBIT_WA205-7HBLE',
- // 蓝牙功能开关
- // 'BLEKG='.$inputs['blekg'],
- //车的最高速度
- // 'MAXECUSPEED='.$inputs['maxecuspeed'],
- //车速度的百分比
- 'MAXSPEEDPERCENT=' . BikeControl::maxecuspeedToMaxspeedpercent($inputs['maxecuspeed'])
- ];
- $boxs = BoxBinding::query()->whereIn('id', $ids)->get();
- foreach ($boxs as $v) {
- // 发指令
- if (strlen($v->box_no) < 10) {
- $data = BikeControl::setBoxSetting($v->box_no, $setting);
- } else {
- $server = explode(':', $inputs['server']);
- $setting = [
- 'auto_lock_time' => $inputs['vibfilterremindt'],
- 'motion_position_reporting_interval' => $inputs['freq'],
- 'static_position_reporting_interval' => ceil($inputs['pulse'] / 60),
- 'ip' => $server[0],
- 'port' => $server[1],
- ];
- if (!filter_var($server[0], FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
- return $this->error('ip错误');
- }
- \App\Handlers\Weikemu\BikeControl::limitSpeed($v->box_no, \App\Handlers\Weikemu\BikeControl::maxecuspeedToMaxspeedpercent($inputs['maxecuspeed']));
- sleep(1);
- // \App\Handlers\Weikemu\BikeControl::setBoxSetting($v->box_no, $setting);
- }
- }
- return $this->ok('操作成功');
- } else {
- return $this->error('操作失败');
- }
- }
- /**
- * 获取服务器配置选项 getServerOptions
- *
- * @return \Illuminate\Http\JsonResponse
- * @author Fx
- *
- */
- public function getServerOptions()
- {
- $servers = BoxBinding::query()->where(AdminMerchant::getMerchantWhere())->distinct()->pluck('server')->toArray();
- $data = [];
- foreach ($servers as $v) {
- $data[] = ['label' => $v, 'value' => $v];
- }
- return $this->ok($data);
- }
- public function queryConfig(Request $request)
- {
- $box_no = $request->get('box_no');
- if (strlen($box_no) < 10) {
- $data = BikeControl::downBoxSetting($box_no);
- return $this->ok(object_array($data));
- } else {
- $data = \App\Handlers\Weikemu\BikeControl::queryConfig($box_no);
- $data = object_array($data);
- return $this->ok($data);
- }
- }
- public function warningLogsIndex(WarningLogsFilter $filter)
- {
- $data = WarningLog::query()
- ->filter($filter)
- ->where(AdminMerchant::getMerchantWhere())
- ->orderByDesc('id')
- ->paginate();
- return $this->ok(WarningLogResource::collection($data));
- }
- public function getWarningType()
- {
- $data = [];
- foreach (WarningLog::$typeMaps as $k => $v) {
- $data[] = [
- 'label' => $v,
- 'value' => $k,
- ];
- }
- return $this->ok($data);
- }
- }
|