Notify.php
11.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
<?php
/**
* Created by PhpStorm.
* User: ty01
* Date: 2022/3/25
* Time: 11:41
*/
namespace app\api\controller\haikang;
use app\api\controller\getui\JgPush;
use app\api\controller\tencentim\Openim;
use think\Controller;
use think\Db;
use Qiniu\Auth as QAuth;
use Qiniu\Storage\UploadManager;
//允许所有的跨域请求
header('Access-Control-Allow-Origin: *');
header("Access-Control-Allow-Headers: token,Origin, X-Requested-With, Content-Type, Accept");
header('Access-Control-Allow-Methods: POST,GET,OPTIONS');
header('Access-Control-Allow-Credentials: true');
header('Access-Control-Max-Age: 86400');
header('Content-type:text/html; Charset=utf-8');
date_default_timezone_set('PRC');
/**
* 海康威视回调
* Class Notify
* @package app\api\haikang
*
*/
class Notify extends Controller
{
protected $noNeedLogin = ['*'];
/**
* 接收海康威视回调12123
*/
public function info()
{
// file_put_contents("haikang.log", date("Y-m-d H:i:s") . PHP_EOL, FILE_APPEND);
$param = $this->request->param();
// file_put_contents("haikang.log", date("Y-m-d H:i:s") . json_encode($param, JSON_UNESCAPED_UNICODE) . PHP_EOL, FILE_APPEND);
// file_put_contents("haikang.log", "延时10秒开始:".date("Y-m-d H:i:s") . PHP_EOL, FILE_APPEND);
sleep(8);
//读取目录最新文件
// file_put_contents("haikang.log", "延时10秒结束:".date("Y-m-d H:i:s") . PHP_EOL, FILE_APPEND);
$res=Db::name("reservoir_hkws_hardware")->select();
foreach ($res as $k=>$v){
$this->readdir($res[$k]["indexCode"]);
}
// $file = $this->request->file("lineCrossImage");
// $name = $file->getInfo()['name'];
// file_put_contents("haikang.log", date("Y-m-d H:i:s") . $name . PHP_EOL, FILE_APPEND);
//
// $path = ROOT_PATH . "public" . DS . 'haikangpic';
// $info = $file->move($path,$name.".jpg");
//// rename($path.DS.date("Ymd").DS.$file->getSaveName(),$path.DS.date("Ymd").DS.$file->getSaveName().".jpg");
// file_put_contents("haikang.log", date("Y-m-d H:i:s") . $path.DS.$name.".jpg" . PHP_EOL, FILE_APPEND);
return "SUCCESS";
}
/**
* 读取目录最新文件
*/
public function readdir($cameraIndexCode = "")
{
if (empty($cameraIndexCode)) {
return "";
}
$dir = ROOT_PATH . 'public/' . $cameraIndexCode . '/';
$filearr = scandir($dir);//读取文件夹
unset($filearr[0]);// '.'
unset($filearr[1]);// '..'
$filearr = array_values($filearr);//所有文件名的一维数组
$newest_file = null;//最新写入的一个图片
$time = null;//每个图片日期如
$type = null;//文件名
$pic_date = null;//年-月-日 时:分:秒
if (!empty($filearr)) {
foreach ($filearr as &$file) {
//用文件名判断
$name = [];
$name = explode("_", $file);
if ($time === null || $name[2] > $time) {
$newest_file = $file;
$time = $name[2];
$pre = strlen($name[0] . "_" . $name[1] . "_" . $name[2] . "_");
$type = substr(explode(".", $file)[0], $pre);
$pic_date = substr($time, 0, 4) . "-" . substr($time, 4, 2) . "-" . substr($time, 6, 2) . " " . substr($time, 8, 2) . ":" . substr($time, 10, 2) . ":" . substr($time, 12, 2);
}
//用文件写入时间判断
// $filename = $dir . $file;
// dump(filectime($filename));
// if ($time === null || filectime($filename) > $time) {
// $newest_file = $filename;
// $time = filectime($filename);
// }
}
if (!empty($newest_file)) {
//有新预警图
//将图片上传到七牛云 然后删除本地图片
$domain = 'https://qiniu.ynzhsk.cn/';
$bucket = 'shuiku';
$auth = new QAuth('Eb_QX9JpdaILVbUx0ChfMfo0AFK-12J-eD0uVy25', 'VeqduMD6UzUvxkAiXc6e2N6w51N_KwppZXT3Kaix');
// 生成上传Token
$token = $auth->uploadToken($bucket);
// 构建 UploadManager 对象
$uploadMgr = new UploadManager();
$filepath = $dir . $newest_file;//文件物理路径
$fname = 'alarm/' . $cameraIndexCode . '/' . $newest_file;
list($ret, $err) = $uploadMgr->putFile($token, $fname, $filepath);
// $insert_data['filepath'] = $domain . $ret['key'];
//组装插入报警监测表数据
$sk_id = Db::name("reservoir_hkws_hardware")->where("indexCode", $cameraIndexCode)->value("reservoir_id");
$createtime = $pic_date ? strtotime($pic_date) : time();//创建时间
$insert_data['cameraIndexCode'] = $cameraIndexCode;
$insert_data['alarm_type'] = $type;//报警类型(LINE_CROSSING_DETECTION:越界侦测 REGION_ENTRANCE_DETECTION:进入区域 INTRUSION_DETECTION:区域入侵)
$insert_data['alarmimage'] = '/' . $ret['key'];//报警截图
$insert_data['reservoir_list_id'] = $sk_id;
$insert_data['createtime'] = $createtime;
$insert_data['status'] = "0";//处理状态(未处理/已处理)
//判断上一条数据是多久插入的(一分钟内同类型的,只推送一次,但是数据表记录还是会记录)
$last_deal_time = Db::name("reservoir_hkws_alarm_monitoring")->where(["cameraIndexCode"=>$cameraIndexCode,"alarm_type"=>$type])->order("createtime desc")->value("createtime");
$last_deal_time = intval($last_deal_time);
$alarm_id = Db::name("reservoir_hkws_alarm_monitoring")->insertGetId($insert_data);
if ($alarm_id) {
$path = '../public/' . $cameraIndexCode . '/' . $newest_file;
is_file($path) && unlink($path);//删除服务器上文件
$confirm_tuisong = 0;
// file_put_contents("kevin_alarm.log", "推不推送1?" . $last_deal_time . PHP_EOL, FILE_APPEND);
// file_put_contents("kevin_alarm.log", "推不推送2?" . time() . PHP_EOL, FILE_APPEND);
$pd = time()-$last_deal_time;
// file_put_contents("kevin_alarm.log", "推不推送4?" . $pd . PHP_EOL, FILE_APPEND);
if(empty($last_deal_time) || $pd>60){
//无同类型报警数据记录 或者 上次报警在一分钟以前 则可以推送
$confirm_tuisong = 1;
}
// file_put_contents("kevin_alarm.log", "推不推送4?" . $confirm_tuisong . PHP_EOL, FILE_APPEND);
//推送(大屏和监管APP)
if($confirm_tuisong==1){
//查下有哪些大屏账号(大屏登陆)所属部门是包含了这个报警水库的
//根据水库id查到部门id
$dpt_id = Db::name("reservoir_list")->where("id", $sk_id)->value("inspection_depart_id");
//根据部门id查到所有上级部门id集合
$pids = get_parent_id($dpt_id);//获取父级id(多个用“,”连接)
$pids = $pids ? $pids . "," . $dpt_id : $dpt_id;
//查询所有大屏账号
$w1 = [];
$w1["depart_id"] = ["IN", $pids];
$w1["is_screen_mng"] = ["=", "1"];
$w1["is_regulatory_mng"] = ["=", "0"];
$screen_staff_arr = Db::name("inspection_staff")->where($w1)->order("weigh asc,id asc")->column("id,user_id,mobile");
if (!empty($screen_staff_arr)) {
$mobile_arr = [];
$log_data = [];
foreach ($screen_staff_arr as $k => $v) {
$mobile_arr[] = $v['mobile'];
//存一下推送的未读状态
$log_data[] = [
"user_id" => $v['user_id'],
"reservoir_hkws_alarm_monitoring_id" => $alarm_id,
"is_read" => "0",
"createtime" => $createtime,
];
}
Db::name("reservoir_hkws_alarm_monitoring_read")->insertAll($log_data);
//推送IM消息给大屏
$openim_api = new Openim();
$param_data = [
"to_user_list" => $mobile_arr,
"text" => "您有新的报警信息待查看_" . $alarm_id
];
$res = $openim_api->batchsendmsg($param_data);
// file_put_contents("kevin_alarm.log", "大屏准备推送" . $alarm_id . "!!!" . implode(",", $mobile_arr) . "!!!" . json_encode($res, JSON_UNESCAPED_UNICODE) . PHP_EOL, FILE_APPEND);
}
//查下哪些监管账号(监管APP登陆)所属部门是包含了这个报警水库的
$w2 = [];
$w2["depart_id"] = ["IN", $pids];
$w2["is_screen_mng"] = ["=", "0"];
$w2["is_regulatory_mng"] = ["=", "1"];
$jg_staff_arr = Db::name("inspection_staff")->where($w2)->order("weigh asc,id asc")->column("user_id");
if (!empty($jg_staff_arr)) {
//unipush推送入侵报警至监管APP点击跳转实时监控详情
$jgPush = new JgPush();
$u_ids = implode(",", $jg_staff_arr);
$push_data = [
"alarm_id" => $alarm_id,
"u_ids" => $u_ids,
"indexCode" => $cameraIndexCode,
"name" => Db::name("reservoir_hkws_hardware")->where("indexCode", $cameraIndexCode)->value("name"),
"title" => "您有新的入侵报警消息,请及时查看",
"desc" => "你好,系统监测反馈有新的入侵报警,请及时查看并处理",
"jump_page" => "jump_to_rtmonitor_page",
];
$rr = $jgPush->pushAlarm($push_data);
// file_put_contents("kevin_alarm.log", "监管APP准备推送" . $alarm_id . "!!!" . $u_ids . "!!!" . json_encode($rr, JSON_UNESCAPED_UNICODE) . PHP_EOL, FILE_APPEND);
}
}
return $alarm_id;
}
}
}
// file_put_contents("kevin_alarm.log", "失败,无最新图片" . date("Y-m-d H:i:s") . PHP_EOL, FILE_APPEND);
}
}