Files
zmkgC/api/saft_hat/hat_sos.go
2025-07-07 20:11:59 +08:00

63 lines
1.8 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package saft_hat
import (
"context"
"errors"
"fmt"
"github.com/gogf/gf/v2/encoding/gjson"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/os/glog"
"io/ioutil"
"log"
)
// SOS报警数据结构体
type SOS struct {
Flag int `json:"flag"` // 两次数据上传标志0:第二次 1:第一次
UTCDateTime int64 `json:"utcDateTime"` // 设备内部上传时间戳
Latitude float64 `json:"latitude"` // WGS-84 纬度坐标
IMEI string `json:"IMEI"` // 设备IMEI编号
Type string `json:"type"` // 设备上传类型和时间
Longitude float64 `json:"longitude"` // WGS-84 经度坐标
}
// SOS报警数据上传请求【其它平台发给我们】
type SOSReq struct {
g.Meta `path:"/device/alarm" method:"post" tags:"安全帽相关" summary:"接收SOS报警数据(不需要前端调用)"`
}
type SOSRes struct {
}
func (h Hat) SOS(ctx context.Context, req *SOSReq) (res *SOSRes, err error) {
r := g.RequestFromCtx(ctx) // 从上下文中获取请求对象
body, err := ioutil.ReadAll(r.Body) // 读取请求体
if err != nil {
log.Printf("读取请求头失败: %v", err)
return nil, err
}
defer r.Body.Close() // 延迟关闭请求体
// 从请求头中获取时间戳和签名
timestamp := r.GetHeader("timestamp")
signature := r.GetHeader("signature")
// 验证签名的有效性
if !VerifySignature(string(body)+timestamp, signature, secret) {
glog.Errorf(ctx, "签名验证失败")
return nil, errors.New("验证签名失败")
}
// 解析请求体中的 JSON 数据到预定义的结构体
var sos SOS
if err := gjson.DecodeTo(body, &sos); err != nil {
return nil, err
}
// 输出 SOS 的报警信息
fmt.Println("报警信息:", sos)
// 返回响应对象
return &SOSRes{}, nil
}