schema

package
v1.3.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 14, 2025 License: AGPL-3.0 Imports: 10 Imported by: 0

Documentation

Overview

Package device 这个文件提供基础信息的物模型定义

Package schema 这个文件定义和模板相关的接口及dto定义

Index

Constants

View Source
const (
	IDFFormatCheck   = true //是否检查ID是否以数字开头
	IDLen            = 128  //标识符的长度
	NameLen          = 128  //参数名称的长度
	DescLen          = 80   //描述的最大长度
	DefineMappingLen = 50
	DefineUnitLen    = 12
	DefineIntMax     = 9999999999999
	DefineIntMin     = -9999999999999
	DefineStringMax  = 2048
	DefineArrayMax   = 256
	DefineSpecsLen   = 10
	ParamsLen        = 20
)

Variables

View Source
var (
	DefaultSchema string
)

Functions

func CheckDefine

func CheckDefine(oldDef *Define, newDef *Define) bool

func CheckModify

func CheckModify(oldT *Model, newT *Model) error

func DescValidate

func DescValidate(desc string) error

func DoToAffordanceStr

func DoToAffordanceStr(in any) string

func GenArray

func GenArray(identifier string, num int) string

func GetArray

func GetArray(identifier string) (ident string, num int, ok bool)

func IDValidate

func IDValidate(id string) error

func NameValidate

func NameValidate(name string) error

Types

type Action

type Action struct {
	CommonParam
	Dir    ActionDir         `json:"dir"`    //调用方向
	Input  Params            `json:"input"`  //调用参数
	Output Params            `json:"output"` //返回参数
	In     map[string]*Param `json:"-"`      //内部使用,使用map加速匹配,key为id
	Out    map[string]*Param `json:"-"`      //内部使用,使用map加速匹配,key为id
}

行为

func ActionFromCommonSchema

func ActionFromCommonSchema(product *Action, common *Action) *Action

func (*Action) ValidateWithFmt

func (a *Action) ValidateWithFmt() error

type ActionAffordance

type ActionAffordance struct {
	Dir    ActionDir `json:"dir"`    //调用方向
	Input  Params    `json:"input"`  //调用参数
	Output Params    `json:"output"` //返回参数
}

type ActionDir

type ActionDir string

行为的执行方向

const (
	ActionDirUp   ActionDir = "up"   //向上调用
	ActionDirDown ActionDir = "down" //向下调用
)

type ActionMap

type ActionMap map[string]*Action

type ActionSimple

type ActionSimple struct {
	Identifier string       `json:"id"`     //标识符 (统一)
	Name       string       `json:"name"`   //功能名称
	Dir        ActionDir    `json:"dir"`    //调用方向
	Input      ParamSimples `json:"input"`  //调用参数
	Output     ParamSimples `json:"output"` //返回参数
}

行为

type Actions

type Actions []Action

type ActionsSimple

type ActionsSimple []ActionSimple

type AffordanceType

type AffordanceType int64

物模型功能类型 1:property属性 2:event事件 3:action行为

const (
	//物模型功能类型:1-property 属性
	AffordanceTypeProperty AffordanceType = 1
	//物模型功能类型:2-event 事件
	AffordanceTypeEvent AffordanceType = 2
	//物模型功能类型:3-action 行为
	AffordanceTypeAction AffordanceType = 3
)

func (AffordanceType) String

func (m AffordanceType) String() string

type BasicParam

type BasicParam struct {
	Name           string            `json:"name"`           //设备名(是否保留待定)
	Imei           string            `json:"imei"`           //设备的 IMEI 号信息,非必填项
	FwVer          string            `json:"fwVer"`          //mcu固件版本
	ModuleHardInfo string            `json:"moduleHardInfo"` //模组具体硬件型号
	ModuleSoftInfo string            `json:"moduleSoftInfo"` //模组软件版本
	Mac            string            `json:"mac"`            //设备的 MAC 信息,非必填项
	DeviceLabel    map[string]string `json:"deviceLabel"`    //设备商自定义的产品基础信息,以 KV 方式上报
}

