|
@@ -4,7 +4,13 @@ import (
|
|
|
"encoding/xml"
|
|
|
"fmt"
|
|
|
"github.com/sirupsen/logrus"
|
|
|
+ mail "github.com/xhit/go-simple-mail/v2"
|
|
|
+ "io"
|
|
|
"io/ioutil"
|
|
|
+ "lc-fangdaosha/model/app"
|
|
|
+ "lc-fangdaosha/service"
|
|
|
+ "log"
|
|
|
+ "mime/multipart"
|
|
|
"net/http"
|
|
|
"strings"
|
|
|
"time"
|
|
@@ -33,7 +39,7 @@ func handler(w http.ResponseWriter, r *http.Request) {
|
|
|
w.Header().Add("Date", time.Now().String())
|
|
|
w.Header().Add("Connection", "close")
|
|
|
}()
|
|
|
- //logRequest(r)
|
|
|
+ logRequest(r)
|
|
|
contentType := r.Header.Get("Content-Type")
|
|
|
if strings.Contains(contentType, "application/xml") {
|
|
|
bytes, err := ioutil.ReadAll(r.Body)
|
|
@@ -50,13 +56,91 @@ func handler(w http.ResponseWriter, r *http.Request) {
|
|
|
//处理事件 todo 邮箱
|
|
|
handleEvent_(event)
|
|
|
} else if strings.Contains(contentType, "multipart/form-data") {
|
|
|
- fmt.Println("multipart/form-data 事件")
|
|
|
- //handleMultipart(r)
|
|
|
+ handleMultipart(r)
|
|
|
} else {
|
|
|
logrus.WithField("Content-Type", contentType).Error("该Content-Type没有写处理逻辑")
|
|
|
return
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+//TODO 限制事件重复触发,一段时间内不重复发送邮件,但保持报警状态
|
|
|
+
|
|
|
+var eventService = service.ServiceGroupApp.AppServiceGroup.EventService
|
|
|
+var carInfoService = service.ServiceGroupApp.AppServiceGroup.CarInfoService
|
|
|
+
|
|
|
+// 处理多文件事件
|
|
|
+func handleMultipart(r *http.Request) {
|
|
|
+ // todo 远程联动
|
|
|
+ multipartReader := multipart.NewReader(r.Body, "boundary")
|
|
|
+ var msg string //邮件消息
|
|
|
+ // 循环读取每个 part
|
|
|
+ var eventAlert EventNotificationAlert
|
|
|
+ for {
|
|
|
+ part, err := multipartReader.NextPart()
|
|
|
+ if err == io.EOF {
|
|
|
+ break
|
|
|
+ }
|
|
|
+ if err != nil {
|
|
|
+ log.Println("Failed to read part:", err)
|
|
|
+ continue
|
|
|
+ }
|
|
|
+ // 检查 part 的 Content-Disposition
|
|
|
+ formName := part.FormName()
|
|
|
+ //if formName != "intrusionImage" {
|
|
|
+ if !strings.Contains(formName, "Image") {
|
|
|
+ //不含图片的xml部分数据
|
|
|
+ xmlData, err := ioutil.ReadAll(part)
|
|
|
+ if err != nil {
|
|
|
+ return
|
|
|
+ }
|
|
|
+ xml.Unmarshal(xmlData, &eventAlert)
|
|
|
+
|
|
|
+ msg = handleEvent_(eventAlert)
|
|
|
+ continue
|
|
|
+ }
|
|
|
+ //处理图片部分数据
|
|
|
+ contentType := part.Header.Get("Content-Type")
|
|
|
+ eventCode := part.Header.Get("Content-ID")
|
|
|
+ picName := timeFmt(eventAlert.DateTime) + ".jpeg"
|
|
|
+ data, _ := ioutil.ReadAll(part)
|
|
|
+ if strings.Contains(eventAlert.ChannelName, "PlateNumber") {
|
|
|
+ //todo 识别车牌,记录次数
|
|
|
+ number, err := CallLicensePlateRecognitionAPI(data)
|
|
|
+ if err != nil {
|
|
|
+ fmt.Println("车牌识别失败:", err)
|
|
|
+ //time.Sleep(1000)
|
|
|
+ }
|
|
|
+ //todo 处理车牌号
|
|
|
+ carInfoService.ProcessPlateNumber(number)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ f := &mail.File{
|
|
|
+ Name: picName,
|
|
|
+ MimeType: contentType,
|
|
|
+ Data: data,
|
|
|
+ Inline: true,
|
|
|
+ }
|
|
|
+ picture := &app.Picture{
|
|
|
+ Name: picName,
|
|
|
+ Time: time.Now(),
|
|
|
+ Mime: contentType,
|
|
|
+ Size: len(data),
|
|
|
+ }
|
|
|
+ pictureData := &app.PictureData{
|
|
|
+ Data: data,
|
|
|
+ }
|
|
|
+ event := &app.Event{
|
|
|
+ EventCode: eventCode,
|
|
|
+ MacAddress: eventAlert.MacAddress,
|
|
|
+ EventType: eventAlert.EventType,
|
|
|
+ }
|
|
|
+ //保存图片
|
|
|
+ go eventService.Save(event, picture, pictureData)
|
|
|
+ //邮件通知
|
|
|
+ SendAlarmEmail(eventAlert.MacAddress, msg, f)
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
func handleEvent_(event EventNotificationAlert) string {
|
|
|
var eType string
|
|
|
if event.EventType == "duration" {
|