{"id":11530,"date":"2023-02-01T17:49:51","date_gmt":"2023-02-01T09:49:51","guid":{"rendered":"https:\/\/www.modbus.cn\/?p=11530"},"modified":"2023-02-01T17:54:43","modified_gmt":"2023-02-01T09:54:43","slug":"node-red-zhong-chuang-jian-zi-ding-yi-jie-dian-javascript-wen-jian-api-bian-xie-xiang-jie","status":"publish","type":"post","link":"https:\/\/www.modbus.cn\/en\/11530.html","title":{"rendered":"node-red\u4e2d\u521b\u5efa\u81ea\u5b9a\u4e49\u8282\u70b9 JavaScript \u6587\u4ef6API\u7f16\u5199\u8be6\u89e3"},"content":{"rendered":"<h2 class=\"wp-block-heading\">\u524d\u8a00<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728node-red\u4e2d\u5982\u679c\u4f60\u6ca1\u6709\u627e\u5230\u81ea\u5df1\u9700\u8981\u7684\u8282\u70b9\u65f6\uff0c\u90a3\u4e48\u4f60\u53ef\u4ee5\u81ea\u5b9a\u4e49\u4e00\u4e2a\u8282\u70b9\u6765\u6ee1\u8db3\u81ea\u5df1\u7684\u9700\u6c42\u3002\u4e4b\u524d\u7684\u6587\u7ae0\u4e2d\uff0c\u6211\u6709\u7b80\u5355\u4ecb\u7ecd\u8fc7\u5982\u4f55\u521b\u5efa\u4e00\u4e2a\u8282\u70b9\uff0c\u5e76\u4ee5\u8f6c\u6362\u5927\u5c0f\u5199\u6765\u4e3e\u4f8b\u3002\u4f8b\u5b50\u867d\u7136\u7b80\u5355\uff0c\u4f46\u53ef\u4ee5\u8ba9\u5927\u5bb6\u4e86\u89e3\u521b\u5efa\u81ea\u5b9a\u4e49\u8282\u70b9\u7684\u6b65\u9aa4\u4ee5\u53ca\u4e00\u4e2a\u8282\u70b9\u7684\u7ec4\u6210\u90e8\u5206\u3002\u90a3\u4e48\u672c\u7bc7\u5c06\u4f1a\u805a\u7126\u5728\u81ea\u5b9a\u4e49\u8282\u70b9\u7684js\u6587\u4ef6\u4e2d\u3002\u8be6\u7ec6\u8bb2\u89e3\u5176\u4e2d\u7684api\uff0c\u53ca\u5176\u4ed6\u914d\u7f6e\u9879\u3002\u901a\u8fc7\u4e4b\u524d\u7684\u6587\u7ae0\u6211\u4eec\u77e5\u9053\uff0c\u8282\u70b9\u7684js\u6587\u4ef6\u662f\u4e00\u4e2a\u8282\u70b9\u7684\u6838\u5fc3\u6587\u4ef6\uff0c\u7528\u4e8e\u6536\u53d6\u914d\u7f6e\u53c2\u6570\uff0c\u5904\u7406\u4e1a\u52a1\u903b\u8f91\uff0c\u5e76\u5411\u4e0b\u6e38\u53d1\u9001\u6570\u636e\u3002\u4e0b\u9762\u8ba9\u6211\u4eec\u4e00\u8d77\u770b\u770b\u5427\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><a><\/a>\u81ea\u5b9a\u4e49\u8282\u70b9JavaScript \u6587\u4ef6<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\"><a><\/a>\u8282\u70b9\u6784\u5efa\u5668<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u6240\u6709\u7684\u8282\u70b9\u90fd\u662f\u7531\u4e00\u4e2a\u6784\u5efa\u5668\u6765\u751f\u6210\u7684\u51fd\u6570\uff0c\u662f\u521b\u5efa\u7684\u4e00\u4e2a\u65b0\u7684\u5b9e\u4f8b\u3002\u8fd9\u4e2a\u51fd\u6570\u88ab\u6ce8\u518c\u5230\u8fd0\u884c\u65f6\u4e2d\uff0c<br>\u8fd9\u4e2a\u51fd\u6570\u901a\u8fc7\u8bbe\u7f6e\u4e00\u4e2a\u914d\u7f6e\u5bf9\u8c61\u6765\u5b58\u5728\u4e8e\u6d41\u7f16\u8f91\u5668\u4e2d\u3002<br>\u5728\u8be5\u51fd\u6570\u4e2d\uff0c\u7b2c\u4e00\u4ef6\u4e8b\u5c31\u662f\u8c03\u7528<code>RED.nodes.createNode<\/code>\u51fd\u6570\uff0c\u6765\u5e8f\u5217\u5316\u7279\u6027\uff0c\u88ab\u5176\u4ed6\u8282\u70b9\u5171\u4eab\u3002\u5728\u8fd9\u4e4b\u540e<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5c31\u53ef\u4ee5\u7f16\u5199\u4e1a\u52a1\u4ee3\u7801\u5566\u3002<br>\u5982\u4e0b<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><a><\/a>\u63a5\u53d7\u4fe1\u606f<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u8282\u70b9\u901a\u8fc7\u6ce8\u518c\u4e00\u4e2a<code>input<\/code>\u4e8b\u4ef6\u6765\u63a5\u53d7\u4e0a\u6e38\u8282\u70b9\u4e0b\u53d1\u7684\u4fe1\u606f\u5728\u6574\u4e2a\u6d41\u4e2d\u3002\u6ce8\u518c\u4e8b\u4ef6\u65f6\uff0c\u5728\u4e8b\u4ef6\u7684\u56de\u8c03\u51fd\u6570\u91cc\uff0c\u4f1a\u6709msg\u548csend\u4e24\u4e2a\u53c2\u6570\uff0cmsg\u5c31\u662f\u4e0a\u6e38\u4f20\u9012\u8fc7\u6765\u7684\u6570\u636e\u3002send\u662f\u8981\u4e0b\u53d1\u5230\u4e0b\u6e38\u7684\u4e00\u4e2a\u51fd\u6570\u3002\u7b2c\u4e09\u4e2a\u51fd\u6570 done \u662f\u8282\u70b9\u5b8c\u6210\u4e1a\u52a1\u903b\u8f91\u5904\u7406\u540e\u8c03\u7528\u7684\u51fd\u6570\u3002<br>\u793a\u4f8b<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>function SampleNode(config) {\n    RED.nodes.createNode(this,config);\n    \/\/ node-specific code goes here\n\n}\n\nRED.nodes.registerType(\"sample\",SampleNode);<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u63a5\u53d7\u4fe1\u606f<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u8282\u70b9\u901a\u8fc7\u6ce8\u518c\u4e00\u4e2a<code>input<\/code>\u4e8b\u4ef6\u6765\u63a5\u53d7\u4e0a\u6e38\u8282\u70b9\u4e0b\u53d1\u7684\u4fe1\u606f\u5728\u6574\u4e2a\u6d41\u4e2d\u3002\u6ce8\u518c\u4e8b\u4ef6\u65f6\uff0c\u5728\u4e8b\u4ef6\u7684\u56de\u8c03\u51fd\u6570\u91cc\uff0c\u4f1a\u6709msg\u548csend\u4e24\u4e2a\u53c2\u6570\uff0cmsg\u5c31\u662f\u4e0a\u6e38\u4f20\u9012\u8fc7\u6765\u7684\u6570\u636e\u3002send\u662f\u8981\u4e0b\u53d1\u5230\u4e0b\u6e38\u7684\u4e00\u4e2a\u51fd\u6570\u3002\u7b2c\u4e09\u4e2a\u51fd\u6570 done \u662f\u8282\u70b9\u5b8c\u6210\u4e1a\u52a1\u903b\u8f91\u5904\u7406\u540e\u8c03\u7528\u7684\u51fd\u6570\u3002<br>\u793a\u4f8b<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>this.on('input', function(msg, send, done) {\n    \/\/ do something with 'msg'\n\n    \/\/ Once finished, call 'done'.\n    \/\/ This call is wrapped in a check that 'done' exists\n    \/\/ so the node will work in earlier versions of Node-RED (&lt;1.0)\n    if (done) {\n        done();\n    }\n});<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u4e0b\u53d1\u4fe1\u606f<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u81ea\u5b9a\u4e49\u8282\u70b9\u4e2d\u5411\u4e0b\u6e38\u53d1\u9001\u4fe1\u606f\u975e\u5e38\u7684\u7b80\u5355<br>\u53ea\u9700\u8981\u4f7f\u7528send\u51fd\u6570\u5373\u53ef\uff0c\u5982\u4e0b<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>var msg = { payload:\"hi\" }\nthis.send(msg);<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u5982\u679c\u8282\u70b9\u60f3\u8981\u5728input\u4e8b\u4ef6\u4e2d\u5411\u4e0b\u6e38\u53d1\u9001\u4fe1\u606f\uff0c\u53ef\u4ee5\u8fd9\u6837\u505a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>let node = this;\nthis.on('input', function(msg, send, done) {\n    \/\/ For maximum backwards compatibility, check that send exists.\n    \/\/ If this node is installed in Node-RED 0.x, it will need to\n    \/\/ fallback to using `node.send`\n    send = send || function() { node.send.apply(node,arguments) }\n\n    msg.payload = \"hi\";\n    send(msg);\n\n    if (done) {\n        done();\n    }\n});<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728input\u4e8b\u4ef6\u7684\u56de\u8c03\u51fd\u6570\u4e2d\uff0c\u6709\u4e09\u4e2a\u53c2\u6570\uff0c\u7b2c\u4e00\u4e2a\u662f\u63a5\u53d7\u5230\u7684\u4fe1\u606f\uff0c\u7b2c\u4e8c\u4e2a\u662fsend\u51fd\u6570\uff0c<br>\u7b2c\u4e09\u4e2a\u662fdone\u51fd\u6570\u3002\u5206\u522b\u6709\u4e0d\u540c\u7684\u4f7f\u7528\u573a\u666f\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5982\u679c\u8981\u5411\u4e0b\u6e38\u8f93\u5165\u591a\u4e2a\u53c2\u6570\u53ef\u4ee5\u8fd9\u6837\u5199<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>this.send(&#91; msg1 , msg2 ]);\n\nthis.send(&#91; &#91;msgA1 , msgA2 , msgA3] , msg2 ]);<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><a><\/a>\u65e5\u8bb0\u4e8b\u4ef6<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u81ea\u5b9a\u4e49\u8282\u70b9\u4e2d\u6253\u5370\u65e5\u5fd7\uff0c\u53ea\u9700\u5728this\u4e0b\u8c03\u7528log\uff0cwarn\u7b49\u65b9\u6cd5\u5373\u53ef\u3002\u65e5\u5fd7\u4f1a\u5728\u63a7\u5236\u53f0\u4e2d\u6253\u5370\u51fa\u6765\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>this.log(\"Something happened\");\nthis.warn(\"Something happened you should know about\");\nthis.error(\"Oh no, something bad happened\");\n\n\/\/ Since Node-RED 0.17\nthis.trace(\"Log some internal detail not needed for normal operation\");\nthis.debug(\"Log something more details for debugging the node's behaviour\");<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u8bbe\u7f6e\u72b6\u6001<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u6709\u4e9b\u8282\u70b9\u4e3a\u4e86\u8868\u660e\u5df2\u7ecf\u914d\u7f6e\u6210\u529f\uff0c\u6216\u914d\u7f6e\u6709\u8bef\uff0c\u4f1a\u6709\u4e00\u4e2a\u72b6\u6001\u663e\u793a\u3002<br>\u8be5\u72b6\u6001\u4e5f\u662f\u5728js\u6587\u4ef6\u4e2d\u8bbe\u7f6e\u7684\uff0c\u53ea\u9700\u8981\u8c03\u7528status\u5373\u53ef\u3002<br>\u5982\u4e0b<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>this.status({fill:\"red\",shape:\"ring\",text:\"disconnected\"});<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">fill \u72b6\u6001\u7684\u989c\u8272<br>shape \u72b6\u6001\u7684\u56fe\u6807<br>text \u72b6\u6001\u7684\u6587\u672c<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><a><\/a>\u81ea\u5b9a\u4e49\u8282\u70b9\u8bbe\u7f6e<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5982\u679c\u4f60\u5728\u5236\u4f5c\u8282\u70b9\u65f6\uff0c\u60f3\u8981\u5c06\u8282\u70b9\u7684\u4e00\u4e2a\u5c5e\u6027\u914d\u7f6e\u5230setting\u6587\u4ef6\u4e2d\uff0c\u90a3\u4e48\u53ef\u4ee5\u8fd9\u6837\u505a\u3002<br>\u5047\u5982\u4f60\u7684\u8282\u70b9\u7c7b\u578b\u4e3a\uff0c<code>fizz-node<\/code>\uff0c\u5e76\u5c06\u5176\u4e2d\u7684<code>color<\/code>&nbsp;\u653e\u5230setting\u6587\u4ef6\u4e2d\uff0c\u90a3\u4e48\u5728setting\u6587\u4ef6\u4e2d\u4f60\u9700\u8981\u8fd9\u6837\u914d\u7f6e<code>fizzNodeColor<\/code>\u3002<br>\u5728\u4f7f\u7528\u65f6\uff0c\u4f7f\u7528<code>RED.settings.fizzNodeColor<\/code>\u6765\u8bbf\u95ee\u3002<\/p>\n<span id=\"magicpostMarker\"><\/span>","protected":false},"excerpt":{"rendered":"<p>\u524d\u8a00 \u5728node-red\u4e2d\u5982\u679c\u4f60\u6ca1\u6709\u627e\u5230\u81ea\u5df1\u9700\u8981\u7684\u8282\u70b9\u65f6\uff0c\u90a3&#8230;<\/p>","protected":false},"author":106,"featured_media":27977,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":"","_members_access_role":[],"_members_access_error":""},"categories":[1381],"tags":[1406,1404,1405,1376,1378,1375],"class_list":["post-11530","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-node-red","tag-1406","tag-1404","tag-1405","tag-1376","tag-1378","tag-1375"],"_links":{"self":[{"href":"https:\/\/www.modbus.cn\/en\/wp-json\/wp\/v2\/posts\/11530","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.modbus.cn\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.modbus.cn\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.modbus.cn\/en\/wp-json\/wp\/v2\/users\/106"}],"replies":[{"embeddable":true,"href":"https:\/\/www.modbus.cn\/en\/wp-json\/wp\/v2\/comments?post=11530"}],"version-history":[{"count":0,"href":"https:\/\/www.modbus.cn\/en\/wp-json\/wp\/v2\/posts\/11530\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.modbus.cn\/en\/wp-json\/wp\/v2\/media\/27977"}],"wp:attachment":[{"href":"https:\/\/www.modbus.cn\/en\/wp-json\/wp\/v2\/media?parent=11530"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.modbus.cn\/en\/wp-json\/wp\/v2\/categories?post=11530"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.modbus.cn\/en\/wp-json\/wp\/v2\/tags?post=11530"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}