「连载」边缘计算(二十一)02-26:边缘部分源码(源码分析篇)

(接上篇)

DeviceTwin struct组成剖析

该部分对DeviceTwin struct的组成进行剖析。接着devicetwin struct调用链剖析的实例化DeviceTwin struct(dt := DeviceTwin{})往下剖析,进入DeviceTwin struct的定义,具体如下所示。

|----------------------------------------------------------------------------------------------------------|
| KubeEdge/edge/pkg/devicetwin/devicetwin.go |
| //DeviceTwin the module type DeviceTwin struct { context *context.Context dtcontroller *DTController } |

DeviceTwin struct的定义由 *context.Context 和 *DTController两部分组成。其中,*context.Context可以参考8.3.2,这里不再赘述。下面重点剖析DTController。

DTController的定义具体如下所示。

|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| KubeEdge/edge/pkg/devicetwin/dtcontroller.go |
| //DTController controller for devicetwin type DTController struct { HeartBeatToModule mapstringchan interface{} DTContexts *dtcontext.DTContext DTModules mapstringdtmodule.DTModule Stop chan bool } |

在DTController struct定义中发现\*dtcontext.DTContext和dtmodule.DTModule的定义具体如下所示。

|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| KubeEdge/edge/pkg/devicetwin/dtcontext/dtcontext.go |
| //DTContext context for devicetwin type DTContext struct { GroupID string NodeID string CommChan mapstringchan interface{} ConfirmChan chan interface{} ConfirmMap *sync.Map ModulesHealth *sync.Map ModulesContext *context.Context DeviceList *sync.Map DeviceMutex *sync.Map Mutex *sync.RWMutex // DBConn *dtclient.Conn State string } |

从DTContext struct的定义可以看出,DTContext struct主要用来实现devicetwin的通信和缓存。DTModule.DTModule struct定义如下所示。

|-------------------------------------------------------------------------------------------------|
| KubeEdge/edge/pkg/devicetwin/dtmodule/dtmodule.go |
| //DTModule module for devicetwin type DTModule struct { Name string Worker dtmanager.DTWorker } |

在 DTModule struct定义中,dtmanager.DTWorker是interface type,定义如下所示。

|----------------------------------------------------------------------|
| KubeEdge/edge/pkg/devicetwin/dtmanager/dtworker.go |
| //DTWorker worker for devicetwin type DTWorker interface { Start() } |

从dtmanager.DTWorker的interface type可以推测DTModule有多种类型,而且都实现了DTWorker interface。KubeEdge/edge/pkg/devicetwin/dtmodule/dtmodule.go中的InitWorker()就是用来实例化DTModule的多种类型的,具体定义如下所示。

|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| KubeEdge/edge/pkg/devicetwin/dtmodule/dtmodule.go |
| // InitWorker init worker func (dm *DTModule) InitWorker(recv chan interface{}, confirm chan interface{}, heartBeat chan interface{}, dtContext *dtcontext.DTContext) { switch dm.Name { case dtcommon.MemModule: dm.Worker = dtmanager.MemWorker{ Group: dtcommon.MemModule, Worker: dtmanager.Worker{ ReceiverChan: recv, ConfirmChan: confirm, HeartBeatChan: heartBeat, DTContexts: dtContext, }, } ... } |

从InitWorker()函数的定义中可以梳理出,DTModule有MemWorker、TwinWorker、DeviceWorker和CommWorker四种类型。

到此,EdgeCore中devicetwin的struct调用链剖析就全部结束了。

「 未完待续...... 」

相关推荐
回眸&啤酒鸭3 天前
【回眸】Minicart 电商购物车核心功能落地指南
人工智能
一隅论数智3 天前
给AI一张“业务概念地图“:本体如何从哲学走向企业智能
大数据·人工智能·经验分享·笔记·学习·学习方法·政务
AI的探索之旅3 天前
97 个 OpenCV 实例(三十):双目立体,从标定到点云
人工智能·opencv·计算机视觉
AlbertZein3 天前
Step-5-Preview 上手实测:3D 游戏、金融分析、网页设计一次跑完
人工智能·aigc
LaughingZhu3 天前
Product Hunt 每日热榜 | 2026-09-19
人工智能·深度学习·神经网络·搜索引擎·百度
美狐美颜SDK开放平台3 天前
开发直播APP时如何接入视频美颜SDK?开发流程与注意事项
android·人工智能·计算机视觉·音视频·直播美颜sdk
wukangjupingbb3 天前
智能网联汽车安全能力框架
人工智能
龙亘川3 天前
明月照湾区,智启新赛道:从顶流文旅IP盛会看智慧文旅升级路径
人工智能·智慧城市·开源软件·数据可视化
飞猫的边缘AI3 天前
边缘AI应用:家用AI摄像头怎么做数据训练?
人工智能·边缘计算·ai算法·边缘ai