Skip to content

Commit 9c4f5d8

Browse files
committed
Add fix mqtt-broker in flows.json befor run NodeRed
1 parent 14bed02 commit 9c4f5d8

File tree

1 file changed

+35
-1
lines changed

1 file changed

+35
-1
lines changed

src/background/NodeREDWorker.js

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,34 @@ function copyFolderRecursiveSync( source, target ) {
4545
}
4646
}
4747

48+
function fixFlowNodeV2(filename) {
49+
console.log('fixFlowNodeV2 load:', filename);
50+
let flow = JSON.parse(fs.readFileSync(filename, { encoding: 'utf-8' }));
51+
if (flow.length < 1) return;
52+
let mofify = false;
53+
for (let i = 0; i < flow.length; i++) {
54+
if (flow[i].type === 'mqtt-broker' && flow[i].compatmode !== undefined) {
55+
delete flow[i].compatmode;
56+
flow[i] = Object.assign({
57+
"protocolVersion": "4",
58+
"autoConnect": true,
59+
"birthMsg": {},
60+
"closeTopic": "",
61+
"closePayload": "",
62+
"closeMsg": {},
63+
"willMsg": {},
64+
"sessionExpiry": "",
65+
"name": ""
66+
}, flow[i]);
67+
mofify = true;
68+
}
69+
}
70+
if (mofify) {
71+
console.log('fixFlowNodeV2 save:', filename);
72+
fs.writeFileSync(filename, JSON.stringify(flow), { encoding: 'utf-8' })
73+
}
74+
}
75+
4876
function setup() {
4977
const listenPort = 1880;
5078
const flowFile = "flows.json";
@@ -76,6 +104,12 @@ function setup() {
76104
}
77105
});
78106

107+
try {
108+
fixFlowNodeV2(path.join(userDir, 'flows.json'));
109+
} catch (error) {
110+
console.log('fixFlowNodeV2', error);
111+
}
112+
79113
var config = {
80114
uiPort: listenPort,
81115
verbose: true,
@@ -126,7 +160,7 @@ function setup() {
126160

127161
resolve();
128162
});
129-
}).otherwise(function(err) {
163+
}).catch(function(err) {
130164
RED.log.error(RED.log._("server.failed-to-start"));
131165
if (err.stack) {
132166
RED.log.error(err.stack);

0 commit comments

Comments
 (0)