DeviceManagementController.class.php.bak.2021-12-15 15 KB

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