-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
185 lines (173 loc) · 4.77 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
const server = require('noflo-nodejs');
const nofloServer = require('noflo-nodejs/src/server');
const fbpGraph = require('fbp-graph');
const { v4: uuidv4 } = require('uuid');
const { readdir, mkdir } = require('node:fs/promises');
const path = require('path');
const componentLoader = require('./componentLoader');
function ensureGraphs(baseDir) {
const graphDir = path.resolve(baseDir, './graphs');
return readdir(graphDir)
.catch((err) => {
if (err.code === 'ENOENT') {
return mkdir(graphDir)
.then(() => []);
}
throw err;
})
.then((res) => {
if (!res.length) {
// No graphs, create a "main"
const graphPath = path.resolve(graphDir, 'main.json');
const graph = fbpGraph.graph.createGraph('main');
graph.setProperties({
environment: {
type: 'noflo-nodejs',
},
});
return graph.save(graphPath)
.then(() => [
graphPath,
]);
}
return res.map((r) => path.resolve(graphDir, r));
});
}
function findMain(graphs) {
const mainable = graphs.find((g) => g.includes('main'));
if (mainable) {
return mainable;
}
return graphs[0];
}
module.exports = (app) => {
let runtime = null;
let runtimeConfig = null;
const plugin = {};
plugin.id = 'noflo-signalk';
plugin.name = 'NoFlo Signal K';
plugin.description = 'Signal K automation with the NoFlo visual programming framework';
let skUuid = app.getSelfPath('uuid');
if (skUuid) {
skUuid = skUuid.split(':').pop();
}
function preStart(rt, config) {
runtimeConfig = config;
// Custom component loading with app context here
const customLoader = componentLoader(app);
const loader = rt.component.getLoader(config.baseDir, rt.options);
return loader.listComponents()
.then(() => new Promise((resolve, reject) => {
loader.registerLoader(customLoader, (err) => {
if (err) {
reject(err);
return;
}
resolve();
});
}));
}
plugin.start = (options) => {
const port = options.port || 3569;
// FIXME: Determine whether to use HTTPS or HTTP
const ide = options.ide || 'https://app.noflojs.org';
// We need to use the .signalk directory as baseDir to be able to load components
const baseDir = path.resolve(app.getDataDirPath(), '../../');
const config = {
id: options.uuid,
label: `NoFlo on ${app.getSelfPath('name')}`,
secret: options.secret,
open: false,
autoSave: true,
trace: options.trace,
protocol: options.protocol || 'websocket',
catchExceptions: true,
ide,
baseDir,
};
ensureGraphs(baseDir)
.then((graphs) => {
const main = findMain(graphs);
server(main, config, preStart)
.then((rt) => {
runtime = rt;
app.setPluginStatus(`NoFlo runtime running in port ${port}`);
// TODO: Start all other graphs as well
}, (err) => {
app.debug(err);
app.setPluginError(`Failed to start NoFlo runtime: ${err.message}`);
});
});
};
plugin.registerWithRouter = (router) => {
router.get('/url', (req, res) => {
if (!runtime) {
res.sendStatus(404);
return;
}
// FIXME: We need options here
res.send(nofloServer.liveUrl(runtimeConfig));
});
};
plugin.stop = () => {
if (!runtime) {
return;
}
app.debug('Stopping NoFlo runtime');
// TODO: Stop running NoFlo networks as well
nofloServer.stop(runtime)
.then(() => {
app.debug('NoFlo runtime stopped');
runtime = null;
}, (err) => {
app.debug('Failed to stop the NoFlo runtime');
app.debug(err);
});
};
plugin.schema = {
type: 'object',
required: [
'uuid',
'secret',
],
properties: {
uuid: {
title: 'Server instance UUID',
type: 'string',
format: 'uuid',
default: skUuid || uuidv4(),
},
secret: {
title: 'Server instance password',
type: 'string',
default: uuidv4(),
},
ide: {
title: 'NoFlo UI instance URL',
type: 'string',
format: 'uri',
default: 'https://app.noflojs.org',
},
protocol: {
title: 'FBP protocol transport to use',
type: 'string',
enum: [
'websocket',
'webrtc',
],
default: 'websocket',
},
port: {
title: 'FBP Protocol port for the IDE to connect to',
type: 'number',
default: 3569,
},
trace: {
title: 'Whether to capture and store a Flowtrace for each graph',
type: 'boolean',
default: false,
},
},
};
return plugin;
};