mirror of
https://gitee.com/XM-GO/PandaX.git
synced 2026-04-25 11:58:33 +08:00
iot init
This commit is contained in:
214
apps/device/api/device.go
Normal file
214
apps/device/api/device.go
Normal file
@@ -0,0 +1,214 @@
|
||||
package api
|
||||
|
||||
// ==========================================================================
|
||||
// 生成日期:2023-06-30 09:19:43 +0800 CST
|
||||
// 生成路径: apps/device/api/devices.go
|
||||
// 生成人:panda
|
||||
// ==========================================================================
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/XM-GO/PandaKit/biz"
|
||||
"github.com/XM-GO/PandaKit/model"
|
||||
"github.com/XM-GO/PandaKit/restfulx"
|
||||
"github.com/kakuilan/kgo"
|
||||
"pandax/pkg/global"
|
||||
"pandax/pkg/mqtt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"pandax/apps/device/entity"
|
||||
"pandax/apps/device/services"
|
||||
)
|
||||
|
||||
type DeviceApi struct {
|
||||
DeviceApp services.DeviceModel
|
||||
ProductApp services.ProductModel
|
||||
ProductTemplateApp services.ProductTemplateModel
|
||||
}
|
||||
|
||||
// GetDeviceList Device列表数据
|
||||
func (p *DeviceApi) GetDeviceList(rc *restfulx.ReqCtx) {
|
||||
data := entity.Device{}
|
||||
pageNum := restfulx.QueryInt(rc, "pageNum", 1)
|
||||
pageSize := restfulx.QueryInt(rc, "pageSize", 10)
|
||||
data.Name = restfulx.QueryParam(rc, "name")
|
||||
data.Status = restfulx.QueryParam(rc, "status")
|
||||
data.Pid = restfulx.QueryParam(rc, "pid")
|
||||
data.Gid = restfulx.QueryParam(rc, "gid")
|
||||
data.DeviceType = restfulx.QueryParam(rc, "deviceType")
|
||||
data.ParentId = restfulx.QueryParam(rc, "parentId")
|
||||
data.LinkStatus = restfulx.QueryParam(rc, "linkStatus")
|
||||
|
||||
list, total := p.DeviceApp.FindListPage(pageNum, pageSize, data)
|
||||
|
||||
rc.ResData = model.ResultPage{
|
||||
Total: total,
|
||||
PageNum: int64(pageNum),
|
||||
PageSize: int64(pageNum),
|
||||
Data: list,
|
||||
}
|
||||
}
|
||||
|
||||
// GetDeviceListAll Device获取所有
|
||||
func (p *DeviceApi) GetDeviceListAll(rc *restfulx.ReqCtx) {
|
||||
data := entity.Device{}
|
||||
data.Name = restfulx.QueryParam(rc, "name")
|
||||
data.Status = restfulx.QueryParam(rc, "status")
|
||||
data.Pid = restfulx.QueryParam(rc, "pid")
|
||||
data.DeviceType = restfulx.QueryParam(rc, "deviceType")
|
||||
|
||||
list := p.DeviceApp.FindList(data)
|
||||
rc.ResData = list
|
||||
}
|
||||
|
||||
// GetDevice 获取Device
|
||||
func (p *DeviceApi) GetDevice(rc *restfulx.ReqCtx) {
|
||||
id := restfulx.PathParam(rc, "id")
|
||||
rc.ResData = p.DeviceApp.FindOne(id)
|
||||
}
|
||||
|
||||
// GetDeviceStatus 获取Device状态信息
|
||||
func (p *DeviceApi) GetDeviceStatus(rc *restfulx.ReqCtx) {
|
||||
id := restfulx.PathParam(rc, "id")
|
||||
classify := restfulx.QueryParam(rc, "classify")
|
||||
device := p.DeviceApp.FindOne(id)
|
||||
template := p.ProductTemplateApp.FindList(entity.ProductTemplate{Classify: classify, Pid: device.Pid})
|
||||
|
||||
res := make([]entity.DeviceStatusVo, 0)
|
||||
rs, err := global.TdDb.GetOne(fmt.Sprintf(`select * from %s_%s ORDER BY ts DESC LIMIT 1`, device.Name, classify))
|
||||
biz.ErrIsNil(err, "查询设备状态信息失败")
|
||||
for _, tel := range *template {
|
||||
sdv := entity.DeviceStatusVo{
|
||||
Name: tel.Name,
|
||||
Key: tel.Key,
|
||||
Type: tel.Type,
|
||||
Define: tel.Define,
|
||||
Time: rs["ts"],
|
||||
}
|
||||
if v, ok := rs[tel.Key]; ok {
|
||||
sdv.Value = v
|
||||
} else {
|
||||
sdv.Value = tel.Define["default_value"]
|
||||
}
|
||||
res = append(res, sdv)
|
||||
}
|
||||
|
||||
rc.ResData = res
|
||||
}
|
||||
|
||||
// 下发设备属性
|
||||
func (p *DeviceApi) DownAttribute(rc *restfulx.ReqCtx) {
|
||||
//id := restfulx.PathParam(rc, "id")
|
||||
key := restfulx.QueryParam(rc, "key")
|
||||
value := restfulx.QueryParam(rc, "value")
|
||||
// 下发指令
|
||||
contentMap := map[string]interface{}{
|
||||
key: value,
|
||||
}
|
||||
content, _ := json.Marshal(contentMap)
|
||||
var rpc = &mqtt.RpcRequest{Client: global.MqttClient, RequestId: 1, Mode: "single"}
|
||||
rpc.GetRequestId()
|
||||
err := rpc.RequestAttributes(mqtt.RpcPayload{Params: string(content)})
|
||||
biz.ErrIsNil(err, "属性下发失败")
|
||||
}
|
||||
|
||||
// InsertDevice 添加Device
|
||||
func (p *DeviceApi) InsertDevice(rc *restfulx.ReqCtx) {
|
||||
var data entity.Device
|
||||
restfulx.BindJsonAndValid(rc, &data)
|
||||
list := p.DeviceApp.FindList(entity.Device{Name: data.Name})
|
||||
biz.IsTrue(!(list != nil && len(*list) > 0), fmt.Sprintf("名称%s已存在,设置其他命名", data.Name))
|
||||
data.Id = kgo.KStr.Uniqid("d_")
|
||||
data.Owner = rc.LoginAccount.UserName
|
||||
data.LinkStatus = global.INACTIVE
|
||||
data.LastAt = time.Now()
|
||||
p.DeviceApp.Insert(data)
|
||||
// 视频设备不创建超级表
|
||||
if data.DeviceType != global.MONITOR {
|
||||
createDeviceTable(data.Pid, data.Name)
|
||||
}
|
||||
}
|
||||
|
||||
// UpdateDevice 修改Device
|
||||
func (p *DeviceApi) UpdateDevice(rc *restfulx.ReqCtx) {
|
||||
var data entity.Device
|
||||
restfulx.BindJsonAndValid(rc, &data)
|
||||
|
||||
p.DeviceApp.Update(data)
|
||||
}
|
||||
|
||||
// DeleteDevice 删除Device
|
||||
func (p *DeviceApi) DeleteDevice(rc *restfulx.ReqCtx) {
|
||||
id := restfulx.PathParam(rc, "id")
|
||||
ids := strings.Split(id, ",")
|
||||
for _, id := range ids {
|
||||
list := p.DeviceApp.FindOne(id)
|
||||
// 删除表
|
||||
if list.DeviceType != global.MONITOR {
|
||||
deleteDeviceTable(list.Name)
|
||||
}
|
||||
}
|
||||
p.DeviceApp.Delete(ids)
|
||||
}
|
||||
|
||||
func (p *DeviceApi) ScreenTwinData(rc *restfulx.ReqCtx) {
|
||||
pageNum := restfulx.QueryInt(rc, "pageNum", 1)
|
||||
pageSize := restfulx.QueryInt(rc, "pageSize", 10)
|
||||
classId := restfulx.QueryParam(rc, "classId")
|
||||
if classId == "" {
|
||||
vp := make([]entity.VisualClass, 0)
|
||||
list := p.ProductApp.FindList(entity.Product{})
|
||||
for _, pro := range *list {
|
||||
data := p.ProductTemplateApp.FindListAttrs(entity.ProductTemplate{Pid: pro.Id})
|
||||
vta := make([]entity.VisualTwinAttr, 0)
|
||||
for _, attr := range *data {
|
||||
twinAttr := entity.VisualTwinAttr{
|
||||
Key: attr.Key,
|
||||
Name: attr.Name,
|
||||
Type: attr.Type,
|
||||
}
|
||||
if attr.Classify == "attributes" {
|
||||
if rw, ok := attr.Define["rw"].(string); ok {
|
||||
twinAttr.Rw = rw
|
||||
} else {
|
||||
twinAttr.Rw = "r"
|
||||
}
|
||||
} else {
|
||||
twinAttr.Rw = "r"
|
||||
}
|
||||
vta = append(vta, twinAttr)
|
||||
}
|
||||
vp = append(vp, entity.VisualClass{
|
||||
ClassId: pro.Id,
|
||||
Name: pro.Name,
|
||||
Attrs: vta,
|
||||
})
|
||||
}
|
||||
rc.ResData = vp
|
||||
} else {
|
||||
findList, _ := p.DeviceApp.FindListPage(pageNum, pageSize, entity.Device{Pid: classId})
|
||||
vt := make([]entity.VisualTwin, 0)
|
||||
for _, device := range *findList {
|
||||
vt = append(vt, entity.VisualTwin{
|
||||
TwinId: device.Id,
|
||||
Name: device.Name + "-" + device.Alias,
|
||||
})
|
||||
}
|
||||
rc.ResData = vt
|
||||
}
|
||||
}
|
||||
|
||||
func createDeviceTable(productId, device string) {
|
||||
err := global.TdDb.CreateTable(productId+"_"+entity.ATTRIBUTES_TSL, device+"_"+entity.ATTRIBUTES_TSL)
|
||||
biz.ErrIsNil(err, "创建时序属性表失败")
|
||||
err = global.TdDb.CreateTable(productId+"_"+entity.TELEMETRY_TSL, device+"_"+entity.TELEMETRY_TSL)
|
||||
biz.ErrIsNil(err, "创建时序遥测表失败")
|
||||
}
|
||||
|
||||
func deleteDeviceTable(device string) {
|
||||
err := global.TdDb.DropTable(device + "_" + entity.ATTRIBUTES_TSL)
|
||||
biz.ErrIsNil(err, "删除时序属性表失败")
|
||||
err = global.TdDb.DropTable(device + "_" + entity.TELEMETRY_TSL)
|
||||
biz.ErrIsNil(err, "删除时序遥测表失败")
|
||||
}
|
||||
49
apps/device/api/device_alarm.go
Normal file
49
apps/device/api/device_alarm.go
Normal file
@@ -0,0 +1,49 @@
|
||||
package api
|
||||
|
||||
// ==========================================================================
|
||||
import (
|
||||
"github.com/XM-GO/PandaKit/model"
|
||||
"github.com/XM-GO/PandaKit/restfulx"
|
||||
"strings"
|
||||
|
||||
"pandax/apps/device/entity"
|
||||
"pandax/apps/device/services"
|
||||
)
|
||||
|
||||
type DeviceAlarmApi struct {
|
||||
DeviceAlarmApp services.DeviceAlarmModel
|
||||
}
|
||||
|
||||
// GetDeviceAlarmList 告警列表数据
|
||||
func (p *DeviceAlarmApi) GetDeviceAlarmList(rc *restfulx.ReqCtx) {
|
||||
data := entity.DeviceAlarm{}
|
||||
pageNum := restfulx.QueryInt(rc, "pageNum", 1)
|
||||
pageSize := restfulx.QueryInt(rc, "pageSize", 10)
|
||||
data.DeviceId = restfulx.QueryParam(rc, "deviceId")
|
||||
data.Type = restfulx.QueryParam(rc, "type")
|
||||
data.Level = restfulx.QueryParam(rc, "level")
|
||||
data.State = restfulx.QueryParam(rc, "state")
|
||||
|
||||
list, total := p.DeviceAlarmApp.FindListPage(pageNum, pageSize, data)
|
||||
|
||||
rc.ResData = model.ResultPage{
|
||||
Total: total,
|
||||
PageNum: int64(pageNum),
|
||||
PageSize: int64(pageNum),
|
||||
Data: list,
|
||||
}
|
||||
}
|
||||
|
||||
// UpdateDeviceAlarm 修改告警
|
||||
func (p *DeviceAlarmApi) UpdateDeviceAlarm(rc *restfulx.ReqCtx) {
|
||||
var data entity.DeviceAlarm
|
||||
restfulx.BindJsonAndValid(rc, &data)
|
||||
p.DeviceAlarmApp.Update(data)
|
||||
}
|
||||
|
||||
// DeleteDeviceAlarm 删除告警
|
||||
func (p *DeviceAlarmApi) DeleteDeviceAlarm(rc *restfulx.ReqCtx) {
|
||||
id := restfulx.PathParam(rc, "id")
|
||||
ids := strings.Split(id, ",")
|
||||
p.DeviceAlarmApp.Delete(ids)
|
||||
}
|
||||
64
apps/device/api/device_cmd.go
Normal file
64
apps/device/api/device_cmd.go
Normal file
@@ -0,0 +1,64 @@
|
||||
package api
|
||||
|
||||
// ==========================================================================
|
||||
import (
|
||||
"github.com/XM-GO/PandaKit/biz"
|
||||
"github.com/XM-GO/PandaKit/model"
|
||||
"github.com/XM-GO/PandaKit/restfulx"
|
||||
"github.com/kakuilan/kgo"
|
||||
"pandax/pkg/global"
|
||||
"pandax/pkg/mqtt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"pandax/apps/device/entity"
|
||||
"pandax/apps/device/services"
|
||||
)
|
||||
|
||||
type DeviceCmdLogApi struct {
|
||||
DeviceCmdLogApp services.DeviceCmdLogModel
|
||||
}
|
||||
|
||||
// GetDeviceCmdLogList 告警列表数据
|
||||
func (p *DeviceCmdLogApi) GetDeviceCmdLogList(rc *restfulx.ReqCtx) {
|
||||
data := entity.DeviceCmdLog{}
|
||||
pageNum := restfulx.QueryInt(rc, "pageNum", 1)
|
||||
pageSize := restfulx.QueryInt(rc, "pageSize", 10)
|
||||
data.DeviceId = restfulx.QueryParam(rc, "deviceId")
|
||||
data.State = restfulx.QueryParam(rc, "state")
|
||||
data.Type = restfulx.QueryParam(rc, "type")
|
||||
|
||||
list, total := p.DeviceCmdLogApp.FindListPage(pageNum, pageSize, data)
|
||||
|
||||
rc.ResData = model.ResultPage{
|
||||
Total: total,
|
||||
PageNum: int64(pageNum),
|
||||
PageSize: int64(pageNum),
|
||||
Data: list,
|
||||
}
|
||||
}
|
||||
|
||||
// InsertDeviceCmdLog 添加DeviceCmdLog
|
||||
func (p *DeviceCmdLogApi) InsertDeviceCmdLog(rc *restfulx.ReqCtx) {
|
||||
var data entity.DeviceCmdLog
|
||||
restfulx.BindJsonAndValid(rc, &data)
|
||||
data.Id = kgo.KStr.Uniqid("cmd_")
|
||||
data.State = "2"
|
||||
data.RequestTime = time.Now().Format("2006-01-02 15:04:05")
|
||||
err := p.DeviceCmdLogApp.Insert(data)
|
||||
biz.ErrIsNil(err, "添加指令记录失败")
|
||||
// 下发指令
|
||||
var rpc = &mqtt.RpcRequest{Client: global.MqttClient, Mode: "single"}
|
||||
rpc.GetRequestId()
|
||||
err = rpc.RequestCmd(mqtt.RpcPayload{Method: data.CmdName, Params: data.CmdContent})
|
||||
if err != nil {
|
||||
global.Log.Error("指令下发失败")
|
||||
}
|
||||
}
|
||||
|
||||
// DeleteDeviceCmdLog 删除告警
|
||||
func (p *DeviceCmdLogApi) DeleteDeviceCmdLog(rc *restfulx.ReqCtx) {
|
||||
id := restfulx.PathParam(rc, "id")
|
||||
ids := strings.Split(id, ",")
|
||||
p.DeviceCmdLogApp.Delete(ids)
|
||||
}
|
||||
79
apps/device/api/device_group.go
Normal file
79
apps/device/api/device_group.go
Normal file
@@ -0,0 +1,79 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"github.com/XM-GO/PandaKit/restfulx"
|
||||
"github.com/kakuilan/kgo"
|
||||
"pandax/apps/device/entity"
|
||||
"pandax/apps/device/services"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type DeviceGroupApi struct {
|
||||
DeviceGroupApp services.DeviceGroupModel
|
||||
}
|
||||
|
||||
// GetDeviceGroupTree DeviceGroup 树
|
||||
func (p *DeviceGroupApi) GetDeviceGroupTree(rc *restfulx.ReqCtx) {
|
||||
name := restfulx.QueryParam(rc, "name")
|
||||
status := restfulx.QueryParam(rc, "status")
|
||||
id := restfulx.QueryParam(rc, "id")
|
||||
|
||||
sg := entity.DeviceGroup{Name: name, Status: status}
|
||||
sg.Id = id
|
||||
rc.ResData = p.DeviceGroupApp.SelectDeviceGroup(sg)
|
||||
}
|
||||
|
||||
func (p *DeviceGroupApi) GetDeviceGroupTreeLabel(rc *restfulx.ReqCtx) {
|
||||
sg := entity.DeviceGroup{}
|
||||
rc.ResData = p.DeviceGroupApp.SelectDeviceGroupLabel(sg)
|
||||
}
|
||||
|
||||
func (p *DeviceGroupApi) GetDeviceGroupList(rc *restfulx.ReqCtx) {
|
||||
name := restfulx.QueryParam(rc, "name")
|
||||
status := restfulx.QueryParam(rc, "status")
|
||||
id := restfulx.QueryParam(rc, "id")
|
||||
|
||||
sg := entity.DeviceGroup{Name: name, Status: status}
|
||||
sg.Id = id
|
||||
if sg.Name == "" {
|
||||
rc.ResData = p.DeviceGroupApp.SelectDeviceGroup(sg)
|
||||
} else {
|
||||
rc.ResData = p.DeviceGroupApp.FindList(sg)
|
||||
}
|
||||
}
|
||||
|
||||
// GetDeviceGroupAllList 查询所有
|
||||
func (p *DeviceGroupApi) GetDeviceGroupAllList(rc *restfulx.ReqCtx) {
|
||||
var vsg entity.DeviceGroup
|
||||
rc.ResData = p.DeviceGroupApp.FindList(vsg)
|
||||
}
|
||||
|
||||
// GetDeviceGroup 获取DeviceGroup
|
||||
func (p *DeviceGroupApi) GetDeviceGroup(rc *restfulx.ReqCtx) {
|
||||
id := restfulx.PathParam(rc, "id")
|
||||
rc.ResData = p.DeviceGroupApp.FindOne(id)
|
||||
}
|
||||
|
||||
// InsertDeviceGroup 添加DeviceGroup
|
||||
func (p *DeviceGroupApi) InsertDeviceGroup(rc *restfulx.ReqCtx) {
|
||||
var data entity.DeviceGroup
|
||||
restfulx.BindJsonAndValid(rc, &data)
|
||||
data.Id = kgo.KStr.Uniqid("dg_")
|
||||
data.Owner = rc.LoginAccount.UserName
|
||||
p.DeviceGroupApp.Insert(data)
|
||||
}
|
||||
|
||||
// UpdateDeviceGroup 修改DeviceGroup
|
||||
func (p *DeviceGroupApi) UpdateDeviceGroup(rc *restfulx.ReqCtx) {
|
||||
var data entity.DeviceGroup
|
||||
restfulx.BindJsonAndValid(rc, &data)
|
||||
|
||||
p.DeviceGroupApp.Update(data)
|
||||
}
|
||||
|
||||
// DeleteDeviceGroup 删除DeviceGroup
|
||||
func (p *DeviceGroupApi) DeleteDeviceGroup(rc *restfulx.ReqCtx) {
|
||||
id := restfulx.PathParam(rc, "id")
|
||||
ids := strings.Split(id, ",")
|
||||
p.DeviceGroupApp.Delete(ids)
|
||||
}
|
||||
118
apps/device/api/product.go
Normal file
118
apps/device/api/product.go
Normal file
@@ -0,0 +1,118 @@
|
||||
package api
|
||||
|
||||
// ==========================================================================
|
||||
// 生成日期:2023-06-30 08:57:48 +0800 CST
|
||||
// 生成路径: apps/device/api/products.go
|
||||
// 生成人:panda
|
||||
// ==========================================================================
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/XM-GO/PandaKit/biz"
|
||||
"github.com/XM-GO/PandaKit/model"
|
||||
"github.com/XM-GO/PandaKit/restfulx"
|
||||
"github.com/kakuilan/kgo"
|
||||
"pandax/pkg/global"
|
||||
"strings"
|
||||
|
||||
"pandax/apps/device/entity"
|
||||
"pandax/apps/device/services"
|
||||
)
|
||||
|
||||
type ProductApi struct {
|
||||
ProductApp services.ProductModel
|
||||
DeviceApp services.DeviceModel
|
||||
TemplateApp services.ProductTemplateModel
|
||||
OtaAPP services.ProductOtaModel
|
||||
}
|
||||
|
||||
// GetProductList Product列表数据
|
||||
func (p *ProductApi) GetProductList(rc *restfulx.ReqCtx) {
|
||||
data := entity.Product{}
|
||||
pageNum := restfulx.QueryInt(rc, "pageNum", 1)
|
||||
pageSize := restfulx.QueryInt(rc, "pageSize", 10)
|
||||
data.Status = restfulx.QueryParam(rc, "status")
|
||||
data.ProductCategoryId = restfulx.QueryParam(rc, "productCategoryId")
|
||||
data.ProtocolName = restfulx.QueryParam(rc, "protocolName")
|
||||
data.DeviceType = restfulx.QueryParam(rc, "deviceType")
|
||||
data.Name = restfulx.QueryParam(rc, "name")
|
||||
|
||||
list, total := p.ProductApp.FindListPage(pageNum, pageSize, data)
|
||||
|
||||
rc.ResData = model.ResultPage{
|
||||
Total: total,
|
||||
PageNum: int64(pageNum),
|
||||
PageSize: int64(pageNum),
|
||||
Data: list,
|
||||
}
|
||||
}
|
||||
|
||||
func (p *ProductApi) GetProductListAll(rc *restfulx.ReqCtx) {
|
||||
data := entity.Product{}
|
||||
data.Status = restfulx.QueryParam(rc, "status")
|
||||
data.ProductCategoryId = restfulx.QueryParam(rc, "productCategoryId")
|
||||
data.ProtocolName = restfulx.QueryParam(rc, "protocolName")
|
||||
data.DeviceType = restfulx.QueryParam(rc, "deviceType")
|
||||
data.Name = restfulx.QueryParam(rc, "name")
|
||||
|
||||
rc.ResData = p.ProductApp.FindList(data)
|
||||
}
|
||||
|
||||
// GetProduct 获取Product
|
||||
func (p *ProductApi) GetProduct(rc *restfulx.ReqCtx) {
|
||||
id := restfulx.PathParam(rc, "id")
|
||||
rc.ResData = p.ProductApp.FindOne(id)
|
||||
}
|
||||
|
||||
// InsertProduct 添加Product
|
||||
func (p *ProductApi) InsertProduct(rc *restfulx.ReqCtx) {
|
||||
var data entity.Product
|
||||
restfulx.BindJsonAndValid(rc, &data)
|
||||
data.Id = kgo.KStr.Uniqid("p_")
|
||||
data.Owner = rc.LoginAccount.UserName
|
||||
p.ProductApp.Insert(data)
|
||||
// 创建taos数据库超级表 摄像头产品不创建
|
||||
if data.DeviceType != global.MONITOR {
|
||||
createDeviceStable(data.Id)
|
||||
}
|
||||
}
|
||||
|
||||
// UpdateProduct 修改Product
|
||||
func (p *ProductApi) UpdateProduct(rc *restfulx.ReqCtx) {
|
||||
var data entity.Product
|
||||
restfulx.BindJsonAndValid(rc, &data)
|
||||
|
||||
p.ProductApp.Update(data)
|
||||
}
|
||||
|
||||
// DeleteProduct 删除Product
|
||||
func (p *ProductApi) DeleteProduct(rc *restfulx.ReqCtx) {
|
||||
id := restfulx.PathParam(rc, "id")
|
||||
ids := strings.Split(id, ",")
|
||||
for _, id := range ids {
|
||||
list := p.DeviceApp.FindList(entity.Device{Pid: id})
|
||||
biz.IsTrue(!(list != nil && len(*list) > 0), fmt.Sprintf("产品ID%s下存在设备,不能被删除", id))
|
||||
}
|
||||
// 删除产品
|
||||
p.ProductApp.Delete(ids)
|
||||
for _, id := range ids {
|
||||
// 删除所有模型,固件
|
||||
p.TemplateApp.Delete([]string{id})
|
||||
p.OtaAPP.Delete([]string{id})
|
||||
// 删除超级表
|
||||
deleteDeviceStable(id)
|
||||
}
|
||||
}
|
||||
|
||||
func createDeviceStable(productId string) {
|
||||
err := global.TdDb.CreateStable(productId + "_" + entity.ATTRIBUTES_TSL)
|
||||
biz.ErrIsNil(err, "创建时序属性超级表失败")
|
||||
err = global.TdDb.CreateStable(productId + "_" + entity.TELEMETRY_TSL)
|
||||
biz.ErrIsNil(err, "创建时序遥测超级表失败")
|
||||
}
|
||||
|
||||
func deleteDeviceStable(productId string) {
|
||||
err := global.TdDb.DropStable(productId + "_" + entity.ATTRIBUTES_TSL)
|
||||
biz.ErrIsNil(err, "删除时序属性超级表失败")
|
||||
err = global.TdDb.DropStable(productId + "_" + entity.TELEMETRY_TSL)
|
||||
biz.ErrIsNil(err, "删除时序遥测超级表失败")
|
||||
}
|
||||
80
apps/device/api/product_category.go
Normal file
80
apps/device/api/product_category.go
Normal file
@@ -0,0 +1,80 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"github.com/XM-GO/PandaKit/restfulx"
|
||||
"github.com/kakuilan/kgo"
|
||||
"pandax/apps/device/entity"
|
||||
"pandax/apps/device/services"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type ProductCategoryApi struct {
|
||||
ProductCategoryApp services.ProductCategoryModel
|
||||
}
|
||||
|
||||
// GetProductCategoryTree ProductCategory 树
|
||||
func (p *ProductCategoryApi) GetProductCategoryTree(rc *restfulx.ReqCtx) {
|
||||
name := restfulx.QueryParam(rc, "name")
|
||||
status := restfulx.QueryParam(rc, "status")
|
||||
id := restfulx.QueryParam(rc, "id")
|
||||
/*err := rc.Validate.Var(id, "required")
|
||||
biz.ErrIsNil(err, "id 必传")*/
|
||||
sg := entity.ProductCategory{Name: name, Status: status}
|
||||
sg.Id = id
|
||||
rc.ResData = p.ProductCategoryApp.SelectProductCategory(sg)
|
||||
}
|
||||
|
||||
func (p *ProductCategoryApi) GetProductCategoryTreeLabel(rc *restfulx.ReqCtx) {
|
||||
sg := entity.ProductCategory{}
|
||||
rc.ResData = p.ProductCategoryApp.SelectProductCategoryLabel(sg)
|
||||
}
|
||||
|
||||
func (p *ProductCategoryApi) GetProductCategoryList(rc *restfulx.ReqCtx) {
|
||||
name := restfulx.QueryParam(rc, "name")
|
||||
status := restfulx.QueryParam(rc, "status")
|
||||
id := restfulx.QueryParam(rc, "id")
|
||||
|
||||
sg := entity.ProductCategory{Name: name, Status: status}
|
||||
sg.Id = id
|
||||
if sg.Name == "" {
|
||||
rc.ResData = p.ProductCategoryApp.SelectProductCategory(sg)
|
||||
} else {
|
||||
rc.ResData = p.ProductCategoryApp.FindList(sg)
|
||||
}
|
||||
}
|
||||
|
||||
// GetProductCategoryAllList 查询所有
|
||||
func (p *ProductCategoryApi) GetProductCategoryAllList(rc *restfulx.ReqCtx) {
|
||||
var vsg entity.ProductCategory
|
||||
rc.ResData = p.ProductCategoryApp.FindList(vsg)
|
||||
}
|
||||
|
||||
// GetProductCategory 获取ProductCategory
|
||||
func (p *ProductCategoryApi) GetProductCategory(rc *restfulx.ReqCtx) {
|
||||
id := restfulx.PathParam(rc, "id")
|
||||
rc.ResData = p.ProductCategoryApp.FindOne(id)
|
||||
}
|
||||
|
||||
// InsertProductCategory 添加ProductCategory
|
||||
func (p *ProductCategoryApi) InsertProductCategory(rc *restfulx.ReqCtx) {
|
||||
var data entity.ProductCategory
|
||||
restfulx.BindJsonAndValid(rc, &data)
|
||||
data.Id = kgo.KStr.Uniqid("pc_")
|
||||
data.Owner = rc.LoginAccount.UserName
|
||||
p.ProductCategoryApp.Insert(data)
|
||||
}
|
||||
|
||||
// UpdateProductCategory 修改ProductCategory
|
||||
func (p *ProductCategoryApi) UpdateProductCategory(rc *restfulx.ReqCtx) {
|
||||
var data entity.ProductCategory
|
||||
restfulx.BindJsonAndValid(rc, &data)
|
||||
|
||||
p.ProductCategoryApp.Update(data)
|
||||
}
|
||||
|
||||
// DeleteProductCategory 删除ProductCategory
|
||||
func (p *ProductCategoryApi) DeleteProductCategory(rc *restfulx.ReqCtx) {
|
||||
id := restfulx.PathParam(rc, "id")
|
||||
ids := strings.Split(id, ",")
|
||||
p.ProductCategoryApp.Delete(ids)
|
||||
}
|
||||
63
apps/device/api/product_ota.go
Normal file
63
apps/device/api/product_ota.go
Normal file
@@ -0,0 +1,63 @@
|
||||
package api
|
||||
|
||||
// ==========================================================================
|
||||
import (
|
||||
"github.com/XM-GO/PandaKit/model"
|
||||
"github.com/XM-GO/PandaKit/restfulx"
|
||||
"github.com/kakuilan/kgo"
|
||||
"strings"
|
||||
|
||||
"pandax/apps/device/entity"
|
||||
"pandax/apps/device/services"
|
||||
)
|
||||
|
||||
type ProductOtaApi struct {
|
||||
ProductOtaApp services.ProductOtaModel
|
||||
}
|
||||
|
||||
// GetProductOtaList Ota列表数据
|
||||
func (p *ProductOtaApi) GetProductOtaList(rc *restfulx.ReqCtx) {
|
||||
data := entity.ProductOta{}
|
||||
pageNum := restfulx.QueryInt(rc, "pageNum", 1)
|
||||
pageSize := restfulx.QueryInt(rc, "pageSize", 10)
|
||||
data.Name = restfulx.QueryParam(rc, "name")
|
||||
data.Pid = restfulx.QueryParam(rc, "pid")
|
||||
|
||||
list, total := p.ProductOtaApp.FindListPage(pageNum, pageSize, data)
|
||||
|
||||
rc.ResData = model.ResultPage{
|
||||
Total: total,
|
||||
PageNum: int64(pageNum),
|
||||
PageSize: int64(pageNum),
|
||||
Data: list,
|
||||
}
|
||||
}
|
||||
|
||||
// GetProductOta 获取Ota
|
||||
func (p *ProductOtaApi) GetProductOta(rc *restfulx.ReqCtx) {
|
||||
id := restfulx.PathParam(rc, "id")
|
||||
rc.ResData = p.ProductOtaApp.FindOne(id)
|
||||
}
|
||||
|
||||
// InsertProductOta 添加Ota
|
||||
func (p *ProductOtaApi) InsertProductOta(rc *restfulx.ReqCtx) {
|
||||
var data entity.ProductOta
|
||||
restfulx.BindJsonAndValid(rc, &data)
|
||||
data.Id = kgo.KStr.Uniqid("ota_")
|
||||
p.ProductOtaApp.Insert(data)
|
||||
}
|
||||
|
||||
// UpdateProductOta 修改Ota
|
||||
func (p *ProductOtaApi) UpdateProductOta(rc *restfulx.ReqCtx) {
|
||||
var data entity.ProductOta
|
||||
restfulx.BindJsonAndValid(rc, &data)
|
||||
|
||||
p.ProductOtaApp.Update(data)
|
||||
}
|
||||
|
||||
// DeleteProductOta 删除Ota
|
||||
func (p *ProductOtaApi) DeleteProductOta(rc *restfulx.ReqCtx) {
|
||||
id := restfulx.PathParam(rc, "id")
|
||||
ids := strings.Split(id, ",")
|
||||
p.ProductOtaApp.Delete(ids)
|
||||
}
|
||||
118
apps/device/api/product_template.go
Normal file
118
apps/device/api/product_template.go
Normal file
@@ -0,0 +1,118 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"github.com/XM-GO/PandaKit/biz"
|
||||
"github.com/XM-GO/PandaKit/model"
|
||||
"github.com/XM-GO/PandaKit/restfulx"
|
||||
"github.com/kakuilan/kgo"
|
||||
"log"
|
||||
"pandax/pkg/global"
|
||||
"strings"
|
||||
|
||||
"pandax/apps/device/entity"
|
||||
"pandax/apps/device/services"
|
||||
)
|
||||
|
||||
type ProductTemplateApi struct {
|
||||
ProductTemplateApp services.ProductTemplateModel
|
||||
}
|
||||
|
||||
// GetProductTemplateList Template列表数据
|
||||
func (p *ProductTemplateApi) GetProductTemplateList(rc *restfulx.ReqCtx) {
|
||||
data := entity.ProductTemplate{}
|
||||
pageNum := restfulx.QueryInt(rc, "pageNum", 1)
|
||||
pageSize := restfulx.QueryInt(rc, "pageSize", 10)
|
||||
data.Pid = restfulx.QueryParam(rc, "pid")
|
||||
data.Classify = restfulx.QueryParam(rc, "classify")
|
||||
data.Name = restfulx.QueryParam(rc, "name")
|
||||
|
||||
list, total := p.ProductTemplateApp.FindListPage(pageNum, pageSize, data)
|
||||
|
||||
rc.ResData = model.ResultPage{
|
||||
Total: total,
|
||||
PageNum: int64(pageNum),
|
||||
PageSize: int64(pageNum),
|
||||
Data: list,
|
||||
}
|
||||
}
|
||||
|
||||
// GetProductTemplateListAll Template所有列表数据
|
||||
func (p *ProductTemplateApi) GetProductTemplateListAll(rc *restfulx.ReqCtx) {
|
||||
data := entity.ProductTemplate{}
|
||||
data.Pid = restfulx.QueryParam(rc, "pid")
|
||||
data.Classify = restfulx.QueryParam(rc, "classify")
|
||||
data.Name = restfulx.QueryParam(rc, "name")
|
||||
list := p.ProductTemplateApp.FindList(data)
|
||||
rc.ResData = list
|
||||
}
|
||||
|
||||
// GetProductTemplate 获取Template
|
||||
func (p *ProductTemplateApi) GetProductTemplate(rc *restfulx.ReqCtx) {
|
||||
id := restfulx.PathParam(rc, "id")
|
||||
rc.ResData = p.ProductTemplateApp.FindOne(id)
|
||||
}
|
||||
|
||||
// InsertProductTemplate 添加Template
|
||||
func (p *ProductTemplateApi) InsertProductTemplate(rc *restfulx.ReqCtx) {
|
||||
var data entity.ProductTemplate
|
||||
restfulx.BindJsonAndValid(rc, &data)
|
||||
data.Id = kgo.KStr.Uniqid("tm_")
|
||||
p.ProductTemplateApp.Insert(data)
|
||||
// 向超级表中添加字段
|
||||
if data.Classify == entity.ATTRIBUTES_TSL {
|
||||
err := global.TdDb.AddSTableField(data.Pid+"_"+entity.ATTRIBUTES_TSL, data.Key, data.Type, 0)
|
||||
biz.ErrIsNil(err, "添加字段失败")
|
||||
}
|
||||
if data.Classify == entity.TELEMETRY_TSL {
|
||||
len := 0
|
||||
if data.Type == "string" {
|
||||
len = int(data.Define["length"].(int64))
|
||||
}
|
||||
err := global.TdDb.AddSTableField(data.Pid+"_"+entity.TELEMETRY_TSL, data.Key, data.Type, len)
|
||||
biz.ErrIsNil(err, "添加字段失败")
|
||||
}
|
||||
// todo 已经创建的表,超级表字段更新时,需要同步设备表字段
|
||||
}
|
||||
|
||||
// UpdateProductTemplate 修改Template
|
||||
func (p *ProductTemplateApi) UpdateProductTemplate(rc *restfulx.ReqCtx) {
|
||||
var data entity.ProductTemplate
|
||||
restfulx.BindJsonAndValid(rc, &data)
|
||||
one := p.ProductTemplateApp.FindOne(data.Id)
|
||||
if data.Classify == entity.ATTRIBUTES_TSL {
|
||||
// 删除原来的key
|
||||
global.TdDb.DelSTableField(data.Pid+"_"+entity.ATTRIBUTES_TSL, one.Key)
|
||||
// 添加修改完成的key
|
||||
err := global.TdDb.AddSTableField(data.Pid+"_"+entity.ATTRIBUTES_TSL, data.Key, data.Type, 0)
|
||||
biz.ErrIsNil(err, "添加字段失败")
|
||||
}
|
||||
if data.Classify == entity.TELEMETRY_TSL {
|
||||
len := 0
|
||||
if data.Type == "string" {
|
||||
len = int(data.Define["length"].(int64))
|
||||
}
|
||||
global.TdDb.DelSTableField(data.Pid+"_"+entity.TELEMETRY_TSL, one.Key)
|
||||
err := global.TdDb.AddSTableField(data.Pid+"_"+entity.TELEMETRY_TSL, data.Key, data.Type, len)
|
||||
log.Println(err)
|
||||
biz.ErrIsNil(err, "添加字段失败")
|
||||
}
|
||||
|
||||
p.ProductTemplateApp.Update(data)
|
||||
}
|
||||
|
||||
// DeleteProductTemplate 删除Template
|
||||
func (p *ProductTemplateApi) DeleteProductTemplate(rc *restfulx.ReqCtx) {
|
||||
id := restfulx.PathParam(rc, "id")
|
||||
data := p.ProductTemplateApp.FindOne(id)
|
||||
// 向超级表中添加字段
|
||||
if data.Classify == entity.ATTRIBUTES_TSL {
|
||||
err := global.TdDb.DelSTableField(data.Pid+"_"+entity.ATTRIBUTES_TSL, data.Key)
|
||||
biz.ErrIsNil(err, "删除字段失败")
|
||||
}
|
||||
if data.Classify == entity.TELEMETRY_TSL {
|
||||
err := global.TdDb.DelSTableField(data.Pid+"_"+entity.TELEMETRY_TSL, data.Key)
|
||||
biz.ErrIsNil(err, "删除字段失败")
|
||||
}
|
||||
ids := strings.Split(id, ",")
|
||||
p.ProductTemplateApp.Delete(ids)
|
||||
}
|
||||
Reference in New Issue
Block a user