deviceModel = $deviceModel; } /** * 列表 * @time 2021年08月28日 16:57 * @param Request $request */ public function index(Request $request): \think\Response { $device_type = $request->get('device_type'); $field = $request->get('field') ?: 'id'; $order = $request->get('order') ?: 'desc'; $list = $this->deviceModel->getDeviceList($device_type, $field, $order); foreach ($list as &$item) { if ($item['online_time'] < $item['wifi_online_time']) { $item['last_online_time'] = $item['wifi_online_time']; $item['longitude'] = $item['wifi_longitude']; $item['latitude'] = $item['wifi_latitude']; } else { $item['last_online_time'] = $item['online_time']; } } return CatchResponse::paginate($list); } /** * 根据用户id获取设备 * @time 2021年08月28日 16:57 * @param Request $request */ public function getDeviceByUserId(Request $request): \think\Response { return CatchResponse::paginate($this->deviceModel->getList()); } /** * 保存信息 * @time 2021年08月28日 16:57 * @param Request $request */ public function save(Request $request): \think\Response { $depart_id = $request->post('department_id'); if (!$depart_id) { return CatchResponse::fail('请选择所属部门'); } $imei = $request->post('imei'); if (!$imei) { return CatchResponse::fail('请输入设备imei号'); } if (mb_strlen($imei) != 15) { return CatchResponse::fail('imei号格式不正确'); } if ($this->deviceModel->where('imei', $imei)->find()) { return CatchResponse::fail('imei号已存在'); } $creator_id = $request->post('creator_id'); $type = $request->post('type'); if ($type == 1) { //个人卡牌导入 $data = array( 'department_id' => $depart_id, 'imei' => $imei, 'rfid' => substr($imei, -8), 'device_name' => '设备' . substr($imei, -4), 'alarm_state' => 0, 'device_type' => 0, 'use_state' => 0, 'net_state' => 2, 'creator_id' => $creator_id, 'created_at' => time(), 'updated_at' => time() ); if (!$this->deviceModel->storeBy($data)) { return CatchResponse::fail('添加设备失败'); } $redis=Cache::store('redis')->handler(); $redis->rpush('c61_voice_cneter_add_list', $imei); return CatchResponse::success(); } else { $realname = $request->post('realname'); $user_no = $request->post('user_no'); $phone = $request->post('phone'); //realname user_no phone 有一个存在,其他也应该必填 if (!$realname) { return CatchResponse::fail('请输入用户姓名'); } if (!$user_no) { return CatchResponse::fail('请输入用户编号'); } if (!$phone) { return CatchResponse::fail('请输入手机号码'); } //添加用户 $role_id_card_group = (new Roles())->where('identify', 'group_card_user')->value('id'); $user_info = (new Users()) ->alias('u') ->where('u.username', $phone) ->join('user_has_roles r', 'u.id = r.uid and r.role_id=' . $role_id_card_group) ->find(); if ($user_info) { $uid = $user_info['uid']; (new Users())->where('id', $uid)->update(['realname' => $realname, 'user_no' => $user_no]); } else { //添加用户 $user = array( 'username' => $phone, 'password' => substr($imei, -8), 'realname' => $realname, 'user_no' => $user_no, 'phone' => $phone, 'department_id' => $depart_id, 'creator_id' => $creator_id, 'created_at' => time(), 'updated_at' => time() ); // var_dump($user); $uid = (new Users())->createBy($user); Db::table('user_has_roles')->insert(['uid' => $uid, 'role_id' => $role_id_card_group]); } //保存设备信息 $data = array( 'department_id' => $depart_id, 'imei' => $imei, 'rfid' => substr($imei, -8), 'device_name' => '设备' . substr($imei, -4), 'user_id' => $uid, 'alarm_state' => 0, 'device_type' => 0, 'use_state' => 1, 'net_state' => 2, 'creator_id' => $creator_id, 'created_at' => time(), 'updated_at' => time() ); //添加设备 if ($this->deviceModel->storeBy($data)) { $redis=Cache::store('redis')->handler(); $redis->rpush('c61_voice_cneter_add_list', $imei); return CatchResponse::success(); } return CatchResponse::fail('添加设备失败'); } } /** * 读取 * @time 2021年08月28日 16:57 * @param $id */ public function read($id): \think\Response { return CatchResponse::success($this->deviceModel->findBy($id)); } /** * 更新 * @time 2021年08月28日 16:57 * @param Request $request * @param $id */ public function update(Request $request, $id): \think\Response { $depart_id = $request->post('department_id'); if (!$depart_id) { return CatchResponse::fail('请选择所属部门'); } $imei = $request->post('imei'); if (count(explode(',', $imei)) > 1) { return CatchResponse::fail('imei格式不正确'); } $check_id = $this->deviceModel->where('imei', $imei)->value('id'); if ($check_id && $check_id != $id) { return CatchResponse::fail('imei号已存在'); } //更新设备信息 $data = array( 'department_id' => $depart_id, 'imei' => $imei, 'rfid' => substr($imei, -8), 'updated_at' => time() ); if (isset(($request->post())['creator'])) { $data['creator_id'] = $request->post('creator'); } if ($this->deviceModel->where('id', $id)->update($data)) { return CatchResponse::success(); } return CatchResponse::fail('更新设备失败'); // return CatchResponse::success($this->deviceModel->updateBy($id, $request->post())); } /** * 删除 * @time 2021年08月28日 16:57 * @param $id */ public function delete($id): \think\Response { return CatchResponse::success($this->deviceModel->deleteBy($id, true)); } /** * 查询告警列表 * @time 2021年05月24日 * @return \think\response\Json */ public function queryAlarmList(Request $request): \think\Response { return CatchResponse::paginate($this->deviceModel->queryAlarmList()); } /** * 导出 * * @time 2020年09月08日 * @param Excel $excel * @param DeviceExport $deviceExport * @throws \PhpOffice\PhpSpreadsheet\Exception * @return \think\response\Json */ public function export_card(Excel $excel, CardsExport $CardsExport) { // var_dump(Utils::publicPath('export/users'));//导出路径 return CatchResponse::success($excel->save($CardsExport, Utils::publicPath('export/cards'), 'local', '卡牌设备列表')); } /** * 导入卡牌设备 * * @time 2020年09月08日 * @param Excel $excel * @param DeviceExport $deviceExport * @throws \PhpOffice\PhpSpreadsheet\Exception * @return \think\response\Json */ public function importCards(Request $request) { $url = $request->post('url'); if (!$url) { return CatchResponse::fail('请上传文件'); } $depart_id = $request->post('depart_id'); if (!$depart_id) { return CatchResponse::fail('请选择部门'); } $creator_id = $request->post('creator_id'); //解析地址 $parse_url = parse_url($url)['path']; //载入excel表格 $objPHPExcel = IOFactory::load(public_path() . $parse_url); // var_dump($objPHPExcel); //获取表名,一维数组,值是表名。如:array('sheet1', 'sheet2', 'sheet3') // $nameArr = $objPHPExcel->getSheetNames(); // var_dump($nameArr); //获取表的数量 $sheetCount = $objPHPExcel->getSheetCount(); $fail = 0; //失败条数 $success = 0; //成功条数 $total = 0; //总共学生数 $personal_data = []; //个人卡牌 $group_data = []; //团体卡牌 //循环读取每一张表 for ($index = 0; $index < $sheetCount; $index++) { //设置当前要读取的表 $sheet = $objPHPExcel->getSheet($index); //excel中的第一张sheet // var_dump($sheet);exit; $highestRow = $sheet->getHighestRow(); // 取得总行数 // var_dump($highestRow); if ($highestRow <= 2) { continue; } $total += $highestRow - 2; for ($j = 3; $j <= $highestRow; $j++) { $data = array(); //每条学生信息 $imei = trim($sheet->getCell("A" . $j)->getFormattedValue()); //imei // var_dump($imei); if (mb_strlen($imei) != 15) { $fail++; $msg = '导入设备:' . $imei . '失败:imei编号格式不正确'; $this->importFailLog($msg); continue; } $realname = trim($sheet->getCell("B" . $j)->getFormattedValue()); //姓名 $user_no = trim($sheet->getCell("C" . $j)->getFormattedValue()); //用户编号 $phone = trim($sheet->getCell("D" . $j)->getFormattedValue()); //手机号 if (!$realname && !$user_no && !$phone) { //导入个人用户设备 $data = array( 'department_id' => $depart_id, 'imei' => $imei, 'rfid' => substr($imei, -8), 'device_name' => '设备' . substr($imei, -4), 'alarm_state' => 0, 'device_type' => 0, 'use_state' => 0, 'net_state' => 2, 'creator_id' => $creator_id, 'created_at' => time(), 'updated_at' => time() ); //查询设备 $info = $this->deviceModel->where('imei', $imei)->find(); if ($info) { $fail++; $msg = '导入设备:' . $imei . '失败:imei编号已存在'; $this->importFailLog($msg); continue; } array_push($personal_data, $data); } else { //团体用户设备 if (!$realname) { $fail++; $msg = '导入设备:' . $imei . '失败:用户姓名不存在'; $this->importFailLog($msg); continue; } if (!$user_no) { $fail++; $msg = '导入设备:' . $imei . '失败:用户编号不存在'; $this->importFailLog($msg); continue; } if (!$phone) { $fail++; $msg = '导入设备:' . $imei . '失败:用户手机号不存在'; $this->importFailLog($msg); continue; } //保存用户信息 $role_id_card_group = (new Roles())->where('identify', 'group_card_user')->value('id'); $user_info = (new Users()) ->alias('u') ->where('u.phone', $phone) ->join('user_has_roles r', 'u.id = r.uid and r.role_id=' . $role_id_card_group) ->find(); if ($user_info) { $uid = $user_info['uid']; (new Users())->where('id', $uid)->update(['realname' => $realname, 'user_no' => $user_no]); } else { //添加用户 $user = array( 'username' => $phone, 'password' => substr($imei, -8), 'realname' => $realname, 'user_no' => $user_no, 'phone' => $phone, 'department_id' => $depart_id, 'creator_id' => $creator_id, 'created_at' => time(), 'updated_at' => time() ); // var_dump($user); $uid = (new Users())->createBy($user); Db::table('user_has_roles')->insert(['uid' => $uid, 'role_id' => $role_id_card_group]); } //保存设备信息 $data = array( 'department_id' => $depart_id, 'imei' => $imei, 'rfid' => substr($imei, -8), 'device_name' => '设备' . substr($imei, -4), 'user_id' => $uid, 'alarm_state' => 0, 'device_type' => 0, 'use_state' => 1, 'net_state' => 2, 'creator_id' => $creator_id, 'created_at' => time(), 'updated_at' => time() ); //查询设备 $info = $this->deviceModel->where('imei', $imei)->find(); if ($info) { $fail++; $msg = '导入设备:' . $imei . '失败:imei编号已存在'; $this->importFailLog($msg); continue; } //添加设备 // if ($this->deviceModel->createBy($data)) { // $success++; // } else { // $fail++; // $msg = '导入设备:' . $imei . '失败:添加设备失败'; // $this->importFailLog($msg); // continue; // } array_push($group_data, $data); } } } array_unique($personal_data, SORT_REGULAR); array_unique($group_data, SORT_REGULAR); $count1 = $this->deviceModel->limit(100)->insertAll($personal_data); $count2 = $this->deviceModel->limit(100)->insertAll($group_data); $redis=Cache::store('redis')->handler(); foreach($personal_data as $value){ $redis->rpush('c61_voice_cneter_add_list', $value['imei']); } foreach($group_data as $value){ $redis->rpush('c61_voice_cneter_add_list', $value['imei']); } if ($success = $count1 + $count2) { return CatchResponse::success('共' . $total . '条数据,成功' . $success . '条,失败' . $fail . '条'); } return CatchResponse::success(['error' => true, 'msg' => '导入失败']); // return CatchResponse::success('共' . $total . '条数据,成功' . $success . '条,失败' . $fail . '条'); } /** * 导入设备失败日志 */ public function importFailLog($msg) { $file = runtime_path() . '/log/' . date("Ymd", time()) . "/import_devices_fail.log"; $folder = dirname($file); if (!is_dir($folder)) { mkdir($folder, 0777, true); } file_put_contents($file, '[' . date('Y-m-d H:i:s') . ']' . $msg . PHP_EOL, FILE_APPEND); } /** * 检测下发配置选项 */ public function checkConfig(Request $request): \think\Response { // var_dump($request->post('ids')); $ids = $request->post('ids'); if (empty($ids)) { return CatchResponse::fail('未选中设备'); } $has_use_num = 0; foreach ($ids as $v) { if ($this->deviceModel->where('id', $v)->value('use_state')) { $has_use_num++; } } if (count($ids) != $has_use_num) { // return CatchResponse::fail('所选项含有未注册设备'); } return CatchResponse::success(); } /** * 下发录音配置 */ public function sendVoiceConfig(Request $request): \think\Response { $ids = $request->post('ids'); $config = $request->post('config'); if (empty($ids)) { return CatchResponse::fail('未选择设备'); } if (empty($config)) { return CatchResponse::fail('至少配置一项'); } // $config['is_enabled'] = $config['is_enabled'] ? 1 : 0; if (!$config['srv_addr']) { unset($config['srv_addr']); } if (!$config['srv_port']) { unset($config['srv_port']); } else { $config['srv_port'] = intval($config['srv_port']); } // if (!$config['max_ppt_duration_ms']) { // unset($config['max_ppt_duration_ms']); // } else { // $config['max_ppt_duration_ms'] = intval($config['max_ppt_duration_ms']); // } $config['msg_id'] = 0xAAAA0008; Db::startTrans(); $fail_imei = ''; foreach ($ids as $id) { $imei = $this->deviceModel->where('id', $id)->value('imei'); if (!$imei) { continue; } //下发日志 $log_data = array( 'send_contents' => json_encode($config), 'imei' => $imei, 'type' => 'voice', 'created_at' => time(), 'device_id' => $id ); $id = Db::table('send_config_log')->insertGetId($log_data); if (!$id) { $fail_imei += $imei . ' '; Db::rollback(); } $config['id'] = $id; $redis = Cache::store('redis')->handler(); $res = $redis->rpush('c61_gps_device_configs_' . $imei, json_encode($config)); if (!$res) { $fail_imei += $imei . ' '; Db::rollback(); } } if ($fail_imei != '') { return CatchResponse::fail('下发失败'); } Db::commit(); return CatchResponse::success('下发成功'); } /** * 下发传感器配置 */ public function sendSensorConfig(Request $request): \think\Response { $ids = $request->post('ids'); $config = $request->post('config'); if (empty($ids)) { return CatchResponse::fail('未选择设备'); } if (empty($config)) { return CatchResponse::fail('至少配置一项'); } if (!$config['stopped_rtc_invl']) { unset($config['stopped_rtc_invl']); } else { $config['stopped_rtc_invl'] = intval($config['stopped_rtc_invl']); } if (!$config['stopped_rpt_invl']) { unset($config['stopped_rpt_invl']); } else { $config['stopped_rpt_invl'] = intval($config['stopped_rpt_invl']); } if (!$config['not_moving_threshold']) { unset($config['not_moving_threshold']); } else { $config['not_moving_threshold'] = intval($config['not_moving_threshold']); } if (!$config['stopped_rtc_invl']) { unset($config['stopped_rtc_invl']); } else { $config['stopped_rtc_invl'] = intval($config['stopped_rtc_invl']); } if (!$config['stopped_rpt_invl']) { unset($config['stopped_rpt_invl']); } else { $config['stopped_rpt_invl'] = intval($config['stopped_rpt_invl']); } if (!$config['tbd_cnt_max']) { unset($config['tbd_cnt_max']); } else { $config['tbd_cnt_max'] = intval($config['tbd_cnt_max']); } if (empty($config)) { return CatchResponse::fail('您还没有配置任何一项'); } $config['msg_id'] = 0xAAAA0007; Db::startTrans(); $fail_imei = ''; foreach ($ids as $id) { $imei = $this->deviceModel->where('id', $id)->value('imei'); if (!$imei) { continue; } //下发日志 $log_data = array( 'send_contents' => json_encode($config), 'imei' => $imei, 'created_at' => time(), 'type' => 'sensor', 'device_id' => $id ); $id = Db::table('send_config_log')->insertGetId($log_data); if (!$id) { $fail_imei += $imei . ' '; Db::rollback(); } $config['id'] = $id; $redis = Cache::store('redis')->handler(); $res = $redis->rpush('c61_gps_device_configs_' . $imei, json_encode($config)); if (!$res) { $fail_imei += $imei . ' '; Db::rollback(); } } if ($fail_imei != '') { return CatchResponse::fail('下发失败'); } Db::commit(); return CatchResponse::success('下发成功'); } /** * 下发gps配置 */ public function sendGpsConfig(Request $request): \think\Response { $ids = $request->post('ids'); $config = $request->post('config'); if (empty($ids)) { return CatchResponse::fail('未选择设备'); } if (empty($config)) { return CatchResponse::fail('至少配置一项'); } $config['pos_mode'] = intval($config['pos_mode']); // $config['msg_id'] = 0xAAAA0001; $config['msg_id'] = 0xAAAA0010; Db::startTrans(); $fail_imei = ''; foreach ($ids as $id) { $imei = $this->deviceModel->where('id', $id)->value('imei'); if (!$imei) { continue; } //判断是不是定时模式 if ($config['pos_mode'] == 2) { $config_arr = array( 'stopped_rtc_invl' => intval($config['stopped_rtc_invl']), 'stopped_rpt_invl' => intval($config['stopped_rpt_invl']), 'msg_id' => 0xAAAA0007 ); //下发日志 $log_data = array( 'send_contents' => json_encode($config_arr), 'imei' => $imei, 'type' => 'sensor', 'created_at' => time(), 'device_id' => $id ); $lg_id = Db::table('send_config_log')->insertGetId($log_data); if (!$lg_id) { $fail_imei += $imei . ' '; Db::rollback(); } $config_arr['id'] = $lg_id; $redis = Cache::store('redis')->handler(); $res = $redis->rpush('c61_gps_device_configs_' . $imei, json_encode($config_arr)); if (!$res) { $fail_imei += $imei . ' '; Db::rollback(); } }else{ $config_arr = array( 'stopped_rtc_invl' => 3000, 'stopped_rpt_invl' => 3600, 'msg_id' => 0xAAAA0007 ); //下发日志 $log_data = array( 'send_contents' => json_encode($config_arr), 'imei' => $imei, 'type' => 'sensor', 'created_at' => time(), 'device_id' => $id ); $lg_id = Db::table('send_config_log')->insertGetId($log_data); if (!$lg_id) { $fail_imei += $imei . ' '; Db::rollback(); } $config_arr['id'] = $lg_id; $redis = Cache::store('redis')->handler(); $res = $redis->rpush('c61_gps_device_configs_' . $imei, json_encode($config_arr)); if (!$res) { $fail_imei += $imei . ' '; Db::rollback(); } } //重新赋值 删除多余参数 $mode_config=$config; unset($mode_config['stopped_rpt_invl'],$mode_config['stopped_rtc_invl']); //下发日志 $log_data = array( 'send_contents' => json_encode($mode_config), 'imei' => $imei, 'type' => 'pos_mode', 'created_at' => time(), 'device_id' => $id ); $id = Db::table('send_config_log')->insertGetId($log_data); if (!$id) { $fail_imei += $imei . ' '; Db::rollback(); } $mode_config['id'] = $id; $redis = Cache::store('redis')->handler(); $res = $redis->rpush('c61_gps_device_configs_' . $imei, json_encode($mode_config)); if (!$res) { $fail_imei += $imei . ' '; Db::rollback(); } } if ($fail_imei != '') { return CatchResponse::fail('下发失败'); } Db::commit(); return CatchResponse::success('下发成功'); } /** * 设备终端操作命令 */ public function sendcmd(Request $request): \think\Response { $ids = $request->post('ids'); if (empty($ids)) { return CatchResponse::fail('未选择设备'); } $config['msg_id'] = 0xAAAA0011; $config['cmd'] = intval($request->post('cmd')); Db::startTrans(); $fail_imei = ''; foreach ($ids as $id) { $imei = $this->deviceModel->where('id', $id)->value('imei'); if (!$imei) { continue; } //下发日志 $log_data = array( 'send_contents' => json_encode($config), 'imei' => $imei, 'type' => 'cmd', 'created_at' => time(), 'device_id' => $id ); $id = Db::table('send_config_log')->insertGetId($log_data); if (!$id) { $fail_imei += $imei . ' '; Db::rollback(); } $config['id'] = $id; $redis = Cache::store('redis')->handler(); $res = $redis->rpush('c61_gps_device_configs_' . $imei, json_encode($config)); if (!$res) { $fail_imei += $imei . ' '; Db::rollback(); } } if ($fail_imei != '') { return CatchResponse::fail('操作失败'); } Db::commit(); return CatchResponse::success('操作成功'); } /* 默认配置 */ public function getDefaultConfig() { $type_id = Db::table('sys_dict_type')->where('code', 'DeviceConfigDefault')->value('id'); $list = Db::table('sys_dict_data')->where('type_id', $type_id)->field('code,value,sort')->select(); $cnf1 = []; $cnf2 = []; $cnf3 = []; foreach ($list as $value) { if ($value['sort'] == 1) { // array_push($cnf1,[$value['code']=>$value['value']]); if ($value['value'] == 'true') { $value['value'] = true; } $cnf1[$value['code']] = $value['value']; } if ($value['sort'] == 2) { $cnf2[$value['code']] = $value['value']; } if ($value['sort'] == 3) { $cnf3[$value['code']] = $value['value']; $cnf3['stopped_rtc_invl'] = '50'; $cnf3['stopped_rpt_invl'] = '300'; } } $config = array( 'voice_config' => $cnf1, 'sensor_config' => $cnf2, 'gps_config' => $cnf3, ); return CatchResponse::success($config); } }