BasicParam 小程序或 App 展示设备详细信息时,一般会展示设备的 MAC 地址、IMEI 号、时区等基础信息。设备信息上报使用的 Topic: 上行请求 Topic: $thing/up/property/{ProductID}/{DeviceName} 下行响应 Topic: $thing/down/property/{ProductID}/{DeviceName}

type CommonParam

type CommonParam struct {
	Identifier        string   `json:"identifier"`        //标识符 (统一)
	Tag               Tag      `json:"tag"`               //物模型标签 1:自定义 2:可选 3:必选  必选不可删除
	Name              string   `json:"name"`              //功能名称
	Desc              string   `json:"desc"`              //描述
	Required          bool     `json:"required"`          //是否必须
	ExtendConfig      string   `json:"extendConfig"`      //拓展参数,json格式
	IsCanSceneLinkage int64    `json:"isCanSceneLinkage"` //是否支持场景联动控制 (统一)
	IsShareAuthPerm   int64    `json:"isShareAuthPerm"`   // 分享是否需要校验权限 (统一)
	FuncGroup         int64    `json:"funcGroup"`         // 功能分类: 1:普通功能 2:系统功能
	ControlMode       int64    `json:"controlMode"`       //控制模式: 1: 可以群控,可以单控  2:只能单控
	UserPerm          int64    `json:"userPerm"`          //用户权限操作: 1:r(只读) 3:rw(可读可写)
	IsHistory         def.Bool `json:"isHistory"`         // 是否存储历史记录
	IsPassword        def.Bool `json:"isPassword"`        //是否是密码类型,密码类型需要加掩码
	Order             int64    `json:"order"`             //排序
}

func CommonParamFromCommonSchema

func CommonParamFromCommonSchema(product CommonParam, common CommonParam) CommonParam

type DataType

type DataType string

数据类型

const (
	DataTypeBool      DataType = "bool"
	DataTypeInt       DataType = "int"
	DataTypeString    DataType = "string"
	DataTypeStruct    DataType = "struct"
	DataTypeFloat     DataType = "float"
	DataTypeTimestamp DataType = "timestamp"
	DataTypeArray     DataType = "array"
	DataTypeEnum      DataType = "enum"
)

type Define

type Define struct {
	Type      DataType          `json:"type"`                //参数类型:bool int string struct float timestamp array enum
	Mapping   map[string]string `json:"mapping,omitempty"`   //枚举及bool类型:bool enum
	Min       string            `json:"min,omitempty"`       //数值最小值:int  float
	Max       string            `json:"max,omitempty"`       //数值最大值:int string float array
	Start     string            `json:"start,omitempty"`     //初始值:int float
	Step      string            `json:"step,omitempty"`      //步长:int float
	Unit      string            `json:"unit,omitempty"`      //单位:int float
	Specs     Specs             `json:"specs,omitempty"`     //结构体:struct
	ArrayInfo *Define           `json:"arrayInfo,omitempty"` //数组:array
	Spec      map[string]*Spec  `json:"-"`                   //内部使用,使用map加速匹配,key为id
}

数据类型定义

func (Define) Copy

func (d Define) Copy() Define

func (*Define) FmtValue

func (d *Define) FmtValue(val any) (any, error)

func (*Define) GetDefaultValue

func (d *Define) GetDefaultValue() (retAny any, err error)

func (*Define) GetValueDesc

func (d *Define) GetValueDesc(value any) string

func (*Define) String

func (d *Define) String() string

func (Define) ToSimple

func (d Define) ToSimple() Define

func (*Define) ValidateWithFmt

func (d *Define) ValidateWithFmt() error

func (*Define) ValidateWithFmtArray

func (d *Define) ValidateWithFmtArray() error

func (*Define) ValidateWithFmtBool

func (d *Define) ValidateWithFmtBool() error

func (*Define) ValidateWithFmtEnum

func (d *Define) ValidateWithFmtEnum() error

func (*Define) ValidateWithFmtFloat

func (d *Define) ValidateWithFmtFloat() error

func (*Define) ValidateWithFmtInt

func (d *Define) ValidateWithFmtInt() error

func (*Define) ValidateWithFmtString

func (d *Define) ValidateWithFmtString() error

