134 lines
3.2 KiB
Go
134 lines
3.2 KiB
Go
package saft_hat
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"github.com/gogf/gf/v2/frame/g"
|
|
"io/ioutil"
|
|
"net/http"
|
|
"strconv"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
// 获取设备状态的请求结构
|
|
type DeviceStatusReq struct {
|
|
g.Meta `path:"/device/status" method:"post" tags:"安全帽相关" summary:"获取安全帽状态"`
|
|
Data []string `json:"data" dc:"设备号"`
|
|
}
|
|
|
|
// 获取设备状态
|
|
func (h Hat) DeviceStatus(ctx context.Context, req *DeviceStatusReq) (res *DeviceStatusRes, err error) {
|
|
return StatusCheck(ctx, req.Data)
|
|
}
|
|
|
|
type DeviceStatusRes struct {
|
|
Code int `json:"code"` // 响应代码
|
|
Data []DeviceStatusInfo `json:"data"` // 设备数据
|
|
Msg string `json:"msg"` // 响应消息
|
|
}
|
|
|
|
// 定义数组中每个对象的结构
|
|
type DeviceStatusInfo struct {
|
|
IMEI string `json:"imei"`
|
|
Status int `json:"status"`
|
|
}
|
|
|
|
// 检查设备的状态情况
|
|
func StatusCheck(ctx context.Context, devNums []string) (res *DeviceStatusRes, err error) {
|
|
res = new(DeviceStatusRes)
|
|
data := strings.Join(devNums, ",")
|
|
timestamp := time.Now().Unix()
|
|
|
|
// 准备生成签名的参数
|
|
params := map[string]string{
|
|
"appid": appid,
|
|
"data": data,
|
|
"secret": secret,
|
|
"timestamp": strconv.FormatInt(timestamp, 10),
|
|
}
|
|
|
|
// 生成签名
|
|
sign := GenerateSignature(params)
|
|
|
|
// 构造请求体
|
|
payload := map[string]interface{}{
|
|
"appid": appid,
|
|
"data": data,
|
|
"secret": secret,
|
|
"sign": sign,
|
|
"timestamp": timestamp,
|
|
}
|
|
payloadBytes, err := json.Marshal(payload)
|
|
if err != nil {
|
|
return res, err
|
|
}
|
|
|
|
// 发送POST请求
|
|
resp, err := http.Post("https://www.loctp.com/api/crm/v1/getStatus", "application/json", bytes.NewBuffer(payloadBytes))
|
|
|
|
if err != nil {
|
|
return res, err
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
// 检查响应
|
|
respBody, err := ioutil.ReadAll(resp.Body)
|
|
fmt.Println(string(respBody))
|
|
if err != nil {
|
|
return res, err
|
|
}
|
|
|
|
err = json.Unmarshal(respBody, res)
|
|
if err != nil {
|
|
return res, err
|
|
}
|
|
|
|
return res, nil
|
|
}
|
|
|
|
// 心跳检测
|
|
func HeartCheck() {
|
|
ticker := time.NewTicker(60 * time.Second)
|
|
defer ticker.Stop() // 确保ticker被适当释放
|
|
|
|
for {
|
|
select {
|
|
case <-ticker.C:
|
|
var devices []Device
|
|
err := g.Model("device").Fields("dev_num").Scan(&devices)
|
|
if err != nil {
|
|
fmt.Println("获取设备列表出错:", err)
|
|
continue // 如果获取失败,则跳过本次循环
|
|
}
|
|
|
|
if len(devices) == 0 {
|
|
fmt.Println("没有找到任何设备")
|
|
continue // 如果没有设备,则跳过本次循环
|
|
}
|
|
|
|
// 构建devNums切片
|
|
var devNums []string
|
|
for _, device := range devices {
|
|
devNums = append(devNums, device.DevNum)
|
|
}
|
|
res, err := StatusCheck(context.Background(), devNums)
|
|
if err != nil {
|
|
fmt.Println("检查设备状态出错:", err)
|
|
continue // 如果检查失败,则跳过本次循环
|
|
}
|
|
|
|
for _, deviceStatus := range res.Data {
|
|
// 更新 device 表中的 status 字段
|
|
_, err := g.Model("device").Data("status", deviceStatus.Status).Where("dev_num", deviceStatus.IMEI).Update()
|
|
if err != nil {
|
|
fmt.Printf("更新设备 %s 状态出错: %v\n", deviceStatus.IMEI, err)
|
|
continue // 如果更新失败,则跳过本次设备的更新
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|