entity

package
v1.9.1 Latest Latest
Warning

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

Go to latest
Published: Mar 9, 2022 License: Apache-2.0 Imports: 33 Imported by: 0

Documentation

Overview

Package entity 数据库表定义,与数据库无关的基础数据结构请定义在 types

Index

Constants

View Source
const (
	// StatusInstallFail 添加插件失败
	StatusInstallFail = -1

	StatusInstalling = 0

	// StatusInstallSuccess 添加插件成功
	StatusInstallSuccess = 1
)
View Source
const (
	SourceTypeDefault     = "default"     // 默认插件
	SourceTypeDevelopment = "development" // 开发者插件
)
View Source
const (
	OwnerRoleID = -1 // 拥有者角色的roleID
	Owner       = "拥有者"
)
View Source
const (
	MatchAllCondition = 1 // 全部满足
	MatchAnyCondition = 2 // 任一满足
)
View Source
const (
	UserCredentialFoundType = "user_credential_found"
	LogSwitch               = "log_switch"
	GetCloudDiskCredential  = "get_cloud_disk_credential"
)

配置类型

View Source
const (
	AreaIDFieldName = "AreaID"
)

Variables

View Source
var Tables []interface{} = []interface{}{
	Device{}, Location{}, Area{}, Role{}, RolePermission{},
	User{}, UserRole{}, Scene{}, SceneCondition{},
	SceneTask{}, TaskLog{}, GlobalSetting{}, PluginInfo{}, Client{}, Department{}, DepartmentUser{},
}

Functions

func AddDevice

func AddDevice(d *Device, tx *gorm.DB) (err error)

func AddSADevice added in v1.5.0

func AddSADevice(device *Device, tx *gorm.DB) (err error)

AddSADevice 添加SA设备

func BatchAddChildDevice added in v1.9.1

func BatchAddChildDevice(childDevices []*Device, tx *gorm.DB) error

BatchAddChildDevice 批量添加子设备

func CheckIllegalRepeatDate

func CheckIllegalRepeatDate(repeatDate string) bool

CheckIllegalRepeatDate 判断生效时间格式是否合法

func CheckSAExist added in v1.9.1

func CheckSAExist(device Device, tx *gorm.DB) (err error)

CheckSAExist SA是否已存在

func CheckSceneExitById

func CheckSceneExitById(sceneId int) (err error)

func CopyTable added in v1.9.1

func CopyTable(src *gorm.DB, dst *gorm.DB, table interface{}, delete bool) (err error)

CopyTable 复制一个数据库中的表到另一个数据库中的表上

func CreateDepartment added in v1.9.1

func CreateDepartment(department *Department) error

func CreateDepartmentUser added in v1.9.1

func CreateDepartmentUser(departmentUsers []DepartmentUser) (err error)

func CreateHistory added in v1.5.0

func CreateHistory(areaID uint64, deviceID, userID int, context []byte) (err error)

func CreateLocation

func CreateLocation(location *Location) error

func CreateScene

func CreateScene(scene *Scene) (err error)

func CreateSceneTask

func CreateSceneTask(sceneTask []SceneTask) (err error)

func CreateUser

func CreateUser(user *User, tx *gorm.DB) (err error)

func CreateUserRole

func CreateUserRole(uRoles []UserRole) (err error)

func DelAreaByID

func DelAreaByID(id uint64) (err error)

func DelDepartment added in v1.9.1

func DelDepartment(id int) (err error)

DelDepartment 删除部门

func DelDepartmentUserByDepID added in v1.9.1

func DelDepartmentUserByDepID(departmentID int, db *gorm.DB) (err error)

func DelDepartmentUserByUId added in v1.9.1

func DelDepartmentUserByUId(userID int, db *gorm.DB) (err error)

func DelDeviceByID

func DelDeviceByID(id int) (err error)

func DelDeviceByPID added in v1.9.1

func DelDeviceByPID(pid int, tx *gorm.DB) error

DelDeviceByPID 根据PID删除对应的子设备,子设备使用硬删除

func DelDevicesByPlgID

func DelDevicesByPlgID(plgID string) (err error)

func DelLocation

func DelLocation(id int) (err error)

func DelPlugin

func DelPlugin(pluginID string, areaID uint64) (err error)

DelPlugin 删除插件

func DelUser

func DelUser(id int) (err error)

func DelUserRoleByUid

func DelUserRoleByUid(userId int, db *gorm.DB) (err error)

func DeleteRole

func DeleteRole(roleID int) error

func DeleteScene

func DeleteScene(sceneId int) (err error)

func DepartmentNameExist added in v1.9.1

func DepartmentNameExist(areaID uint64, name string) bool

DepartmentNameExist 部门名字是否存在

func EditLocationSort

func EditLocationSort(id int, sort int) (err error)

func EditUser

func EditUser(id int, updateUser User) (err error)

func FromArea

func FromArea(areaID uint64) func(db *gorm.DB) *gorm.DB

FromArea 查找家庭对应的数据

func GetAreaCount

func GetAreaCount() (count int64, err error)

func GetDB

func GetDB() *gorm.DB

func GetDBWithAreaScope

func GetDBWithAreaScope(areaID uint64) *gorm.DB

func GetDBWithAreaScopeTx added in v1.5.0

func GetDBWithAreaScopeTx(tx *gorm.DB, areaID uint64) *gorm.DB

func GetDefaultScope added in v1.5.0

func GetDefaultScope() (scope string)