func (*Define) ValidateWithFmtStruct

func (d *Define) ValidateWithFmtStruct() error

func (*Define) ValidateWithFmtTimeStamp

func (d *Define) ValidateWithFmtTimeStamp() error

type Event

type Event struct {
	CommonParam
	Type   EventType         `json:"type"`   //事件类型: 1:信息:info  2:告警alert  3:故障:fault
	Params Params            `json:"params"` //事件参数
	Param  map[string]*Param `json:"-"`      //内部使用,使用map加速匹配,key为id
}

事件

func EventFromCommonSchema

func EventFromCommonSchema(product *Event, common *Event) *Event

func (*Event) ValidateWithFmt

func (e *Event) ValidateWithFmt() error

type EventAffordance

type EventAffordance struct {
	Type   EventType `json:"type"`   //事件类型: 信息:info  告警alert  故障:fault
	Params Params    `json:"params"` //事件参数
}

type EventMap

type EventMap map[string]*Event

type EventSimple

type EventSimple struct {
	Identifier string       `json:"id"`     //标识符 (统一)
	Name       string       `json:"name"`   //功能名称
	Type       EventType    `json:"type"`   //事件类型: 1:信息:info  2:告警alert  3:故障:fault
	Params     ParamSimples `json:"params"` //事件参数
}

事件

type EventType

type EventType = string

事件类型: 信息:info 告警alert 故障:fault

const (
	EventTypeInfo  EventType = "info"
	EventTypeAlert EventType = "alert"
	EventTypeFault EventType = "fault"
)

type Events

type Events []Event

type EventsSimple

type EventsSimple []EventSimple

type GetSchemaModel

type GetSchemaModel func(ctx context.Context, productID devices.Core) (*Model, error)

type Info

type Info struct {
	ProductID   string // 产品id
	Schema      string // 数据模板
	CreatedTime time.Time
}

type Model

type Model struct {
	Version    string      `json:"version,omitempty"`    //版本号
	Properties Properties  `json:"properties,omitempty"` //属性
	Events     Events      `json:"events,omitempty"`     //事件
	Actions    Actions     `json:"actions,omitempty"`    //行为
	Profile    Profile     `json:"profile"`              //配置信息
	Property   PropertyMap `json:"-"`                    //内部使用,使用map加速匹配,key为id
	Event      EventMap    `json:"-"`                    //内部使用,使用map加速匹配,key为id
	Action     ActionMap   `json:"-"`                    //内部使用,使用map加速匹配,key为id
}

Model 物模型协议-数据模板定义

func NewSchemaTsl

func NewSchemaTsl(schemaStr []byte) (*Model, error)

func ValidateWithFmt

func ValidateWithFmt(schemaStr []byte) (*Model, error)

func (*Model) Aggregation

func (m *Model) Aggregation(m2 *Model) *Model

func (*Model) Copy

func (m *Model) Copy() *Model

func (*Model) String

func (m *Model) String() string

func (*Model) ToSimple

func (m *Model) ToSimple() *ModelSimple

func (*Model) ValidateWithFmt

func (m *Model) ValidateWithFmt() error

type ModelSimple

type ModelSimple struct {
	Properties PropertiesSimple `json:"properties,omitempty"` //属性
	Events     EventsSimple     `json:"events,omitempty"`     //事件
	Actions    ActionsSimple    `json:"actions,omitempty"`    //行为
}

Model 物模型协议-数据模板定义

func (*ModelSimple) ToModel

func (m *ModelSimple) ToModel() *Model

type Param

type Param struct {
	Identifier string `json:"identifier"`       //参数标识符
	Name       string `json:"name"`             //参数名称
	Define     Define `json:"define,omitempty"` //参数定义
}

参数

func (Param) ToSimple

func (p Param) ToSimple() ParamSimple

func (*Param) ValidateWithFmt

func (p *Param) ValidateWithFmt() error

type ParamSimple

type ParamSimple struct {
	Identifier string `json:"id"`   //参数标识符
	Name       string `json:"name"` //参数名称
	Define            //参数定义
}

func (ParamSimple) ToModel

func (p ParamSimple) ToModel() Param

