「连载」边缘计算(二十一)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 map[string]chan interface{} DTContexts *dtcontext.DTContext DTModules map[string]dtmodule.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 map[string]chan 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调用链剖析就全部结束了。

未完待续......

相关推荐
致Great2 小时前
Gemma 3 27B版本超越DeepSeek V3:技术要点分析!
人工智能·llm
音视频牛哥3 小时前
SmartMediakit在四足机器人和无人机巡检中的创新应用方案
人工智能·机器学习·计算机视觉
致Great3 小时前
推理大模型的后训练增强技术-强化学习篇
人工智能·llm
致Great3 小时前
关于DeepResearch设计实现的碎碎念
人工智能·llm
致Great3 小时前
推理大模型的后训练增强技术--LLM 推理模型的现状
人工智能·llm
致Great3 小时前
大语言模型对软件工程师的影响曲线
人工智能·llm
AI Echoes3 小时前
大语言模型(LLM)的微调与应用
人工智能·语言模型·自然语言处理
Python测试之道4 小时前
Camel AI Owl + 阿里云QWQ 本地部署
人工智能·阿里云·云计算
訾博ZiBo4 小时前
AI日报 - 2025年3月13日
人工智能