func GetDepartmentCount added in v1.9.1

func GetDepartmentCount(areaID uint64) (count int64, err error)

GetDepartmentCount 该公司部门数量

func GetDepartmentCountByIds added in v1.9.1

func GetDepartmentCountByIds(departmentIds []int) (count int64, err error)

GetDepartmentCountByIds 获取通过部门id获取已有的部门数量

func GetLocationCount

func GetLocationCount(areaID uint64) (count int64, err error)

func GetRoleIdsByUid

func GetRoleIdsByUid(userId int) (roleIds []int, err error)

func GetSetting

func GetSetting(settingType string, setting interface{}, areaID uint64) (err error)

GetSetting 获取全局设置

func GetUIds

func GetUIds(areaID uint64) (ids []int, err error)

func InitClient added in v1.5.0

func InitClient() (err error)

InitClient 初始化Client

func InitRole

func InitRole(db *gorm.DB, areaId uint64) (err error)

func IsAccountNameExist

func IsAccountNameExist(accountName string) bool

func IsAreaType added in v1.9.1

func IsAreaType(areaType AreaType) bool

func IsCompany added in v1.9.1

func IsCompany(areaType AreaType) bool

func IsDepartmentExist added in v1.9.1

func IsDepartmentExist(areaID uint64, departmentID int) bool

IsDepartmentExist 是否存在该部门

func IsDepartmentManager added in v1.9.1

func IsDepartmentManager(areaID uint64, departmentID, userID int) (isManager bool, err error)

IsDepartmentManager 该用户是否是该部门主管

func IsDeviceActionPermit

func IsDeviceActionPermit(roleID int, action string, tx *gorm.DB) bool

func IsDeviceControlPermit

func IsDeviceControlPermit(userID, deviceID int, attr Attribute) bool

IsDeviceControlPermit 判断用户是否有该设备的某个控制权限

func IsDeviceControlPermitByAttr

func IsDeviceControlPermitByAttr(userID, deviceID, instanceID int, attr string) bool

IsDeviceControlPermitByAttr 判断用户是否有该设备的某个控制权限

func IsHome added in v1.9.1

func IsHome(areaType AreaType) bool

func IsLocationExist

func IsLocationExist(areaID uint64, locationID int) bool

func IsMeiHuiZhiJuBrand added in v1.9.1

func IsMeiHuiZhiJuBrand(pluginID string) bool

func IsOwner added in v1.5.0

func IsOwner(userID int) bool

IsOwner 是否是area拥有者

func IsOwnerOfArea added in v1.5.0

func IsOwnerOfArea(userID int, areaID uint64) bool

IsOwnerOfArea 是否是area拥有者

func IsPermit

func IsPermit(roleID int, action, target, attribute string, tx *gorm.DB) bool

func IsPluginAdd

func IsPluginAdd(pluginID string, areaID uint64) bool

func IsPluginDevelop added in v1.5.0

func IsPluginDevelop(pluginID string, areaID uint64) bool

IsPluginDevelop 是否是开发插件

func IsRoleNameExist

func IsRoleNameExist(name string, roleID int, areaID uint64) bool

func IsSceneNameExist

func IsSceneNameExist(name string, sceneId int, areaId uint64) (err error)

func JudgePermit

func JudgePermit(userID int, p types.Permission) bool

func LocationNameExist

func LocationNameExist(areaID uint64, name string) bool

func NewTaskLog

func NewTaskLog(target interface{}, taskID string, parentTaskID *string) error

func Open added in v1.9.1

func Open(dsn string) gorm.Dialector

func OpenSqlite added in v1.5.0

func OpenSqlite(path string, enableForeign bool) (*gorm.DB, error)

func PluginDeviceAttr

func PluginDeviceAttr(instanceID int, attr string) string

func ResetDepartmentManager added in v1.9.1

func ResetDepartmentManager(areaID uint64, departmentID ...int) (err error)

ResetDepartmentManager 重置部门的主管

func SavePluginInfo

func SavePluginInfo(pi PluginInfo) (err error)

func SetAreaOwnerID

func SetAreaOwnerID(id uint64, ownerID int, tx *gorm.DB) (err error)

func SwitchAutoScene

func SwitchAutoScene(scene *Scene, isExecute bool) error

SwitchAutoScene 切换自动场景开关

func SwitchAutoSceneByID

func SwitchAutoSceneByID(sceneID int, isExecute bool) error

SwitchAutoSceneByID 切换自动场景开关

func UnBindDepartmentDevice added in v1.9.1

func UnBindDepartmentDevice(deviceID int) (err error)

UnBindDepartmentDevice 解绑该设备与部门的绑定

func UnBindDepartmentDevices added in v1.9.1

func UnBindDepartmentDevices(departmentID int, tx *gorm.DB) (err error)

UnBindDepartmentDevices 解绑部门下的设备

func UnBindLocationDevice

func UnBindLocationDevice(deviceID int) (err error)

func UnBindLocationDevices

func UnBindLocationDevices(locationID int) (err error)

func UnScopedDelDepartmentUsers added in v1.9.1

func UnScopedDelDepartmentUsers(departmentID int) (err error)

UnScopedDelDepartmentUsers 通过部门ID删除部门与用户的关联

func UnScopedDelURoleByUid

func UnScopedDelURoleByUid(userID int) (err error)

func UnScopedDelUserDepartments added in v1.9.1

func UnScopedDelUserDepartments(userID int) (err error)

