mirror of
https://gitee.com/XM-GO/PandaX.git
synced 2026-04-23 02:48:34 +08:00
iot init
This commit is contained in:
63
apps/device/entity/device.go
Normal file
63
apps/device/entity/device.go
Normal file
@@ -0,0 +1,63 @@
|
||||
package entity
|
||||
|
||||
import (
|
||||
"database/sql/driver"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"pandax/pkg/global"
|
||||
"time"
|
||||
)
|
||||
|
||||
// DeviceGroup 设备分组
|
||||
type DeviceGroup struct {
|
||||
global.BaseAuthModel
|
||||
Name string `json:"name" gorm:"type:varchar(128);comment:设备分组名称" validate:"required"`
|
||||
Pid string `json:"pid" gorm:"type:varchar(64);comment:设备分组类型"`
|
||||
Path string `json:"path" gorm:"type:varchar(255);comment:设备分组路径"`
|
||||
Description string `json:"description" gorm:"type:varchar(255);comment:设备分组说明"`
|
||||
Sort int64 `json:"sort" gorm:"type:int;comment:排序"`
|
||||
Status string `gorm:"status;type:varchar(1);comment:状态" json:"status"`
|
||||
Ext Ext `json:"ext" gorm:"type:json;comment:扩展"` //可扩展的kv map,承载设备组的外围信息
|
||||
Children []DeviceGroup `json:"children" gorm:"-"` //子节点
|
||||
}
|
||||
|
||||
type DeviceGroupLabel struct {
|
||||
Id string `gorm:"-" json:"id"`
|
||||
Name string `gorm:"-" json:"name"`
|
||||
Children []DeviceGroupLabel `gorm:"-" json:"children"`
|
||||
}
|
||||
|
||||
type Device struct {
|
||||
global.BaseAuthModel
|
||||
Name string `json:"name" gorm:"type:varchar(128);comment:设备名称" validate:"required,alphanum"` // mqtt 用户名英文
|
||||
ParentId string `json:"parentId" gorm:"type:varchar(64);comment:父设备"`
|
||||
DeviceType string `json:"deviceType" gorm:"type:varchar(64);comment:设备类型"`
|
||||
Token string `json:"token" gorm:"type:varchar(128);comment:设备token"`
|
||||
Alias string `json:"alias" gorm:"type:varchar(128);comment:设备别名" `
|
||||
Pid string `json:"pid" gorm:"comment:产品Id" validate:"required"`
|
||||
Gid string `json:"gid" gorm:"comment:分组Id" validate:"required"`
|
||||
Description string `json:"description" gorm:"type:varchar(255);comment:说明"`
|
||||
Status string `json:"status" gorm:"type:varchar(1);comment:状态"` //0 正常 1禁用
|
||||
LinkStatus string `json:"linkStatus" gorm:"type:varchar(10);comment:连接状态"` //inactive未激活 online在线 offline离线
|
||||
LastAt time.Time `gorm:"column:last_time;comment:最后一次在线时间" json:"lastTime"`
|
||||
OtaVersion string `json:"otaVersion" gorm:"type:varchar(64);comment:固件版本" ` //上一次固件升级的版本
|
||||
Ext Ext `json:"ext" gorm:"type:json;comment:扩展"` //可扩展的kv map,承载设备组的外围信息
|
||||
}
|
||||
type DeviceRes struct {
|
||||
Device
|
||||
DeviceGroup DeviceGroup `json:"deviceGroup" gorm:"foreignKey:Gid;references:Id"`
|
||||
Product Product `json:"product" gorm:"foreignKey:Pid;references:Id"`
|
||||
}
|
||||
|
||||
type Ext map[string]interface{}
|
||||
|
||||
func (a Ext) Value() (driver.Value, error) {
|
||||
return json.Marshal(a)
|
||||
}
|
||||
func (a *Ext) Scan(value interface{}) error {
|
||||
b, ok := value.([]byte)
|
||||
if !ok {
|
||||
return errors.New("type assertion to []byte failed")
|
||||
}
|
||||
return json.Unmarshal(b, &a)
|
||||
}
|
||||
42
apps/device/entity/device_exp.go
Normal file
42
apps/device/entity/device_exp.go
Normal file
@@ -0,0 +1,42 @@
|
||||
package entity
|
||||
|
||||
import "time"
|
||||
|
||||
// 说明
|
||||
// 设备上报属性,遥测,连接事件存储到时序数据库中
|
||||
// 其他存在MySQL中
|
||||
|
||||
// DeviceAlarm 设备告警表 需要更改告警状态不能存在时序数据库中
|
||||
type DeviceAlarm struct {
|
||||
Id string `json:"id" gorm:"primary_key;"`
|
||||
Time time.Time `gorm:"comment:告警时间" json:"time"`
|
||||
Name string `gorm:"type:varchar(64);comment:告警名称" json:"name"`
|
||||
DeviceId string `gorm:"type:varchar(64);comment:所属设备" json:"deviceId"`
|
||||
ProductId string `gorm:"type:varchar(64);comment:所属产品" json:"productId"`
|
||||
Type string `gorm:"type:varchar(64);comment:告警类型" json:"type"`
|
||||
Level string `gorm:"type:varchar(64);comment:告警级别" json:"level"` // 危险 重要 次要 警告 不确定
|
||||
State string `gorm:"type:varchar(1);comment:告警状态" json:"state"` // 告警中 0 已确认 1 已清除 2 已关闭 3
|
||||
Details string `gorm:"type:varchar(255);comment:告警详情" json:"details"`
|
||||
}
|
||||
|
||||
type DeviceCmdLog struct {
|
||||
Id string `json:"id" gorm:"primary_key;"`
|
||||
DeviceId string `gorm:"type:varchar(64);comment:所属设备" json:"deviceId"`
|
||||
CmdName string `gorm:"type:varchar(64);comment:命令名称" json:"cmdName"`
|
||||
CmdContent string `gorm:"type:longtext;comment:命令内容" json:"cmdContent"`
|
||||
State string `gorm:"type:varchar(1);comment:命令状态" json:"state"`
|
||||
Type string `gorm:"type:varchar(1);comment:命令类型" json:"type"` // 0 自定义 1 命令
|
||||
ResponseContent string `gorm:"type:longtext;comment:响应内容" json:"responseContent"`
|
||||
RequestTime string `gorm:"comment:命令下发时间" json:"requestTime"`
|
||||
ResponseTime string `gorm:"comment:命令响应时间" json:"responseTime"`
|
||||
}
|
||||
|
||||
func (DeviceCmdLog) TableName() string {
|
||||
return "device_cmd_log"
|
||||
}
|
||||
|
||||
// DeviceStatistics 设备统计表
|
||||
// 每小时、每天、每周、每月的平均值、最大值、最小值等。可以使用定时任务或实时计算框架来更新数据统计表。
|
||||
type DeviceStatistics struct {
|
||||
Time time.Time `gorm:"comment:时间" json:"time"`
|
||||
}
|
||||
34
apps/device/entity/device_vo.go
Normal file
34
apps/device/entity/device_vo.go
Normal file
@@ -0,0 +1,34 @@
|
||||
package entity
|
||||
|
||||
type DeviceStatusVo struct {
|
||||
Name string `json:"name"`
|
||||
Key string `json:"key"`
|
||||
Type string `json:"type"`
|
||||
Define any `json:"define"`
|
||||
Value any `json:"value"`
|
||||
Time any `json:"time"`
|
||||
}
|
||||
|
||||
type VisualClass struct {
|
||||
ClassId string `json:"classId"`
|
||||
Name string `json:"name"`
|
||||
Attrs []VisualTwinAttr `json:"attrs"`
|
||||
}
|
||||
|
||||
type VisualTwinAttr struct {
|
||||
Key string `json:"key"`
|
||||
Name string `json:"name"`
|
||||
Type string `json:"type"`
|
||||
Rw string `json:"rw"` //属性的操作权限
|
||||
}
|
||||
|
||||
type VisualTwin struct {
|
||||
TwinId string `json:"twinId"`
|
||||
Name string `json:"name"`
|
||||
}
|
||||
|
||||
// 发送数据
|
||||
type VisualTwinSendAttrs struct {
|
||||
TwinId string `json:"twinId"`
|
||||
Attrs map[string]interface{} `json:"attrs"`
|
||||
}
|
||||
91
apps/device/entity/product.go
Normal file
91
apps/device/entity/product.go
Normal file
@@ -0,0 +1,91 @@
|
||||
package entity
|
||||
|
||||
import (
|
||||
"database/sql/driver"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"pandax/pkg/global"
|
||||
)
|
||||
|
||||
const (
|
||||
DIRECT_DEVICE = "direct" //直连设备
|
||||
GATEWAY_DEVICE = "gateway" //网关设备
|
||||
GATEWAYS_DEVICE = "gatewayS" //网关子设备
|
||||
MONITOR_DEVICE = "monitor" //监控设备
|
||||
)
|
||||
|
||||
const (
|
||||
ATTRIBUTES_TSL = "attributes"
|
||||
TELEMETRY_TSL = "telemetry"
|
||||
COMMANDS_TSL = "commands"
|
||||
TAGS_TSL = "tags"
|
||||
)
|
||||
|
||||
type ProductCategory struct {
|
||||
global.BaseAuthModel
|
||||
Name string `json:"name" gorm:"type:varchar(128);comment:产品类型名称" validate:"required"`
|
||||
Pid string `json:"pid" gorm:"type:varchar(64);comment:父产品类型"`
|
||||
Path string `json:"path" gorm:"type:varchar(255);comment:产品类型路径"`
|
||||
Description string `json:"description" gorm:"type:varchar(255);comment:产品类型说明"`
|
||||
Sort int64 `json:"sort" gorm:"type:int;comment:排序"`
|
||||
Status string `gorm:"status;type:varchar(1);comment:状态" json:"status"`
|
||||
Children []ProductCategory `json:"children" gorm:"-"` //子节点
|
||||
}
|
||||
|
||||
type ProductCategoryLabel struct {
|
||||
Id string `gorm:"-" json:"id"`
|
||||
Name string `gorm:"-" json:"name"`
|
||||
Children []ProductCategoryLabel `gorm:"-" json:"children"`
|
||||
}
|
||||
|
||||
type Product struct {
|
||||
global.BaseAuthModel
|
||||
Name string `json:"name" gorm:"type:varchar(128);comment:产品名称" validate:"required"`
|
||||
PhotoUrl string `json:"photoUrl" gorm:"type:varchar(255);comment:图片地址"`
|
||||
Description string `json:"description" gorm:"type:varchar(255);comment:产品说明"`
|
||||
ProductCategoryId string `json:"productCategoryId" gorm:"type:varchar(64);comment:产品类型Id" validate:"required"`
|
||||
ProtocolName string `json:"protocolName" gorm:"type:varchar(64);comment:协议名称"` //MQTT COAP WebSocket LwM2M
|
||||
DeviceType string `json:"deviceType" gorm:"type:varchar(64);comment:设备类型"` // 直连设备 网关设备 网关子设备 监控设备
|
||||
SelfLearn bool `json:"selfLearn" gorm:"default:0;comment:自学习开关"`
|
||||
RuleChainId string `json:"ruleChainId" gorm:"type:varchar(64);comment:规则链Id"` //可空,如果空就走根规则链
|
||||
Status string `gorm:"status;type:varchar(1);comment:状态" json:"status"`
|
||||
}
|
||||
|
||||
type ProductRes struct {
|
||||
Product
|
||||
ProductCategory ProductCategory `json:"productCategory"`
|
||||
}
|
||||
|
||||
type ProductTemplate struct {
|
||||
global.BaseModel
|
||||
Pid string `json:"pid" gorm:"type:varchar(64);comment:产品Id" validate:"required"`
|
||||
Classify string `json:"classify" gorm:"type:varchar(64);comment:模型归类" validate:"required"` // 属性 遥测 命令 事件
|
||||
Name string `json:"name" gorm:"type:varchar(64);comment:名称" validate:"required"`
|
||||
Key string `json:"key" gorm:"type:varchar(64);comment:标识" validate:"required"`
|
||||
Description string `json:"description" gorm:"type:varchar(255);comment:属性说明"`
|
||||
Type string `json:"type" gorm:"type:varchar(64);comment:数据类型"` //["int32","float","double","array","bool","enum","date","struct","string"]
|
||||
Define Define `json:"define" gorm:"type:json;comment:数据约束"`
|
||||
}
|
||||
|
||||
type ProductOta struct {
|
||||
global.BaseModel
|
||||
Pid string `json:"pid" gorm:"comment:产品Id" validate:"required"`
|
||||
Name string `json:"name" gorm:"type:varchar(64);comment:固件名称" validate:"required"`
|
||||
Version string `json:"version" gorm:"type:varchar(64);comment:固件版本" validate:"required"`
|
||||
IsLatest bool `json:"isLatest" gorm:"comment:是最新固件"`
|
||||
Url string `json:"url" gorm:"type:varchar(128);comment:下载地址" validate:"required"`
|
||||
Description string `json:"description" gorm:"type:varchar(255);comment:说明"`
|
||||
}
|
||||
|
||||
type Define map[string]interface{}
|
||||
|
||||
func (a Define) Value() (driver.Value, error) {
|
||||
return json.Marshal(a)
|
||||
}
|
||||
func (a *Define) Scan(value interface{}) error {
|
||||
b, ok := value.([]byte)
|
||||
if !ok {
|
||||
return errors.New("type assertion to []byte failed")
|
||||
}
|
||||
return json.Unmarshal(b, &a)
|
||||
}
|
||||
Reference in New Issue
Block a user