This commit is contained in:
tfl
2024-08-21 17:35:50 +08:00
parent 34ea7472b7
commit dd5b38b4e3
24 changed files with 328 additions and 133 deletions

View File

@@ -7,7 +7,6 @@ package api
// ==========================================================================
import (
"fmt"
"pandax/apps/device/util"
"pandax/kit/biz"
"pandax/kit/model"
"pandax/kit/restfulx"
@@ -131,6 +130,18 @@ func (p *DeviceApi) GetDeviceStatus(rc *restfulx.ReqCtx) {
rc.ResData = res
}
func (p *DeviceApi) GetDeviceEvents(rc *restfulx.ReqCtx) {
pageNum := restfulx.QueryInt(rc, "pageNum", 1)
pageSize := restfulx.QueryInt(rc, "pageSize", 10)
deviceId := restfulx.PathParam(rc, "id")
ty := restfulx.QueryParam(rc, "type")
offset := pageSize * (pageNum - 1)
sql := `select * from ? where deviceId = ? and type = ? DESC LIMIT ?,? `
list, err := global.TdDb.GetAllEvents(sql, deviceId, ty, offset, pageSize)
biz.ErrIsNilAppendErr(err, "查询设备事件历史失败")
rc.ResData = list
}
// GetDeviceTelemetryHistory 获取Device属性的遥测历史
func (p *DeviceApi) GetDeviceTelemetryHistory(rc *restfulx.ReqCtx) {
id := restfulx.PathParam(rc, "id")
@@ -146,21 +157,6 @@ func (p *DeviceApi) GetDeviceTelemetryHistory(rc *restfulx.ReqCtx) {
rc.ResData = rs
}
// 下发设备属性
func (p *DeviceApi) DownAttribute(rc *restfulx.ReqCtx) {
id := restfulx.PathParam(rc, "id")
key := restfulx.QueryParam(rc, "key")
value := restfulx.QueryParam(rc, "value")
biz.NotEmpty(value, "请设置属性值")
err := util.BuildRunDeviceRpc(id, "single", map[string]interface{}{
"method": "setAttributes",
"params": map[string]interface{}{
key: value,
},
})
biz.ErrIsNilAppendErr(err, "下发失败:")
}
// InsertDevice 添加Device
func (p *DeviceApi) InsertDevice(rc *restfulx.ReqCtx) {
var data entity.Device

View File

@@ -3,17 +3,15 @@ package api
// ==========================================================================
import (
"encoding/json"
"pandax/apps/device/util"
"pandax/kit/biz"
"pandax/kit/model"
"pandax/kit/restfulx"
"pandax/kit/utils"
"pandax/pkg/global"
devicerpc "pandax/pkg/device_rpc"
"strings"
"time"
"pandax/apps/device/entity"
"pandax/apps/device/services"
"pandax/apps/device/util"
)
type DeviceCmdLogApi struct {
@@ -50,22 +48,12 @@ func (p *DeviceCmdLogApi) InsertDeviceCmdLog(rc *restfulx.ReqCtx) {
err := json.Unmarshal([]byte(data.CmdContent), &ms)
biz.ErrIsNil(err, "指令格式不正确")
biz.IsTrue(len(ms) > 0, "指令格式不正确")
data.Id = utils.GenerateID()
data.State = "2"
data.RequestTime = time.Now().Format("2006-01-02 15:04:05")
go func() {
err := util.BuildRunDeviceRpc(data.DeviceId, data.Mode, map[string]interface{}{
"method": data.CmdName,
"params": ms,
})
if err != nil {
global.Log.Error("规则链执行失败", err)
data.State = "1"
} else {
data.State = "0"
rpc := devicerpc.RpcPayload{
Method: data.CmdName,
Params: ms,
}
data.ResponseTime = time.Now().Format("2006-01-02 15:04:05.000")
err = p.DeviceCmdLogApp.Insert(data)
err := util.BuildRunDeviceRpc(data.DeviceId, data.Mode, rpc)
biz.ErrIsNil(err, "添加指令记录失败")
}()
}