UnScopedDelUserDepartments 通过用户ID删除用户与部门的关联

func UnbindDepartmentManager added in v1.9.1

func UnbindDepartmentManager(managerID int, areaID uint64, tx *gorm.DB) (err error)

UnbindDepartmentManager 解除部门与主管关联

func UpdateArea

func UpdateArea(id uint64, updates map[string]interface{}) (err error)

UpdateArea 修改Area名称后,同时需要修改location中旧名称

func UpdateDepartment added in v1.9.1

func UpdateDepartment(id int, updateDepartment Department) (err error)

UpdateDepartment 更新部门数据

func UpdateDepartmentSort added in v1.9.1

func UpdateDepartmentSort(id int, sort int) (err error)

UpdateDepartmentSort 更新部门排序

func UpdateDevice

func UpdateDevice(id int, updateDevice Device) (err error)

func UpdateDeviceById added in v1.9.1

func UpdateDeviceById(id int, values interface{}, tx *gorm.DB) error

UpdateDeviceById 根据主键修改设备的值

func UpdateLocation

func UpdateLocation(id int, updateLocation Location) (err error)

func UpdateParentLog

func UpdateParentLog(parentTaskID string) error

UpdateParentLog 更新父任务的日志

func UpdatePluginInfo added in v1.5.0

func UpdatePluginInfo(pluginID string, pluginInfo PluginInfo) (err error)

UpdatePluginInfo 更新插件

func UpdateSceneByIDWithTx added in v1.9.1

func UpdateSceneByIDWithTx(sceneID int, update *Scene, tx *gorm.DB) (err error)

UpdateSceneByIDWithTx 根据SceneID更新场景

func UpdateSetting

func UpdateSetting(settingType string, setting interface{}, areaID uint64) (err error)

UpdateSetting 添加全局设置

func UpdateTaskLog

func UpdateTaskLog(taskID string, taskErr error) error

UpdateTaskLog 更新任务日志

func UserRolePermissionsScope added in v1.5.0

func UserRolePermissionsScope(userID int) func(db *gorm.DB) *gorm.DB

Types

type ActionType

type ActionType string

type Area

type Area struct {
	ID             uint64    `json:"id" gorm:"type:bigint"`
	Name           string    `json:"name"`
	CreatedAt      time.Time `json:"created_at"`
	OwnerID        int       `json:"owner_id"`
	Deleted        gorm.DeletedAt
	AreaType       AreaType `json:"area_type" gorm:"default:1"`
	IsSendAuthToSC bool     `json:"-"`
	IsBindCloud    bool     `json:"is_bind_cloud"`
}

Area 家庭

func CreateArea

func CreateArea(name string, areaType AreaType) (area Area, err error)

func GetAreaByID

func GetAreaByID(id uint64) (area Area, err error)

func GetAreaResultById

func GetAreaResultById(id uint64) (area Area, err error)

func GetAreas

func GetAreas() (areas []Area, err error)

func (*Area) AfterDelete

func (d *Area) AfterDelete(tx *gorm.DB) (err error)

func (*Area) BeforeCreate

func (d *Area) BeforeCreate(tx *gorm.DB) (err error)

func (Area) TableName

func (d Area) TableName() string

type AreaType added in v1.9.1

type AreaType int
const (
	AreaOfHome AreaType = iota + 1
	AreaOfCompany
)

func (AreaType) String added in v1.9.1

func (at AreaType) String() string

type Attr

type Attr struct {
	DeviceID   int
	InstanceID int
	Attribute  string
}

type AttrMetadata

type AttrMetadata struct {
	Timestamp int64 `json:"timestamp"`
}

type Attribute

type Attribute struct {
	server.Attribute
	InstanceID int `json:"instance_id"`
}

func (*Attribute) Operate added in v1.9.1

func (attr *Attribute) Operate(operatorType OperatorType, val interface{}) bool

type Client added in v1.5.0

type Client struct {
	ID           int
	Name         string `gorm:"UniqueIndex"`
	ClientID     string `gorm:"UniqueIndex"`
	ClientSecret string `gorm:"UniqueIndex"`
	GrantType    string
	AllowScope   string // 允许客户端申请的权限
}

func CreateClient added in v1.5.0

func CreateClient(grantType, allowScope, name string) (client Client, err error)

CreateClient 创建应用

func GetClientByClientID added in v1.5.0

func GetClientByClientID(clientID string) (client Client, err error)

GetClientByClientID 根据ClientID获取Client信息

func GetSAClient added in v1.5.0

func GetSAClient() (client Client, err error)

GetSAClient 获取SAClient

func GetSCClient added in v1.5.0

func GetSCClient() (client Client, err error)

GetSCClient 获取SCClient

func (*Client) BeforeCreate added in v1.5.0

func (c *Client) BeforeCreate(tx *gorm.DB) error

func (Client) TableName added in v1.5.0

func (c Client) TableName() string

type ConditionInfo

type ConditionInfo struct {
	SceneCondition
	Timing int64 `json:"timing"`
}

func (ConditionInfo) CheckCondition

func (c ConditionInfo) CheckCondition(userId int) (err error)

CheckCondition 触发条件校验

type ConditionType

type ConditionType int
const (
	ConditionTypeTiming ConditionType = iota + 1
	ConditionTypeDeviceStatus
)

type Department added in v1.9.1

