RouteMap.php 12 KB

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