RtuwarnController.class.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <?php
  2. namespace Home\Controller;
  3. use Think\Controller;
  4. use Think\Cache\Driver;
  5. class RtuwarnController extends PushWarnController
  6. {
  7. public function message_rtu()
  8. {
  9. $Data = I();
  10. //日志打印
  11. $filename_log = "/var/www/html/jdxf/log/rtu_b_log" . date("Y-m-d") . ".log";
  12. $this->printing_log($Data, $filename_log);
  13. //需要用到的参数
  14. $device_code = $Data['id'];
  15. $unique_number = $Data['unique_number'] . $Data['types'];
  16. $insert_id = $Data['insert_id'];
  17. $remarks = $Data['bz'];
  18. $types = $Data['types'];
  19. $time = $Data['time'];
  20. if (empty($insert_id) || empty($device_code) || empty($types) || empty($unique_number)) {
  21. echo '缺少重要参数,请检查推送代码!';
  22. exit();
  23. }
  24. if ($types == '离线') {
  25. $where = " AND data2 like '%5%'";
  26. } else {
  27. $where = " AND data2 like '%4%'";
  28. }
  29. //3小时以内相同设备报警未处理则不推送;
  30. $redis = new \Redis();
  31. $result = $redis->connect('127.0.0.1', 6379);
  32. $if_push = 1;
  33. if ($result == 1) {
  34. $res = $redis->get("$unique_number");
  35. if ($res == 1) {
  36. $if_push = 1;
  37. } else if (!empty($res)) {
  38. if (floor((strtotime($Data['time']) - strtotime($res)) / 3600) > 3) {
  39. $if_push = 1;
  40. } else {
  41. $if_push = 0;
  42. }
  43. } else {
  44. $if_push = 1;
  45. }
  46. $redis->get("$unique_number");
  47. $redis->set("$unique_number", "$time");
  48. $redis->close();
  49. }
  50. $data = M()->query("select s.owner_name,s.unitinfo,s.company,c.owner_address from sp_owner s ,sp_owner_company c where s.company=c.owner_id and s.owner_code ='" . $device_code . "'");
  51. $union_sql = M()->query("select phone from sp_owner_phone WHERE owner_code='" . $data[0]['company'] . "' " . $where . " union select phone from sp_owner_phone WHERE owner_code in (select other_code from sp_owner_fujin where owner_code='" . $data[0]['company'] . "') " . $where);
  52. $openids = $this->array_column1($union_sql, "phone");
  53. if (empty($openids)) {
  54. echo 'open is null';
  55. exit;
  56. }
  57. $op = M()->query("SELECT openid FROM sp_wx_customuser WHERE type='wx' and idphone in(" . $openids . ")");
  58. $status11 = M()->query("SELECT statustime FROM sp_devices_status where deviceid='" . $device_code . "'");
  59. $access_token = GetAccess_Token1();
  60. $address = '警报地址:' . $data[0]['owner_address'] . $data[0]['unitinfo'];
  61. $type = $types;
  62. //keyword3.DATA
  63. if (empty($time)) {
  64. $time = $status11[0]['statustime'];
  65. }
  66. //remark.DATA
  67. if (empty($remarks)) {
  68. $remarks = '设备名称:' . $data[0]['owner_name'];
  69. }
  70. $url = "https://iot.usky.cn/jdxf/wxapp2.php/Home/Fireinfo/index/type/6/id/" . $insert_id;
  71. if ($if_push == 1) {
  72. foreach ($op as $key => $o) {
  73. if (!empty($o['openid'])) {
  74. $R = $this->SendDeviceAlarm($access_token, $o['openid'], $address, $device_code, $type, $time, $remarks, $url);
  75. $filename_log = "/var/www/html/jdxf/log/rtu_t_log" . date("Y-m-d") . ".log";
  76. $this->printing_log($R, $filename_log);
  77. print_r($R);
  78. }
  79. }
  80. }
  81. }
  82. }