schema

package
v0.6.4 Latest Latest
Warning

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

Go to latest
Published: Oct 24, 2023 License: AGPL-3.0 Imports: 9 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 = 20
	DefineUnitLen    = 12
	DefineIntMax     = 9999999999999
	DefineIntMin     = -9999999999999
	DefineStringMax  = 2048
	DefineSpecsLen   = 10
	ParamsLen        = 20
)

Variables

View Source
var (
	DefaultSchema string
)
View Source
var (
	ReadOnce sync.Once
)

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 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 (*Action) ValidateWithFmt

func (a *Action) ValidateWithFmt() error

type ActionDir added in v0.6.0

type ActionDir string

行为的执行方向

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

type ActionMap

type ActionMap map[string]*Action

type Actions

type Actions []Action

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 CacheReadRepo added in v0.5.1

type CacheReadRepo struct {
	// contains filtered or unexported fields
}

func (CacheReadRepo) ClearCache added in v0.5.1

func (t CacheReadRepo) ClearCache(ctx context.Context, productID string) error

func (CacheReadRepo) GetSchemaInfo added in v0.5.1

func (t CacheReadRepo) GetSchemaInfo(ctx context.Context, productID string) (*Model, error)

func (CacheReadRepo) GetSchemaModel added in v0.5.1

func (t CacheReadRepo) GetSchemaModel(ctx context.Context, productID string) (*Model, error)

type CommonParam added in v0.5.1

type CommonParam struct {
	Identifier string `json:"identifier"` //标识符
	Name       string `json:"name"`       //功能名称
	Desc       string `json:"desc"`       //描述
	Required   bool   `json:"required"`   //是否必须
}

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
	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) FmtValue added in v0.5.1

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

func (*Define) GetDefaultValue added in v0.5.1

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

func (*Define) String added in v0.5.1

func (d *Define) String() string

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 (*Event) ValidateWithFmt

func (e *Event) ValidateWithFmt() error

type EventMap

type EventMap map[string]*Event

type EventType

type EventType string

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

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

type Events

type Events []Event

type GetReadInfo added in v0.5.1

type GetReadInfo func(ctx context.Context, productID string) (info *Model, err error)

type GetSchemaModel

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

type Info

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

type Model

type Model struct {
	Version    string      `json:"version"`    //版本号
	Properties Properties  `json:"properties"` //属性
	Events     Events      `json:"events"`     //事件
	Actions    Actions     `json:"actions"`    //行为
	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) String

func (m *Model) String() string

func (*Model) ValidateWithFmt

func (m *Model) ValidateWithFmt() error

type Param

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

参数

func (*Param) ValidateWithFmt

func (p *Param) ValidateWithFmt() error

type ParamType

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

type Params

type Params []Param

func (Params) ValidateWithFmt

func (p Params) ValidateWithFmt() error

type Profile

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

配置信息

type Properties

type Properties []Property

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 (*Property) ValidateWithFmt

func (p *Property) ValidateWithFmt() error

type PropertyMap

type PropertyMap map[string]*Property

内部使用

func (*PropertyMap) GetIDs

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

type PropertyMode

type PropertyMode string

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

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

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
}

func NewReadRepo added in v0.5.1

func NewReadRepo(t GetReadInfo) ReadRepo

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 //必选 必选不可删除
)

Jump to

Keyboard shortcuts

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