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

未完待续......

相关推荐
陈橘又青8 分钟前
100% AI 写的开源项目三周多已获得 800 star 了
人工智能·后端·ai·restful·数据
中杯可乐多加冰26 分钟前
逻辑控制案例详解|基于smardaten实现OA一体化办公系统逻辑交互
人工智能·深度学习·低代码·oa办公·无代码·一体化平台·逻辑控制
IT_陈寒1 小时前
Redis实战:5个高频应用场景下的性能优化技巧,让你的QPS提升50%
前端·人工智能·后端
龙智DevSecOps解决方案1 小时前
Perforce《2025游戏技术现状报告》Part 1:游戏引擎技术的广泛影响以及生成式AI的成熟之路
人工智能·unity·游戏引擎·游戏开发·perforce
大佬,救命!!!1 小时前
更换适配python版本直接进行机器学习深度学习等相关环境配置(非仿真环境)
人工智能·python·深度学习·机器学习·学习笔记·详细配置
星空的资源小屋1 小时前
VNote:程序员必备Markdown笔记神器
javascript·人工智能·笔记·django
梵得儿SHI1 小时前
(第七篇)Spring AI 基础入门总结:四层技术栈全景图 + 三大坑根治方案 + RAG 进阶预告
java·人工智能·spring·springai的四大核心能力·向量维度·prompt模板化·向量存储检索
亚马逊云开发者1 小时前
Amazon Bedrock助力飞书深诺电商广告分类
人工智能
2301_823438021 小时前
解析论文《复杂海上救援环境中无人机群的双阶段协作路径规划与任务分配》
人工智能·算法·无人机
无心水2 小时前
【Python实战进阶】4、Python字典与集合深度解析
开发语言·人工智能·python·python字典·python集合·python实战进阶·python工业化实战进阶