{"id":598,"date":"2020-10-23T16:43:57","date_gmt":"2020-10-23T08:43:57","guid":{"rendered":"https:\/\/www.yinyubo.cn\/?p=598"},"modified":"2022-05-17T09:30:45","modified_gmt":"2022-05-17T01:30:45","slug":"golang","status":"publish","type":"post","link":"https:\/\/www.yinyubo.com\/?p=598","title":{"rendered":"Golang\u4f7f\u7528\u534f\u7a0b\u5e76\u53d1\u591a\u4e2amqtt\u7684publish\u4fe1\u606f(\u8bfb\u53d6csv,\u53d1\u9001json\u683c\u5f0f\u62a5\u6587)"},"content":{"rendered":"<h1>\u9700\u6c42<\/h1>\n<p>\u5f00\u53d1\u8bed\u8a00\uff1agolang<br \/>\n\u76ee\u7684\uff1a\u5e76\u53d110000\u4e2amqtt\u8fde\u63a5\uff0c\u5faa\u73af\u53d1\u9001publish\u4fe1\u606f\uff0c\u5f53\u65f6\u95f4\u6233\u5c0f\u4e8e\u67d0\u4e2a\u503c\u7684\u65f6\u5019\uff0c\u4e2d\u6b62\u5faa\u73af\uff0c\u9000\u51fa\u8fde\u63a5<br \/>\npublish\u5185\u5bb9\u662fjson\u683c\u5f0f\u7684\uff0c\u672a\u8bbe\u7f6e\u65f6\uff0c\u6709\u9ed8\u8ba4\u503c\uff0c\u53ef\u4ee5\u901a\u8fc7golang\u4ee3\u7801\u4fee\u6539json\u5185\u5bb9<br \/>\n\u767b\u5f55\u4fe1\u606f\u5b58\u53d6\u5728csv\u6587\u4ef6\u4e2d\uff0ccsv\u6587\u4ef6\u6709\u591a\u5c11\u5217\uff0c\u5c31\u5e76\u53d1\u591a\u5c11\u4e2a\u8bbe\u5907\u8fde\u63a5<\/p>\n<h1>\u8bdd\u4e0d\u591a\u8bf4\uff0c\u76f4\u63a5\u4e0a\u4ee3\u7801<\/h1>\n<p>main.go<\/p>\n<pre class=\"lang:go decode:true  EnlighterJSRAW\" data-enlighter-language=\"golang\"><code class=\"EnlighterJSRAW\" data-enlighter-language=\"golang\">package main\nimport (\n\t\"encoding\/csv\"\n\t\"encoding\/json\"\n\t\"fmt\"\n\t\"os\"\n\t\"strconv\"\n\t\"time\"\n\tmqtt \"github.com\/eclipse\/paho.mqtt.golang\"\n)\nvar total int\n\/\/ \u8bfb\u53d6csv\u6587\u4ef6\u91cc\u7684\u7b2c3\u5217\u6570\u636e\uff0c\u5b58\u5165\u4e00\u4e2astring\u6570\u7ec4\u91cc\nfunc readcsv(filename string) []string {\n\tvar userNameList []string\n\tf, _ := os.Open(filename)\n\tdefer f.Close()\n\tw := csv.NewReader(f)\n\tdata, err := w.ReadAll()\n\tif err != nil {\n\t\tfmt.Println(err)\n\t}\n\tfor i := range data {\n\t\tuserNameList = append(userNameList, data[i][2])\n\t}\n\treturn userNameList\n}\nfunc mqttDevice(username string, end_number chan int) {\n\t\/\/ mqtt\u8bbe\u5907\u8fde\u63a5,\u8bbe\u7f6eIP\u5730\u5740\n\topts := mqtt.NewClientOptions().AddBroker(\"localhost:1883\")\n\t\/\/ \u8bbe\u7f6e\u8fde\u63a5\u7684\u7528\u6237\u540d\u5bc6\u7801\n\topts.SetUsername(username)\n\t\/\/ \u4f7f\u7528\u8fde\u63a5\u4fe1\u606f\u8fdb\u884c\u8fde\u63a5\n\tclient := mqtt.NewClient(opts)\n\tif token := client.Connect(); token.Wait() &amp;&amp; token.Error() != nil {\n\t\tpanic(token.Error())\n\t}\n\ttime.Sleep(1 * time.Second)\n\tfmt.Println(\"connect success:\" + username)\n\t\/\/ \u8bfb\u53d6json\u6587\u4ef6\uff0cjson\u6587\u4ef6\u91cc\u7684\u662f\u9ed8\u8ba4\u53c2\u6570\n\tfileReader, _ := os.Open(\"test.json\")\n\tvar eiopJsonMap map[string]interface{}\n\tjson.NewDecoder(fileReader).Decode(&amp;eiopJsonMap)\n\t\/\/ \u8bbe\u7f6e\u4e00\u4e2a\u5f00\u59cb\u65f6\u95f4\u6233\u548c\u7ed3\u675f\u65f6\u95f4\u6233\n\tstartTime := 1598167852000\n\tendTime := 1598167852000\n\t\/\/ \u5faa\u73af\u53d1\u9001\u9065\u6d4b\uff0c\u6bcf\u6b21\u9065\u6d4b\u95f4\u9694\u65f6\u95f4\u6233\u4e3a15\u5206\u949f\n\tfor ; startTime &lt; endTime; startTime = startTime + 900000 {\n\t\t\/\/ \u5b9a\u4e49\u4e00\u4e2aep\u7684\u521d\u59cb\u503c\u4e3a1,\u6bcf\u5faa\u73af\u4e00\u6b21\u5c31+1\n\t\tvar ep int\n\t\tep++\n\t\teiopJsonMap[\"ts\"] = startTime\n\t\teiopJsonMap[\"values\"].(map[string]interface{})[\"ep\"] = ep\n\t\t\/\/ \u628a\u4fee\u6539\u8fc7\u7684json\u5185\u5bb9\u4ecemap\u8f6c\u6362\u4e3ajson\u683c\u5f0f\n\t\teiopJsonText, _ := json.Marshal(eiopJsonMap)\n\t\tfmt.Println(string(eiopJsonText))\n\t\t\/\/ \u53d1\u9001\u9065\u6d4b\uff0c\u53d1\u5b8c\u4e4b\u540e\u4f11\u77201\u79d2\n\t\tresult := client.Publish(\"topic\", 0, true, eiopJsonText)\n\t\tresult.Wait()\n\t\ttime.Sleep(1 * time.Second)\n\t}\n\t\/\/ \u53d1\u9001\u5b8c\u4fe1\u606f\u4e4b\u540e\uff0c\u9000\u51fa\u8fde\u63a5\n\tfmt.Println(\"disconnect:\" + username)\n\tclient.Disconnect(250)\n\ttotal++\n\tend_number &lt;- total\n}\nfunc main() {\n\tuserNameList := readcsv(\"connect_info.csv\")\n\tendNumber := make(chan int, len(userNameList))\n\t\/\/ \u53d8\u91cf\u6240\u6709\u7684username\uff0c\u901a\u8fc7go\u5173\u952e\u5b57\u5e76\u53d1\u591a\u4e2a\u8bbe\u5907\n\tfor _, userName := range userNameList {\n\t\tgo mqttDevice(userName, endNumber)\n\t}\n\t\/\/ \u5f53\u6240\u6709\u7684\u8bbe\u5907\u90fd\u53d1\u9001\u5b8c\u6bd5\u540e\uff0c\u5173\u95ed\u7a0b\u5e8f\n\tfor i := range endNumber {\n\t\tfmt.Println(\"\u5df2\u7ecf\u6709\" + strconv.Itoa(i) + \"\u4e2a\u8bbe\u5907\u53d1\u9001\u5b8c\u6bd5\")\n\t\tif i == len(userNameList) {\n\t\t\treturn\n\t\t}\n\t}\n}\n<\/code><\/pre>\n<p>test.json<\/p>\n<pre class=\"lang:python decode:true  EnlighterJSRAW\" data-enlighter-language=\"golang\"><code class=\"EnlighterJSRAW\" data-enlighter-language=\"golang\">{\n\t\"ts\": 1603088274000,\n\t\"values\": {\n\t\t\"ep\": 12\n\t}\n}<\/code><\/pre>\n<p>connect_info.csv<\/p>\n<pre class=\"lang:python decode:true  EnlighterJSRAW\" data-enlighter-language=\"golang\"><code class=\"EnlighterJSRAW\" data-enlighter-language=\"golang\">localhost,1883,XecUwSmMGiYJp2BspMK2\nlocalhost,1883,GqVqoPP2wblDjS2P9pQ9\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u9700\u6c42 \u5f00\u53d1\u8bed\u8a00\uff1agolang \u76ee\u7684\uff1a\u5e76\u53d110000\u4e2amqtt\u8fde\u63a5\uff0c\u5faa\u73af\u53d1\u9001publish\u4fe1\u606f\uff0c\u5f53\u65f6\u95f4\u6233\u5c0f\u4e8e\u67d0\u4e2a [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[10],"tags":[],"class_list":["post-598","post","type-post","status-publish","format-standard","hentry","category-10"],"_links":{"self":[{"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=\/wp\/v2\/posts\/598","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=598"}],"version-history":[{"count":1,"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=\/wp\/v2\/posts\/598\/revisions"}],"predecessor-version":[{"id":872,"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=\/wp\/v2\/posts\/598\/revisions\/872"}],"wp:attachment":[{"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=598"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=598"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=598"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}