我尝试解决中提出的问题 Node-red: custom nodes waiting for missing types 通过创建一个新节点。
我尝试使用" mqtt.js示例"获取预先配置的mqtt-client / subscriber以添加到我的调色板中。
所以在节点文件夹中我有配置文件,名为mqttConfig.json,其中放置了mqtt.js使用的所有数据来建立连接(即。broker,topic,qos ...),这个文件的结构与前一个相同。
{
"receiver": {
"broker":"127.0.0.1",
"topic":"topicRec",
"qos":"2"
}
}
然后我创建了新的preconf_mqtt.js:
var mqtt = require("/usr/local/lib/node_modules/node-red/node_modules/mqtt");
var mqttConfig = require("mqttConfig");
'use strict'
module.exports = function(RED)
{
function ConfiguredMqttOutNode(config)
{
RED.nodes.createNode(this,config);
var node = this;
var m = mqttConf.receiver;
this.topic = m.topic;
this.qos = parseInt(m.qos);
if (isNaN(this.qos) || this.qos < 0 || this.qos > 2)
this.qos = 2;
this.broker = m.broker;
this.client = mqtt.connect(this.broker);
this.client.subscribe(this.topic);
this.client.on('message', function (topic, message) {
var msg = {};
msg.topic = this.topic;
msg.payload = JSON.stringify(message);
node.send(msg);
});
}
RED.nodes.registerType("configured-mqtt-out",ConfiguredMqttOutNode);
}
虽然preconf_mqtt.html是:
<script type="text/javascript">
RED.nodes.registerType('configured-mqtt-out',{
category: 'processing',
color: '#a6bbcf',
defaults: {
name: {value:""}
},
inputs:0,
outputs:1,
icon: "bridge.png",
label: function()
{
return this.name||"c-mqtt-out";
}
});
</script>
<script type="text/x-red" data-template-name="configured-mqtt-out">
<div class="form-row">
<label for="node-input-name"><i class="icon-tag"></i> Name</label>
<input type="text" id="node-input-name" placeholder="Name">
</div>
</script>
<script type="text/x-red" data-help-name="configured-mqtt-out">
<p>Pre-configured MQTT subscriber</p>
</script>
无论何时尝试安装程序:
ute@preprocnr:~/.node-red$ sudo npm install /home/ute/mqtt_rules_definer
> node-red-dashboard@2.9.1 postinstall /home/ute/.node-red/node_modules/node-red-dashboard
> node fixfa.js
node-red-project@0.0.1 /home/ute/.node-red
├─┬ mqtt@2.18.0
│ ├─┬ concat-stream@1.6.2
│ │ └─┬ readable-stream@2.3.6
│ │ ├── isarray@1.0.0
│ │ └── string_decoder@1.1.1
│ ├─┬ help-me@1.1.0
│ │ ├─┬ glob-stream@6.1.0
│ │ │ ├─┬ ordered-read-streams@1.0.1
│ │ │ │ └─┬ readable-stream@2.3.6
│ │ │ │ ├── isarray@1.0.0
│ │ │ │ └── string_decoder@1.1.1
│ │ │ ├─┬ pumpify@1.5.0
│ │ │ │ └── pump@2.0.1
│ │ │ └─┬ readable-stream@2.3.6
│ │ │ ├── isarray@1.0.0
│ │ │ └── string_decoder@1.1.1
│ │ └─┬ through2@2.0.3
│ │ └─┬ readable-stream@2.3.6
│ │ ├── isarray@1.0.0
│ │ └── string_decoder@1.1.1
│ ├─┬ mqtt-packet@5.6.0
│ │ └─┬ bl@1.2.2
│ │ └─┬ readable-stream@2.3.6
│ │ ├── isarray@1.0.0
│ │ └── string_decoder@1.1.1
│ ├─┬ readable-stream@2.3.6
│ │ ├── isarray@1.0.0
│ │ └── string_decoder@1.1.1
│ └─┬ websocket-stream@5.1.2
│ ├─┬ duplexify@3.6.0
│ │ └─┬ readable-stream@2.3.6
│ │ ├── isarray@1.0.0
│ │ └── string_decoder@1.1.1
│ └─┬ readable-stream@2.3.6
│ ├── isarray@1.0.0
│ └── string_decoder@1.1.1
├── mqtt_rules_definer@1.0.0 extraneous
├── node-red-dashboard@2.9.1 extraneous
└── rule-definer@1.0.0 extraneous
npm WARN node-red-project@0.0.1 No repository field.
npm WARN node-red-project@0.0.1 No license field.
ute@preprocnr:~/.node-red$ node-red
当我启动它时,node-red没有显示任何特定的错误/警告:
16 May 15:21:23 - [info]
Welcome to Node-RED
===================
16 May 15:21:23 - [info] Node-RED version: v0.18.4
16 May 15:21:23 - [info] Node.js version: v4.2.6
16 May 15:21:23 - [info] Linux 4.4.0-124-generic x64 LE
16 May 15:21:23 - [info] Loading palette nodes
16 May 15:21:23 - [info] Dashboard version 2.9.1 started at /ui
16 May 15:21:23 - [warn] ------------------------------------------------------
16 May 15:21:23 - [warn] [node-red/rpi-gpio] Info : Ignoring Raspberry Pi
specific node
16 May 15:21:23 - [warn] [node-red-node-twitter/twitter] ReferenceError:
Invalid left-hand side in assignment
16 May 15:21:23 - [warn] ------------------------------------------------------
16 May 15:21:23 - [info] Settings file : /home/ute/.node-red/settings.js
16 May 15:21:23 - [info] User directory : /home/ute/.node-red
16 May 15:21:23 - [info] Server now running at http://127.0.0.1:1880/
16 May 15:21:23 - [info] Active project : pre-proc
16 May 15:21:23 - [info] Flows file : /home/ute/.node-red/projects/pre- proc/preprocessor.json
但我的调色板中没有该节点,我也不知道如何解决它。 请帮我, 亲切的问候, 詹卢卡
[编辑] 我忘了在包中添加节点:S 我有它,然后我得到以下警告,从节点红色开始:
16 May 16:36:41 - [warn] ------------------------------------------------------
16 May 16:36:41 - [warn] [node-red/rpi-gpio] Info : Ignoring Raspberry Pi specific node
16 May 16:36:41 - [warn] [node-red-node-twitter/twitter] ReferenceError:
Invalid left-hand side in assignment
16 May 16:36:41 - [warn] [mqtt_rules_definer/mqtt_rules_definer] SyntaxError: Unexpected token .
16 May 16:36:41 - [warn] ------------------------------------------------------
并且未生成节点...
答案 0 :(得分:1)
错误消息非常清楚,您的mqtt_rules_definer.js
文件中存在语法错误(您的代码中某处有.
错位符号)。您需要在Node-RED加载之前解决此问题。
找出问题所在的最快方法是执行以下操作:
$ node
这将启动一个交互式shell,然后您可以键入以下内容:
require('mqtt_rules_definer')