123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177 |
- package eventServer
- import (
- "encoding/xml"
- "fmt"
- "github.com/sirupsen/logrus"
- mail "github.com/xhit/go-simple-mail/v2"
- "io"
- "io/ioutil"
- "lcfns/model/app"
- "lcfns/service"
- "log"
- "mime/multipart"
- "net/http"
- "strings"
- "time"
- )
- // MQDEvent (MQTT Data Event) 用于mqtt传输事件
- type MQDEvent struct {
- EventCode string `json:"eventCode"`
- EId int `json:"eId"`
- Ip string `json:"ip"`
- Type string `json:"type"`
- StartTime string `json:"startTime"`
- Name string `json:"name"`
- Data []byte `json:"data"`
- }
- func StartEventServer() {
- http.HandleFunc("/event", handler)
- logrus.Fatal("事件监听服务启动失败", http.ListenAndServe(":8850", nil))
- }
- func handler(w http.ResponseWriter, r *http.Request) {
- go func() {
- //监听主机应答固定,直接先应答
- w.WriteHeader(200)
- w.Header().Add("Date", time.Now().String())
- w.Header().Add("Connection", "close")
- }()
- //logRequest(r)
- contentType := r.Header.Get("Content-Type")
- if strings.Contains(contentType, "application/xml") {
- bytes, err := ioutil.ReadAll(r.Body)
- if err != nil {
- logrus.Error("事件处理-读取错误:", err)
- return
- }
- var event EventNotificationAlert
- err = xml.Unmarshal(bytes, &event)
- if err != nil {
- logrus.Error("事件处理-解析错误:", err)
- return
- }
- //处理事件 todo 邮箱
- fmt.Println("application/xml 事件")
- handleEvent_(event)
- } else if strings.Contains(contentType, "multipart/form-data") {
- fmt.Println("multipart/form-data 事件")
- handleMultipart(r)
- } else {
- logrus.WithField("Content-Type", contentType).Error("该Content-Type没有写处理逻辑")
- return
- }
- }
- //TODO 限制事件重复触发,一段时间内不重复发送邮件,但保持报警状态
- var eventService = service.ServiceGroupApp.AppServiceGroup.EventService
- // 处理多文件事件
- func handleMultipart(r *http.Request) {
- // todo 远程联动-ip音柱
- multipartReader := multipart.NewReader(r.Body, "boundary")
- var msg string //邮件消息
- // 循环读取每个 part
- var eventAlert EventNotificationAlert
- for {
- part, err := multipartReader.NextPart()
- //defer part.Close()
- if err == io.EOF {
- break
- }
- if err != nil {
- log.Println("Failed to read part:", err)
- continue
- }
- // 检查 part 的 Content-Disposition
- formName := part.FormName()
- //fmt.Println("formName", formName)
- if !strings.Contains(formName, "Image") {
- //不含图片的xml部分数据
- xmlData, err := ioutil.ReadAll(part)
- if err != nil {
- return
- }
- //fmt.Printf("xml:%s\n", string(xmlData))
- xml.Unmarshal(xmlData, &eventAlert)
- //event.Ip = eventAlert.IpAddress
- //event.Type = eventAlert.EventType
- //event.StartTime = eventAlert.DateTime
- msg = handleEvent_(eventAlert)
- continue
- }
- //fmt.Printf("eventAlert:%+v\n", eventAlert)
- //处理图片部分数据
- contentType := part.Header.Get("Content-Type")
- eventCode := part.Header.Get("Content-ID")
- picName := timeFmt(eventAlert.DateTime) + ".jpeg"
- //event.Name = picName
- data, _ := ioutil.ReadAll(part)
- 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,
- }
- fmt.Printf("事件信息:%+v\n", eventAlert)
- //保存图片
- go eventService.Save(event, picture, pictureData)
- //邮件通知
- SendAlarmEmail(eventAlert.MacAddress, msg, f)
- //utils.EmailPicture("1104038181@qq.com",
- // "防溺水通知",
- // "",
- // f)
- }
- }
- func handleEvent_(event EventNotificationAlert) string {
- var eType string
- if event.EventType == "duration" {
- eType = event.DurationList.Duration[0].RelationEvent
- } else {
- eType = event.EventType
- }
- return fmt.Sprintf("事件类型:%s,时间:%s", sMap[eType], timeFmt(event.DateTime))
- }
- func timeFmt(str string) string {
- s := strings.Split(str, "T")
- t := s[0]
- s1 := strings.Split(s[1], ".")
- s2 := strings.Split(s1[0], "+")
- return t + " " + s2[0]
- }
- func logRequest(req *http.Request) {
- // 打印请求行
- fmt.Printf("%s %s %s\n", req.Method, req.URL.Path, req.Proto)
- // 打印请求头
- for name, headers := range req.Header {
- for _, h := range headers {
- fmt.Printf("%v: %v\n", name, h)
- }
- }
- // 打印请求体
- body, _ := ioutil.ReadAll(req.Body)
- fmt.Printf("%s\n", body)
- }
|