BoxBindingController.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  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. $inputs['merchant_id'] = AdminMerchant::putMerchantId();
  70. $box = $box->updateOrCreate(['box_no' => $inputs['box_no']], $inputs);
  71. return $this->ok(BoxBindingResource::make($box));
  72. }
  73. /**
  74. * Display the specified resource.
  75. *
  76. * @param int $id
  77. * @return \Illuminate\Http\Response
  78. */
  79. public function show($id)
  80. {
  81. //
  82. }
  83. /**
  84. * Show the form for editing the specified resource.
  85. *
  86. * @param int $id
  87. * @return \Illuminate\Http\Response
  88. */
  89. public function edit($id)
  90. {
  91. //
  92. }
  93. /**
  94. * update 更新中控
  95. *
  96. * @param Request $request
  97. * @param $id
  98. * @return void
  99. * @author Fx
  100. *
  101. */
  102. public function update(Request $request, $id)
  103. {
  104. //
  105. }
  106. public function updateRemark(RemarkRequest $request, $id)
  107. {
  108. //
  109. $box = BoxBinding::find($id);
  110. $inputs = $request->validated();
  111. // Log::info($id);
  112. $box->update($inputs);
  113. return $this->ok(BoxBindingResource::make($box));
  114. }
  115. /**
  116. * Remove the specified resource from storage.
  117. *
  118. * @param int $id
  119. * @return \Illuminate\Http\Response
  120. */
  121. public function destroy($id)
  122. {
  123. //
  124. }
  125. /**
  126. * import 导入excle
  127. *
  128. * @param Request $request
  129. * @return \Illuminate\Http\JsonResponse
  130. * @author Fx
  131. *
  132. */
  133. public function import(Request $request)
  134. {
  135. try {
  136. Excel::import(new BoxBindingImport, $request->file('boxs'));
  137. return $this->ok('导入成功');
  138. } catch (\Exception $e) {
  139. Log::error($e->getMessage());
  140. return $this->error('导入失败');
  141. }
  142. }
  143. /**
  144. * download 下载模板
  145. *
  146. * @return \Symfony\Component\HttpFoundation\BinaryFileResponse
  147. * @author Fx
  148. *
  149. */
  150. public function download()
  151. {
  152. return response()->download(public_path('example_box.xlsx'));
  153. }
  154. /**
  155. * unbindBox 解绑
  156. *
  157. * @param $id
  158. * @return \Illuminate\Http\JsonResponse
  159. * @author Fx
  160. *
  161. */
  162. public function unbindBox($id)
  163. {
  164. $box = BoxBinding::find($id);
  165. if (empty($box)) return $this->error('找不到该中控信息');
  166. $box_no = $box->box_no;
  167. $bike = Bike::query()->where('box_no', $box_no)->first();
  168. try {
  169. DB::beginTransaction();
  170. // 更新绑定表
  171. $box->is_binding = BoxBinding::BINDING_NO;
  172. $box->save();
  173. // 更新车辆信息
  174. if (!empty($bike)) {
  175. $bike->box_no = '';
  176. $bike->blu_key = '';
  177. $bike->blu_ase_key = '';
  178. $bike->last_location = json_encode(['lng' => 116.397546, 'lat' => 39.909153]);
  179. $bike->save();
  180. }
  181. DB::commit();
  182. return $this->ok('解绑成功');
  183. } catch (\Exception $e) {
  184. DB::rollBack();
  185. Log::error($e->getMessage());
  186. return $this->error('解绑失败,请联系管理员');
  187. }
  188. }
  189. /**
  190. * 单个配置中控 setting
  191. *
  192. * @param BoxSettingRequest $boxSettingRequest
  193. * @param $id
  194. * @return \Illuminate\Http\JsonResponse
  195. * @author Fx
  196. *
  197. */
  198. public function setting(BoxSettingRequest $boxSettingRequest, $id)
  199. {
  200. $inputs = $boxSettingRequest->validated();
  201. unset($inputs['box_no']);
  202. unset($inputs['box_ids']);
  203. $boxBinding = BoxBinding::query()->find($id);
  204. $res = $boxBinding->update($inputs);
  205. // $res = $boxBinding->query()->save($inputs);
  206. if ($res) {
  207. $setting = [
  208. // 心跳间隔时间(秒)
  209. 'PULSE=' . $inputs['pulse'],
  210. // 骑行定位包间隔(秒)
  211. 'FREQ=' . $inputs['freq'],
  212. // 骑行时静止多少分钟报锁车(分)
  213. 'VIBFILTERREMINDT=' . $inputs['vibfilterremindt'],
  214. // 配置服务器地址
  215. 'SERVER=' . $inputs['server'],
  216. // 蓝牙秘钥
  217. // 'DFTBLEENCKEY' => 'TBIT_WA205-7HBLE',
  218. // 蓝牙功能开关
  219. // 'BLEKG='.$inputs['blekg'],
  220. //车的最高速度
  221. // 'MAXECUSPEED=' . $inputs['maxecuspeed'],
  222. //车速度的百分比
  223. 'MAXSPEEDPERCENT=' . BikeControl::maxecuspeedToMaxspeedpercent($inputs['maxecuspeed'])
  224. ];
  225. // 发指令
  226. // $data = BikeControl::setBoxSetting($boxBinding->box_no, $setting, true);
  227. if (strlen($boxBinding->box_no) < 10) {
  228. $data = BikeControl::setBoxSetting($boxBinding->box_no, $setting, true);
  229. } else {
  230. $server = explode(':', $inputs['server']);
  231. $setting = [
  232. 'auto_lock_time' => $inputs['vibfilterremindt'],
  233. 'motion_position_reporting_interval' => $inputs['freq'],
  234. 'static_position_reporting_interval' => ceil($inputs['pulse'] / 60),
  235. 'ip' => $server[0],
  236. 'port' => $server[1],
  237. ];
  238. // if (!filter_var($server[0], FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
  239. // return $this->error('ip错误');
  240. // }
  241. // 限速
  242. \App\Handlers\Weikemu\BikeControl::limitSpeed($boxBinding->box_no, \App\Handlers\Weikemu\BikeControl::maxecuspeedToMaxspeedpercent($inputs['maxecuspeed']));
  243. usleep(5000);
  244. $data = [];
  245. // $data = \App\Handlers\Weikemu\BikeControl::setBoxSetting($boxBinding->box_no, $setting);
  246. // sleep(1);
  247. // $data = \App\Handlers\Weikemu\BikeControl::queryConfig($boxBinding->box_no);
  248. // $data = object_array($data);
  249. // return $this->ok($data);
  250. }
  251. return $this->ok($data);
  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. 'ip' => $server[0],
  303. 'port' => $server[1],
  304. ];
  305. if (!filter_var($server[0], FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
  306. return $this->error('ip错误');
  307. }
  308. \App\Handlers\Weikemu\BikeControl::limitSpeed($v->box_no, \App\Handlers\Weikemu\BikeControl::maxecuspeedToMaxspeedpercent($inputs['maxecuspeed']));
  309. sleep(1);
  310. // \App\Handlers\Weikemu\BikeControl::setBoxSetting($v->box_no, $setting);
  311. }
  312. }
  313. return $this->ok('操作成功');
  314. } else {
  315. return $this->error('操作失败');
  316. }
  317. }
  318. /**
  319. * 获取服务器配置选项 getServerOptions
  320. *
  321. * @return \Illuminate\Http\JsonResponse
  322. * @author Fx
  323. *
  324. */
  325. public function getServerOptions()
  326. {
  327. $servers = BoxBinding::query()->where(AdminMerchant::getMerchantWhere())->distinct()->pluck('server')->toArray();
  328. $data = [];
  329. foreach ($servers as $v) {
  330. $data[] = ['label' => $v, 'value' => $v];
  331. }
  332. return $this->ok($data);
  333. }
  334. public function queryConfig(Request $request)
  335. {
  336. $box_no = $request->get('box_no');
  337. if (strlen($box_no) < 10) {
  338. $data = BikeControl::downBoxSetting($box_no);
  339. return $this->ok(object_array($data));
  340. } else {
  341. $data = \App\Handlers\Weikemu\BikeControl::queryConfig($box_no);
  342. $data = object_array($data);
  343. return $this->ok($data);
  344. }
  345. }
  346. public function warningLogsIndex(WarningLogsFilter $filter)
  347. {
  348. $data = WarningLog::query()
  349. ->filter($filter)
  350. ->where(AdminMerchant::getMerchantWhere())
  351. ->orderByDesc('id')
  352. ->paginate();
  353. return $this->ok(WarningLogResource::collection($data));
  354. }
  355. public function getWarningType()
  356. {
  357. $data = [];
  358. foreach (WarningLog::$typeMaps as $k => $v) {
  359. $data[] = [
  360. 'label' => $v,
  361. 'value' => $k,
  362. ];
  363. }
  364. return $this->ok($data);
  365. }
  366. }