「连载」边缘计算(二十一)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调用链剖析就全部结束了。

未完待续......

相关推荐
Moshow郑锴3 小时前
人工智能中的(特征选择)数据过滤方法和包裹方法
人工智能
TY-20253 小时前
【CV 目标检测】Fast RCNN模型①——与R-CNN区别
人工智能·目标检测·目标跟踪·cnn
CareyWYR4 小时前
苹果芯片Mac使用Docker部署MinerU api服务
人工智能
失散134 小时前
自然语言处理——02 文本预处理(下)
人工智能·自然语言处理
mit6.8245 小时前
[1Prompt1Story] 滑动窗口机制 | 图像生成管线 | VAE变分自编码器 | UNet去噪神经网络
人工智能·python
sinat_286945195 小时前
AI应用安全 - Prompt注入攻击
人工智能·安全·prompt
迈火6 小时前
ComfyUI-3D-Pack:3D创作的AI神器
人工智能·gpt·3d·ai·stable diffusion·aigc·midjourney
Moshow郑锴7 小时前
机器学习的特征工程(特征构造、特征选择、特征转换和特征提取)详解
人工智能·机器学习
CareyWYR8 小时前
每周AI论文速递(250811-250815)
人工智能
AI精钢8 小时前
H20芯片与中国的科技自立:一场隐形的博弈
人工智能·科技·stm32·单片机·物联网