mirror of
https://gitee.com/XM-GO/PandaX.git
synced 2026-05-12 14:52:07 +08:00
iot init
This commit is contained in:
168
apps/device/tsl/convert.go
Normal file
168
apps/device/tsl/convert.go
Normal file
@@ -0,0 +1,168 @@
|
||||
package tsl
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
"time"
|
||||
)
|
||||
|
||||
func (t DefineAttribute) ConvertAttributeValue(v interface{}) interface{} {
|
||||
if *t.Rw == "w" {
|
||||
return ""
|
||||
}
|
||||
if v == nil {
|
||||
return t.DefaultValue
|
||||
}
|
||||
return v
|
||||
}
|
||||
|
||||
func (t ValueType) ConvertValue(v interface{}) interface{} {
|
||||
var transfer Transfer
|
||||
switch t.Type {
|
||||
case TypeInt:
|
||||
transfer = TInt(t.DefineBase)
|
||||
case TypeFloat:
|
||||
transfer = TFloat(t.DefineBase)
|
||||
case TypeString:
|
||||
transfer = TText(t.DefineBase)
|
||||
case TypeBool:
|
||||
transfer = TBoolean(t.DefineBase)
|
||||
case TypeDate:
|
||||
transfer = TDate(t.DefineBase)
|
||||
case TypeEnum:
|
||||
transfer = TEnum(t.DefineBase)
|
||||
case TypeStruct:
|
||||
transfer = TStruct(t.DefineBase)
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
return transfer.Convert(v)
|
||||
}
|
||||
|
||||
type Transfer interface {
|
||||
Convert(interface{}) interface{}
|
||||
}
|
||||
|
||||
type TInt DefineBase
|
||||
|
||||
func (tInt TInt) Convert(v interface{}) interface{} {
|
||||
number, ok := v.(int64)
|
||||
if !ok {
|
||||
floatNumber, floatNumberOk := v.(float64)
|
||||
if !floatNumberOk {
|
||||
return 0
|
||||
}
|
||||
number = int64(floatNumber)
|
||||
}
|
||||
if tInt.Min != nil && int64(*tInt.Min) > number {
|
||||
return *tInt.Min
|
||||
}
|
||||
if tInt.Max != nil && int64(*tInt.Max) > number {
|
||||
return *tInt.Max
|
||||
}
|
||||
return number
|
||||
}
|
||||
|
||||
type TFloat DefineBase
|
||||
|
||||
func (tFloat TFloat) Convert(v interface{}) interface{} {
|
||||
number, ok := v.(float64)
|
||||
if !ok {
|
||||
return 0
|
||||
}
|
||||
if tFloat.Min != nil && *tFloat.Min > number {
|
||||
number = *tFloat.Min
|
||||
}
|
||||
if tFloat.Max != nil && *tFloat.Max > number {
|
||||
number = *tFloat.Max
|
||||
}
|
||||
defaultDecimal := 2
|
||||
if tFloat.Decimals != nil {
|
||||
defaultDecimal = *tFloat.Decimals
|
||||
}
|
||||
number64, _ := strconv.ParseFloat(strconv.FormatFloat(number, 'f', defaultDecimal, 64), 32)
|
||||
return number64
|
||||
}
|
||||
|
||||
type TText DefineBase
|
||||
|
||||
func (tText TText) Convert(v interface{}) interface{} {
|
||||
text, ok := v.(string)
|
||||
if !ok {
|
||||
return ""
|
||||
}
|
||||
if tText.MaxLength != nil && *tText.MaxLength > 0 && len(text) > *tText.MaxLength {
|
||||
return text[:*tText.MaxLength-1]
|
||||
} else {
|
||||
return text
|
||||
}
|
||||
}
|
||||
|
||||
type TBoolean DefineBase
|
||||
|
||||
func (tBoolean TBoolean) Convert(v interface{}) interface{} {
|
||||
b, ok := v.(bool)
|
||||
if !ok {
|
||||
return ""
|
||||
}
|
||||
if b {
|
||||
return tBoolean.DefineBool[1].Value
|
||||
} else {
|
||||
return tBoolean.DefineBool[0].Value
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
type TDate DefineBase
|
||||
|
||||
const layout = "2006-01-02 15:04:05"
|
||||
|
||||
func (TDate TDate) Convert(v interface{}) interface{} {
|
||||
str, ok := v.(string)
|
||||
if !ok {
|
||||
return time.Time{}
|
||||
}
|
||||
t, err := time.Parse(layout, str)
|
||||
if err != nil {
|
||||
return time.Time{}
|
||||
} else {
|
||||
return t
|
||||
}
|
||||
}
|
||||
|
||||
type TEnum DefineBase
|
||||
|
||||
func (tEnum TEnum) Convert(v interface{}) interface{} {
|
||||
tE, ok := v.(string)
|
||||
if !ok {
|
||||
return ""
|
||||
}
|
||||
if tEnum.Enums == nil {
|
||||
return ""
|
||||
}
|
||||
for _, node := range tEnum.Enums {
|
||||
if node.Value == tE {
|
||||
return node.Key
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
type TStruct DefineBase
|
||||
|
||||
func (tStruct TStruct) Convert(v interface{}) interface{} {
|
||||
m, ok := v.(map[string]interface{})
|
||||
if !ok {
|
||||
return nil
|
||||
}
|
||||
result := make(map[string]interface{})
|
||||
if tStruct.Struct != nil {
|
||||
for k, value := range m {
|
||||
for _, t := range tStruct.Struct {
|
||||
if t.Name == k {
|
||||
result[t.Name] = t.ValueType.ConvertValue(value)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
72
apps/device/tsl/tsl.go
Normal file
72
apps/device/tsl/tsl.go
Normal file
@@ -0,0 +1,72 @@
|
||||
package tsl
|
||||
|
||||
const (
|
||||
TypeEnum = "enum" // 枚举类型
|
||||
TypeInt = "int64"
|
||||
TypeString = "string"
|
||||
TypeBool = "bool"
|
||||
TypeFloat = "float64"
|
||||
TypeDate = "date"
|
||||
TypeStruct = "struct"
|
||||
)
|
||||
|
||||
// DefineAttribute 属性拓展
|
||||
type DefineAttribute struct {
|
||||
DefaultValue *string `json:"defaultValue"` // 属性时
|
||||
Rw *string `json:"rw"`
|
||||
}
|
||||
|
||||
// DefineBase 基础类型参数
|
||||
type DefineBase struct {
|
||||
Max *float64 `json:"max" ` // 最大,数字类型:int64、float64
|
||||
Min *float64 `json:"min" ` // 最小,数字类型:int64、float64
|
||||
Step *float64 `json:"step"` // 小数位数,数字类型:int64、float64
|
||||
Decimals *int `json:"decimals"` // 小数位数,数字类型:float64
|
||||
Unit *string `json:"unit"` // 单位,数字类型: int64、float64
|
||||
|
||||
MaxLength *int `json:"maxLength"` // 最大长度,字符类型:string
|
||||
|
||||
DefineBool []DefineBool `json:"boolDefine"`
|
||||
Enums []DefineEnum `json:"enumDefine"` // 枚举类型:enum
|
||||
Struct []DefineStruct `json:"structDefine" ` // 对象类型:Struct
|
||||
}
|
||||
|
||||
type DefineBool struct {
|
||||
Key string `json:"key"` // 键 0、1
|
||||
Value string `json:"value"` // 枚举值
|
||||
}
|
||||
|
||||
type DefineEnum struct {
|
||||
Key string `json:"key"` // 键 0、1
|
||||
Value string `json:"value"` // 枚举值
|
||||
}
|
||||
|
||||
// DefineStruct 扩展类型参数:对象型
|
||||
type DefineStruct struct {
|
||||
Key string `json:"key"` //参数标识
|
||||
Name string `json:"name"` // 参数名称
|
||||
ValueType ValueType `json:"valueType"` // 参数值
|
||||
Desc string `json:"desc"` // 描述
|
||||
}
|
||||
|
||||
type ValueType struct {
|
||||
Type string `json:"type" dc:"数据类型" v:"required#请选择数据类型"` // 类型
|
||||
DefineBase // 参数
|
||||
}
|
||||
|
||||
// DefineCommands 命令
|
||||
type DefineCommands struct {
|
||||
Key string `json:"key" dc:"功能标识" v:"required|regex:^[A-Za-z_]+[\\w]*$#请输入功能标识|标识由字母、数字和下划线组成,且不能以数字开头"`
|
||||
Name string `json:"name" dc:"功能名称" v:"required#请输入功能名称"` // 功能名称
|
||||
Inputs []DefineCommandsInput `json:"inputs" dc:"输入参数"` // 输入参数
|
||||
Output ValueType `json:"output" dc:"输出参数"` // 输出参数
|
||||
Desc string `json:"desc" dc:"描述"` // 描述
|
||||
}
|
||||
|
||||
// DefineCommandsInput 命令:输入参数
|
||||
type DefineCommandsInput struct {
|
||||
Key string `json:"key"`
|
||||
Name string `json:"name"` // 输入参数名称
|
||||
ValueType ValueType `json:"valueType"` // 参数值
|
||||
Desc string `json:"desc"` // 描述
|
||||
}
|
||||
Reference in New Issue
Block a user