type Department struct {
	ID   int    `json:"id"`
	Name string `json:"name" gorm:"uniqueIndex:dep_area_id_name" `
	Sort int    `json:"sort" `

	CreatedAt time.Time `json:"created_at"`

	AreaID uint64 `gorm:"type:bigint;uniqueIndex:dep_area_id_name"`
	Area   Area   `gorm:"constraint:OnDelete:CASCADE;"`

	ParentDepartmentID *int
	ChildDepartments   []Department `gorm:"foreignkey:parent_department_id"`

	ManagerID *int `gorm:"index"` // 主管
	Deleted   gorm.DeletedAt
}

func GetAllDepartments added in v1.9.1

func GetAllDepartments(parentDepartment []Department) (allDepartment []Department)

GetAllDepartments 获取父部门下所有子部门

func GetDepartmentByID added in v1.9.1

func GetDepartmentByID(id int) (department Department, err error)

GetDepartmentByID 通过id获取部门

func GetDepartments added in v1.9.1

func GetDepartments(areaID uint64) (departments []Department, err error)

GetDepartments 获取对应公司下的所有部门

func GetManagerDepartments added in v1.9.1

func GetManagerDepartments(areaID uint64, managerID int) (departments []Department, err error)

GetManagerDepartments 获取主管的部门

func GetUserDepartments added in v1.9.1

func GetUserDepartments(userID int) (departments []Department, err error)

GetUserDepartments 根据userID获取用户所在部门

func (*Department) BeforeCreate added in v1.9.1

func (d *Department) BeforeCreate(tx *gorm.DB) (err error)

func (Department) BeforeDelete added in v1.9.1

func (d Department) BeforeDelete(tx *gorm.DB) (err error)

func (Department) TableName added in v1.9.1

func (d Department) TableName() string

type DepartmentInfo added in v1.9.1

type DepartmentInfo struct {
	ID        int    `json:"id,omitempty" uri:"id"`
	Name      string `json:"name,omitempty"`
	IsManager bool   `json:"is_manager,omitempty"`
	PId       *int   `json:"pid,omitempty"`
}

func GetDepartmentsByUser added in v1.9.1

func GetDepartmentsByUser(user User) (departmentInfos []DepartmentInfo, err error)

GetDepartmentsByUser 获取用户所在部门信息

type DepartmentUser added in v1.9.1

type DepartmentUser struct {
	ID           int        `json:"id" gorm:"primary_key"`
	DepartmentID int        `gorm:"uniqueIndex:department_user_id"`
	Department   Department `gorm:"constraint:OnDelete:CASCADE;"`
	UserID       int        `gorm:"uniqueIndex:department_user_id"`
	User         User       `gorm:"constraint:OnDelete:CASCADE;"`
}

func WrapDepUsersOfDepID added in v1.9.1

func WrapDepUsersOfDepID(departmentId int, userIds []int) (departmentUsers []DepartmentUser)

WrapDepUsersOfDepID 包装部门对应用户的实体关系

func WrapDepUsersOfUId added in v1.9.1

func WrapDepUsersOfUId(userID int, departmentIds []int) (departmentUsers []DepartmentUser)

WrapDepUsersOfUId 包装部门对应用户的实体关系

func (DepartmentUser) TableName added in v1.9.1

func (du DepartmentUser) TableName() string

type Device

type Device struct {
	ID   int    `json:"id"`
	PID  int    `json:"pid"` // 子设备有值,所属父设备
	Name string `json:"name"`

	// Identity 设备在插件中的唯一标识符
	Identity     string    `json:"identity" gorm:"uniqueIndex:area_id_identity_plugin_id"`
	Model        string    `json:"model"`        // 型号
	Manufacturer string    `json:"manufacturer"` // 制造商
	Type         string    `json:"type"`         // 设备类型,如:light,switch...
	PluginID     string    `json:"plugin_id" gorm:"uniqueIndex:area_id_identity_plugin_id"`
	CreatedAt    time.Time `json:"created_at"`
	LocationID   int       `json:"location_id"`
	DepartmentID int       `json:"department_id"`
	Deleted      gorm.DeletedAt

	AreaID uint64 `json:"area_id" gorm:"type:bigint;uniqueIndex:area_id_identity_plugin_id"`
	Area   Area   `gorm:"constraint:OnDelete:CASCADE;"`

	Shadow     datatypes.JSON `json:"-"`
	ThingModel datatypes.JSON `json:"-"`
}

Device 识别的设备

func GetDeviceByID

func GetDeviceByID(id int) (device Device, err error)

func GetDeviceByIDWithUnscoped

func GetDeviceByIDWithUnscoped(id int) (device Device, err error)

GetDeviceByIDWithUnscoped 获取设备,包括已删除

func GetDeviceByIdentity added in v1.5.0

func GetDeviceByIdentity(identity string) (*Device, error)

func GetDevices

func GetDevices(areaID uint64) (devices []Device, err error)

func GetDevicesByDepartmentID added in v1.9.1

func GetDevicesByDepartmentID(departmentId int) (devices []Device, err error)

func GetDevicesByLocationID

func GetDevicesByLocationID(locationId int) (devices []Device, err error)

func GetDevicesByPluginID

func GetDevicesByPluginID(pluginID string) (devices []Device, err error)

func GetManufacturerDevice

func GetManufacturerDevice(areaID uint64, manufacturer, identity string) (device Device, err error)

GetManufacturerDevice 获取厂商的设备

func GetPluginDevice

func GetPluginDevice(areaID uint64, pluginID, identity string) (device Device, err error)

GetPluginDevice 获取插件的设备

func GetSaDevice