type ParamSimples

type ParamSimples []ParamSimple

func (ParamSimples) ToModel

func (p ParamSimples) ToModel() Params

type ParamType

type ParamType int64
const (
	//请求参数的类型:设备属性上报参数
	ParamProperty ParamType = iota + 1
	//请求参数的类型:设备行为调用的下行参数
	ParamActionInput
	//请求参数的类型:设备行为调用上行的回复参数
	ParamActionOutput
	//请求参数的类型:设备事件上报参数
	ParamEvent
)

type Params

type Params []Param

func (Params) ToSimple

func (p Params) ToSimple() ParamSimples

func (Params) ValidateWithFmt

func (p Params) ValidateWithFmt() error

type Profile

type Profile struct {
	ProductID string `json:"productID"` //产品ID
}

配置信息

type Properties

type Properties []Property

type PropertiesSimple

type PropertiesSimple []PropertySimple

type Property

type Property struct {
	CommonParam
	Mode        PropertyMode `json:"mode"`        //读写类型:rw(可读可写) r(只读)
	Define      Define       `json:"define"`      //数据定义
	IsUseShadow bool         `json:"isUseShadow"` //是否使用设备影子
	IsNoRecord  bool         `json:"isNoRecord"`  //不存储历史记录
}

属性

func PropertyFromCommonSchema

func PropertyFromCommonSchema(product *Property, common *Property) *Property

func (*Property) ValidateWithFmt

func (p *Property) ValidateWithFmt() error

type PropertyAffordance

type PropertyAffordance struct {
	IsUseShadow bool         `json:"isUseShadow"` //是否使用设备影子
	IsNoRecord  bool         `json:"isNoRecord"`  //不存储历史记录
	Define      Define       `json:"define"`      //数据定义
	Mode        PropertyMode `json:"mode"`        //读写类型: 1:r(只读) 2:rw(可读可写)
}

func DoToPropertyAffordance

func DoToPropertyAffordance(in *Property) *PropertyAffordance

type PropertyMap

type PropertyMap map[string]*Property

内部使用

func (*PropertyMap) GetIDs

func (p *PropertyMap) GetIDs() []string

func (*PropertyMap) GetMap

func (p *PropertyMap) GetMap() map[string]*Property

func (*PropertyMap) GetMapWithIDs

func (p *PropertyMap) GetMapWithIDs(datas ...string) map[string]*Property

type PropertyMode

type PropertyMode string

属性读写类型: r(只读) rw(可读可写)

const (
	PropertyModeR  PropertyMode = "r"
	PropertyModeRW PropertyMode = "rw"
)

type PropertySimple

type PropertySimple struct {
	Identifier string       `json:"id"`   //标识符 (统一)
	Name       string       `json:"name"` //功能名称
	Mode       PropertyMode `json:"mode"` //读写类型:rw(可读可写) r(只读)
	Define                  //数据定义
}

属性

type ReadRepo

type ReadRepo interface {
	GetSchemaModel(ctx context.Context, productID string) (*Model, error)
	//GetReadInfo(ctx context.Context, productID string) (*Info, error)
	ClearCache(ctx context.Context, productID string) error
}

type Repo

type Repo interface {
	ReadRepo
	TslImport(ctx context.Context, productID string, template *Model) error
	//Update(ctx context.Context, productID string, template *Model) error
	//TslRead(ctx context.Context, productID string) (*Model, error)
	Delete(ctx context.Context, productID string) error
}

type Spec

type Spec struct {
	Identifier string `json:"identifier"` //参数标识符
	Name       string `json:"name"`       //参数名称
	DataType   Define `json:"dataType"`   //参数定义
}

结构体说明

func (*Spec) ValidateWithFmt

func (s *Spec) ValidateWithFmt() error

type Specs

type Specs []Spec

func (Specs) ValidateWithFmt

func (s Specs) ValidateWithFmt() error

type Tag

type Tag = int64

物模型标签

const (
	TagCustom   Tag = 1 //自定义
	TagOptional Tag = 2 //可选
	TagRequired Tag = 3 //必选 必选不可删除
	TagDevice   Tag = 4 //设备物模型
)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL