BoxBindingController.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. <?php
  2. namespace App\Http\Controllers\Admin;
  3. use App\Filters\BoxBindingFilter;
  4. use App\Filters\WarningLogsFilter;
  5. use App\Handlers\BikeControl;
  6. use App\Http\Requests\BoxBindingRequest;
  7. use App\Http\Requests\BoxSettingRequest;
  8. use App\Http\Requests\RemarkRequest;
  9. use App\Http\Resources\BoxBindingResource;
  10. use App\Http\Resources\WarningLogResource;
  11. use App\Imports\BikesImport;
  12. use App\Imports\BoxBindingImport;
  13. use App\Models\AdminMerchant;
  14. use App\Models\Bike;
  15. use App\Models\BoxBinding;
  16. use App\Models\WarningLog;
  17. use App\Utils\Admin;
  18. use Illuminate\Http\Request;
  19. use App\Http\Controllers\Controller;
  20. use Illuminate\Support\Facades\DB;
  21. use Illuminate\Support\Facades\Log;
  22. use Maatwebsite\Excel\Facades\Excel;
  23. /**
  24. * Class BoxBindingController
  25. * @package App\Http\Controllers\Admin
  26. */
  27. class BoxBindingController extends Controller
  28. {
  29. /**
  30. * index 中控列表
  31. *
  32. * @param BoxBindingFilter $filter
  33. * @param Request $request
  34. * @return \Illuminate\Http\JsonResponse
  35. * @author Fx
  36. *
  37. */
  38. public function index(BoxBindingFilter $filter, Request $request)
  39. {
  40. //
  41. $admin_id = Admin::user()->id;
  42. $box = BoxBinding::query()->filter($filter)
  43. ->where(AdminMerchant::getMerchantWhere())->orderByDesc('id');
  44. $box = $request->get('all') ? $box->get() : $box->paginate();
  45. return $this->ok(BoxBindingResource::collection($box));
  46. }
  47. /**
  48. * Show the form for creating a new resource.
  49. *
  50. * @return \Illuminate\Http\Response
  51. */
  52. public function create()
  53. {
  54. //
  55. }
  56. /**
  57. * store 添加中控
  58. *
  59. * @param Request $request
  60. * @param BoxBinding $box
  61. * @return \Illuminate\Http\JsonResponse
  62. * @author Fx
  63. *
  64. */
  65. public function store(BoxBindingRequest $request, BoxBinding $box)
  66. {
  67. $inputs = $request->validated();
  68. //
  69. if (!$inputs['merchant_id']) {
  70. $inputs['merchant_id'] = AdminMerchant::putMerchantId();
  71. }
  72. $box = $box->updateOrCreate(['box_no' => $inputs['box_no']], $inputs);
  73. return $this->ok(BoxBindingResource::make($box));
  74. }
  75. /**
  76. * Display the specified resource.
  77. *
  78. * @param int $id
  79. * @return \Illuminate\Http\Response
  80. */
  81. public function show($id)
  82. {
  83. //
  84. }
  85. /**
  86. * Show the form for editing the specified resource.
  87. *
  88. * @param int $id
  89. * @return \Illuminate\Http\Response
  90. */
  91. public function edit($id)
  92. {
  93. //
  94. }
  95. /**
  96. * update 更新中控
  97. *
  98. * @param Request $request
  99. * @param $id
  100. * @return void
  101. * @author Fx
  102. *
  103. */
  104. public function update(Request $request, $id)
  105. {
  106. //
  107. }
  108. public function updateRemark(RemarkRequest $request, $id)
  109. {
  110. //
  111. $box = BoxBinding::find($id);
  112. $inputs = $request->validated();
  113. // Log::info($id);
  114. $box->update($inputs);
  115. return $this->ok(BoxBindingResource::make($box));
  116. }
  117. /**
  118. * Remove the specified resource from storage.
  119. *
  120. * @param int $id
  121. * @return \Illuminate\Http\Response
  122. */
  123. public function destroy($id)
  124. {
  125. //
  126. }
  127. /**
  128. * import 导入excle
  129. *
  130. * @param Request $request
  131. * @return \Illuminate\Http\JsonResponse
  132. * @author Fx
  133. *
  134. */
  135. public function import(Request $request)
  136. {
  137. try {
  138. Excel::import(new BoxBindingImport, $request->file('boxs'));
  139. return $this->ok('导入成功');
  140. } catch (\Exception $e) {
  141. Log::error($e->getMessage());
  142. return $this->error('导入失败');
  143. }
  144. }
  145. /**
  146. * download 下载模板
  147. *
  148. * @return \Symfony\Component\HttpFoundation\BinaryFileResponse
  149. * @author Fx
  150. *
  151. */
  152. public function download()
  153. {
  154. return response()->download(public_path('example_box_template.xlsx'));
  155. }
  156. /**
  157. * unbindBox 解绑
  158. *
  159. * @param $id
  160. * @return \Illuminate\Http\JsonResponse
  161. * @author Fx
  162. *
  163. */
  164. public function unbindBox($id)
  165. {
  166. $box = BoxBinding::find($id);
  167. if (empty($box)) return $this->error('找不到该中控信息');
  168. $box_no = $box->box_no;
  169. $bike = Bike::query()->where('box_no', $box_no)->first();
  170. try {
  171. DB::beginTransaction();
  172. // 更新绑定表
  173. $box->is_binding = BoxBinding::BINDING_NO;
  174. $box->save();
  175. // 更新车辆信息
  176. if (!empty($bike)) {
  177. $bike->box_no = '';
  178. $bike->blu_key = '';
  179. $bike->blu_ase_key = '';
  180. $bike->last_location = json_encode(['lng' => 116.397546, 'lat' => 39.909153]);
  181. $bike->save();
  182. }
  183. DB::commit();
  184. return $this->ok('解绑成功');
  185. } catch (\Exception $e) {
  186. DB::rollBack();
  187. Log::error($e->getMessage());
  188. return $this->error('解绑失败,请联系管理员');
  189. }
  190. }
  191. /**
  192. * 单个配置中控 setting
  193. *
  194. * @param BoxSettingRequest $boxSettingRequest
  195. * @param $id
  196. * @return \Illuminate\Http\JsonResponse
  197. * @author Fx
  198. *
  199. */
  200. public function setting(BoxSettingRequest $boxSettingRequest, $id)
  201. {
  202. $inputs = $boxSettingRequest->validated();
  203. unset($inputs['box_no']);
  204. unset($inputs['box_ids']);
  205. $boxBinding = BoxBinding::query()->find($id);
  206. $res = $boxBinding->update($inputs);
  207. if ($res) {
  208. // 发指令
  209. if (strlen($boxBinding->box_no) < 10) {
  210. $setting = [
  211. // 心跳间隔时间(秒)
  212. 'PULSE=' . $inputs['pulse'],
  213. // 骑行定位包间隔(秒)
  214. 'FREQ=' . $inputs['freq'],
  215. // 骑行时静止多少分钟报锁车(分)
  216. 'VIBFILTERREMINDT=' . $inputs['vibfilterremindt'],
  217. // 配置服务器地址
  218. 'SERVER=' . $inputs['server'],
  219. // 蓝牙秘钥
  220. // 'DFTBLEENCKEY' => 'TBIT_WA205-7HBLE',
  221. // 蓝牙功能开关
  222. // 'BLEKG='.$inputs['blekg'],
  223. //车的最高速度
  224. // 'MAXECUSPEED=' . $inputs['maxecuspeed'],
  225. //车速度的百分比
  226. 'MAXSPEEDPERCENT=' . BikeControl::maxecuspeedToMaxspeedpercent($inputs['maxecuspeed'])
  227. ];
  228. $result = BikeControl::setBoxSetting($boxBinding->box_no, $setting, true);
  229. } else {
  230. $setting = [
  231. 'auto_lock_time' => $inputs['vibfilterremindt'],
  232. 'motion_position_reporting_interval' => $inputs['freq'],
  233. 'static_position_reporting_interval' => ceil($inputs['pulse'] / 60),
  234. ];
  235. // 限速
  236. // \App\Handlers\Weikemu\BikeControl::limitSpeed($boxBinding->box_no, \App\Handlers\Weikemu\BikeControl::maxecuspeedToMaxspeedpercent($inputs['maxecuspeed']));
  237. // //配置域名
  238. // $server = explode(':', $inputs['server']);
  239. //
  240. // if ($inputs['server'] !== $boxBinding->server) {
  241. // \App\Handlers\Weikemu\BikeControl::setBoxServerUrl($boxBinding->box_no, $server[0], $server[1]);
  242. // }
  243. \App\Handlers\Weikemu\BikeControl::setBoxSetting($boxBinding->box_no, $setting);
  244. $data = \App\Handlers\Weikemu\BikeControl::queryConfig($boxBinding->box_no);
  245. $result = object_array($data);
  246. $data1 = \App\Handlers\Weikemu\BikeControl::queryUrlConfig($boxBinding->box_no);
  247. $data1 = object_array($data1);
  248. $result['SERVER'] = $data1['SERVER'];
  249. }
  250. $result = [];
  251. return $this->ok($result);
  252. } else {
  253. return $this->error('操作失败');
  254. }
  255. }
  256. /**
  257. * 批量配置中控 settingMul
  258. *
  259. * @param BoxSettingRequest $boxSettingRequest
  260. * @return \Illuminate\Http\JsonResponse
  261. * @author Fx
  262. *
  263. */
  264. public function settingMul(BoxSettingRequest $boxSettingRequest)
  265. {
  266. $inputs = $boxSettingRequest->validated();
  267. $ids = $inputs['box_ids'];
  268. unset($inputs['box_ids']);
  269. unset($inputs['box_no']);
  270. $res = BoxBinding::query()->whereIn('id', $ids)->update($inputs);
  271. // 发指令
  272. if ($res) {
  273. $setting = [
  274. // 心跳间隔时间(秒)
  275. 'PULSE=' . $inputs['pulse'],
  276. // 骑行定位包间隔(秒)
  277. 'FREQ=' . $inputs['freq'],
  278. // 骑行时静止多少分钟报锁车(分)
  279. 'VIBFILTERREMINDT=' . $inputs['vibfilterremindt'],
  280. // 配置服务器地址
  281. 'SERVER=' . $inputs['server'],
  282. // 蓝牙秘钥
  283. // 'DFTBLEENCKEY' => 'TBIT_WA205-7HBLE',
  284. // 蓝牙功能开关
  285. // 'BLEKG='.$inputs['blekg'],
  286. //车的最高速度
  287. // 'MAXECUSPEED='.$inputs['maxecuspeed'],
  288. //车速度的百分比
  289. 'MAXSPEEDPERCENT=' . BikeControl::maxecuspeedToMaxspeedpercent($inputs['maxecuspeed'])
  290. ];
  291. $boxs = BoxBinding::query()->whereIn('id', $ids)->get();
  292. foreach ($boxs as $v) {
  293. // 发指令
  294. if (strlen($v->box_no) < 10) {
  295. $data = BikeControl::setBoxSetting($v->box_no, $setting);
  296. } else {
  297. $server = explode(':', $inputs['server']);
  298. $setting = [
  299. 'auto_lock_time' => $inputs['vibfilterremindt'],
  300. 'motion_position_reporting_interval' => $inputs['freq'],
  301. 'static_position_reporting_interval' => ceil($inputs['pulse'] / 60)
  302. ];
  303. // \App\Handlers\Weikemu\BikeControl::limitSpeed($v->box_no, \App\Handlers\Weikemu\BikeControl::maxecuspeedToMaxspeedpercent($inputs['maxecuspeed']));
  304. // sleep(1);
  305. \App\Handlers\Weikemu\BikeControl::setBoxSetting($v->box_no, $setting);
  306. // \App\Handlers\Weikemu\BikeControl::setBoxServerUrl($v->box_no, $server[0], $server[1]);
  307. }
  308. }
  309. return $this->ok('操作成功');
  310. } else {
  311. return $this->error('操作失败');
  312. }
  313. }
  314. /**
  315. * 获取服务器配置选项 getServerOptions
  316. *
  317. * @return \Illuminate\Http\JsonResponse
  318. * @author Fx
  319. *
  320. */
  321. public function getServerOptions()
  322. {
  323. $servers = BoxBinding::query()->where(AdminMerchant::getMerchantWhere())->distinct()->pluck('server')->toArray();
  324. $data = [];
  325. foreach ($servers as $v) {
  326. $data[] = ['label' => $v, 'value' => $v];
  327. }
  328. return $this->ok($data);
  329. }
  330. public function queryConfig(Request $request)
  331. {
  332. $box_no = $request->get('box_no');
  333. if (strlen($box_no) < 10) {
  334. $data = BikeControl::downBoxSetting($box_no);
  335. return $this->ok(object_array($data));
  336. } else {
  337. $data = \App\Handlers\Weikemu\BikeControl::queryConfig($box_no);
  338. $data = object_array($data);
  339. $data1 = \App\Handlers\Weikemu\BikeControl::queryUrlConfig($box_no);
  340. $data1 = object_array($data1);
  341. $data['SERVER'] = $data1['SERVER'];
  342. return $this->ok($data);
  343. }
  344. }
  345. public function warningLogsIndex(WarningLogsFilter $filter)
  346. {
  347. $data = WarningLog::query()
  348. ->filter($filter)
  349. ->where(AdminMerchant::getMerchantWhere())
  350. ->orderByDesc('id')
  351. ->paginate();
  352. return $this->ok(WarningLogResource::collection($data));
  353. }
  354. public function getWarningType()
  355. {
  356. $data = [];
  357. foreach (WarningLog::$typeMaps as $k => $v) {
  358. $data[] = [
  359. 'label' => $v,
  360. 'value' => $k,
  361. ];
  362. }
  363. return $this->ok($data);
  364. }
  365. }