154 lines
4.8 KiB
Go
154 lines
4.8 KiB
Go
package ws2
|
||
|
||
import (
|
||
"encoding/json"
|
||
"errors"
|
||
"fmt"
|
||
"github.com/gogf/gf/v2/frame/g"
|
||
"github.com/tiger1103/gfast/v3/api/constant"
|
||
"github.com/tiger1103/gfast/v3/api/video_hat/alarm"
|
||
"io/ioutil"
|
||
"log"
|
||
"net/http"
|
||
"strconv"
|
||
"time"
|
||
)
|
||
|
||
type CaInfo struct {
|
||
UserID string `json:"user_id"`
|
||
DeviceID string `json:"device_id"`
|
||
CaVer string `json:"ca_ver"`
|
||
Mobile string `json:"mobile"`
|
||
SIM string `json:"sim"`
|
||
Password string `json:"pwd"`
|
||
CTime string `json:"c_time"`
|
||
AppLastLoginTime string `json:"app_last_login_time"`
|
||
CaLastLoginTime string `json:"ca_last_login_time"`
|
||
UserName string `json:"user_name"`
|
||
RealName string `json:"real_name"`
|
||
UserImg string `json:"user_img"`
|
||
Department string `json:"department"`
|
||
Role string `json:"role"`
|
||
FID string `json:"f_id"`
|
||
CapType string `json:"cap_type"`
|
||
}
|
||
|
||
type ServerPushCaSipSos struct {
|
||
Cmd string `json:"cmd"`
|
||
Type string `json:"type"`
|
||
RoomID string `json:"room_id"`
|
||
SosRoomID string `json:"sos_room_id"`
|
||
PictureQuality string `json:"picture_quality"`
|
||
CaInfo CaInfo `json:"ca_info"`
|
||
XPoint string `json:"x_point"`
|
||
YPoint string `json:"y_point"`
|
||
Time int64 `json:"time"`
|
||
}
|
||
|
||
// 处理SOS信息
|
||
func HandlePushCaSipSos(jsonString string) {
|
||
var response ServerPushCaSipSos
|
||
err := json.Unmarshal([]byte(jsonString), &response)
|
||
deviceID := response.CaInfo.DeviceID
|
||
var videoHat VideoDeviceHat
|
||
g.Model("device_video_hat").Where("dev_num = ?", deviceID).Scan(&videoHat)
|
||
|
||
// 发送请求去获取设备的 sip_id 信息 https://caps.runde.pro/api/index.php?ctl=device&act=get_user_sip_id
|
||
res, _ := getSipId(constant.Token, videoHat.Uid)
|
||
sipid := res.Data.SIPID
|
||
|
||
// 收到了报警 SOS 信息,把数据写入数据库,处理标记为未处理
|
||
data := alarm.HatAlarm{
|
||
DevNum: deviceID,
|
||
DevName: videoHat.DevName,
|
||
ProjectID: int(videoHat.ProjectID),
|
||
BatteryLevel: videoHat.BatteryLevel,
|
||
IsLowBattery: videoHat.IsLowBattery,
|
||
RoomID: response.RoomID,
|
||
SipId: sipid,
|
||
IsHandle: strconv.Itoa(0),
|
||
HandleAt: time.Time{},
|
||
}
|
||
g.Model("hat_alarm").Data(data).Insert()
|
||
if err != nil {
|
||
log.Fatal("Error decoding JSON to struct:", err)
|
||
}
|
||
}
|
||
|
||
func getSipId(token string, userId int) (res *Response, err error) {
|
||
res = new(Response)
|
||
|
||
// 创建请求
|
||
req, err := http.NewRequest("GET", "https://caps.runde.pro/api/index.php?ctl=device&act=get_user_sip_id", nil)
|
||
if err != nil {
|
||
return res, err
|
||
}
|
||
|
||
// 添加请求头
|
||
req.Header.Set("Authentication", token)
|
||
|
||
// 添加查询参数
|
||
query := req.URL.Query()
|
||
query.Add("user_id", strconv.Itoa(userId))
|
||
req.URL.RawQuery = query.Encode()
|
||
|
||
// 发送请求
|
||
client := &http.Client{}
|
||
resp, err := client.Do(req)
|
||
if err != nil {
|
||
return res, err
|
||
}
|
||
defer resp.Body.Close()
|
||
|
||
// 读取响应体
|
||
respBody, err := ioutil.ReadAll(resp.Body)
|
||
if err != nil {
|
||
return res, err
|
||
}
|
||
|
||
// 检查响应状态码
|
||
if resp.StatusCode != http.StatusOK {
|
||
return res, errors.New(fmt.Sprintf("unexpected status code: %d", resp.StatusCode))
|
||
}
|
||
|
||
// 反序列化响应体到结构体
|
||
err = json.Unmarshal(respBody, &res)
|
||
if err != nil {
|
||
return res, err
|
||
}
|
||
|
||
return res, nil
|
||
}
|
||
|
||
type Response struct {
|
||
Status bool `json:"status"`
|
||
Msg string `json:"msg"`
|
||
Data DataObject `json:"data"`
|
||
MsgCode string `json:"msg_code"`
|
||
}
|
||
|
||
type DataObject struct {
|
||
SIPID string `json:"sip_id"`
|
||
}
|
||
|
||
type VideoDeviceHat struct {
|
||
DevNum string `json:"dev_num"` // 设备编号
|
||
DevName string `json:"dev_name"` // 设备名
|
||
Status int `json:"status"` // 安全帽状态(0:离线,1:在线,2:监控中,3:通话中,4:隐私模式)
|
||
ProjectID int64 `json:"project_id"` // 项目id
|
||
UserID string `json:"user_id"` // 用户id
|
||
BatteryLevel string `json:"battery_level"` // 电量
|
||
IsLowBattery bool `json:"is_low_battery"` // 是否处于低电量,true代表低电量,false则不是低电量
|
||
CreateTime time.Time `json:"create_time"` // 创建时间
|
||
UpdatedAt time.Time `json:"updated_at"` // 更新时间
|
||
Longitude string `json:"longitude"` // 最新的经度
|
||
Latitude string `json:"latitude"` // 最新的维度
|
||
SipId int `json:"sip_id"` // SIPID
|
||
Uid int `json:"uid"` // 安全帽厂商返回的用户ID
|
||
PushStatus string `json:"push_status"` // 推流状态
|
||
Nickname string `json:"nick_name"` // 用户名称
|
||
UserName string `json:"user_name"` // 真实名称
|
||
HeadIcon string `json:"head_icon"` // 头像
|
||
Phone string `json:"phone"` // 电话
|
||
}
|