{"id":742,"date":"2024-11-10T22:51:40","date_gmt":"2024-11-10T14:51:40","guid":{"rendered":"https:\/\/www.db2go.net\/?p=742"},"modified":"2024-11-10T22:51:40","modified_gmt":"2024-11-10T14:51:40","slug":"%e4%b8%89-%e8%ae%be%e5%a4%87%e7%ae%a1%e7%90%86%e5%92%8c%e5%8d%8f%e8%ae%ae%e9%80%82%e9%85%8d%e7%9a%84%e6%a0%b8%e5%bf%83%e4%bb%a3%e7%a0%81","status":"publish","type":"post","link":"https:\/\/www.db2go.net\/?p=742","title":{"rendered":"\u4e09.\u8bbe\u5907\u7ba1\u7406\u548c\u534f\u8bae\u9002\u914d\u7684\u6838\u5fc3\u4ee3\u7801"},"content":{"rendered":"<h2>\u4e09.\u8bbe\u5907\u7ba1\u7406\u548c\u534f\u8bae\u9002\u914d\u7684\u6838\u5fc3\u4ee3\u7801<\/h2>\n<p>\u5728 KubeEdge \u4e2d\uff0c\u8bbe\u5907\u7ba1\u7406\u548c\u534f\u8bae\u9002\u914d\u662f\u901a\u8fc7 <code>devicecontroller<\/code>\uff08\u4e91\u7aef\uff09\u548c <code>devicetwin<\/code>\uff08\u8fb9\u7aef\uff09\u6a21\u5757\u5171\u540c\u5b9e\u73b0\u7684\u3002\u8fd9\u4e9b\u6a21\u5757\u8d1f\u8d23\u8fb9\u7f18\u8bbe\u5907\u7684\u8fde\u63a5\u3001\u72b6\u6001\u7ba1\u7406\u548c\u534f\u8bae\u9002\u914d\uff0c\u5c24\u5176\u9002\u7528\u4e8e\u7269\u8054\u7f51\uff08IoT\uff09\u573a\u666f\uff0c\u4f7f KubeEdge \u53ef\u4ee5\u76f4\u63a5\u7ba1\u7406\u548c\u63a7\u5236\u8fb9\u7f18\u8bbe\u5907\u3002\u4ee5\u4e0b\u662f\u8bbe\u5907\u7ba1\u7406\u548c\u534f\u8bae\u9002\u914d\u6838\u5fc3\u4ee3\u7801\u7684\u8be6\u7ec6\u5206\u6790\uff1a<\/p>\n<h3>1. \u4e91\u7aef\u8bbe\u5907\u7ba1\u7406\u6a21\u5757 (<code>devicecontroller<\/code>)<\/h3>\n<p><code>devicecontroller<\/code> \u662f\u4e91\u7aef\u8bbe\u5907\u7ba1\u7406\u6a21\u5757\uff0c\u8d1f\u8d23\u8bbe\u5907\u4fe1\u606f\u7684\u7ef4\u62a4\u3001\u6570\u636e\u7684\u6536\u96c6\u548c\u4e0b\u53d1\u7b49\u5de5\u4f5c\u3002\u8bbe\u5907\u7ba1\u7406\u7684\u6838\u5fc3\u4ee3\u7801\u4e3b\u8981\u4f4d\u4e8e <code>cloud\/pkg\/devicecontroller<\/code> \u76ee\u5f55\u4e2d\u3002<\/p>\n<h4>\u8bbe\u5907\u72b6\u6001\u540c\u6b65<\/h4>\n<p>\u5728 <code>devicecontroller<\/code> \u4e2d\uff0c\u8bbe\u5907\u72b6\u6001\u540c\u6b65\u4ee3\u7801\u8d1f\u8d23\u4ece\u4e91\u7aef\u53d1\u9001\u6307\u4ee4\u5230\u8fb9\u7aef\u8bbe\u5907\uff0c\u540c\u65f6\u6536\u96c6\u8bbe\u5907\u7684\u72b6\u6001\u4fe1\u606f\uff0c\u5e76\u5c06\u8fd9\u4e9b\u72b6\u6001\u4fe1\u606f\u540c\u6b65\u5230\u4e91\u7aef\u3002<\/p>\n<ul>\n<li><strong>\u4e0b\u884c\u6d88\u606f\uff08Downstream\uff09<\/strong>\uff1a<code>DownstreamController<\/code> \u662f <code>devicecontroller<\/code> \u7684\u4e00\u4e2a\u5b50\u6a21\u5757\uff0c\u8d1f\u8d23\u5c06\u8bbe\u5907\u7684\u63a7\u5236\u547d\u4ee4\u4ece\u4e91\u7aef\u53d1\u9001\u5230\u8fb9\u7aef\u8bbe\u5907\u3002\n<p>\u4ee3\u7801\u6587\u4ef6\uff1a<code>cloud\/pkg\/devicecontroller\/controller\/downstream.go<\/code><\/p>\n<pre><code class=\"language-go line-numbers\">func (dc *DownstreamController) syncDeviceModel() {\n  \/\/ \u83b7\u53d6\u8bbe\u5907\u6a21\u578b\uff0c\u5e76\u5c06\u66f4\u65b0\u63a8\u9001\u5230\u8fb9\u7aef\n  deviceModelList, err := dc.deviceService.QueryDeviceModel()\n  if err == nil {\n      dc.deviceManager.UpdateDeviceModel(deviceModelList)\n  }\n}\n<\/code><\/pre>\n<p>\u8fd9\u4e2a\u51fd\u6570\u4ece\u4e91\u7aef\u67e5\u8be2\u8bbe\u5907\u6a21\u578b\uff0c\u5e76\u5c06\u5176\u4e0b\u53d1\u5230\u8fb9\u7aef\uff0c\u66f4\u65b0\u8fb9\u7aef\u7684\u8bbe\u5907\u72b6\u6001\u3002<code>deviceService<\/code> \u63a5\u53e3\u8d1f\u8d23\u4e0e\u4e91\u7aef\u6570\u636e\u5e93\u4ea4\u4e92\uff0c\u800c <code>UpdateDeviceModel<\/code> \u51fd\u6570\u8d1f\u8d23\u5c06\u8bbe\u5907\u6a21\u578b\u540c\u6b65\u5230\u8fb9\u7aef\u3002<\/p>\n<\/li>\n<li>\n<p><strong>\u4e0a\u884c\u6d88\u606f\uff08Upstream\uff09<\/strong>\uff1a\u8bbe\u5907\u7684\u4e0a\u884c\u6d88\u606f\u5904\u7406\u5728 <code>UpstreamController<\/code> \u6a21\u5757\u4e2d\u5b8c\u6210\uff0c\u8d1f\u8d23\u63a5\u6536\u6765\u81ea\u8fb9\u7aef\u7684\u8bbe\u5907\u6570\u636e\uff0c\u5e76\u5728\u4e91\u7aef\u8fdb\u884c\u66f4\u65b0\u3002<\/p>\n<p>\u4ee3\u7801\u6587\u4ef6\uff1a<code>cloud\/pkg\/devicecontroller\/controller\/upstream.go<\/code><\/p>\n<pre><code class=\"language-go line-numbers\">func (uc *UpstreamController) handleDeviceStatusUpdate(message model.Message) {\n  deviceStatus := message.Content.(DeviceStatus)\n  err := uc.deviceService.UpdateDeviceStatus(deviceStatus)\n  if err != nil {\n      log.Errorf(\"Failed to update device status: %v\", err)\n  }\n}\n<\/code><\/pre>\n<p>\u8fd9\u4e2a\u51fd\u6570\u4ece\u8fb9\u7aef\u63a5\u6536\u8bbe\u5907\u7684\u72b6\u6001\u66f4\u65b0\uff0c\u5e76\u8c03\u7528 <code>UpdateDeviceStatus<\/code> \u51fd\u6570\u5c06\u72b6\u6001\u540c\u6b65\u5230\u4e91\u7aef\u6570\u636e\u5e93\u3002<\/p>\n<\/li>\n<\/ul>\n<h3>2. \u8fb9\u7aef\u8bbe\u5907\u7ba1\u7406\u6a21\u5757 (<code>devicetwin<\/code>)<\/h3>\n<p><code>devicetwin<\/code> \u662f\u8fb9\u7aef\u8bbe\u5907\u7ba1\u7406\u6a21\u5757\uff0c\u4e3b\u8981\u8d1f\u8d23\u8bbe\u5907\u7684\u72b6\u6001\u540c\u6b65\u3001\u63a7\u5236\u6307\u4ee4\u7684\u63a5\u6536\u4e0e\u6267\u884c\u3002\u6838\u5fc3\u4ee3\u7801\u4f4d\u4e8e <code>edge\/pkg\/devicetwin<\/code> \u76ee\u5f55\u4e2d\uff0c<code>devicetwin<\/code> \u8fd8\u7ef4\u62a4\u4e86\u6bcf\u4e2a\u8bbe\u5907\u7684\u5f71\u5b50\uff08Device Twin\uff09\uff0c\u4fbf\u4e8e\u5b9e\u73b0\u8bbe\u5907\u72b6\u6001\u7684\u79bb\u7ebf\u7f13\u5b58\u3002<\/p>\n<h4>\u8bbe\u5907\u5f71\u5b50\u7ba1\u7406\uff08Device Twin\uff09<\/h4>\n<p>\u8bbe\u5907\u5f71\u5b50\u7528\u4e8e\u5b58\u50a8\u8bbe\u5907\u7684\u72b6\u6001\u4fe1\u606f\uff0c\u786e\u4fdd\u5728\u8bbe\u5907\u79bb\u7ebf\u6216\u7f51\u7edc\u65ad\u5f00\u65f6\uff0c\u72b6\u6001\u4f9d\u7136\u53ef\u7528\u3002<\/p>\n<ul>\n<li><strong>\u8bbe\u5907\u5f71\u5b50\u7684\u521b\u5efa\u548c\u7ba1\u7406<\/strong>\uff1a\u5728 <code>devicetwin\/dtmanager\/device.go<\/code> \u6587\u4ef6\u4e2d\uff0c<code>DeviceManager<\/code> \u7ba1\u7406\u7740\u6240\u6709\u8bbe\u5907\u5f71\u5b50\u3002\n<pre><code class=\"language-go line-numbers\">func (dm *DeviceManager) AddDeviceTwin(deviceID string, twin *DeviceTwin) {\n  dm.deviceTwins[deviceID] = twin\n}\n<\/code><\/pre>\n<p>\u8fd9\u4e2a\u51fd\u6570\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u8bbe\u5907\u5f71\u5b50\uff08<code>DeviceTwin<\/code>\uff09\uff0c\u5e76\u5c06\u5176\u5b58\u50a8\u5728 <code>DeviceManager<\/code> \u7684 <code>deviceTwins<\/code> \u5b57\u5178\u4e2d\u3002<code>DeviceTwin<\/code> \u5305\u542b\u8bbe\u5907\u7684\u5c5e\u6027\u548c\u72b6\u6001\u4fe1\u606f\u3002<\/p>\n<\/li>\n<li>\n<p><strong>\u5f71\u5b50\u72b6\u6001\u540c\u6b65<\/strong>\uff1a<code>syncDeviceStatus<\/code> \u51fd\u6570\u8d1f\u8d23\u5c06\u8fb9\u7aef\u7684\u8bbe\u5907\u72b6\u6001\u540c\u6b65\u5230\u5f71\u5b50\u4e2d\uff0c\u5e76\u5728\u7f51\u7edc\u6062\u590d\u540e\uff0c\u5c06\u5f71\u5b50\u72b6\u6001\u66f4\u65b0\u5230\u4e91\u7aef\u3002<\/p>\n<pre><code class=\"language-go line-numbers\">func (dm *DeviceManager) syncDeviceStatus(deviceID string, status DeviceStatus) {\n  twin := dm.deviceTwins[deviceID]\n  if twin != nil {\n      twin.Status = status\n      \/\/ \u5904\u7406\u72b6\u6001\u66f4\u65b0\u903b\u8f91\n  }\n}\n<\/code><\/pre>\n<\/li>\n<\/ul>\n<h4>MQTT \u534f\u8bae\u9002\u914d<\/h4>\n<p>KubeEdge \u901a\u8fc7 <code>eventbus<\/code> \u6a21\u5757\u652f\u6301 MQTT \u534f\u8bae\uff0c\u5141\u8bb8\u8bbe\u5907\u901a\u8fc7 MQTT \u534f\u8bae\u8fdb\u884c\u6570\u636e\u4f20\u8f93\uff0c\u9002\u7528\u4e8e\u4f20\u611f\u5668\u6570\u636e\u4e0a\u62a5\u7b49\u573a\u666f\u3002MQTT \u5ba2\u6237\u7aef\u7684\u6838\u5fc3\u4ee3\u7801\u4f4d\u4e8e <code>edge\/pkg\/eventbus\/mqtt\/client.go<\/code> \u4e2d\u3002<\/p>\n<ul>\n<li><strong>MQTT \u5ba2\u6237\u7aef\u521d\u59cb\u5316<\/strong>\uff1a<code>NewMqttClient<\/code> \u51fd\u6570\u8d1f\u8d23\u521d\u59cb\u5316 MQTT \u5ba2\u6237\u7aef\uff0c\u5305\u62ec\u8fde\u63a5\u8bbe\u7f6e\u3001\u8ba4\u8bc1\u914d\u7f6e\u7b49\u3002\n<pre><code class=\"language-go line-numbers\">func NewMqttClient(config MqttConfig) (*MqttClient, error) {\n  opts := mqtt.NewClientOptions()\n  opts.AddBroker(config.Broker)\n  opts.SetClientID(config.ClientID)\n  client := mqtt.NewClient(opts)\n  token := client.Connect()\n  if token.Wait() &amp;&amp; token.Error() != nil {\n      return nil, token.Error()\n  }\n  return &amp;MqttClient{Client: client}, nil\n}\n<\/code><\/pre>\n<\/li>\n<li><strong>\u6570\u636e\u53d1\u5e03\u4e0e\u8ba2\u9605<\/strong>\uff1a<code>Publish<\/code> \u548c <code>Subscribe<\/code> \u65b9\u6cd5\u5206\u522b\u8d1f\u8d23\u5c06\u6d88\u606f\u53d1\u5e03\u5230 MQTT \u4e3b\u9898\u548c\u8ba2\u9605\u6765\u81ea\u8bbe\u5907\u7684\u6d88\u606f\u3002\n<pre><code class=\"language-go line-numbers\">func (mc *MqttClient) Publish(topic string, payload []byte) error {\n  token := mc.Client.Publish(topic, 0, false, payload)\n  token.Wait()\n  return token.Error()\n}\n\nfunc (mc *MqttClient) Subscribe(topic string, callback mqtt.MessageHandler) error {\n  token := mc.Client.Subscribe(topic, 0, callback)\n  token.Wait()\n  return token.Error()\n}\n<\/code><\/pre>\n<p><code>Publish<\/code> \u65b9\u6cd5\u7528\u4e8e\u5c06\u8fb9\u7aef\u8bbe\u5907\u7684\u72b6\u6001\u6216\u4f20\u611f\u6570\u636e\u53d1\u5e03\u5230\u6307\u5b9a\u7684 MQTT \u4e3b\u9898\uff0c\u4ece\u800c\u4e0a\u62a5\u5230\u4e91\u7aef\u3002<code>Subscribe<\/code> \u65b9\u6cd5\u7528\u4e8e\u63a5\u6536\u6765\u81ea\u4e91\u7aef\u7684\u63a7\u5236\u6307\u4ee4\u3002<\/p>\n<\/li>\n<\/ul>\n<h3>3. \u6d88\u606f\u8def\u7531\u4e0e\u6570\u636e\u5904\u7406<\/h3>\n<p>\u5728 <code>devicetwin<\/code> \u548c <code>devicecontroller<\/code> \u4e2d\uff0c\u6d88\u606f\u8def\u7531\u548c\u6570\u636e\u5904\u7406\u6a21\u5757\u8d1f\u8d23\u5728\u4e91\u7aef\u548c\u8fb9\u7aef\u4e4b\u95f4\u4f20\u8f93\u6d88\u606f\u3002<\/p>\n<ul>\n<li><strong>\u8fb9\u7aef\u6d88\u606f\u5904\u7406\u5668<\/strong>\uff1a<code>process.go<\/code> \u4e2d\u7684 <code>ProcessDownstream<\/code> \u51fd\u6570\u8d1f\u8d23\u63a5\u6536\u6765\u81ea\u4e91\u7aef\u7684\u63a7\u5236\u6307\u4ee4\uff0c\u5e76\u5c06\u5176\u8f6c\u53d1\u7ed9\u672c\u5730\u8bbe\u5907\u3002\n<pre><code class=\"language-go line-numbers\">func (dt *DeviceTwin) ProcessDownstream(msg model.Message) {\n  deviceID := msg.GetResource()\n  twin := dt.deviceTwins[deviceID]\n  if twin != nil {\n      twin.HandleDeviceMessage(msg)\n  }\n}\n<\/code><\/pre>\n<\/li>\n<li><strong>\u4e91\u7aef\u6d88\u606f\u5904\u7406\u5668<\/strong>\uff1a\u4e91\u7aef\u7684 <code>handleDeviceStatusUpdate<\/code> \u8d1f\u8d23\u5904\u7406\u8bbe\u5907\u72b6\u6001\u4e0a\u62a5\uff0c\u5c06\u6765\u81ea\u8fb9\u7aef\u7684\u8bbe\u5907\u72b6\u6001\u6570\u636e\u8fdb\u884c\u66f4\u65b0\u6216\u5b58\u50a8\u3002<\/p>\n<\/li>\n<\/ul>\n<h3>4. \u6570\u636e\u7ed3\u6784\u5b9a\u4e49<\/h3>\n<p>\u8bbe\u5907\u7ba1\u7406\u548c\u534f\u8bae\u9002\u914d\u4e2d\u4f7f\u7528\u7684\u6838\u5fc3\u6570\u636e\u7ed3\u6784\u5b9a\u4e49\u5728 <code>pkg\/common<\/code> \u76ee\u5f55\u4e2d\uff0c\u4e3b\u8981\u7ed3\u6784\u4f53\u5305\u62ec\uff1a<\/p>\n<ul>\n<li><strong>DeviceTwin<\/strong>\uff1a\u5b9a\u4e49\u4e86\u8bbe\u5907\u5f71\u5b50\u6a21\u578b\uff0c\u5305\u62ec\u5c5e\u6027\u3001\u72b6\u6001\u3001\u671f\u671b\u503c\u7b49\u5b57\u6bb5\u3002<\/li>\n<li><strong>DeviceStatus<\/strong>\uff1a\u7528\u4e8e\u5b58\u50a8\u8bbe\u5907\u7684\u5b9e\u65f6\u72b6\u6001\u4fe1\u606f\uff0c\u5305\u62ec\u5728\u7ebf\u72b6\u6001\u3001\u6570\u636e\u503c\u7b49\u3002<\/li>\n<\/ul>\n<pre><code class=\"language-go line-numbers\">type DeviceTwin struct {\n    DeviceID   string\n    Desired    map[string]interface{}\n    Reported   map[string]interface{}\n    Status     DeviceStatus\n}\n\ntype DeviceStatus struct {\n    Online   bool\n    Metadata map[string]interface{}\n}\n<\/code><\/pre>\n<h3>\u603b\u7ed3<\/h3>\n<p>KubeEdge \u7684\u8bbe\u5907\u7ba1\u7406\u548c\u534f\u8bae\u9002\u914d\u6838\u5fc3\u4ee3\u7801\u4e3b\u8981\u5728 <code>devicecontroller<\/code>\uff08\u4e91\u7aef\uff09\u548c <code>devicetwin<\/code>\uff08\u8fb9\u7aef\uff09\u6a21\u5757\u4e2d\u5b9e\u73b0\uff0c\u901a\u8fc7 MQTT \u534f\u8bae\u652f\u6301\u8bbe\u5907\u6570\u636e\u7684\u6536\u96c6\u548c\u63a7\u5236\u6307\u4ee4\u7684\u4e0b\u53d1\u3002\u4e91\u7aef\u7684 <code>devicecontroller<\/code> \u7ba1\u7406\u8bbe\u5907\u72b6\u6001\u3001\u6570\u636e\u540c\u6b65\u548c\u6307\u4ee4\u4e0b\u53d1\uff0c\u8fb9\u7aef\u7684 <code>devicetwin<\/code> \u8d1f\u8d23\u8bbe\u5907\u5f71\u5b50\u3001\u72b6\u6001\u540c\u6b65\u548c\u6d88\u606f\u5904\u7406\uff0c<code>eventbus<\/code> \u6a21\u5757\u63d0\u4f9b\u4e86\u5bf9 MQTT \u534f\u8bae\u7684\u652f\u6301\uff0c\u786e\u4fdd\u4e86 KubeEdge \u5bf9\u7269\u8054\u7f51\u8bbe\u5907\u7684\u5e7f\u6cdb\u9002\u914d\u80fd\u529b\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e09.\u8bbe\u5907\u7ba1\u7406\u548c\u534f\u8bae\u9002\u914d\u7684\u6838\u5fc3\u4ee3\u7801 \u5728 KubeEdge \u4e2d\uff0c\u8bbe\u5907\u7ba1\u7406\u548c\u534f\u8bae\u9002\u914d\u662f\u901a\u8fc7 devicecontro&hellip;<\/p>\n <a href=\"https:\/\/www.db2go.net\/?p=742\" title=\"\u4e09.\u8bbe\u5907\u7ba1\u7406\u548c\u534f\u8bae\u9002\u914d\u7684\u6838\u5fc3\u4ee3\u7801\" class=\"entry-more-link\"><span>Read More<\/span> <span class=\"screen-reader-text\">\u4e09.\u8bbe\u5907\u7ba1\u7406\u548c\u534f\u8bae\u9002\u914d\u7684\u6838\u5fc3\u4ee3\u7801<\/span><\/a>","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"Layout":"","footnotes":""},"categories":[20],"tags":[],"class_list":["entry","author-suredandan","post-742","post","type-post","status-publish","format-standard","category-20"],"views":1051,"_links":{"self":[{"href":"https:\/\/www.db2go.net\/index.php?rest_route=\/wp\/v2\/posts\/742","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.db2go.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.db2go.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.db2go.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.db2go.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=742"}],"version-history":[{"count":1,"href":"https:\/\/www.db2go.net\/index.php?rest_route=\/wp\/v2\/posts\/742\/revisions"}],"predecessor-version":[{"id":743,"href":"https:\/\/www.db2go.net\/index.php?rest_route=\/wp\/v2\/posts\/742\/revisions\/743"}],"wp:attachment":[{"href":"https:\/\/www.db2go.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=742"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.db2go.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=742"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.db2go.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=742"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}