func GetSaDevice() (device Device, err error)

func GetZhitingDevices

func GetZhitingDevices() (devices []Device, err error)

GetZhitingDevices 获取所有智汀设备

func (*Device) AfterDelete

func (d *Device) AfterDelete(tx *gorm.DB) (err error)

func (Device) Clear added in v1.9.1

func (d Device) Clear() error

Clear 清除物模型信息

func (Device) IsInit added in v1.9.1

func (d Device) IsInit() bool

IsInit 是否已经初始化信息

func (Device) TableName

func (d Device) TableName() string

type DeviceStatusHistory added in v1.5.0

type DeviceStatusHistory struct {
	ID       int            `json:"id"`
	DeviceID int            `json:"device_id" gorm:"uniqueIndex:area_id_mau_device"`
	Context  datatypes.JSON `json:"context"`
	UserID   int            `json:"user_id"`
	AreaID   uint64         `json:"area_id" gorm:"type:bigint;uniqueIndex:area_id_mau_device"`
	CreateAt time.Time      `json:"create_at"`
}

func GetDeviceHistoryByCondition added in v1.5.0

func GetDeviceHistoryByCondition(deviceID int, areaID uint64) (histories []DeviceStatusHistory, err error)

func (DeviceStatusHistory) TableName added in v1.5.0

func (d DeviceStatusHistory) TableName() string

type Dialector added in v1.9.1

type Dialector struct {
	sqlite.Dialector
}

func (Dialector) DataTypeOf added in v1.9.1

func (dialector Dialector) DataTypeOf(field *schema.Field) string

func (Dialector) Migrator added in v1.9.1

func (dialector Dialector) Migrator(db *gorm.DB) gorm.Migrator

type GetCloudDiskCredentialSetting added in v1.9.1

type GetCloudDiskCredentialSetting struct {
	GetCloudDiskCredentialSetting bool `json:"get_cloud_disk_credential_setting"`
}

func GetDefaultCloudDiskCredentialSetting added in v1.9.1

func GetDefaultCloudDiskCredentialSetting() GetCloudDiskCredentialSetting

type GlobalSetting

type GlobalSetting struct {
	ID      int
	Type    string `gorm:"uniqueIndex:area_id_type"`
	Value   datatypes.JSON
	AreaID  uint64 `gorm:"type:bigint;uniqueIndex:area_id_type"`
	Area    Area   `gorm:"constraint:OnDelete:CASCADE;"`
	Deleted gorm.DeletedAt
}

GlobalSetting SA全局设置

func GetAllSetting added in v1.9.1

func GetAllSetting(areaID uint64) (gsList []GlobalSetting, err error)

func (GlobalSetting) TableName

func (g GlobalSetting) TableName() string

type Location

type Location struct {
	ID   int    `json:"id"`
	Name string `json:"name" gorm:"uniqueIndex:area_id_name" `
	Sort int    `json:"sort" `

	CreatedAt time.Time `json:"created_at"`

	AreaID uint64 `gorm:"type:bigint;uniqueIndex:area_id_name"`
	Area   Area   `gorm:"constraint:OnDelete:CASCADE;"`

	Deleted gorm.DeletedAt
}

Location 房间

func GetLocationByID

func GetLocationByID(id int) (location Location, err error)

func GetLocations

func GetLocations(areaID uint64) (locations []Location, err error)

func (*Location) BeforeCreate

func (d *Location) BeforeCreate(tx *gorm.DB) (err error)

func (Location) IsBelongsToUserArea

func (d Location) IsBelongsToUserArea(user User) bool

IsBelongsToUserArea 是否属于用户的家庭

func (Location) TableName

func (d Location) TableName() string

type LogSwitchSetting added in v1.9.1

type LogSwitchSetting struct {
	LogSwitch bool `json:"log_switch"`
}

日志开关

func GetDefaultLogSetting added in v1.9.1

func GetDefaultLogSetting() LogSwitchSetting

GetDefaultLogSetting 获取日志开关默认配置

type Metadata

type Metadata struct {
	Desired  map[int]map[string]AttrMetadata `json:"desired"`
	Reported map[int]map[string]AttrMetadata `json:"reported"`
}

type Migrator added in v1.9.1

type Migrator struct {
	sqlite.Migrator
}

func (Migrator) CreateTable added in v1.9.1

func (m Migrator) CreateTable(values ...interface{}) error

CreateTable

gorm Migrator包的CreateTable() 方法拼接建表语句时,默认会将dataType中不含"PRIMARY KEY"的主键字段加上primary Key 如果使用gorm的自定义type标签,当sqlite类型为INTEGER PRIMARY KEY AUTOINCREMENT 时gorm无法正常建表,且会导致云端sa使用的pg数据库失去bigserial特性, 所以兼容两个数据库,重写CreateTable方法,且加入strings.Contains(strings.ToUpper(field.TagSettings["SQLITETYPE"]), "PRIMARY KEY")判断,保证 数据库运行正常

type OperatorType

type OperatorType string
const (
	OperatorGT OperatorType = ">"
	OperatorLT OperatorType = "<"
	OperatorEQ OperatorType = "="
)

type PluginInfo

type PluginInfo struct {
	ID        int
	AreaID    uint64 `gorm:"uniqueIndex:area_plugin"`
	Area      Area   `gorm:"constraint:OnDelete:CASCADE;"`
	PluginID  string `gorm:"uniqueIndex:area_plugin"`
	Image     string
	Info      string
	ConfigMsg datatypes.JSON
	Status    int
	Version   string
	Source    string
	Brand     string
	ErrorInfo string
}

