ComprehensiveAlarmController.class.php.bak.2021-12-14 33 KB

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