ComprehensiveAlarmController.class.php 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850
  1. <?php
  2. namespace Home\Controller;
  3. use Think\Controller;
  4. include '../Common/function.php';
  5. include '../../../../USKYZHAF/Public/vendor/phpMQTT/phpMQTT.php';
  6. class ComprehensiveAlarmController extends ComController
  7. {
  8. //综合告警各类型数据统计
  9. public function getAlarmStatistics()
  10. {
  11. $Date = I();
  12. $company_code = $Date['company_code'];
  13. if (empty($company_code)) {
  14. $res['msg'] = '操作失败,缺少关键参数company_code';
  15. $res['flag'] = false;
  16. echo json_encode($res);
  17. exit();
  18. }
  19. $device_list = $this->getDeviceCode($company_code);
  20. $device_list1 = $this->getDeviceCode1($company_code);
  21. $where2['company_code'] = $company_code;
  22. $data_statistics = M('company_sync')->where($where2)->find();
  23. if(!empty($data_statistics)){
  24. //报警主机未处理数量统计
  25. $untreated_alarm_host_count=$data_statistics['device_01_alarm']-$data_statistics['device_01_alarm_confirm']+$data_statistics['device_01_out']-$data_statistics['device_01_out_confirm'];
  26. //消防水未处理数量统计
  27. $untreated_fire_water_count1=$data_statistics['device_02_alarm']+$data_statistics['device_00_alarm']-($data_statistics['device_02_alarm_confirm']+$data_statistics['device_00_alarm_confirm'])
  28. +$data_statistics['device_00_out']-$data_statistics['device_00_out_confirm']+$data_statistics['device_02_out']-$data_statistics['device_02_out_confirm'];
  29. //RTU未处理数量统计
  30. $untreated_rtu_count=$data_statistics['device_06_alarm'] - $data_statistics['device_06_alarm_confirm']+$data_statistics['device_06_out']-$data_statistics['device_06_out_confirm'];
  31. //电气火灾未处理数量统计
  32. $untreated_electrical_fire_count= $data_statistics['device_07_alarm'] - $data_statistics['device_07_alarm_confirm']+$data_statistics['device_07_out']-$data_statistics['device_07_out_confirm'];
  33. //视频监测未处理数量统计
  34. $untreated_video_monitoring_count=$data_statistics['device_16_alarm'] - $data_statistics['device_16_alarm_confirm']+$data_statistics['device_16_out']-$data_statistics['device_16_out_confirm'];
  35. $data[0]['untreated_alarm_host_count'] = (int)$untreated_alarm_host_count;
  36. $data[0]['untreated_fire_water_count'] = (int)$untreated_fire_water_count1;
  37. $data[0]['untreated_rtu_count'] = (int)$untreated_rtu_count;
  38. $data[0]['untreated_electrical_fire_count'] = (int)$untreated_electrical_fire_count;
  39. $data[0]['untreated_video_monitoring_count'] = (int)$untreated_video_monitoring_count;
  40. }else{
  41. $data[0]['untreated_alarm_host_count'] = 0;
  42. $data[0]['untreated_fire_water_count'] = 0;
  43. $data[0]['untreated_rtu_count'] = 0;
  44. $data[0]['untreated_electrical_fire_count'] = 0;
  45. $data[0]['untreated_video_monitoring_count'] = 0;
  46. }
  47. if (!empty($device_list)){
  48. // //报警主机未处理数量统计
  49. // $untreated_alarm_host_count = M()->query("select count(a.id) as untreated_alarm_host_count from sp_hj2017 as a join sp_owner as b on a.device_code=b.owner_code where a.clzt=0 and b.dwtype=1 and b.company='".$company_code."'");
  50. //// $untreated_alarm_host_count = M()->query("select count(*) as untreated_alarm_host_count from sp_hj2017 where clzt=0 and device_code in(" . $device_list . ")");
  51. // //消防水未处理数量统计
  52. //// $untreated_fire_water_count1 = M()->query("select count(*) as untreated_fire_water_count from sp_sj2017 where clzt=0 and device_code in(" . $device_list . ")");
  53. // $untreated_fire_water_count1 = M()->query("select count(a.id) as untreated_fire_water_count from sp_sj2017 as a join sp_owner as b on a.device_code=b.owner_code where a.clzt=0 and b.dwtype in(2,3) and b.company='".$company_code."'");
  54. //
  55. // //$untreated_fire_water_count = (int)$untreated_fire_water_count1[0]['untreated_fire_water_count'] - (int)$untreated_fire_hydrant_count[0]['untreated_fire_hydrant_count'];
  56. // //电气火灾未处理数量统计
  57. // $untreated_electrical_fire_count = M()->query("select count(*) as untreated_electrical_fire_count from sp_ef2017 where clzt=0 and device_code in(" . $device_list . ")");
  58. // //视频监测未处理数量统计
  59. // $untreated_video_monitoring_count = M()->query("select count(*) as untreated_video_monitoring_count from sp_video2017 where clzt=0 and device_code in(" . $device_list . ")");
  60. //井盖未处理数量统计
  61. $untreated_manhole_cover_count = M()->query("select count(*) as untreated_manhole_cover_count from sp_iw2017 where clzt=0 and device_code in(" . $device_list . ")");
  62. //门禁未处理数量统计
  63. $untreated_door_count = M()->query("select count(*) as untreated_door_count from sp_door2020 where clzt=0 and device_code in(" . $device_list . ")");
  64. //可燃气体未处理数量统计
  65. $untreated_gas_count = M()->query("select count(*) as untreated_gas_count from sp_gas2020 where clzt=0 and device_code in(" . $device_list . ")");
  66. //地磁未处理数量统计
  67. $untreated_geomagnetism_count = M()->query("select count(*) as untreated_geomagnetism_count from sp_dc2020 where clzt=0 and device_code in(" . $device_list . ")");
  68. // //RTU未处理数量统计
  69. // $untreated_rtu_count = M()->query("select count(*) as untreated_rtu_count from sp_rtu2017 where clzt=0 and device_code in(" . $device_list . ")");
  70. //电梯未处理数量统计
  71. $untreated_elevator_count = M()->query("select count(*) as untreated_elevator_count from sp_lifter2017 where clzt=0 and device_code in(" . $device_list . ")");
  72. // $data[0]['untreated_alarm_host_count'] = (int)$untreated_alarm_host_count[0]['untreated_alarm_host_count'];
  73. // $data[0]['untreated_fire_water_count'] = (int)$untreated_fire_water_count1[0]['untreated_fire_water_count'];
  74. // $data[0]['untreated_electrical_fire_count'] = (int)$untreated_electrical_fire_count[0]['untreated_electrical_fire_count'];
  75. // $data[0]['untreated_video_monitoring_count'] = (int)$untreated_video_monitoring_count[0]['untreated_video_monitoring_count'];
  76. $data[0]['untreated_manhole_cover_count'] = (int)$untreated_manhole_cover_count[0]['untreated_manhole_cover_count'];
  77. $data[0]['untreated_door_count'] = (int)$untreated_door_count[0]['untreated_door_count'];
  78. $data[0]['untreated_gas_count'] = (int)$untreated_gas_count[0]['untreated_gas_count'];
  79. $data[0]['untreated_geomagnetism_count'] = (int)$untreated_geomagnetism_count[0]['untreated_geomagnetism_count'];
  80. // $data[0]['untreated_rtu_count'] = (int)$untreated_rtu_count[0]['untreated_rtu_count'];
  81. $data[0]['untreated_elevator_count'] = (int)$untreated_elevator_count[0]['untreated_elevator_count'];
  82. }else{
  83. // $data[0]['untreated_alarm_host_count'] = 0;
  84. // $data[0]['untreated_fire_water_count'] = 0;
  85. // $data[0]['untreated_electrical_fire_count'] = 0;
  86. // $data[0]['untreated_video_monitoring_count'] = 0;
  87. $data[0]['untreated_manhole_cover_count'] = 0;
  88. $data[0]['untreated_door_count'] = 0;
  89. $data[0]['untreated_gas_count'] = 0;
  90. $data[0]['untreated_geomagnetism_count'] = 0;
  91. // $data[0]['untreated_rtu_count'] = 0;
  92. $data[0]['untreated_elevator_count'] = 0;
  93. }
  94. if (!empty($device_list1)){
  95. //消火栓未处理统计
  96. $untreated_fire_hydrant_count = M()->query("select count(*) as untreated_fire_hydrant_count from sp_sj2017 where clzt=0 and device_code in(" . $device_list1 . ")");
  97. $data[0]['untreated_fire_hydrant_count'] = (int)$untreated_fire_hydrant_count[0]['untreated_fire_hydrant_count'];
  98. }else{
  99. $data[0]['untreated_fire_hydrant_count'] = 0;
  100. }
  101. $data[0]['company_code'] = $company_code;
  102. $res['msg'] = '操作成功';
  103. $res['flag'] = true;
  104. $res['data'] = $data;
  105. echo json_encode($res);
  106. }
  107. public function getIntegratedAlarmList()
  108. {
  109. $Date = I();
  110. $company_code = $Date['company_code'];
  111. $type = $Date['type'];
  112. $processing_status = $Date['processing_status'];
  113. $start_time = $Date['start_time'];
  114. $end_time = $Date['end_time'];
  115. if (empty($company_code) && empty($type)) {
  116. $res['msg'] = '操作失败,缺少关键参数';
  117. $res['flag'] = false;
  118. echo json_encode($res);
  119. exit();
  120. }
  121. $where = '';
  122. if (!empty($start_time)) {
  123. $where .= " and a.time>='" . $start_time . "' ";
  124. }
  125. if (isset($processing_status)) {
  126. $where .= " and a.clzt='" . $processing_status . "' ";
  127. }
  128. if (!empty($start_time)) {
  129. $where .= " and a.time<'" . $end_time . "' ";
  130. }
  131. switch ($type) {
  132. case '1'://报警主机
  133. $table = 'sp_hj2017';
  134. $types = '1';
  135. break;
  136. case '2'://水表和液位
  137. $table = 'sp_sj2017';
  138. $types = '2,5';
  139. break;
  140. case '3'://烟感
  141. $table = 'sp_hj2017';
  142. $types = '3';
  143. break;
  144. case '4'://消防栓
  145. $table = 'sp_sj2017';
  146. $types = $type;
  147. break;
  148. case '5'://液位
  149. $table = 'sp_sj2017';
  150. $types = '5';
  151. break;
  152. case '6'://RTU
  153. $table = 'sp_rtu2017';
  154. $types = $type;
  155. break;
  156. case '7'://电气火灾
  157. $table = 'sp_ef2017';
  158. $types = $type;
  159. break;
  160. case '16'://视频监测
  161. $table = 'sp_video2017';
  162. $types = $type;
  163. break;
  164. case '17'://电梯监测
  165. $table = 'sp_lifter2017';
  166. $types = $type;
  167. break;
  168. case '128'://井盖
  169. $table = 'sp_iw2017';
  170. $types = $type;
  171. break;
  172. case '129'://地磁
  173. $table = 'sp_dc2020';
  174. $types = $type;
  175. break;
  176. case '130'://门禁
  177. $table = 'sp_door2020';
  178. $types = $type;
  179. break;
  180. case '131'://可燃气体
  181. $table = 'sp_gas2020';
  182. $types = $type;
  183. break;
  184. default:
  185. $res['msg'] = '操作失败,设备类型尚未定义,请联系管理员';
  186. $res['flag'] = 3002;
  187. echo json_encode($res);
  188. exit();
  189. break;
  190. }
  191. $start=($Date['page']-1)*$Date['pageSize'];
  192. if (!empty($Date['pageSize'])){
  193. $LIMIT=" LIMIT ".$start.",".$Date['pageSize'];
  194. }
  195. if ($type==1||$type==2||$type==6||$type==7||$type==16){
  196. $where2['company_code'] = $company_code;
  197. $data_statistics = M('company_sync')->where($where2)->find();
  198. if(!empty($data_statistics)){
  199. switch ($type) {
  200. case '1'://报警主机
  201. if ($processing_status==0){
  202. $alarm_count = $data_statistics['device_01_alarm']-$data_statistics['device_01_alarm_confirm']+$data_statistics['device_01_out']-$data_statistics['device_01_out_confirm'];
  203. }else{
  204. $alarm_count = $data_statistics['device_01_alarm_confirm']+$data_statistics['device_01_out_confirm'];
  205. }
  206. break;
  207. case '2'://水表和液位
  208. if ($processing_status==0){
  209. $alarm_count=$data_statistics['device_02_alarm']+$data_statistics['device_00_alarm']-($data_statistics['device_02_alarm_confirm']+$data_statistics['device_00_alarm_confirm'])
  210. +$data_statistics['device_00_out']-$data_statistics['device_00_out_confirm']+$data_statistics['device_02_out']-$data_statistics['device_02_out_confirm'];
  211. }else{
  212. $alarm_count=$data_statistics['device_02_alarm_confirm']+$data_statistics['device_00_alarm_confirm'] +$data_statistics['device_00_out_confirm']+$data_statistics['device_02_out_confirm'];
  213. }
  214. break;
  215. case '5'://液位和水表
  216. if ($processing_status==0){
  217. $alarm_count=$data_statistics['device_02_alarm']+$data_statistics['device_00_alarm']-($data_statistics['device_02_alarm_confirm']+$data_statistics['device_00_alarm_confirm'])
  218. +$data_statistics['device_00_out']-$data_statistics['device_00_out_confirm']+$data_statistics['device_02_out']-$data_statistics['device_02_out_confirm'];
  219. }else{
  220. $alarm_count=$data_statistics['device_02_alarm_confirm']+$data_statistics['device_00_alarm_confirm'] +$data_statistics['device_00_out_confirm']+$data_statistics['device_02_out_confirm'];
  221. }
  222. break;
  223. case '6'://RTU未处理数量统计
  224. if ($processing_status==0){
  225. $alarm_count=$data_statistics['device_06_alarm'] - $data_statistics['device_06_alarm_confirm']+$data_statistics['device_06_out']-$data_statistics['device_06_out_confirm'];
  226. }else{
  227. $alarm_count=$data_statistics['device_06_alarm_confirm']+$data_statistics['device_06_out_confirm'];
  228. }
  229. break;
  230. case '7'://电气火灾未处理数量统计
  231. if ($processing_status==0){
  232. $alarm_count= $data_statistics['device_07_alarm'] - $data_statistics['device_07_alarm_confirm']+$data_statistics['device_07_out']-$data_statistics['device_07_out_confirm'];
  233. }else{
  234. $data_statistics['device_07_alarm_confirm']+$data_statistics['device_07_out_confirm'];
  235. }
  236. break;
  237. case '16'://视频监测未处理数量统计
  238. if ($processing_status==0){
  239. $alarm_count=$data_statistics['device_16_alarm'] - $data_statistics['device_16_alarm_confirm']+$data_statistics['device_16_out']-$data_statistics['device_16_out_confirm'];
  240. }else{
  241. $alarm_count=$data_statistics['device_16_alarm_confirm']+$data_statistics['device_16_out_confirm'];
  242. }
  243. break;
  244. default:
  245. break;
  246. }
  247. }else{
  248. $alarm_count = 0;
  249. }
  250. }else{
  251. $alarm_list = M()->query("SELECT count(*) as alarm_count FROM " . $table . " AS a JOIN sp_owner AS b ON a.device_code=b.owner_code WHERE b.dwtype in(" . $types . ") AND b.company='" . $company_code . "' " . $where);
  252. $alarm_count = $alarm_list[0]['alarm_count'];
  253. }
  254. // $alarm_count = M()->query("SELECT count(*) as alarm_count FROM " . $table . " AS a JOIN sp_owner AS b ON a.device_code=b.owner_code WHERE b.dwtype in(" . $types . ") AND b.company='" . $company_code . "' AND clzt=" . $processing_status);
  255. if ($type==1){
  256. $alarm_list = M()->query("SELECT a.id,a.device_code,a.time,a.data1,a.data4,a.data2,a.clzt,b.company AS company_code,b.owner_name,b.unitinfo,b.dwtype as type FROM " . $table . " AS a JOIN sp_owner AS b ON a.device_code=b.owner_code WHERE b.company='" . $company_code . "' " . $where . " ORDER BY a.id DESC ".$LIMIT);
  257. }elseif ($type==2){
  258. $alarm_list = M()->query("SELECT a.id,a.device_code,a.time,a.data1,a.data4,a.data2,a.clzt,b.company AS company_code,b.owner_name,b.unitinfo,b.dwtype as type FROM " . $table . " AS a JOIN sp_owner AS b ON a.device_code=b.owner_code WHERE b.dwtype in(2,5) AND b.company='" . $company_code . "' " . $where . " ORDER BY a.id DESC ".$LIMIT);
  259. }else{
  260. $alarm_list = M()->query("SELECT a.id,a.device_code,a.time,a.data1,a.data4,a.data2,a.clzt,b.company AS company_code,b.owner_name,b.unitinfo,b.dwtype as type FROM " . $table . " AS a JOIN sp_owner AS b ON a.device_code=b.owner_code WHERE b.dwtype in(" . $types . ") AND b.company='" . $company_code . "' " . $where . " ORDER BY a.id DESC ".$LIMIT);
  261. }
  262. // $alarm_count =sizeof($alarm_list);
  263. if ($type == 2 || $type == 4) {
  264. foreach ($alarm_list as $key => $value) {
  265. switch ($alarm_list[$key]['data1']) {
  266. case 'WP1':
  267. $alarm_list[$key]['data4'] = '低压';
  268. break;
  269. case 'WP2':
  270. $alarm_list[$key]['data4'] = '高压';
  271. break;
  272. case 'WP3':
  273. $alarm_list[$key]['data4'] = '故障';
  274. break;
  275. case 'WP4':
  276. $alarm_list[$key]['data4'] = '离线';
  277. break;
  278. case 'LL1':
  279. $alarm_list[$key]['data4'] = '低水位';
  280. break;
  281. case 'LL2':
  282. $alarm_list[$key]['data4'] = '高水位';
  283. break;
  284. default:
  285. $alarm_list[$key]['data4'] = '其他' . $alarm_list[$key]['data1'];
  286. break;
  287. }
  288. }
  289. } else if ($type == 7) {
  290. foreach ($alarm_list as $key => $value) {
  291. if ($alarm_list[$key]['data1'] == 'EF1') {
  292. $alarm_list[$key]['data4'] = '欠压报警';
  293. } elseif ($alarm_list[$key]['data1'] == 'EF2') {
  294. $alarm_list[$key]['data4'] = '过压报警';
  295. } elseif ($alarm_list[$key]['data1'] == 'EF3') {
  296. $alarm_list[$key]['data4'] = '过流报警';
  297. } elseif ($alarm_list[$key]['data1'] == 'EF4') {
  298. $alarm_list[$key]['data4'] = '漏电报警';
  299. } elseif ($alarm_list[$key]['data1'] == 'EF5') {
  300. $alarm_list[$key]['data4'] = '回路1温度超限报警';
  301. } elseif ($alarm_list[$key]['data1'] == 'EF6') {
  302. $alarm_list[$key]['data4'] = '回路2温度超限报警';
  303. } elseif ($alarm_list[$key]['data1'] == 'EF7') {
  304. $alarm_list[$key]['data4'] = '回路3温度超限报警';
  305. } elseif ($alarm_list[$key]['data1'] == 'EF8') {
  306. $alarm_list[$key]['data4'] = '回路4温度超限报警';
  307. } elseif ($alarm_list[$key]['data1'] == 'EF9') {
  308. $alarm_list[$key]['data4'] = '离线';
  309. } else {
  310. $alarm_list[$key]['data4'] = '其他' . $alarm_list[$key]['data1'];
  311. }
  312. }
  313. }
  314. $res['msg'] = '操作成功';
  315. $res['flag'] = true;
  316. $res['alarm_count'] = (int)$alarm_count;
  317. $res['data'] = $alarm_list;
  318. echo json_encode($res);
  319. }
  320. public function getAlarmDetails()
  321. {
  322. $Date = I();
  323. // print_r($Date);
  324. $id = $Date['id'];
  325. $type = $Date['type'];
  326. // $processing_status = $Date['processing_status'];
  327. if (empty($id) && empty($type)) {
  328. $res['msg'] = '操作失败,缺少关键参数';
  329. $res['flag'] = 3001;
  330. echo json_encode($res);
  331. exit();
  332. }
  333. switch ($type) {
  334. case '1'://报警主机
  335. $table = 'sp_hj2017';
  336. $types = '1';
  337. break;
  338. case '2'://水表和液位
  339. $table = 'sp_sj2017';
  340. $types = '2,';
  341. break;
  342. case '3'://烟感
  343. $table = 'sp_hj2017';
  344. $types = '3';
  345. break;
  346. case '4'://消防栓
  347. $table = 'sp_sj2017';
  348. $types = $type;
  349. break;
  350. case '5'://液位
  351. $table = 'sp_sj2017';
  352. $types = '5';
  353. break;
  354. case '6'://RTU
  355. $table = 'sp_rtu2017';
  356. $types = $type;
  357. break;
  358. case '7'://电气火灾
  359. $table = 'sp_ef2017';
  360. $types = $type;
  361. break;
  362. case '16'://视频监测
  363. $table = 'sp_video2017';
  364. $types = $type;
  365. break;
  366. case '17'://电梯监测
  367. $table = 'sp_lifter2017';
  368. $types = $type;
  369. break;
  370. case '128'://井盖
  371. $table = 'sp_iw2017';
  372. $types = $type;
  373. break;
  374. case '129'://地磁
  375. $table = 'sp_dc2020';
  376. $types = $type;
  377. break;
  378. case '130'://门禁
  379. $table = 'sp_door2020';
  380. $types = $type;
  381. break;
  382. case '131'://可燃气体
  383. $table = 'sp_gas2020';
  384. $types = $type;
  385. break;
  386. default:
  387. $res['msg'] = '操作失败,设备类型尚未定义,请联系管理员';
  388. $res['flag'] = 3002;
  389. echo json_encode($res);
  390. exit();
  391. break;
  392. }
  393. if ($type == 128){
  394. $list = M()->query("SELECT a.device_status,a.id,a.port,a.device_code,b.owner_name as device_name,a.data1,a.data4,a.data2,a.data3,a.data5,a.clwb,a.clzt,a.clr,a.clnr,b.company AS company_code,a.time,b.unitinfo,b.dwtype,a.ncmd,a.clsj FROM " . $table . " AS a JOIN sp_owner AS b ON a.device_code=b.owner_code WHERE a.id=" . $id);
  395. }elseif($type == 130||$type == 131){
  396. $list = M()->query("SELECT a.id,a.device_code,b.owner_name as device_name,a.data1,a.data4,a.data2,a.data3,a.data5,a.clwb,a.clzt,a.clr,a.clnr,b.company AS company_code,a.time,b.unitinfo,b.dwtype,a.ncmd,a.clsj FROM " . $table . " AS a JOIN sp_owner AS b ON a.device_code=b.owner_code WHERE a.id=" . $id);
  397. }else{
  398. // print_r("SELECT a.id,a.port,a.device_code,b.owner_name as device_name,a.data1,a.data4,a.data2,a.data3,a.data5,a.clwb,a.clzt,a.clr,a.clnr,b.company AS company_code,a.time,b.unitinfo,b.dwtype,a.ncmd,a.clsj FROM " . $table . " AS a JOIN sp_owner AS b ON a.device_code=b.owner_code WHERE a.id=" . $id);
  399. $list = M()->query("SELECT a.id,a.port,a.device_code,b.owner_name as device_name,a.data1,a.data4,a.data2,a.data3,a.data5,a.clwb,a.clzt,a.clr,a.clnr,b.company AS company_code,a.time,b.unitinfo,b.dwtype,a.ncmd,a.clsj FROM " . $table . " AS a JOIN sp_owner AS b ON a.device_code=b.owner_code WHERE a.id=" . $id);
  400. }
  401. if($type == 1){
  402. $list[0]['alarm_information'] = $list[0]['data2'];
  403. }elseif ($type == 2 || $type == 4) {
  404. switch ($list[0]['data1']) {
  405. case 'WP1':
  406. $list[0]['alarm_information'] = '低压';
  407. break;
  408. case 'WP2':
  409. $list[0]['alarm_information'] = '高压';
  410. break;
  411. case 'WP3':
  412. $list[0]['alarm_information'] = '故障';
  413. break;
  414. case 'WP4':
  415. $list[0]['alarm_information'] = '离线';
  416. break;
  417. case 'LL1':
  418. $list[0]['alarm_information'] = '低水位';
  419. break;
  420. case 'LL2':
  421. $list[0]['alarm_information'] = '高水位';
  422. break;
  423. default:
  424. $list[0]['alarm_information'] = '其他' . $list[0]['data1'];
  425. break;
  426. }
  427. if ($list[0]['dwtype'] == 5) {
  428. $list[0]['numerical_value'] = '水位值:' . $list[0]['data4'] . 'M';
  429. } else {
  430. $list[0]['numerical_value'] = '水压值:' . $list[0]['data4'] . 'Mpa';
  431. }
  432. }else if ($type == 6){
  433. $list[0]['alarm_information'] =$list[0]['data2'];
  434. if ($list[0]['port'] == 'E6') {
  435. if ($list[0]['data2'] == '低压' || $list[0]['data2'] == '高压') {
  436. $list[0]['numerical_value'] = '水压值:' . $list[0]['data1'] . 'Mpa';
  437. } elseif ($list[0]['data2'] == '低水位' || $list[0]['data2'] == '高水位') {
  438. $list[0]['numerical_value'] = '水位:' . $list[0]['data1'] . 'M';
  439. } elseif ($list[0]['data2'] == '低温' || $list[0]['data2'] == '高温') {
  440. $list[0]['numerical_value'] = '温度:' . $list[0]['data1'] . '℃';
  441. } elseif ($list[0]['data2'] == '低湿度' || $list[0]['data2'] == '高湿度') {
  442. $list[0]['numerical_value'] = '湿度:' . $list[0]['data1'] . '%';
  443. }else if ($list[0]['data2'] == '设备异常'){
  444. $list[0]['numerical_value'] = $list[0]['data4'].':'.$list[0]['data1'];
  445. } else {
  446. $list[0]['numerical_value'] = $list[0]['data4'];
  447. }
  448. } else {
  449. $list[0]['numerical_value'] = $list[0]['data4'];
  450. }
  451. }else if ($type == 7) {
  452. if ($list[0]['data1'] == 'EF1') {
  453. $list[0]['alarm_information'] = '欠压报警';
  454. $list[0]['numerical_value'] = 'A相欠压值:' . $list[0]['data2'] . 'V B相欠压值:' . $list[0]['data3'] . 'V C相欠压值:' . $list[0]['data4'] . 'V';
  455. } elseif ($list[0]['data1'] == 'EF2') {
  456. $list[0]['alarm_information'] = '过压报警';
  457. $list[0]['numerical_value'] = 'A相过压值:' . $list[0]['data2'] . 'V B相过压值:' . $list[0]['data3'] . 'V C相过压值:' . $list[0]['data4'] . 'V';
  458. } elseif ($list[0]['data1'] == 'EF3') {
  459. $list[0]['alarm_information'] = '过流报警';
  460. $list[0]['numerical_value'] = 'A相过流值:' . $list[0]['data2'] . 'mA B相过流值:' . $list[0]['data3'] . 'mA C相过流值:' . $list[0]['data4'] . 'mA';
  461. } elseif ($list[0]['data1'] == 'EF4') {
  462. $list[0]['alarm_information'] = '漏电报警';
  463. $list[0]['numerical_value'] = '漏电流值:' . $list[0]['data2'] . 'mA';
  464. } elseif ($list[0]['data1'] == 'EF5') {
  465. $list[0]['alarm_information'] = '回路1温度超限报警';
  466. $list[0]['numerical_value'] = '温度1值:' . $list[0]['data2'] . '℃';
  467. } elseif ($list[0]['data1'] == 'EF6') {
  468. $list[0]['alarm_information'] = '回路2温度超限报警';
  469. $list[0]['numerical_value'] = '温度2值:' . $list[0]['data2'] . '℃';
  470. } elseif ($list[0]['data1'] == 'EF7') {
  471. $list[0]['alarm_information'] = '回路3温度超限报警';
  472. $list[0]['numerical_value'] = '温度3值:' . $list[0]['data2'] . '℃';
  473. } elseif ($list[0]['data1'] == 'EF8') {
  474. $list[0]['alarm_information'] = '回路4温度超限报警';
  475. $list[0]['numerical_value'] = '温度4值:' . $list[0]['data2'] . '℃';
  476. } elseif ($list[0]['data1'] == 'EF9') {
  477. $list[0]['alarm_information'] = '离线';
  478. $list[0]['numerical_value'] = '设备离线';
  479. } else {
  480. $list[0]['alarm_information'] = '其他' . $list[0]['data1'];
  481. }
  482. }elseif($type == 128){
  483. if ($list[0]['device_status']==1){
  484. $list[0]['alarm_information'] = "设备异常";
  485. }else{
  486. $list[0]['alarm_information'] = "设备正常";
  487. }
  488. }else{
  489. $list[0]['alarm_information'] = $list[0]['data4'];
  490. }
  491. $res['msg'] = '操作成功';
  492. $res['flag'] = true;
  493. $res['data'] = $list;
  494. echo json_encode($res);
  495. }
  496. public function setAlarmHandling(){
  497. $Data = I();
  498. $id = $Data['id'];
  499. $type = $Data['type'];
  500. $device_code = $Data['device_code'];
  501. $if_batch = $Data['if_batch'];
  502. $clwb = $Data['clwb'];
  503. $clnr = $Data['clnr'];
  504. $clr = $_SESSION['phone'];
  505. $port = $Data['port'];
  506. $Source_code=$Data['ncmd'];
  507. if (empty($id) && empty($type)&& empty($device_code)&& empty($clwb)&& empty($Data['data2'])&& empty($Data['data5'])&& empty($Data['data1'])&& empty($Source_code)) {
  508. $res['msg'] = '操作失败,缺少关键参数';
  509. $res['flag'] = false;
  510. echo json_encode($res);
  511. exit();
  512. }
  513. switch ($type) {
  514. case '1'://报警主机
  515. $table = 'sp_hj2017';
  516. $Tabs = M('hj2017');
  517. $types = '1';
  518. // $where['data2'] = $Data['data2'];
  519. $where['data3'] = $Data['data3'];
  520. $where['data5'] = $Data['data5'];
  521. break;
  522. case '2'://水表和液位
  523. $table = 'sp_sj2017';
  524. $Tabs = M('sj2017');
  525. $types = '2';
  526. $where['data1'] = $Data['data1'];
  527. break;
  528. case '3'://烟感
  529. $table = 'sp_hj2017';
  530. $Tabs = M('hj2017');
  531. $types = '3';
  532. $where['data2'] = $Data['data2'];
  533. break;
  534. case '4'://消防栓
  535. $table = 'sp_sj2017';
  536. $Tabs = M('sj2017');
  537. $types = $type;
  538. break;
  539. case '5'://液位
  540. $table = 'sp_sj2017';
  541. $Tabs = M('sj2017');
  542. $types = '5';
  543. break;
  544. case '6'://RTU
  545. $table = 'sp_rtu2017';
  546. $Tabs = M('rtu2017');
  547. $types = $type;
  548. $where['ncmd'] = $Data['ncmd'];
  549. $where['data2'] = $Data['data2'];
  550. $where['data4'] = $Data['data4'];
  551. break;
  552. case '7'://电气火灾
  553. $table = 'sp_ef2017';
  554. $Tabs = M('ef2017');
  555. $types = $type;
  556. $where['data1'] = $Data['data1'];
  557. break;
  558. case '16'://视频监测
  559. $table = 'sp_video2017';
  560. $Tabs = M('video2017');
  561. $types = $type;
  562. break;
  563. case '17'://电梯监测
  564. $table = 'sp_lifter2017';
  565. $Tabs = M('lifter2017');
  566. $types = $type;
  567. break;
  568. case '128'://井盖
  569. $table = 'sp_iw2017';
  570. $Tabs = M('iw2017');
  571. $types = $type;
  572. break;
  573. case '129'://地磁
  574. $table = 'sp_dc2020';
  575. $Tabs = M('dc2020');
  576. $types = $type;
  577. break;
  578. case '130'://门禁
  579. $table = 'sp_door2020';
  580. $Tabs = M('door2020');
  581. $types = $type;
  582. break;
  583. case '131'://可燃气体
  584. $table = 'sp_gas2020';
  585. $Tabs = M('gas2020');
  586. $types = $type;
  587. break;
  588. default:
  589. $res['msg'] = '操作失败,设备类型尚未定义,请联系管理员';
  590. $res['flag'] = false;
  591. echo json_encode($res);
  592. exit();
  593. break;
  594. }
  595. //修改条件
  596. $data['clzt']=1;
  597. $data['clwb']=$clwb;
  598. $data['clnr']=$clnr;
  599. $data['cldh']=$clr;
  600. $data['clr']=$clr;
  601. $data['clsj']=date("Y-m-d H:i:s");
  602. $types_data=M()->query("select dwtype,company from sp_owner where owner_code='".$Data['device_code']."'");
  603. $typess=$types_data[0]['dwtype'];
  604. $company_data=ytapi_query("select * from corps_company where company_code='".$types_data[0]['company']."' and status=1");
  605. if (!empty($company_data)){
  606. switch ($types_data[0]['dwtype']) {
  607. case '1':
  608. $RESS = M()->query("select data3,data5 from sp_hj2017 where id=".$id);
  609. $res_id = M()->query("select id,data3,data4 from sp_hj2017 where device_code='" . $Data['device_code'] . "' and data3='" . $RESS[0]['data3'] . "' and data5='" . $RESS[0]['data5'] . "' and clzt=0");
  610. break;
  611. case '2':
  612. case '5':
  613. $RESS = M()->query("select data1 from sp_sj2017 where id=".$id);
  614. $res_id = M()->query("select id from sp_sj2017 where device_code='" . $Data['device_code'] . "' and data1='" . $RESS[0]['data1'] . "' and clzt=0");
  615. break;
  616. case '3':
  617. $RESS = M()->query("select data2 from sp_hj2017 where id=".$id);
  618. $res_id = M()->query("select id from sp_hj2017 where device_code='" . $Data['device_code'] . "' and data2='" . $RESS[0]['data2'] . "' and clzt=0");
  619. break;
  620. case '7':
  621. $RESS = M()->query("select data2 from sp_ef2017 where id=".$id);
  622. $res_id = M()->query("select * from sp_ef2017 where device_code='" . $Data['device_code'] . "' and data1='" . $RESS[0]['data1'] . "' and clzt=0");
  623. break;
  624. default:
  625. $cs66 = '';
  626. break;
  627. }
  628. if (!empty($res_id)){
  629. foreach ($res_id as $key=>$value){
  630. $cs66[$key] = [
  631. 'SubType' => $types_data[0]['dwtype'],
  632. 'DeviceId' => $Data['device_code'],
  633. 'InsertId' => $res_id[$key]['id'],
  634. 'Confirmed' => true,
  635. ];
  636. }
  637. }
  638. }
  639. //告警处理
  640. if ($if_batch==1){//批量处理
  641. $where['device_code']=$device_code;
  642. $where['clzt']=0;
  643. $R=$Tabs->where($where)->save($data);
  644. }else{
  645. $where['id']=$id;
  646. $R=$Tabs->where($where)->save($data);
  647. }
  648. $url1 = 'http://47.98.201.187:55335/report';
  649. if ($if_batch==1){//批量个处理
  650. //总队推送
  651. if ($typess==1||$typess==2||$typess==3||$typess==7||$typess==5){
  652. if (!empty($cs66)){
  653. foreach ($cs66 as $key=>$value){
  654. $cs88 = json_encode($cs66[$key]);
  655. http_post_advertise($url1, $cs88);
  656. }
  657. }
  658. }
  659. //Redis储存
  660. if ($typess==1){
  661. if (!empty($res_id[0]['data4'])){
  662. $port=mb_substr($res_id[0]['data4'],4,1,'utf-8');
  663. // $port=substr($res_id[0]['data4'],3,1);
  664. }else{
  665. $port='';
  666. }
  667. if (!empty($res_id[0]['data3'])){
  668. $Source_code=str_replace(',','',$res_id[0]['data3']);
  669. }else{
  670. $Source_code='';
  671. }
  672. $unique_number=$Data['device_code'].$port.$Source_code;
  673. }elseif ($typess==2){
  674. $unique_number=$Data['device_code'];
  675. }elseif ($typess==3){
  676. $unique_number=$Data['device_code'];
  677. }elseif ($typess==6){
  678. $unique_number=$Data['device_code'].$port.$Source_code.$Data['data4'].' '.$Data['data2'];
  679. }elseif ($typess==7){
  680. $port=$res_id[0]['port'];
  681. $unique_number=$Data['device_code'].$port;
  682. }else{
  683. $unique_number=$Data['device_code'];
  684. }
  685. $this->setRedisData($unique_number);
  686. }else{//单个处理
  687. //消防总队推送
  688. if (!empty($company_data)){
  689. if ($typess == 1 || $typess == 2 || $typess == 3 || $typess == 7|| $typess == 5|| $typess == 6) {
  690. $cs = [
  691. 'SubType' => $typess,
  692. 'DeviceId' => $Data['device_code'],
  693. 'InsertId' => $Data['id'],
  694. 'Confirmed' => true,
  695. ];
  696. $cs3 = json_encode($cs);
  697. http_post_advertise($url1, $cs3);
  698. }
  699. }
  700. //MQTT推送
  701. if ($typess == 2) {
  702. $MQTT_RESS = M("sj2017")->where("id",$id)->find();
  703. switch ($MQTT_RESS['data1']) {
  704. case 'WP1':
  705. $ftypewtype='低压';
  706. break;
  707. case 'WP2':
  708. $ftypewtype='高压';
  709. break;
  710. }
  711. if ($ftypewtype=='低压'||$ftypewtype=="高压"){
  712. $device = ytapi_query("SELECT * FROM yt_t_mhwater WHERE device_code='".$Data['device_code']."' AND push_status=1");
  713. if (!empty($device)){
  714. $tiem_data = date('Y-m-d H:i:s', time());
  715. $json_data_four=$this->date_array_turn_json($Data['id'],$device[0],strtotime($tiem_data),$ftypewtype,1,'','');
  716. if (!empty($json_data_four)){
  717. $this->message_push_mh("mh/water/statistics",$json_data_four,0);
  718. }
  719. }
  720. }
  721. }
  722. //Redis储存
  723. if ($typess==1){
  724. $res_id = M()->query("select data3,data4 from sp_hj2017 where id=" . $Data['id']);
  725. if (!empty($res_id[0]['data4'])){
  726. $port=mb_substr($res_id[0]['data4'],4,1,'utf-8');
  727. // $port=substr($res_id[0]['data4'],3,1);
  728. }else{
  729. $port='';
  730. }
  731. if (!empty($res_id[0]['data3'])){
  732. $Source_code=str_replace(',','',$res_id[0]['data3']);
  733. }else{
  734. $Source_code='';
  735. }
  736. $unique_number=$Data['device_code'].$port.$Source_code;
  737. }else if ($typess==6){
  738. $port=$Data['port'];
  739. $Source_code=$Data['ncmd'];
  740. $unique_number=$Data['device_code'].$port.$Source_code.$Data['data4'].' '.$Data['data2'];
  741. }else if ($typess==7){
  742. $res_id = M()->query("select port from sp_ef2017 where id=" . $Data['id']);
  743. $port=$res_id[0]['port'];
  744. $unique_number=$Data['device_code'].$port;
  745. }else{
  746. $unique_number=$Data['device_code'];
  747. }
  748. $this->setRedisData($unique_number);
  749. }
  750. if ($R){
  751. $res['msg'] = '操作成功';
  752. $res['flag'] = true;
  753. echo json_encode($res);
  754. }else{
  755. $res['msg'] = '操作失败';
  756. $res['flag'] = false;
  757. echo json_encode($res);
  758. }
  759. }
  760. //Redis
  761. public function setRedisData($unique_number){
  762. //redis储存处理的设备
  763. $redis = new \Redis();
  764. $result = $redis->connect('47.98.201.73', 6379);
  765. $redis->auth("usky2021");
  766. if ($result==1){
  767. $redis->set("$unique_number",1);
  768. $redis->close();
  769. }
  770. }
  771. //MQTT
  772. public function message_push_mh($topic,$msg,$qos){
  773. // vendor('phpMQTT.phpMQTT');
  774. $server = '47.98.201.73'; // 服务器IP
  775. $port = 1883; // 服务器端口
  776. $username = 'usky'; // 用户名
  777. $password = 'usky'; // 密码
  778. $client_id = 'pub_' . uniqid();
  779. $mqtt = new \phpMQTT($server, $port, $client_id);
  780. if ($mqtt->connect(true, NULL, $username, $password)) {
  781. $mqtt->publish($topic, $msg, $qos);
  782. usleep(100000);
  783. $mqtt->close();
  784. // Log::record('MH—4G-MQTT-Push success!('.$topic.')['.date('Y-m-d H:i:s').']:'.json_encode($msg));
  785. } else {
  786. // Log::record('MH—4G-MQTT-Time out!('.$topic.')['.date('Y-m-d H:i:s').']:'.json_encode($msg));
  787. }
  788. }
  789. //获取消防栓设备编号
  790. public function getDeviceCode1($company)
  791. {
  792. $device = M()->query("SELECT o.owner_code,c.owner_name FROM sp_owner o join sp_owner_company c ON o.company =c.owner_id WHERE o.dwtype=4 and o.company in(".$company.")");
  793. $str = "";
  794. foreach ($device as $val) {
  795. $str .= "'" . $val['owner_code'] . "',";
  796. }
  797. $newstr = substr($str, 0, strlen($str) - 1);
  798. return $newstr;
  799. }
  800. }