PluginInfo 开发者插件信息

func GetDevelopPlugins

func GetDevelopPlugins(areaID uint64) (pis []PluginInfo, err error)

GetDevelopPlugins 获取所有开发插件

func GetInstalledPlugins added in v1.5.0

func GetInstalledPlugins() (pis []PluginInfo, err error)

GetInstalledPlugins 获取所有已安装插件

func GetPlugin

func GetPlugin(pluginID string, areaID uint64) (plugin PluginInfo, err error)

func (PluginInfo) TableName

func (p PluginInfo) TableName() string

type RepeatType

type RepeatType int
const (
	RepeatTypeAllDay RepeatType = iota + 1
	RepeatTypeWorkDay
	RepeatTypeCustom
)

type Role

type Role struct {
	ID        int
	Name      string
	IsManager bool // 管理员角色不允许修改和删除

	AreaID  uint64 `gorm:"type:bigint;index"`
	Area    Area   `gorm:"constraint:OnDelete:CASCADE;"`
	Deleted gorm.DeletedAt
}

func AddManagerRoleWithDB

func AddManagerRoleWithDB(db *gorm.DB, roleName string, areaID uint64) (Role, error)

func AddRole

func AddRole(roleName string, areaID uint64) (Role, error)

func AddRoleWithDB

func AddRoleWithDB(db *gorm.DB, roleName string, areaID uint64) (Role, error)

func GetManagerRoleWithDB

func GetManagerRoleWithDB(db *gorm.DB) (roleInfo Role, err error)

func GetRoleByID

func GetRoleByID(id int) (role Role, err error)

func GetRoles

func GetRoles(areaID uint64) (roles []Role, err error)

func GetRolesByIds

func GetRolesByIds(roleIds []int) (roles []Role, err error)

func GetRolesByUid

func GetRolesByUid(userId int) (roles []Role, err error)

func GetRolesWithTx added in v1.5.0

func GetRolesWithTx(tx *gorm.DB, areaID uint64) (roles []Role, err error)

func UpdateRole

func UpdateRole(roleID int, roleName string) (Role, error)

func (*Role) AddPermissionForRole

func (r *Role) AddPermissionForRole(name, action, target, attr string) error

func (*Role) AddPermissions

func (r *Role) AddPermissions(ps ...types.Permission)

func (*Role) AddPermissionsWithDB

func (r *Role) AddPermissionsWithDB(db *gorm.DB, ps ...types.Permission)

func (*Role) AfterDelete

func (r *Role) AfterDelete(tx *gorm.DB) (err error)

func (*Role) BeforeCreate

func (r *Role) BeforeCreate(tx *gorm.DB) (err error)

func (*Role) BeforeDelete

func (r *Role) BeforeDelete(tx *gorm.DB) (err error)

func (*Role) BeforeUpdate

func (r *Role) BeforeUpdate(tx *gorm.DB) (err error)

func (*Role) DelPermission

func (r *Role) DelPermission(p types.Permission) error

func (Role) IsBelongsToUserArea

func (r Role) IsBelongsToUserArea(user User) bool

IsBelongsToUserArea 是否属于用户的家庭

func (Role) TableName

func (r Role) TableName() string

type RoleInfo

type RoleInfo struct {
	ID   int    `json:"id,omitempty" uri:"id"`
	Name string `json:"name,omitempty"`
}

func GetRoleInfos added in v1.9.1

func GetRoleInfos(uID int) (roleInfos []RoleInfo, err error)

type RolePermission

type RolePermission struct {
	ID        int
	RoleID    int  `gorm:"index:permission,unique"` // 角色
	Role      Role `gorm:"constraint:OnDelete:CASCADE;"`
	Name      string
	Action    string `gorm:"index:permission,unique"` // 动作
	Target    string `gorm:"index:permission,unique"` // 对象
	Attribute string `gorm:"index:permission,unique"` // 属性
}

RolePermission 角色权限role_tmpl

func (RolePermission) TableName

func (p RolePermission) TableName() string

type Scene

type Scene struct {
	ID             int    `json:"id"`
	Name           string `json:"name"`
	ConditionLogic int    `json:"condition_logic"` // 1 为 全部满足,2为满足任一

	// 生效时间的配置
	TimePeriodType TimePeriodType `json:"time_period"` // 全天1、时间段2
	EffectStart    time.Time      `json:"-"`
	EffectEnd      time.Time      `json:"-"`

	// 重复执行的配置
	RepeatType RepeatType `json:"repeat_type"` // 每天1,工作日2,自定义3
	RepeatDate string     `json:"repeat_date"` // 自定义的情况下:1234567

	// 设置为手动:false,则不能再设置其他两种
	AutoRun bool `json:"auto_run"` // true 就需要设置scene_condition,false 只需表示手动
	// 场景会自动执行: true
	IsOn bool `json:"is_on"`

	CreatorID       int              `json:"creator_id"`
	CreatedAt       time.Time        `json:"-"`
	SceneConditions []SceneCondition `json:"-" gorm:"constraint:OnDelete:CASCADE;"`
	SceneTasks      []SceneTask      `json:"scene_tasks" gorm:"constraint:OnDelete:CASCADE;"`
	Deleted         gorm.DeletedAt   `json:"-"`

	AreaID uint64 `gorm:"type:bigint;index"`
	Area   Area   `gorm:"constraint:OnDelete:CASCADE;"`

	Version int `json:"-" gorm:"column:version;default:1;NOT NULL"`
}

