DeviceManagementController.class.php.bak.2021-08-27 13 KB

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