node-red-contrib-relink 0.1.1 → 0.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +87 -93
- package/examples/alert.json +113 -0
- package/examples/camera-snapshots.json +90 -0
- package/examples/data-log.json +65 -0
- package/examples/mqtt-serial.json +139 -0
- package/examples/schedule.json +76 -0
- package/lib/client.js +250 -5
- package/lib/media.js +97 -0
- package/lib/protocol.js +28 -13
- package/nodes/relink.html +75 -3
- package/nodes/relink.js +99 -0
- package/package.json +1 -1
package/nodes/relink.html
CHANGED
|
@@ -34,6 +34,24 @@
|
|
|
34
34
|
inputs: 1, outputs: 0, icon: "font-awesome/fa-exchange", align: "right",
|
|
35
35
|
label: function() { return this.name || (this.pin === "" ? "relink out" : "relink V" + String(this.pin).replace(/^v/i, "")); }
|
|
36
36
|
});
|
|
37
|
+
function mediaNode(kind, dir, icon, extra) {
|
|
38
|
+
var defaults = {
|
|
39
|
+
name: { value: "" },
|
|
40
|
+
connection: { value: "", type: "relink-connection", required: true },
|
|
41
|
+
pin: { value: "", required: true, validate: function(v) { return v !== "" && validPin(v); } }
|
|
42
|
+
};
|
|
43
|
+
for (var k in extra || {}) defaults[k] = extra[k];
|
|
44
|
+
RED.nodes.registerType("relink " + kind + " " + dir, {
|
|
45
|
+
category: "network", color: "#c4d7c0", defaults: defaults,
|
|
46
|
+
inputs: dir === "out" || kind === "camera" ? 1 : 0, outputs: dir === "in" ? 1 : 0,
|
|
47
|
+
icon: icon, align: dir === "out" ? "right" : "left",
|
|
48
|
+
label: function() { return this.name || ("relink " + kind + (this.pin === "" ? "" : " V" + String(this.pin).replace(/^v/i, ""))); }
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
mediaNode("camera", "in", "font-awesome/fa-video-camera");
|
|
52
|
+
mediaNode("camera", "out", "font-awesome/fa-video-camera");
|
|
53
|
+
mediaNode("audio", "in", "font-awesome/fa-volume-up", { quality: { value: "0" } });
|
|
54
|
+
mediaNode("audio", "out", "font-awesome/fa-volume-up", { rate: { value: 8000, validate: function(v) { return /^\d+$/.test(String(v)) && v >= 4000 && v <= 192000; } } });
|
|
37
55
|
})();
|
|
38
56
|
</script>
|
|
39
57
|
|
|
@@ -60,7 +78,7 @@
|
|
|
60
78
|
<div class="form-row"><label for="node-input-pin">Pin</label><input type="text" id="node-input-pin" placeholder="From msg.pin or msg.topic"></div>
|
|
61
79
|
<div class="form-row"><label for="node-input-dtype">Data type</label><select id="node-input-dtype">
|
|
62
80
|
<option value="auto">Automatic</option><option value="int16">Int16</option><option value="int32">Int32</option>
|
|
63
|
-
<option value="float">Float</option><option value="string">String</option><option value="bytes">ByteArray</option>
|
|
81
|
+
<option value="float">Float</option><option value="string">String</option><option value="bytes">ByteArray</option><option value="uint8">UInt8</option><option value="double">Double</option>
|
|
64
82
|
</select></div>
|
|
65
83
|
</script>
|
|
66
84
|
|
|
@@ -68,7 +86,7 @@
|
|
|
68
86
|
<p>Connects Node-RED to one Relink device as an encrypted UDP endpoint. Use the same token and key as your ESP32, C++, or Python program to share virtual pins.</p>
|
|
69
87
|
<p>The token and key use Node-RED credentials and are omitted from exported flows. Encryption is required; an invalid key never falls back to plaintext.</p>
|
|
70
88
|
<p>UDP defaults to port 7445. The API URL is an origin, such as <code>https://relink-iot.com</code>, without <code>/api</code>. Signed API requests fetch declared datastreams. Direct mesh starts after that list is available, and falls back to the relay when a peer becomes unreachable.</p>
|
|
71
|
-
<p>This package handles virtual pins
|
|
89
|
+
<p>This package handles virtual pins, numeric history summaries, camera and audio. It does not flash firmware.</p>
|
|
72
90
|
</script>
|
|
73
91
|
|
|
74
92
|
<script type="text/html" data-help-name="relink in">
|
|
@@ -78,7 +96,7 @@
|
|
|
78
96
|
<dt>payload <span class="property-type">number | string | Buffer</span></dt><dd>The pin value. ByteArray values are Buffers.</dd>
|
|
79
97
|
<dt>topic <span class="property-type">string</span></dt><dd>Pin name, for example <code>V0</code>.</dd>
|
|
80
98
|
<dt>pin <span class="property-type">number</span></dt><dd>Pin number, 0–255.</dd>
|
|
81
|
-
<dt>dtype <span class="property-type">string</span></dt><dd>int16, int32, float, string, or
|
|
99
|
+
<dt>dtype <span class="property-type">string</span></dt><dd>int16, int32, float, string, bytes, uint8 or double.</dd>
|
|
82
100
|
<dt>transport <span class="property-type">string</span></dt><dd>relay or p2p.</dd>
|
|
83
101
|
<dt>checksum <span class="property-type">number</span></dt><dd>The device's numeric identifier.</dd>
|
|
84
102
|
</dl>
|
|
@@ -91,3 +109,57 @@
|
|
|
91
109
|
<p>Text and ByteArray payloads are limited to 255 bytes. Invalid values trigger a Catch node. During a network outage, the latest queued value per pin is retained in memory; a redeploy clears it. At high input rates, writes to the same pin are coalesced.</p>
|
|
92
110
|
<p>Completion means queued, not delivery confirmed. Sending to a control pin can affect real hardware.</p>
|
|
93
111
|
</script>
|
|
112
|
+
|
|
113
|
+
<script type="text/html" data-template-name="relink camera in">
|
|
114
|
+
<div class="form-row"><label for="node-input-name">Name</label><input type="text" id="node-input-name"></div>
|
|
115
|
+
<div class="form-row"><label for="node-input-connection">Connection</label><input type="text" id="node-input-connection"></div>
|
|
116
|
+
<div class="form-row"><label for="node-input-pin">Pin</label><input type="text" id="node-input-pin" placeholder="V10"></div>
|
|
117
|
+
</script>
|
|
118
|
+
<script type="text/html" data-template-name="relink camera out">
|
|
119
|
+
<div class="form-row"><label for="node-input-name">Name</label><input type="text" id="node-input-name"></div>
|
|
120
|
+
<div class="form-row"><label for="node-input-connection">Connection</label><input type="text" id="node-input-connection"></div>
|
|
121
|
+
<div class="form-row"><label for="node-input-pin">Pin</label><input type="text" id="node-input-pin" placeholder="V10"></div>
|
|
122
|
+
</script>
|
|
123
|
+
<script type="text/html" data-template-name="relink audio in">
|
|
124
|
+
<div class="form-row"><label for="node-input-name">Name</label><input type="text" id="node-input-name"></div>
|
|
125
|
+
<div class="form-row"><label for="node-input-connection">Connection</label><input type="text" id="node-input-connection"></div>
|
|
126
|
+
<div class="form-row"><label for="node-input-pin">Pin</label><input type="text" id="node-input-pin" placeholder="V10"></div>
|
|
127
|
+
<div class="form-row"><label for="node-input-quality">Quality</label><select id="node-input-quality">
|
|
128
|
+
<option value="0">Voice (8 kHz)</option><option value="1">Standard (16 kHz)</option><option value="2">High (48 kHz)</option>
|
|
129
|
+
</select></div>
|
|
130
|
+
</script>
|
|
131
|
+
<script type="text/html" data-template-name="relink audio out">
|
|
132
|
+
<div class="form-row"><label for="node-input-name">Name</label><input type="text" id="node-input-name"></div>
|
|
133
|
+
<div class="form-row"><label for="node-input-connection">Connection</label><input type="text" id="node-input-connection"></div>
|
|
134
|
+
<div class="form-row"><label for="node-input-pin">Pin</label><input type="text" id="node-input-pin" placeholder="V10"></div>
|
|
135
|
+
<div class="form-row"><label for="node-input-rate">Sample rate</label><input type="number" id="node-input-rate" min="4000" max="192000" placeholder="8000"></div>
|
|
136
|
+
</script>
|
|
137
|
+
|
|
138
|
+
<script type="text/html" data-help-name="relink camera in">
|
|
139
|
+
<p>Live camera frames from a device on this token (an ESP32 camera, or a C++ / Python / Node-RED program sending on this pin). Each message is one JPEG.</p>
|
|
140
|
+
<h3>Outputs</h3>
|
|
141
|
+
<dl class="message-properties">
|
|
142
|
+
<dt>payload <span class="property-type">Buffer</span></dt><dd>One JPEG frame.</dd>
|
|
143
|
+
<dt>frameId <span class="property-type">number</span></dt><dd>Frame counter from the sender, 0–65535.</dd>
|
|
144
|
+
<dt>pin <span class="property-type">number</span></dt><dd>The Camera datastream's pin.</dd>
|
|
145
|
+
</dl>
|
|
146
|
+
<p>Frames go directly between the two endpoints and never through the server. Without a direct link no frames arrive. A device streams to one viewer at a time: whoever started watching last. If another viewer takes over, the status says so; send any message to this node to take the stream back.</p>
|
|
147
|
+
</script>
|
|
148
|
+
<script type="text/html" data-help-name="relink camera out">
|
|
149
|
+
<p>Streams JPEG frames from <code>msg.payload</code> (a Buffer) on a Camera pin, like an ESP32 camera does. Up to 400 KB per frame.</p>
|
|
150
|
+
<p>Frames are sent only while someone watches (the Android app, or another program with relink camera in). Otherwise they are dropped and the status shows <i>nobody watching</i>. The web dashboard's Live Camera widget needs the ESP32 or C++ / Python sender; Node-RED cannot reach a browser directly.</p>
|
|
151
|
+
</script>
|
|
152
|
+
<script type="text/html" data-help-name="relink audio in">
|
|
153
|
+
<p>Live sound from a device on this token. Each message is one packet: 20 ms at 8 or 16 kHz, 10 ms at 48 kHz.</p>
|
|
154
|
+
<h3>Outputs</h3>
|
|
155
|
+
<dl class="message-properties">
|
|
156
|
+
<dt>payload <span class="property-type">Buffer</span></dt><dd>Signed 16-bit little-endian mono samples.</dd>
|
|
157
|
+
<dt>rate <span class="property-type">number</span></dt><dd>8000, 16000 or 48000. Trust this rather than the quality you chose: an ESP32 always sends 8000.</dd>
|
|
158
|
+
<dt>samples <span class="property-type">number</span></dt><dd>Number of samples in the payload.</dd>
|
|
159
|
+
</dl>
|
|
160
|
+
<p>Peer-to-peer only, like the camera. The device only reads its microphone while someone listens.</p>
|
|
161
|
+
</script>
|
|
162
|
+
<script type="text/html" data-help-name="relink audio out">
|
|
163
|
+
<p>Sends sound to every device on this token that has a direct link. <code>msg.payload</code> is 16-bit mono samples: a Buffer (s16le), an Int16Array or an array of numbers. The rate comes from <code>msg.rate</code>, else the node's setting.</p>
|
|
164
|
+
<p>Each listener gets the quality it asked for: Voice (8 kHz), Standard (16 kHz) or High (48 kHz), resampled once per quality. ESP32s always get Voice. A partial packet waits for the next message.</p>
|
|
165
|
+
</script>
|
package/nodes/relink.js
CHANGED
|
@@ -81,7 +81,106 @@ module.exports = function(RED) {
|
|
|
81
81
|
});
|
|
82
82
|
}
|
|
83
83
|
|
|
84
|
+
// Camera and audio go peer-to-peer only (never through the relay), so they
|
|
85
|
+
// need a direct link to the other endpoint: the status says when there is none.
|
|
86
|
+
function mediaPin(node, config) {
|
|
87
|
+
try { return pinNumber(config.pin); }
|
|
88
|
+
catch (error) { node.error("Choose a pin, V0–V255"); node.status(statusFor("error")); return null; }
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
function CameraInNode(config) {
|
|
92
|
+
RED.nodes.createNode(this, config);
|
|
93
|
+
const connection = attach(this, config);
|
|
94
|
+
if (!connection) return;
|
|
95
|
+
const pin = mediaPin(this, config);
|
|
96
|
+
if (pin === null) return;
|
|
97
|
+
const client = connection.client;
|
|
98
|
+
client.watchCamera(pin);
|
|
99
|
+
let frames = 0;
|
|
100
|
+
const listener = event => {
|
|
101
|
+
if (event.pin !== pin) return;
|
|
102
|
+
frames++;
|
|
103
|
+
this.send({ topic: `V${pin}`, payload: event.frame, pin, frameId: event.frameId, checksum: event.checksum });
|
|
104
|
+
};
|
|
105
|
+
const kicked = () => this.status({ fill: "yellow", shape: "ring", text: "another viewer took the camera" });
|
|
106
|
+
// A frame count each 2 s while frames arrive; otherwise the connection state.
|
|
107
|
+
const timer = setInterval(() => {
|
|
108
|
+
if (frames) this.status({ fill: "green", shape: "dot", text: `${(frames / 2).toFixed(1)} fps` });
|
|
109
|
+
else if (!client.camKicked) this.status(statusFor(connection.connectionState));
|
|
110
|
+
frames = 0;
|
|
111
|
+
}, 2000);
|
|
112
|
+
client.on("camera", listener);
|
|
113
|
+
client.on("camera-kicked", kicked);
|
|
114
|
+
this.on("input", (msg, send, done) => { // any message takes the stream back
|
|
115
|
+
client.watchCamera(pin); this.status(statusFor(connection.connectionState)); if (done) done();
|
|
116
|
+
});
|
|
117
|
+
this.on("close", () => {
|
|
118
|
+
clearInterval(timer);
|
|
119
|
+
client.removeListener("camera", listener); client.removeListener("camera-kicked", kicked);
|
|
120
|
+
client.watchCamera(pin, false);
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
function CameraOutNode(config) {
|
|
125
|
+
RED.nodes.createNode(this, config);
|
|
126
|
+
const connection = attach(this, config);
|
|
127
|
+
const pin = mediaPin(this, config);
|
|
128
|
+
let sent = 0, dropped = 0;
|
|
129
|
+
const timer = setInterval(() => {
|
|
130
|
+
if (!connection || pin === null) return;
|
|
131
|
+
if (sent) this.status({ fill: "green", shape: "dot", text: `watched, ${(sent / 2).toFixed(1)} fps` });
|
|
132
|
+
else if (dropped) this.status({ fill: "grey", shape: "ring", text: "nobody watching" });
|
|
133
|
+
else this.status(statusFor(connection.connectionState));
|
|
134
|
+
sent = dropped = 0;
|
|
135
|
+
}, 2000);
|
|
136
|
+
this.on("input", (msg, send, done) => {
|
|
137
|
+
try {
|
|
138
|
+
if (!connection || pin === null) throw new Error("Configure a valid Relink connection and pin");
|
|
139
|
+
if (connection.client.cameraWrite(pin, msg.payload)) sent++; else dropped++;
|
|
140
|
+
if (done) done();
|
|
141
|
+
} catch (error) { if (done) done(error); else this.error(error.message, msg); }
|
|
142
|
+
});
|
|
143
|
+
this.on("close", () => clearInterval(timer));
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
function AudioInNode(config) {
|
|
147
|
+
RED.nodes.createNode(this, config);
|
|
148
|
+
const connection = attach(this, config);
|
|
149
|
+
if (!connection) return;
|
|
150
|
+
const pin = mediaPin(this, config);
|
|
151
|
+
if (pin === null) return;
|
|
152
|
+
const client = connection.client;
|
|
153
|
+
client.listenAudio(pin, true, Number(config.quality) || 0);
|
|
154
|
+
const listener = event => {
|
|
155
|
+
if (event.pin !== pin) return;
|
|
156
|
+
const s = event.samples;
|
|
157
|
+
this.send({ topic: `V${pin}`, payload: Buffer.from(s.buffer, s.byteOffset, s.byteLength), rate: event.rate,
|
|
158
|
+
samples: s.length, pin, checksum: event.checksum });
|
|
159
|
+
};
|
|
160
|
+
client.on("audio", listener);
|
|
161
|
+
this.on("close", () => { client.removeListener("audio", listener); client.listenAudio(pin, false); });
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
function AudioOutNode(config) {
|
|
165
|
+
RED.nodes.createNode(this, config);
|
|
166
|
+
const connection = attach(this, config);
|
|
167
|
+
const pin = mediaPin(this, config);
|
|
168
|
+
this.on("input", (msg, send, done) => {
|
|
169
|
+
try {
|
|
170
|
+
if (!connection || pin === null) throw new Error("Configure a valid Relink connection and pin");
|
|
171
|
+
const p = msg.payload;
|
|
172
|
+
if (!Buffer.isBuffer(p) && !Array.isArray(p) && !(p instanceof Int16Array)) throw new Error("Audio payload must be 16-bit samples: a Buffer (s16le), Int16Array or array");
|
|
173
|
+
connection.client.audioWrite(pin, Array.isArray(p) ? Int16Array.from(p) : p, Number(msg.rate) || Number(config.rate) || 8000);
|
|
174
|
+
if (done) done();
|
|
175
|
+
} catch (error) { if (done) done(error); else this.error(error.message, msg); }
|
|
176
|
+
});
|
|
177
|
+
}
|
|
178
|
+
|
|
84
179
|
RED.nodes.registerType("relink-connection", Connection, { credentials: { token: { type: "password" }, key: { type: "password" } } });
|
|
85
180
|
RED.nodes.registerType("relink in", InNode);
|
|
86
181
|
RED.nodes.registerType("relink out", OutNode);
|
|
182
|
+
RED.nodes.registerType("relink camera in", CameraInNode);
|
|
183
|
+
RED.nodes.registerType("relink camera out", CameraOutNode);
|
|
184
|
+
RED.nodes.registerType("relink audio in", AudioInNode);
|
|
185
|
+
RED.nodes.registerType("relink audio out", AudioOutNode);
|
|
87
186
|
};
|