Scene 场景

func GetPendingScenesByTime

func GetPendingScenesByTime(t time.Time) (scenes []Scene, err error)

GetPendingScenesByTime 根据时间获取待执行的场景

func GetSceneByIDWithUnscoped

func GetSceneByIDWithUnscoped(id int) (scene Scene, err error)

GetSceneByIDWithUnscoped 获取场景,包括已删除

func GetSceneById

func GetSceneById(id int) (scene Scene, err error)

func GetSceneInfoById

func GetSceneInfoById(id int) (scene Scene, err error)

GetSceneInfoById 获取场景所有信息

func GetScenes

func GetScenes(areaID uint64) (scenes []Scene, err error)

func GetScenesByCondition

func GetScenesByCondition(deviceID int, attr Attribute) (scenes []Scene, err error)

GetScenesByCondition 根据条件获取场景

func (*Scene) BeforeSave

func (s *Scene) BeforeSave(tx *gorm.DB) (err error)

func (Scene) CheckConditionLogic

func (s Scene) CheckConditionLogic() bool

CheckConditionLogic 校验满足条件

func (Scene) CheckPeriodType

func (s Scene) CheckPeriodType() (err error)

CheckPeriodType 生效时间类型校验

func (Scene) CheckRepeatConfig

func (s Scene) CheckRepeatConfig() (err error)

CheckRepeatConfig 校验重复执行的配置

func (Scene) CheckSceneNameLength

func (s Scene) CheckSceneNameLength() (err error)

func (Scene) HaveTimeCondition added in v1.9.1

func (s Scene) HaveTimeCondition() bool

HaveTimeCondition 场景是否有定时条件

func (Scene) IsMatchAllCondition

func (s Scene) IsMatchAllCondition() bool

func (Scene) TableName

func (s Scene) TableName() string

type SceneCondition

type SceneCondition struct {
	ID            int           `json:"id"`
	SceneID       int           `json:"scene_id"`
	ConditionType ConditionType `json:"condition_type"`
	TimingAt      time.Time     `json:"-"` // 定时在某个时间

	// 设备有关配置
	DeviceID      int            `json:"device_id"`      // 或某个设备状态变化时
	Operator      OperatorType   `json:"operator"`       // 操作符,大于、小于、等于
	ConditionAttr datatypes.JSON `json:"condition_attr"` // refer to Attribute
}

SceneCondition 场景条件

func GetConditions

func GetConditions(deviceID int, attr Attribute) (conds []SceneCondition, err error)

GetConditions 获取符合设备属性的条件

func GetConditionsBySceneID

func GetConditionsBySceneID(sceneID int) (conditions []SceneCondition, err error)

func (SceneCondition) CheckConditionItem

func (d SceneCondition) CheckConditionItem(userId, deviceId int) (err error)

CheckConditionItem 触发条件为设备状态变化时,校验对应参数

func (SceneCondition) TableName

func (d SceneCondition) TableName() string

type SceneTask

type SceneTask struct {
	ID             int      `json:"id"`
	SceneID        int      `json:"scene_id"`
	ControlSceneID int      `json:"control_scene_id"` // ControlSceneID 控制场景id
	DelaySeconds   int      `json:"delay_seconds"`    // 延迟的秒数
	Type           TaskType `json:"type"`             // 任务目标:智能设备device或者是场景scene

	DeviceID   int            `json:"device_id"`
	Attributes datatypes.JSON `json:"attributes"` // refer to Attribute
}

SceneTask 场景任务

func GetSceneTasksBySceneID

func GetSceneTasksBySceneID(sceneID int) (sceneTasks []SceneTask, err error)

func (SceneTask) CheckTaskDevice

func (t SceneTask) CheckTaskDevice(userId int) (err error)

CheckTaskDevice 校验设备任务类型

func (SceneTask) CheckTaskType

func (t SceneTask) CheckTaskType() (err error)

CheckTaskType 执行任务类型校验

func (SceneTask) TableName

func (t SceneTask) TableName() string

type Shadow

type Shadow struct {
	State     State    `json:"state"`
	Metadata  Metadata `json:"metadata"`
	Timestamp int      `json:"timestamp"`
	Version   int      `json:"version"`
}

Shadow shadow of device

func NewShadow

func NewShadow() Shadow

func (Shadow) Get

func (s Shadow) Get(instanceID int, attribute string) (val interface{}, err error)

func (*Shadow) UpdateReported

func (s *Shadow) UpdateReported(instanceID int, attr server.Attribute)

type State

type State struct {
	// Desired 期望值
	Desired map[int]map[string]interface{} `json:"desired"`
	// Reported 报告值
	Reported map[int]map[string]interface{} `json:"reported"`
}

type TaskLog

type TaskLog struct {
	ID   int
	Name string   // 设备名称/场景名称
	Type TaskType // 任务类型

	Finish bool           // 是否完成
	Result TaskResultType // 执行结果
	Error  string

	DeviceLocation   string // 设备区域
	DeviceDepartment string // 设备部门

	TaskID        string    `gorm:"unique"` // 任务ID
	ParentTaskID  *string   // 父任务id
	ChildTaskLogs []TaskLog `gorm:"foreignkey:parent_task_id;references:task_id"` // 子任务日志

	FinishedAt time.Time
	CreatedAt  time.Time

	AreaID  uint64 `gorm:"type:bigint;index"`
	Area    Area   `gorm:"constraint:OnDelete:CASCADE;"`
	Deleted gorm.DeletedAt
}

