DeviceManagementController.class.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. <?php
  2. namespace Home\Controller;
  3. use Think\Controller;
  4. class DeviceManagementController extends ComController
  5. {
  6. //设备管理各类型设备统计
  7. public function getDeviceCount(){
  8. $Data=I('');
  9. $company_code=$Data['company_code'];
  10. $data=M()->query("select dwtype,count(*) as count from sp_owner where company='".$company_code."' group by dwtype order by dwtype");
  11. $types_count=[[
  12. "pass_on_device_count"=>0,
  13. "water_meter_device_count"=>0,
  14. "smoke_sensation_device_count"=>0,
  15. "fire_hydrant_device_count"=>0,
  16. "level_device_count"=>0,
  17. "rtu_device_count"=>0,
  18. "electrical_fire_device_count"=>0,
  19. "video_device_count"=>0,
  20. "elevator_device_count"=>0,
  21. "manhole_cover_device_count"=>0,
  22. "geomagnetism_device_count"=>0,
  23. "access_control_device_count"=>0,
  24. "gsa_device_count"=>0
  25. ]];
  26. if (!empty($data)){
  27. foreach ($data as $key=>$value){
  28. if ($data[$key]['dwtype']==1){
  29. $types_count[0]['pass_on_device_count']=(int)$data[$key]['count'];
  30. }elseif ($data[$key]['dwtype']==2){
  31. $types_count[0]['water_meter_device_count']=(int)$data[$key]['count'];
  32. }elseif ($data[$key]['dwtype']==3){
  33. $types_count[0]['smoke_sensation_device_count']=(int)$data[$key]['count'];
  34. }elseif ($data[$key]['dwtype']==4){
  35. $types_count[0]['fire_hydrant_device_count']=(int)$data[$key]['count'];
  36. }elseif ($data[$key]['dwtype']==5){
  37. $types_count[0]['level_device_count']=(int)$data[$key]['count'];
  38. }elseif ($data[$key]['dwtype']==6){
  39. $types_count[0]['rtu_device_count']=(int)$data[$key]['count'];
  40. }elseif ($data[$key]['dwtype']==7){
  41. $types_count[0]['electrical_fire_device_count']=(int)$data[$key]['count'];
  42. }elseif ($data[$key]['dwtype']==16){
  43. $types_count[0]['video_device_count']=(int)$data[$key]['count'];
  44. }elseif ($data[$key]['dwtype']==17){
  45. $types_count[0]['elevator_device_count']=(int)$data[$key]['count'];
  46. }elseif ($data[$key]['dwtype']==128){
  47. $types_count[0]['manhole_cover_device_count']=(int)$data[$key]['count'];
  48. }elseif ($data[$key]['dwtype']==129){
  49. $types_count[0]['geomagnetism_device_count']=(int)$data[$key]['count'];
  50. }elseif ($data[$key]['dwtype']==130){
  51. $types_count[0]['access_control_device_count']=(int)$data[$key]['count'];
  52. }elseif ($data[$key]['dwtype']==131){
  53. $types_count[0]['gsa_device_count']=(int)$data[$key]['count'];
  54. }
  55. }
  56. }else{
  57. $types_count[0]['pass_on_device_count']=0;
  58. $types_count[0]['water_meter_device_count']=0;
  59. $types_count[0]['smoke_sensation_device_count']=0;
  60. $types_count[0]['fire_hydrant_device_count']=0;
  61. $types_count[0]['level_device_count']=0;
  62. $types_count[0]['rtu_device_count']=0;
  63. $types_count[0]['electrical_fire_device_count']=0;
  64. $types_count[0]['video_device_count']=0;
  65. $types_count[0]['elevator_device_count']=0;
  66. $types_count[0]['manhole_cover_device_count']=0;
  67. $types_count[0]['geomagnetism_device_count']=0;
  68. $types_count[0]['access_control_device_count']=0;
  69. $types_count[0]['gsa_device_count']=0;
  70. }
  71. $res['msg'] = '操作成功';
  72. $res['flag'] = true;
  73. $res['data'] = $types_count;
  74. echo json_encode($res);
  75. }
  76. public function getDeviceList(){
  77. $Data=I('');
  78. // print_r($Data);
  79. $company_code=$Data['company_code'];
  80. $dwtype=$Data['dwtype'];
  81. $start_time = $Data['start_time'];
  82. $end_time = $Data['end_time'];
  83. $where='';
  84. if (!empty($start_time)){
  85. $where.=" and a.install_time>='".$start_time."' ";
  86. }
  87. if (!empty($end_time)){
  88. $where.=" and a.install_time<'".$end_time."' ";
  89. }
  90. if ($dwtype==1){
  91. $device_list=M()->query("SELECT a.id,a.owner_code,a.owner_name,a.dwtype,b.true_status,IF(b.true_status=0,'在线','离线') as device_state,a.install_time,a.unitinfo FROM sp_owner AS a JOIN sp_devices_status AS b ON a.owner_code=b.deviceid WHERE a.company='".$company_code."' AND a.dwtype=1 ".$where." order by a.id DESC");
  92. }else{
  93. $device_list=M()->query("SELECT a.id,a.owner_code,a.owner_name,a.dwtype,IF(timestampdiff(HOUR,max(`b`.`data_time`),now())<12,'在线','离线') AS device_state,a.install_time,a.unitinfo FROM sp_owner AS a left JOIN sp_owner_status AS b ON a.owner_code=b.device_id WHERE a.company='".$company_code."' AND a.dwtype=".$dwtype." ".$where." GROUP BY a.owner_code ORDER BY a.id DESC");
  94. }
  95. // print_r("SELECT a.id,a.owner_code,a.owner_name,a.dwtype,b.true_status,IF(b.true_status=0,'在线','离线') as device_state,a.install_time,a.unitinfo FROM sp_owner AS a JOIN sp_devices_status AS b ON a.owner_code=b.deviceid WHERE a.company='".$company_code."' AND a.dwtype=1 ".$where." order by a.id DESC");
  96. $res['msg'] = '操作成功';
  97. $res['flag'] = true;
  98. $res['data'] = $device_list;
  99. echo json_encode($res);
  100. }
  101. // public function getDeviceList(){
  102. // $Data=I('');
  103. // $company_code=$Data['company_code'];
  104. // $dwtype=$Data['dwtype'];
  105. // if ($dwtype==1){
  106. // $device_list=M()->query("SELECT a.id,a.owner_code,a.owner_name,a.dwtype,b.true_status,IF(b.true_status=0,'在线','离线') as device_state,a.install_time,a.unitinfo FROM sp_owner AS a JOIN sp_devices_status AS b ON a.owner_code=b.deviceid WHERE a.company='".$company_code."' AND a.dwtype=1 order by a.install_time DESC");
  107. // }else{
  108. // $device_list=M()->query("SELECT a.id,a.owner_code,a.owner_name,a.dwtype,IF(timestampdiff(HOUR,max(`b`.`data_time`),now())<12,'在线','离线') AS device_state,a.install_time,a.unitinfo FROM sp_owner AS a left JOIN sp_owner_status AS b ON a.owner_code=b.device_id WHERE a.company='".$company_code."' AND a.dwtype=".$dwtype." GROUP BY a.owner_code ORDER BY a.install_time DESC");
  109. // }
  110. //
  111. // $res['msg'] = '操作成功';
  112. // $res['flag'] = true;
  113. // $res['data'] = $device_list;
  114. // echo json_encode($res);
  115. // }
  116. public function getDeviceDetails(){
  117. $Data=I('');
  118. $id=$Data['id'];
  119. $device_details=M()->query("SELECT a.company,a.id,a.owner_code,a.owner_name,a.sim,a.unitinfo,a.install_time,b.owner_address,a.dwtype FROM sp_owner AS a JOIN sp_owner_company AS b ON a.company=b.owner_id WHERE a.id=".$id);
  120. switch ($device_details[0]['dwtype']) {
  121. case '1'://报警主机
  122. $device_details[0]['type_name']='报警主机';
  123. $misinformation=M()->query("SELECT count(*) as count FROM sp_hj2017 WHERE device_code='".$device_details[0]['owner_code']."' AND clwb=1");
  124. $real_alarm=M()->query("SELECT count(*) as count FROM sp_hj2017 WHERE device_code='".$device_details[0]['owner_code']."' AND clwb=0");
  125. $unprocessed=M()->query("SELECT count(*) as count FROM sp_hj2017 WHERE device_code='".$device_details[0]['owner_code']."' AND clzt=0;");
  126. $device_details[0]['misinformation']=(int)$misinformation[0]['count'];
  127. $device_details[0]['real_alarm']=(int)$real_alarm[0]['count'];
  128. $device_details[0]['unprocessed']=(int)$unprocessed[0]['count'];
  129. break;
  130. case '2'://水表
  131. $device_details[0]['type_name']='水表';
  132. $water_data=M()->query("select * from sp_owner_sync_data where device_id='".$device_details[0]['owner_code']."' and point_code=4 order by data_time desc");
  133. $battery_data=M()->query("select point_data,data_time from sp_owner_sync_data where device_id='".$device_details[0]['owner_code']."' and point_code=2 order by data_time desc");
  134. $signal_data=M()->query("select point_data from sp_owner_sync_data where device_id='".$device_details[0]['owner_code']."' and point_code=3 order by data_time desc");
  135. // print_r($water_data);
  136. foreach ($water_data as $key=>$value){
  137. $data[]=$value['point_data'];
  138. $time[]=$value['data_time'];
  139. }
  140. foreach ($battery_data as $key=>$value){
  141. $battery[]=$value['point_data'];
  142. }
  143. foreach ($signal_data as $key=>$value){
  144. $signal[]=$value['point_data'];
  145. }
  146. $device_details[0]['stiem']=$time;
  147. $device_details[0]['water_data']=$data;
  148. $device_details[0]['battery_data']=$battery;
  149. $device_details[0]['signal_data']=$signal;
  150. break;
  151. case '3'://烟感
  152. $device_details[0]['type_name']='烟感';
  153. $fume = M()->query("select RSRQ, DeviceStatus, BatteryLevel, storeTime, device_id from sp_y".$device_details[0]['owner_code']." ORDER BY storeTime DESC limit 1");
  154. if (empty($fume)){
  155. $device_details[0]['device_status']='';
  156. }else{
  157. if ($fume[0]['DeviceStatus']==7){
  158. $device_details[0]['device_status']='正常';
  159. }else if ($fume[0]['DeviceStatus']==5){
  160. $device_details[0]['device_status']='故障';
  161. }else if ($fume[0]['DeviceStatus']==4){
  162. $device_details[0]['device_status']='低压';
  163. }else if ($fume[0]['DeviceStatus']==2){
  164. $device_details[0]['device_status']='报警静音';
  165. }else if ($fume[0]['DeviceStatus']==1){
  166. $device_details[0]['device_status']='报警';
  167. }
  168. }
  169. $device_details[0]['battery_level']=$fume[0]['BatteryLevel'];
  170. $device_details[0]['rsrq']=$fume[0]['RSRQ'];
  171. $device_details[0]['stime']=$fume[0]['storeTime'];
  172. break;
  173. case '4'://消防栓
  174. $device_details[0]['type_name']='消防栓';
  175. $R=M()->query("select data4, data3, data2, data1, time, device_code from sp_d".$device_details[0]['owner_code']." ORDER BY time DESC limit 1");
  176. $device_details[0]['stime'];
  177. $device_details[0]['battery_level'];
  178. $device_details[0]['rsrq'];
  179. if ($R[0]['data1']=='WP1'){
  180. $device_details[0]['device_status']='低压';
  181. }elseif($R[0]['data1']=='WP2'){
  182. $device_details[0]['device_status']='高压';
  183. }elseif($R[0]['data1']=='WP3'){
  184. $device_details[0]['device_status']='故障';
  185. }else{
  186. $device_details[0]['device_status']=$R[0]['data1'];
  187. }
  188. $device_details[0]['device_data']=$R[0]['data1'];
  189. $device_details[0]['stime']=$R[0]['time'];
  190. break;
  191. case '5'://液位
  192. $device_details[0]['type_name']='液位';
  193. $water_data=M()->query("select * from sp_owner_sync_data where device_id='".$device_details[0]['owner_code']."' and point_code=4 order by data_time desc");
  194. foreach ($water_data as $value) {
  195. $time[]=$value['data_time'];
  196. $device_data[]=$value['point_data'];
  197. }
  198. $device_details[0]['stiem']=$time;
  199. $device_details[0]['water_data']=$device_data;
  200. break;
  201. case '6'://RTU
  202. $device_details[0]['type_name']='RTU';
  203. $rtu=M()->query("SELECT id,data4,ncmd,`time`,data1,data2,CASE WHEN `port`='E6' THEN '模拟量' WHEN `port`='E3' THEN '开关量' END as types
  204. FROM sp_d".$device_details[0]['owner_code']." WHERE id in(SELECT MAX(id)
  205. FROM sp_d".$device_details[0]['owner_code']." GROUP BY `port`,ncmd) AND ncmd !='' "
  206. .(($device_details[0]['owner_code']=='4023363030303635')?" AND (ncmd !='02' OR `port`!='E6') ":
  207. (($device_details[0]['owner_code']=='4023363030303639')?" AND (ncmd !='03' OR `port`!='E6') ":
  208. (($device_details[0]['owner_code']=='4023363030303434')?" AND (ncmd !='04' OR `port`!='E3') AND (ncmd !='05' OR `port`!='E3')":
  209. (($device_details[0]['owner_code']=='4023363030303336')? " AND (ncmd !='01' OR `port`!='E6') ":
  210. (($device_details[0]['owner_code']=='4023363030303733')?" AND (ncmd !='05' AND ncmd !='06' )":"")))))//山海大酒店B1楼水泵房02端口模拟量因设备故障,暂时屏蔽
  211. );
  212. foreach ($rtu as $key=>$value){
  213. if ($rtu[$key]['ncmd']=='01'|| $rtu[$key]['ncmd']=='02'|| $rtu[$key]['ncmd']=='03'|| $rtu[$key]['ncmd']=='04'|| $rtu[$key]['ncmd']=='05'
  214. ||$rtu[$key]['ncmd']=='06'||$rtu[$key]['ncmd']=='07'|| $rtu[$key]['ncmd']=='07'|| $rtu[$key]['ncmd']=='08'||$rtu[$key]['ncmd']=='09'){
  215. $rtu[$key]['ncmd']='0'.hexdec($rtu[$key]['ncmd']);
  216. }else{
  217. $rtu[$key]['ncmd']=hexdec($rtu[$key]['ncmd']);
  218. }
  219. }
  220. $device_details[0]['rtu_data']=$rtu;
  221. break;
  222. case '7'://电气火灾
  223. $device_details[0]['type_name']='电气火灾';
  224. $R=M()->query("select * from sp_owner_status where device_id='".$device_details[0]['owner_code']."'");
  225. $tiem=M()->query("select max(data_time) as time from sp_owner_status where device_id='".$device_details[0]['owner_code']."'");
  226. foreach ($R as $key=>$value){
  227. $R[$key]['time']=$tiem[0]['time'];
  228. }
  229. $device_details[0]['electric_data']=$R;
  230. break;
  231. case '16'://视频监测
  232. $device_details[0]['type_name']='视频监测';
  233. $video=M()->query("select data4,time from sp_video2017 where device_code='".$device_details[0]['owner_code']."' order by time desc limit 1");
  234. $device_details[0]['video_data']=$video;
  235. break;
  236. case '17'://电梯监测
  237. $device_details[0]['type_name']='电梯监测';
  238. $elevator=M()->query("select data4,time from sp_lifter2017 where device_code='".$device_details[0]['owner_code']."' order by time desc limit 1");
  239. $device_details[0]['elevator_data']=$elevator;
  240. break;
  241. case '128'://井盖
  242. $device_details[0]['type_name']='井盖监测';
  243. $MC=M()->query("select device_status,time from sp_iw2017 where device_code='".$device_details[0]['owner_code']."' order by time desc limit 1");
  244. if ($MC[0]['device_status']){
  245. $MC[0]['data4']='正常';
  246. }else{
  247. $MC[0]['data4']='打开告警';
  248. }
  249. $MC[0]['time']=$MC[0]['time'];
  250. $device_details[0]['mc_data']=$MC;
  251. break;
  252. case '129'://地磁
  253. $device_details[0]['type_name']='地磁';
  254. break;
  255. case '130'://门禁
  256. $device_details[0]['type_name']='门禁';
  257. break;
  258. case '131'://可燃气体
  259. $device_details[0]['type_name']='可燃气体';
  260. break;
  261. default:
  262. $res['msg'] = '操作失败,设备类型尚未定义,请联系管理员';
  263. $res['flag'] = 3002;
  264. echo json_encode($res);
  265. exit();
  266. break;
  267. }
  268. $res['msg'] = '操作成功';
  269. $res['flag'] = true;
  270. $res['data'] = $device_details;
  271. echo json_encode($res);
  272. }
  273. }