RouteMap.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. <?php
  2. namespace catchAdmin\map\controller;
  3. use catcher\base\CatchRequest as Request;
  4. use catcher\CatchResponse;
  5. use catcher\base\CatchController;
  6. use Aliyun\OTS\OTSClient;
  7. use catchAdmin\hydraulic\model\Hydraulic as deviceModel;
  8. use catchAdmin\fences\model\Fences as fencesModel;
  9. use think\facade\Db;
  10. use think\facade\Env;
  11. class RouteMap extends CatchController
  12. {
  13. protected $deviceModel;
  14. protected $fencesModel;
  15. protected $filterTrack=false;
  16. public function __construct(deviceModel $deviceModel,fencesModel $fencesModel)
  17. {
  18. $this->deviceModel = $deviceModel;
  19. $this->fencesModel = $fencesModel;
  20. }
  21. /**
  22. * 列表
  23. * @time 2021年05月18日 10:15
  24. * @param Request $request
  25. */
  26. public function queryDeviceList(Request $request) : \think\Response
  27. {
  28. $list =$this->deviceModel->getList();
  29. return CatchResponse::paginate($list);
  30. }
  31. /**
  32. * 列表
  33. * @time 2021年05月18日 10:15
  34. * @param Request $request
  35. */
  36. public function queryDeviceRealTimeInfo(Request $request) : \think\Response
  37. {
  38. $params=$request->param();
  39. if(!isset($params['imeis'])){
  40. return CatchResponse::success([]);
  41. }
  42. // var_dump($params);
  43. $imeis=$params['imeis'];
  44. $list=$this->deviceModel->whereIn('imei',$imeis)->select();
  45. foreach ($list as &$value) {
  46. $value['status_icon']='run';
  47. $value['status']='在线';
  48. }
  49. // var_dump($list);
  50. return CatchResponse::success($list);
  51. }
  52. /**
  53. *
  54. * @time 2021年05月24日
  55. * @return \think\response\Json
  56. */
  57. public function queryLocationList(Request $request): \think\Response
  58. {
  59. return CatchResponse::success($this->deviceModel->queryLocationList());
  60. }
  61. /**
  62. *
  63. * @time 2021年05月24日
  64. * @return \think\response\Json
  65. */
  66. public function queryFenceList(Request $request): \think\Response
  67. {
  68. $list=DB::table('fences')->where('fence_type',0)->select()->toArray();
  69. foreach($list as &$val){
  70. $user_depart=Db::table('users')->where('id',$val['creator_id'])->value('department_id');
  71. $creator_level=Db::table('departments')->where('id',$user_depart)->value('level');
  72. if(!$creator_level){
  73. $val['level']=1;
  74. }else{
  75. $level_arr=explode('-',$creator_level);
  76. $val['level']=count($level_arr)+1;
  77. }
  78. }
  79. return CatchResponse::success($list);
  80. }
  81. /**
  82. * 查询当前定位
  83. * @time 2021年05月18日 10:15
  84. *
  85. */
  86. public function queryDeviceCurLocation(Request $request) : \think\Response
  87. {
  88. $params=$request->param();
  89. $device_number=$params['device_number'];
  90. if (!$device_number) {
  91. return CatchResponse::fail("获取设备号失败");
  92. }
  93. $device_info=$this->deviceModel->where('imei',$device_number)->find()->toArray();
  94. // var_dump($device_info);
  95. if($device_info['online_time']<$device_info['wifi_online_time']){
  96. $device_info['online_time']=$device_info['wifi_online_time'];
  97. $device_info['longitude']=$device_info['wifi_longitude'];
  98. $device_info['latitude']=$device_info['wifi_latitude'];
  99. }
  100. return CatchResponse::success($device_info);
  101. }
  102. /**
  103. * 查询学生轨迹RFid
  104. * @time 2021年05月18日 10:15
  105. *
  106. */
  107. public function queryStudentRouteRfid(Request $request) : \think\Response
  108. {
  109. $params=$request->param();
  110. $active_rfid=$params['active_rfid'];
  111. if (!$active_rfid) {
  112. return CatchResponse::fail("获取设备号失败");
  113. }
  114. $start_time = strtotime(date('Y-m-d 00:00:00'));
  115. $end_time = strtotime(date('Y-m-d 23:59:59'));
  116. if(isset($params['time_range']) && $params['time_range'] != ''){
  117. $start_time=strtotime($params['time_range'][0]);
  118. $end_time=strtotime($params['time_range'][1]);
  119. }
  120. // 判断设备是否为第三方的,如果是则调用第三方接口获取
  121. $isThirdParty = \catchAdmin\student\model\ThirdPartyDevices::isThirdPartyDevice($active_rfid);
  122. // var_dump('isThirdParty', $isThirdParty);
  123. if ($isThirdParty) {
  124. $options = [
  125. 'startTime' => $start_time,
  126. 'endTime' => $end_time,
  127. // 'positionTypes' => 1, //1,2
  128. ];
  129. $api_routes = \thirdapi\ThirdDeviceApi2::getHistoryLocations($active_rfid, $options);
  130. if (!$api_routes) {
  131. return CatchResponse::fail("查询轨迹失败");
  132. }
  133. $routes = [];
  134. if (isset($api_routes['content']) && !empty($api_routes['content'])) {
  135. foreach ($api_routes['content'] as $info) {
  136. $routes[] = [
  137. 'AlarmType' => 0,
  138. 'Altitude' => '',
  139. 'DeviceNumber' => $info['imei'],
  140. 'Latitude' => $info['lat'],
  141. 'Longitude' => $info['lon'],
  142. 'PassTime' => $info['receiveAt'],
  143. 'Battery' => $info['battery'],
  144. 'CollectDt' => $info['collectDt'],
  145. 'PositionType' => $info['positionType'],
  146. 'IsOnline' => $info['isOnline'],
  147. ];
  148. }
  149. }
  150. return CatchResponse::success($routes, "查询轨迹成功");
  151. }
  152. // 非第三方设备从表格存储获取
  153. // $otsClient = new OTSClient (array (
  154. // 'EndPoint' => 'https://rlfd.cn-hangzhou.ots.aliyuncs.com',
  155. // 'AccessKeyID' => 'LTAI4FecTAyMPdhAdkUwhAGA',
  156. // 'AccessKeySecret' => 'Ih0knSSfcje3OUi1YrdjeXQZYIfmTK',
  157. // 'InstanceName' => 'rlfd',
  158. // ));
  159. $table_config=Db::table('sys_config')->where('type','table_store')->column('fieldValue','field');
  160. if(!isset($table_config['EndPoint']) || !isset($table_config['AccessKeyID']) || !isset($table_config['AccessKeySecret']) || !isset($table_config['InstanceName'])){
  161. return CatchResponse::fail("请先配置表格存储");
  162. }
  163. $otsClient = new OTSClient (array (
  164. 'EndPoint' => $table_config['EndPoint'],
  165. 'AccessKeyID' => $table_config['AccessKeyID'],
  166. 'AccessKeySecret' => $table_config['AccessKeySecret'],
  167. 'InstanceName' => $table_config['InstanceName'],
  168. ));
  169. $otsClient->getClientConfig()->debugLogHandler = null;
  170. $otsClient->getClientConfig()->errorLogHandler = null;
  171. // var_dump($otsClient);
  172. ini_set('memory_limit', '512M');
  173. $startPK = array (
  174. array('DeviceNumber',$active_rfid),
  175. array('Timestamp', $start_time)
  176. // array('Timestamp', 1620885120)
  177. );
  178. $endPK = array (
  179. array('DeviceNumber', $active_rfid),
  180. array('Timestamp', $end_time)
  181. // array('Timestamp', 1620885441)
  182. );
  183. $limit = 5500;
  184. $rows=[];
  185. while (! empty ($startPK) && $limit > 0) {
  186. $request = array (
  187. 'table_name' => 'wxt_route_rfid',
  188. 'max_versions' => 1,
  189. 'direction' => 'BACKWARD', // 方向可以为 FORWARD 或者 BACKWARD
  190. 'inclusive_start_primary_key' => $endPK, // 开始主键
  191. 'exclusive_end_primary_key' => $startPK, // 结束主键
  192. 'limit' => $limit
  193. );
  194. $response = [];
  195. try {
  196. $response = $otsClient->getRange($request);
  197. } catch (\Exception $e) {
  198. // var_dump($e);
  199. }
  200. if (!isset($response['rows'])){
  201. return CatchResponse::success([]);
  202. }
  203. foreach ($response['rows'] as $rowData) {
  204. // $str=($limit-4000)/2000;
  205. $item=[];
  206. foreach($rowData['primary_key'] as $keyItem){
  207. $item[$keyItem[0]]=$keyItem[1];
  208. }
  209. foreach($rowData['attribute_columns'] as $colItem){
  210. $item[$colItem[0]]=$colItem[1];
  211. }
  212. $station_info=Db::table('station')->where('station_code',$item['StationCode'])->find();
  213. if($station_info['station_type']==3){
  214. $school_name=Db::table('departments')->where('id',$station_info['school_id'])->cache(60)->value('department_name');
  215. $build_name=Db::table('kq_building')->where('id',$station_info['building_id'])->cache(60)->value('name');
  216. $floor_name=Db::table('kq_floor')->where('id',$station_info['floor_id'])->cache(60)->value('name');
  217. $station_address=$school_name.' '.$build_name.' '.$floor_name.' '.$station_info['room_no'].'房间';
  218. $item['Address']=$station_address;
  219. }else{
  220. $item['Address']=$station_info['address'];
  221. }
  222. $item['PassTime']=date('Y-m-d H:i:s',$item['Timestamp']);
  223. // $item['Longitude']+=$str;
  224. // $item['Latitude']+=$str;
  225. $rows[]=$item;
  226. $limit --;
  227. // 处理每一行数据
  228. }
  229. $startPK = $response['next_start_primary_key'];
  230. // 如果 next_start_primary_key 不为空并且 limit > 0 则循环继续
  231. }
  232. // var_dump($rows);
  233. return CatchResponse::success($rows);
  234. }
  235. /**
  236. * 查询学生轨迹GPS
  237. * @time 2021年05月18日 10:15
  238. *
  239. */
  240. public function queryDeviceRouteGps(Request $request) : \think\Response
  241. {
  242. $params=$request->param();
  243. // var_dump($params);
  244. // var_dump(strtotime($params['aData']));
  245. $device_number = $params['imei'];
  246. if (!$device_number) {
  247. return CatchResponse::fail("获取设备号失败");
  248. }
  249. if($params['time']==0 && isset($params['time_range']) && $params['time_range'] != ''){
  250. $start_time=strtotime($params['time_range'][0]);
  251. $end_time=strtotime($params['time_range'][1]);
  252. }else{
  253. $start_time = time()-$params['time'];
  254. $end_time=time();
  255. }
  256. $options=array(
  257. 'device_number'=>$device_number,
  258. 'start_time'=>$start_time,
  259. 'end_time'=>$end_time,
  260. 'table_name'=>'wxt_route_gps'
  261. );
  262. $res = queryTableStoreGpsRoute($options);
  263. if($res['success']){
  264. $data=$res['data'];
  265. $battery_level=Db::table('devices')->where('imei',$device_number)->value('battery_level');
  266. foreach($data as &$val){
  267. $val['Longitude']=round($val['Longitude'],6);
  268. $val['Latitude']=round($val['Latitude'],6);
  269. $val['SignalType']='GPS';
  270. $val['BatteryLevel']=$battery_level;
  271. }
  272. return CatchResponse::success($data);
  273. }else{
  274. return CatchResponse::fail($res['message']);
  275. }
  276. }
  277. /**
  278. * 查询相关建筑信息
  279. * @time 2021年05月18日 10:15
  280. * @param Request $request
  281. */
  282. public function queryBuildingInfo(Request $request) : \think\Response
  283. {
  284. $params=$request->param();
  285. $building_id=Db::table('station')->where('station_mac',$params['station_mac'])->value('building_id');
  286. $build_info=Db::table('kq_building')->where('id',$building_id)->find();
  287. $student_info=Db::table('users')->where('active_rfid',$params['active_rfid'])->find();
  288. $res=array('build_info'=>$build_info,'student_info'=>$student_info);
  289. return CatchResponse::success($res);
  290. }
  291. /**
  292. * 查询告警相关围栏信息
  293. * @time 2021年05月18日 10:15
  294. * @param Request $request
  295. */
  296. public function queryAlarmFenceList(Request $request) : \think\Response
  297. {
  298. $params=$request->param();
  299. $record=$params['record'];
  300. $fence_list=array();
  301. $alarm_list=$record['alarm_reason_list'];
  302. foreach($alarm_list as $val){
  303. $fence_id=Db::table('alarm_report')
  304. ->where('device_number',$record['imei'])
  305. ->where('alarm_type',$val['value'])
  306. ->order('id', 'desc')
  307. ->value('fence_id');
  308. if($fence_id){
  309. $fence_info=$this->fencesModel->where('id',$fence_id)->append(['type_name','in_fence_time_area_text','out_fence_time_area_text','department_name','level'])->find();
  310. if($fence_info){
  311. //$fence_info['department_name']=Db::table('departments')->where('id',$fence_info['departments'])->value('department_name');
  312. $fence_list[]=$fence_info;
  313. }
  314. }
  315. }
  316. return CatchResponse::success($fence_list);
  317. }
  318. }