diff --git a/apps/device/api/device.go b/apps/device/api/device.go index c00c74b..87bc243 100644 --- a/apps/device/api/device.go +++ b/apps/device/api/device.go @@ -11,6 +11,7 @@ import ( "github.com/PandaXGO/PandaKit/model" "github.com/PandaXGO/PandaKit/restfulx" "pandax/apps/device/util" + "pandax/pkg/cache" "pandax/pkg/global" "pandax/pkg/global_model" "pandax/pkg/shadow" @@ -29,7 +30,7 @@ type DeviceApi struct { } func (p *DeviceApi) GetDevicePanel(rc *restfulx.ReqCtx) { - get, err := global.PanelCache.ComputeIfAbsent("panel", func(k any) (any, error) { + get, err := cache.PanelCache.ComputeIfAbsent("panel", func(k any) (any, error) { var data entity.DeviceTotalOutput data.DeviceInfo = p.DeviceApp.FindDeviceCount() data.DeviceLinkStatusInfo = p.DeviceApp.FindDeviceCountGroupByLinkStatus() @@ -158,6 +159,8 @@ func (p *DeviceApi) DownAttribute(rc *restfulx.ReqCtx) { func (p *DeviceApi) InsertDevice(rc *restfulx.ReqCtx) { var data entity.Device restfulx.BindJsonAndValid(rc, &data) + product := p.ProductApp.FindOne(data.Pid) + biz.NotNil(product, "未查到所属产品信息") data.Owner = rc.LoginAccount.UserName data.OrgId = rc.LoginAccount.OrganizationId list := p.DeviceApp.FindList(entity.Device{Name: data.Name}) @@ -165,6 +168,8 @@ func (p *DeviceApi) InsertDevice(rc *restfulx.ReqCtx) { data.Id = global_model.GenerateID() data.LinkStatus = global.INACTIVE data.LastAt = time.Now() + data.Protocol = product.ProtocolName + p.DeviceApp.Insert(data) } @@ -172,6 +177,9 @@ func (p *DeviceApi) InsertDevice(rc *restfulx.ReqCtx) { func (p *DeviceApi) UpdateDevice(rc *restfulx.ReqCtx) { var data entity.Device restfulx.BindJsonAndValid(rc, &data) + product := p.ProductApp.FindOne(data.Pid) + biz.NotNil(product, "未查到所属产品信息") + data.Protocol = product.ProtocolName p.DeviceApp.Update(data) } diff --git a/apps/device/entity/device.go b/apps/device/entity/device.go index 4468af4..9962058 100644 --- a/apps/device/entity/device.go +++ b/apps/device/entity/device.go @@ -18,7 +18,6 @@ type DeviceGroup struct { Sort int64 `json:"sort" gorm:"type:int;comment:排序"` Status string `gorm:"type:varchar(1);comment:状态" json:"status"` Ext Ext `json:"ext" gorm:"type:json;comment:扩展"` //可扩展的kv map,承载设备组的外围信息 - IsDefault string `gorm:"type:varchar(1);comment:是否默认" json:"isDefault"` Children []DeviceGroup `json:"children" gorm:"-"` //子节点 @@ -47,7 +46,8 @@ type Device struct { OtaVersion string `json:"otaVersion" gorm:"type:varchar(64);comment:固件版本" ` //上一次固件升级的版本 Ext Ext `json:"ext" gorm:"type:json;comment:扩展"` //可扩展的kv map,承载设备组的外围信息 - RoleId int64 `gorm:"-"` // 角色数据权限 + Protocol string `json:"protocol" gorm:"-"` + RoleId int64 `gorm:"-"` // 角色数据权限 } type DeviceRes struct { diff --git a/apps/device/entity/product.go b/apps/device/entity/product.go index a1c21c1..2b7080f 100644 --- a/apps/device/entity/product.go +++ b/apps/device/entity/product.go @@ -48,7 +48,6 @@ type Product struct { DeviceType string `json:"deviceType" gorm:"type:varchar(64);comment:设备类型"` // 直连设备 网关设备 网关子设备 监控设备 RuleChainId string `json:"ruleChainId" gorm:"type:varchar(64);comment:规则链Id"` //可空,如果空就走根规则链 Status string `gorm:"type:varchar(1);comment:状态" json:"status"` - IsDefault string `gorm:"type:varchar(1);comment:是否默认" json:"isDefault"` RoleId int64 `gorm:"-"` // 角色数据权限 } diff --git a/apps/device/services/device.go b/apps/device/services/device.go index 525503f..d3a5665 100644 --- a/apps/device/services/device.go +++ b/apps/device/services/device.go @@ -1,9 +1,9 @@ package services import ( - "context" "github.com/PandaXGO/PandaKit/biz" "pandax/apps/device/entity" + "pandax/pkg/cache" "pandax/pkg/global" "pandax/pkg/global_model" "time" @@ -43,11 +43,11 @@ func (m *deviceModelImpl) Insert(data entity.Device) *entity.Device { token := GetDeviceToken(&data) // 子网关不需要设置token if data.DeviceType == global.GATEWAYS { - data.Token = "" - err := global.RedisDb.Set(data.Name, token.GetMarshal(), time.Hour*24*365) + err := cache.SetDeviceEtoken(data.Name, token.GetMarshal(), time.Hour*24*365) biz.ErrIsNil(err, "设备缓存失败") } else { - err := global.RedisDb.Set(data.Token, token.GetMarshal(), time.Hour*24*365) + data.Token = token.MD5ID() + err := cache.SetDeviceEtoken(data.Token, token.GetMarshal(), time.Hour*24*365) biz.ErrIsNil(err, "设备缓存失败") } //3 添加设备 @@ -157,13 +157,13 @@ func (m *deviceModelImpl) FindList(data entity.Device) *[]entity.DeviceRes { return &list } -// TODO 如果更改的是产品,tdengine的设备表也要更改 func (m *deviceModelImpl) Update(data entity.Device) *entity.Device { + token := GetDeviceToken(&data) if data.DeviceType == global.GATEWAYS { - data.Token = "" + err := cache.SetDeviceEtoken(data.Name, token.GetMarshal(), time.Hour*24*365) + biz.ErrIsNil(err, "设备更改缓存失败") } else { - token := GetDeviceToken(&data) - err := global.RedisDb.Set(data.Token, token.GetMarshal(), time.Hour*24*365) + err := cache.SetDeviceEtoken(data.Token, token.GetMarshal(), time.Hour*24*365) biz.ErrIsNil(err, "设备更改缓存失败") } biz.ErrIsNil(global.Db.Table(m.table).Updates(&data).Error, "修改设备失败") @@ -176,12 +176,16 @@ func (m *deviceModelImpl) UpdateStatus(id, linkStatus string) error { func (m *deviceModelImpl) Delete(ids []string) { biz.ErrIsNil(global.Db.Table(m.table).Delete(&entity.Device{}, "id in (?)", ids).Error, "删除设备失败") for _, id := range ids { - list := m.FindOne(id) + device := m.FindOne(id) // 删除表 - err := deleteDeviceTable(list.Name) + err := deleteDeviceTable(device.Name) global.Log.Error("设备时序表删除失败", err) // 删除所有缓存 - global.RedisDb.Del(context.Background(), id) + if device.DeviceType == global.GATEWAYS { + cache.DelDeviceEtoken(device.Name) + } else { + cache.DelDeviceEtoken(device.Token) + } } } @@ -214,19 +218,17 @@ func deleteDeviceTable(device string) error { func GetDeviceToken(data *entity.Device) *global_model.DeviceAuth { now := time.Now() etoken := &global_model.DeviceAuth{ - DeviceId: data.Id, - OrgId: data.OrgId, - Owner: data.Owner, - Name: data.Name, - DeviceType: data.DeviceType, - ProductId: data.Pid, + DeviceId: data.Id, + OrgId: data.OrgId, + Owner: data.Owner, + Name: data.Name, + DeviceType: data.DeviceType, + ProductId: data.Pid, + DeviceProtocol: data.Protocol, } //设备有效期360天 etoken.CreatedAt = now.Unix() etoken.ExpiredAt = now.Add(time.Hour * 24 * 365).Unix() - if data.Token == "" { - data.Token = etoken.MD5ID() - } return etoken } diff --git a/apps/device/services/product.go b/apps/device/services/product.go index 9471436..33c109f 100644 --- a/apps/device/services/product.go +++ b/apps/device/services/product.go @@ -1,10 +1,9 @@ package services import ( - "context" "github.com/PandaXGO/PandaKit/biz" - "log" "pandax/apps/device/entity" + "pandax/pkg/cache" "pandax/pkg/global" ) @@ -12,7 +11,6 @@ type ( ProductModel interface { Insert(data entity.Product) *entity.Product FindOne(id string) *entity.ProductRes - FindDefault() *entity.Product FindListPage(page, pageSize int, data entity.Product) (*[]entity.ProductRes, int64) FindList(data entity.Product) *[]entity.ProductRes Update(data entity.Product) *entity.Product @@ -52,14 +50,6 @@ func (m *productModelImpl) FindOne(id string) *entity.ProductRes { return resData } -func (m *productModelImpl) FindDefault() *entity.Product { - resData := new(entity.Product) - err := global.Db.Table(m.table).Where("is_default = ?", "1").First(resData).Error - log.Println(err) - biz.ErrIsNil(err, "查询默认产品失败") - return resData -} - func (m *productModelImpl) FindListPage(page, pageSize int, data entity.Product) (*[]entity.ProductRes, int64) { list := make([]entity.ProductRes, 0) var total int64 = 0 @@ -130,7 +120,7 @@ func (m *productModelImpl) Delete(ids []string) { err := deleteDeviceStable(id) global.Log.Error("时序数据库超级表删除失败", err) // 删除所有缓存 - global.RedisDb.Del(context.Background(), id) + cache.DelProductRule(id) } } diff --git a/fatal/latest.log b/fatal/latest.log index 0a5fd7d..c327fba 100644 --- a/fatal/latest.log +++ b/fatal/latest.log @@ -1399,3 +1399,19 @@ created by pandax/iothub/server/emqxserver.(*HookGrpcService).OnMessagePublish 2023-10-17 11:28:38.846 [INFO] [D:/workspace/go/project/PandaX/PandaX/pkg/middleware/log.go:34] [uname=panda] [PUT=/device/template] [uid=1] : 修改Template信息 ->528ms 2023-10-17 11:28:38.876 [INFO] [D:/workspace/go/project/PandaX/PandaX/pkg/middleware/log.go:34] [uid=1] [uname=panda] [GET=/device/template/list] : 获取Template分页列表 ->11ms 2023-10-17 11:31:04.746 [ERROR] [D:/workspace/go/project/PandaX/PandaX/pkg/transport/http_server.go:48] : error http serve: http: Server closed +2023-10-17 11:31:12.053 [INFO] [D:/workspace/go/pkg/mod/github.com/!panda!x!g!o/!panda!kit@v0.0.0-20231014010146-3a169666cab5/starter/gorm.go:36] : 连接mysql [root:!MyEMS1@tcp(127.0.0.1:3306)/pandax_iot?charset=utf8&loc=Local&parseTime=true] +2023-10-17 11:31:12.059 [INFO] [D:/workspace/go/project/PandaX/PandaX/main.go:44] : mysql连接成功 +2023-10-17 11:31:12.062 [INFO] [D:/workspace/go/project/PandaX/PandaX/main.go:49] : Redis连接成功 +2023-10-17 11:31:12.062 [INFO] [D:/workspace/go/project/PandaX/PandaX/main.go:56] : Tdengine连接成功 +2023-10-17 11:31:12.069 [INFO] [D:/workspace/go/project/PandaX/PandaX/main.go:74] : 路由初始化完成 +2023-10-17 11:31:12.069 [INFO] [D:/workspace/go/project/PandaX/PandaX/pkg/transport/http_server.go:38] : HTTP Server listen: :7788 +2023-10-17 11:31:12.069 [INFO] [D:/workspace/go/project/PandaX/PandaX/pkg/transport/http_server.go:46] : HTTP Server listen: :7788 +2023-10-17 11:31:12.070 [INFO] [D:/workspace/go/project/PandaX/PandaX/iothub/server/emqxserver/hook.go:33] : IOTHUB HOOK Start SUCCESS,Grpc Server listen: :9001 +2023-10-17 11:31:12.070 [INFO] [D:/workspace/go/project/PandaX/PandaX/iothub/server/tcpserver/hook.go:29] : TCP IOTHUB HOOK Start SUCCESS, Server listen: :9003 +2023-10-17 11:31:12.070 [INFO] [D:/workspace/go/project/PandaX/PandaX/iothub/server/udpserver/hook.go:26] : UDP IOTHUB HOOK Start SUCCESS, Server listen: :9003 +2023-10-17 11:31:12.076 [INFO] [D:/workspace/go/project/PandaX/PandaX/apps/job/jobs/jobbase.go:86] : 2023-10-17 11:31:12 [INFO] JobCore start success. +2023-10-17 11:31:16.701 [INFO] [D:/workspace/go/project/PandaX/PandaX/iothub/server/emqxserver/hook.go:97] : pandax断开连接 +2023-10-17 11:31:16.702 [INFO] [D:/workspace/go/project/PandaX/PandaX/iothub/server/emqxserver/hook.go:113] : 账号pandax,开始认证 +2023-10-17 11:31:16.706 [INFO] [D:/workspace/go/project/PandaX/PandaX/iothub/server/emqxserver/hook.go:84] : Client emqx@172.17.0.3 Connected +2023-10-17 11:31:31.409 [INFO] [D:/workspace/go/project/PandaX/PandaX/pkg/middleware/log.go:34] [uid=1] [uname=panda] [DELETE=/device/template/C-ic8z557Q] : 删除Template信息 ->369ms +2023-10-17 11:31:31.439 [INFO] [D:/workspace/go/project/PandaX/PandaX/pkg/middleware/log.go:34] [GET=/device/template/list] [uid=1] [uname=panda] : 获取Template分页列表 ->8ms diff --git a/iothub/hook_message_work/hook_message_work.go b/iothub/hook_message_work/hook_message_work.go index c16ca23..65d3162 100644 --- a/iothub/hook_message_work/hook_message_work.go +++ b/iothub/hook_message_work/hook_message_work.go @@ -11,6 +11,7 @@ import ( ruleEntity "pandax/apps/rule/entity" ruleService "pandax/apps/rule/services" "pandax/iothub/netbase" + "pandax/pkg/cache" "pandax/pkg/global" "pandax/pkg/global_model" "pandax/pkg/rule_engine" @@ -108,6 +109,7 @@ func (s *HookService) handleOne(msg *netbase.DeviceEventInfo) { }() } +// 根据产品Id从缓存中获取规则链,没有就查询 func getRuleChain(etoken *global_model.DeviceAuth) *ruleEntity.RuleDataJson { defer func() { if err := recover(); err != nil { @@ -115,7 +117,7 @@ func getRuleChain(etoken *global_model.DeviceAuth) *ruleEntity.RuleDataJson { } }() key := etoken.ProductId - get, err := global.ProductCache.ComputeIfAbsent(key, func(k any) (any, error) { + get, err := cache.ComputeIfAbsentProductRule(key, func(k any) (any, error) { one := services.ProductModelDao.FindOne(k.(string)) rule := ruleService.RuleChainModelDao.FindOne(one.RuleChainId) return rule.RuleDataJson, nil @@ -127,6 +129,7 @@ func getRuleChain(etoken *global_model.DeviceAuth) *ruleEntity.RuleDataJson { return &ruleData } +// 构建规则链执行的消息 func buildRuleMessage(etoken *global_model.DeviceAuth, msgVals map[string]interface{}, msgType string) *message.Message { metadataVals := map[string]interface{}{ "deviceId": etoken.DeviceId, @@ -159,7 +162,7 @@ func SendZtWebsocket(deviceId, message string) { // SetDeviceShadow 设置设备点 func SetDeviceShadow(etoken *global_model.DeviceAuth, msgVals map[string]interface{}, msgType string) { defer func() { - if err := recover(); &err != nil { + if err := recover(); err != nil { global.Log.Error(err) } }() diff --git a/iothub/netbase/hook_base.go b/iothub/netbase/hook_base.go index d1e32df..5a2e33e 100644 --- a/iothub/netbase/hook_base.go +++ b/iothub/netbase/hook_base.go @@ -5,6 +5,7 @@ import ( "pandax/apps/device/entity" "pandax/apps/device/services" "pandax/iothub/server/emqxserver/protobuf" + "pandax/pkg/cache" "pandax/pkg/global" "pandax/pkg/global_model" "pandax/pkg/tdengine" @@ -22,27 +23,27 @@ func Auth(authToken string) bool { } etoken := &global_model.DeviceAuth{} // redis 中有就查询,没有就添加 - exists, err := global.RedisDb.Exists(global.RedisDb.Context(), authToken).Result() - if exists == 1 { - err = global.RedisDb.Get(authToken, etoken) + exists := cache.ExistsDeviceEtoken(authToken) + if exists { + err := cache.GetDeviceEtoken(authToken, etoken) + if err != nil { + global.Log.Infof("认证失败,缓存读取设备错误: %s", err) + return false + } } else { device, err := services.DeviceModelDao.FindOneByToken(authToken) if err != nil { - global.Log.Infof("设备token %s 不存在", authToken) + global.Log.Infof("认证失败,设备token %s 不存在", authToken) return false } etoken = services.GetDeviceToken(&device.Device) etoken.DeviceProtocol = device.Product.ProtocolName - err = global.RedisDb.Set(authToken, etoken.GetMarshal(), time.Hour*24*365) + err = cache.SetDeviceEtoken(authToken, etoken.GetMarshal(), time.Hour*24*365) if err != nil { - global.Log.Infof("设备TOKEN %s添加缓存失败", authToken) + global.Log.Infof("认证失败,设备TOKEN %s添加缓存失败", authToken) return false } } - if err != nil { - global.Log.Infof("invalid authToken %s", authToken) - return false - } // 判断token是否过期了, 设备过期 if etoken.ExpiredAt < time.Now().Unix() { global.Log.Infof("设备authToken %s 失效", authToken) @@ -61,9 +62,13 @@ func SubAuth(name string) (*global_model.DeviceAuth, bool) { }() etoken := &global_model.DeviceAuth{} // redis 中有就查询,没有就添加 - exists, err := global.RedisDb.Exists(global.RedisDb.Context(), name).Result() - if exists == 1 { - err = etoken.GetDeviceToken(name) + exists := cache.ExistsDeviceEtoken(name) + if exists { + err := etoken.GetDeviceToken(name) + if err != nil { + global.Log.Infof("认证失败,缓存读取设备错误,无效的设备标识: %s", err) + return nil, false + } } else { device, err := services.DeviceModelDao.FindOneByName(name) // 没有设备就要创建子设备 @@ -73,16 +78,12 @@ func SubAuth(name string) (*global_model.DeviceAuth, bool) { } etoken = services.GetDeviceToken(&device.Device) etoken.DeviceProtocol = device.Product.ProtocolName - err = global.RedisDb.Set(name, etoken.GetMarshal(), time.Hour*24*365) + err = cache.SetDeviceEtoken(name, etoken.GetMarshal(), time.Hour*24*365) if err != nil { global.Log.Infof("设备标识 %s添加缓存失败", name) return nil, false } } - if err != nil { - global.Log.Infof("无效设备标识 %s", name) - return nil, false - } return etoken, true } diff --git a/main.go b/main.go index aa9c291..e56827b 100644 --- a/main.go +++ b/main.go @@ -15,6 +15,7 @@ import ( "os" "os/signal" "pandax/iothub" + "pandax/pkg/cache" "pandax/pkg/config" "pandax/pkg/global" "pandax/pkg/initialize" @@ -48,7 +49,7 @@ var rootCmd = &cobra.Command{ } else { global.Log.Info("Redis连接成功") } - global.RedisDb = client + cache.RedisDb = client tDengine, err := tdengine.NewTdengine(global.Conf.Taos.Username, global.Conf.Taos.Password, global.Conf.Taos.Host, global.Conf.Taos.Database) if err != nil { global.Log.Panic("Tdengine连接错误") diff --git a/pkg/cache/device_etoken.go b/pkg/cache/device_etoken.go new file mode 100644 index 0000000..5783eaa --- /dev/null +++ b/pkg/cache/device_etoken.go @@ -0,0 +1,33 @@ +package cache + +import ( + "context" + "github.com/PandaXGO/PandaKit/rediscli" + "time" +) + +var RedisDb *rediscli.RedisDB + +// SetDeviceEtoken key 是设备的时候为token, 是子设备的时候为设备编码 +func SetDeviceEtoken(key string, value any, duration time.Duration) error { + return RedisDb.Set(key, value, duration) +} + +// GetDeviceEtoken value 是参数指针 +func GetDeviceEtoken(key string, value interface{}) error { + return RedisDb.Get(key, value) +} + +// DelDeviceEtoken 删除指定的key +func DelDeviceEtoken(key string) error { + return RedisDb.Del(context.Background(), key).Err() +} + +func ExistsDeviceEtoken(key string) bool { + exists, _ := RedisDb.Exists(RedisDb.Context(), key).Result() + if exists == 1 { + return true + } else { + return false + } +} diff --git a/pkg/cache/panel.go b/pkg/cache/panel.go new file mode 100644 index 0000000..0302cf4 --- /dev/null +++ b/pkg/cache/panel.go @@ -0,0 +1,8 @@ +package cache + +import ( + "github.com/PandaXGO/PandaKit/cache" + "time" +) + +var PanelCache = cache.NewTimedCache(cache.NoExpiration, 600*time.Second) diff --git a/pkg/cache/product_rule.go b/pkg/cache/product_rule.go new file mode 100644 index 0000000..897569b --- /dev/null +++ b/pkg/cache/product_rule.go @@ -0,0 +1,20 @@ +package cache + +import ( + "github.com/PandaXGO/PandaKit/cache" + "time" +) + +var ProductCache = cache.NewTimedCache(cache.NoExpiration, 24*time.Hour) + +func ComputeIfAbsentProductRule(key string, fun func(any) (any, error)) (any, error) { + return ProductCache.ComputeIfAbsent(key, fun) +} + +func DelProductRule(key string) { + ProductCache.Delete(key) +} + +func PutProductRule(key string, data any) { + ProductCache.Put(key, data) +} diff --git a/pkg/global/global.go b/pkg/global/global.go index 8f938a1..167e56e 100644 --- a/pkg/global/global.go +++ b/pkg/global/global.go @@ -1,26 +1,17 @@ package global import ( - "github.com/PandaXGO/PandaKit/cache" - "github.com/PandaXGO/PandaKit/rediscli" "github.com/sirupsen/logrus" "gorm.io/gorm" "pandax/pkg/config" "pandax/pkg/events" "pandax/pkg/tdengine" - "time" ) var ( - Log *logrus.Logger // 日志 - Db *gorm.DB // gorm - RedisDb *rediscli.RedisDB - TdDb *tdengine.TdEngine - Conf *config.Config + Log *logrus.Logger // 日志 + Db *gorm.DB // gorm + TdDb *tdengine.TdEngine + Conf *config.Config ) var EventEmitter = events.EventEmitter{} - -// Cache 默认10分钟 -var ProductCache = cache.NewTimedCache(cache.NoExpiration, 24*time.Hour) -var SubDeviceCache = cache.NewTimedCache(cache.NoExpiration, 24*time.Hour) -var PanelCache = cache.NewTimedCache(cache.NoExpiration, 600*time.Second) diff --git a/pkg/global_model/device_auth_model.go b/pkg/global_model/device_auth_model.go index 87bec3b..dbe8dfe 100644 --- a/pkg/global_model/device_auth_model.go +++ b/pkg/global_model/device_auth_model.go @@ -10,7 +10,7 @@ import ( "math/rand" "pandax/apps/system/entity" "pandax/apps/system/services" - "pandax/pkg/global" + "pandax/pkg/cache" "strconv" "strings" "time" @@ -30,7 +30,7 @@ type DeviceAuth struct { } func (entity *DeviceAuth) GetDeviceToken(key string) error { - if err := global.RedisDb.Get(key, entity); err != nil { + if err := cache.GetDeviceEtoken(key, entity); err != nil { return err } return nil diff --git a/pkg/initialize/event.go b/pkg/initialize/event.go index 22ecea1..e5afffe 100644 --- a/pkg/initialize/event.go +++ b/pkg/initialize/event.go @@ -3,6 +3,7 @@ package initialize import ( "pandax/apps/device/entity" "pandax/apps/device/services" + "pandax/pkg/cache" "pandax/pkg/events" "pandax/pkg/global" ) @@ -17,7 +18,7 @@ func InitEvents() { }) if list != nil { for _, product := range *list { - global.ProductCache.Put(product.Id, codeData) + cache.PutProductRule(product.Id, codeData) } } }) diff --git a/pkg/shadow/device.go b/pkg/shadow/device.go index 5b4a3c5..09a16eb 100644 --- a/pkg/shadow/device.go +++ b/pkg/shadow/device.go @@ -38,15 +38,3 @@ func NewDevicePoint(pointName string, value interface{}) DevicePoint { UpdatedAt: time.Now(), } } - -func InitDeviceShadow(deviceName, ProductId string) Device { - device, err := DeviceShadowInstance.GetDevice(deviceName) - if err == UnknownDeviceErr { - attributes := make(map[string]DevicePoint) - telemetry := make(map[string]DevicePoint) - device = NewDevice(deviceName, ProductId, attributes, telemetry) - DeviceShadowInstance.AddDevice(device) - //shadow.DeviceShadowInstance.SetDeviceTTL() - } - return device -} diff --git a/pkg/shadow/shadow.go b/pkg/shadow/shadow.go index 52ec9ac..ea98ab1 100644 --- a/pkg/shadow/shadow.go +++ b/pkg/shadow/shadow.go @@ -57,6 +57,18 @@ func init() { DeviceShadowInstance = shadow } +func InitDeviceShadow(deviceName, ProductId string) Device { + device, err := DeviceShadowInstance.GetDevice(deviceName) + if err == UnknownDeviceErr { + attributes := make(map[string]DevicePoint) + telemetry := make(map[string]DevicePoint) + device = NewDevice(deviceName, ProductId, attributes, telemetry) + DeviceShadowInstance.AddDevice(device) + //shadow.DeviceShadowInstance.SetDeviceTTL() + } + return device +} + func (d *deviceShadow) AddDevice(device Device) (err error) { if _, ok := d.m.Load(device.Name); ok { return DeviceRepeatErr