TaskLog 任务日志

func (TaskLog) TableName

func (tl TaskLog) TableName() string

type TaskResultType

type TaskResultType int

TaskResultType 任务执行结果

const (
	TaskSuccess TaskResultType = iota + 1
	TaskPartSuccess
	TaskFail
	TaskTimeout
	TaskDeviceAlreadyDeleted
	TaskDeviceDisConnect
	TaskSceneAlreadyDeleted
)

type TaskType

type TaskType int
const (
	TaskTypeSmartDevice TaskType = iota + 1
	TaskTypeManualRun
	TaskTypeEnableAutoRun
	TaskTypeDisableAutoRun
)

type TimePeriodType

type TimePeriodType int
const (
	TimePeriodTypeAllDay TimePeriodType = iota + 1
	TimePeriodTypeCustom
)

type User

type User struct {
	// 使用自定义tag 配合 ./sqlite.go下的DataTypeOf() 和CreateTable()确保能给字段加上autoIncrement标签
	ID          int       `json:"id" gorm:"sqliteType:integer PRIMARY KEY AUTOINCREMENT"`
	AccountName string    `json:"account_name"`
	Nickname    string    `json:"nickname"`
	Phone       string    `json:"phone"`
	Password    string    `json:"password"`
	Salt        string    `json:"salt"`
	Key         string    `json:"key" gorm:"uniqueIndex"`
	CreatedAt   time.Time `json:"created_at"`

	AreaID             uint64 `gorm:"type:bigint;index"`
	Area               Area   `gorm:"constraint:OnDelete:CASCADE;"`
	PasswordUpdateTime time.Time
	Deleted            gorm.DeletedAt
}

func GetAreaOwner added in v1.5.0

func GetAreaOwner(areaID uint64) (user User, err error)

GetAreaOwner 获取家庭的拥有者

func GetDepartmentUsers added in v1.9.1

func GetDepartmentUsers(departmentID int) (users []User, err error)

GetDepartmentUsers 根据departmentID获取部门下用户

func GetUserByAccountName

func GetUserByAccountName(accountName string) (userInfo User, err error)

func GetUserByID

func GetUserByID(id int) (user User, err error)

func GetUserByIDAndAreaID added in v1.5.0

func GetUserByIDAndAreaID(uID int, areaID uint64) (user User, err error)

func GetUserByToken

func GetUserByToken(token string) (user User, err error)

func GetUsers added in v1.5.0

func GetUsers(areaID uint64) (users []User, err error)

func (*User) BeforeCreate

func (u *User) BeforeCreate(tx *gorm.DB) (err error)

func (User) BeforeDelete

func (u User) BeforeDelete(tx *gorm.DB) (err error)

func (User) BelongsToArea

func (u User) BelongsToArea(areaID uint64) bool

func (User) TableName

func (u User) TableName() string

type UserCredentialFoundSetting

type UserCredentialFoundSetting struct {
	UserCredentialFound bool `json:"user_credential_found"`
}

用户凭证配置

func GetDefaultUserCredentialFoundSetting

func GetDefaultUserCredentialFoundSetting() UserCredentialFoundSetting

GetDefaultUserCredentialFoundSetting 获取找回用户凭证默认配置

type UserInfo

type UserInfo struct {
	UserId        int        `json:"user_id"`
	RoleInfos     []RoleInfo `json:"role_infos"`
	AccountName   string     `json:"account_name"`
	Nickname      string     `json:"nickname"`
	Token         string     `json:"token,omitempty"`
	Phone         string     `json:"phone"`
	IsSetPassword bool       `json:"is_set_password"`
}

type UserPermissions

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

func GetUserPermissions

func GetUserPermissions(userID int) (up UserPermissions, err error)

GetUserPermissions 获取用户的所有权限

func (UserPermissions) IsDeviceAttrControlPermit added in v1.5.0

func (up UserPermissions) IsDeviceAttrControlPermit(deviceID, instanceID int, attr string) bool

IsDeviceAttrControlPermit 判断设备的属性是否有权限

func (UserPermissions) IsDeviceAttrPermit

func (up UserPermissions) IsDeviceAttrPermit(deviceID int, attr Attribute) bool

IsDeviceAttrPermit 判断设备的属性是否有权限

func (UserPermissions) IsDeviceControlPermit

func (up UserPermissions) IsDeviceControlPermit(deviceID int) bool

IsDeviceControlPermit 判断设备是否可控制

func (UserPermissions) IsOwner added in v1.5.0

func (up UserPermissions) IsOwner() bool

func (UserPermissions) IsPermit added in v1.5.0

func (up UserPermissions) IsPermit(tp types.Permission) bool

type UserRole

type UserRole struct {
	ID     int
	UserID int  `gorm:"uniqueIndex:uid_rid"`
	User   User `gorm:"constraint:OnDelete:CASCADE;"`
	RoleID int  `gorm:"uniqueIndex:uid_rid"`
	Role   Role `gorm:"constraint:OnDelete:CASCADE;"`
}

func GetUserRoles added in v1.5.0

func GetUserRoles(areaID uint64) (userRoles []UserRole, err error)

GetUserRoles 获取家庭下所有用户的角色

func (UserRole) TableName

func (ur UserRole) TableName() string

Jump to

Keyboard shortcuts

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