Skip to content

Commit

Permalink
status & error relay to editor pass ID as conversation only; debug im…
Browse files Browse the repository at this point in the history
…plements active & sends console/sidebar to xsbug log, debug to status truncates to 32-characters
  • Loading branch information
phoddie committed Nov 3, 2022
1 parent 5cc394b commit 93c9f67
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 29 deletions.
53 changes: 26 additions & 27 deletions nodered.js
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ export class Node {
const wires = config.wires;
if (wires) {
this.#outputs = wires.map(wire => wire.map(target => this.#flow.getNode(target)));
if (!config.dones && !config.errors)
if (!config.dones && !config.errors && !config.statuses)
return;
}
else {
Expand Down Expand Up @@ -293,25 +293,23 @@ export class Node {
return this.send(result);
}
status(status) {
const source = {
id: this.id
};
trace.left(JSON.stringify({status}), this.id);

const statuses = this.#outputs.statuses;
if (!statuses)
return;

const msg = {
status: {
...status,
},
source
source: {
id: this.id,
type: this.constructor.type,
name: this.name
}
};

trace.left(JSON.stringify(msg), this.id);

const statuses = this.#outputs.statuses;
if (!statuses)
return;

source.type = this.constructor.type;
source.name = this.name;
for (let i = 0, length = statuses.length; i < length; i++)
RED.mcu.enqueue(msg, statuses[i]);
}
Expand Down Expand Up @@ -429,6 +427,7 @@ class DebugNode extends Node {
#statusType;
#statusVal;
#oldStatus;
#active;

onStart(config) {
super.onStart(config);
Expand All @@ -440,6 +439,7 @@ class DebugNode extends Node {
this.#toStatus = config.tostatus;
this.#statusType = config.statusType;
this.#statusVal = config.statusVal;
this.#active = config.active
}
onMessage(msg, done) {
// to prevent endless loops -> 21-debug.js:123
Expand All @@ -448,25 +448,17 @@ class DebugNode extends Node {
return;
}

// Feed msg back to the editor.
trace.left(JSON.stringify({
input: {
...msg,
source: {
id: this.id,
type: this.constructor.type,
name: this.name
}
}
}), this.id);
// Feed msg back to the editor
if (this.#active)
trace.left(JSON.stringify({input: msg}), this.id);

// Process msg for xsbug
let value = this.#getter(msg);

if (this.#console) {
if (value instanceof Uint8Array)
value = Hex.toString(value);
trace(("object" === typeof value) ? JSON.stringify(value) : value, "\n");
trace("<warn>", ("object" === typeof value) ? JSON.stringify(value) : value, "\n");
}

if (this.#sidebar) {
Expand All @@ -478,20 +470,26 @@ class DebugNode extends Node {
type: this.constructor.type,
name: this.name
}
}
trace.right(JSON.stringify(value));
};
trace("<info>", JSON.stringify(value), "\n");
}

if (this.#toStatus) {
const statusVal = this.#statusVal(msg); // NR says: #statusVal shall return typeof string!
if (statusVal !== this.#oldStatus) {
if (statusVal.length > 32)
statusVal = statusVal.slice(0, 32) + "…";
this.status({fill: "grey", shape: "dot", text: statusVal});
this.#oldStatus = statusVal;
}
}

done();
}
onCommand(options) {
if ("debug" === options.command)
this.#active = !!options.active;
}

static type = "debug";
static {
Expand Down Expand Up @@ -1402,6 +1400,7 @@ globalThis.clearInterval = Timer.clear;
globalThis.setTimeout = Timer.set;
globalThis.clearTimeout = Timer.clear;
globalThis.console = Console;
globalThis.Buffer = Buffer;

globalThis.RED = RED;
globalThis.module = Object.freeze({
Expand Down
8 changes: 6 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Node-RED MCU Edition
Copyright 2022, Moddable Tech, Inc. All rights reserved.<br>
Peter Hoddie<br>
Updated October 31, 2022<br>
Updated November 2, 2022<br>

## Introduction
This document introduces an implementation of the Node-RED runtime that runs on resource-constrained microcontrollers (MCUs). [Node-RED](https://nodered.org/) is a popular visual environment that describes itself as "a programming tool for wiring together hardware devices, APIs and online services in new and interesting ways."
Expand Down Expand Up @@ -297,9 +297,12 @@ Comment nodes are removed at build-time.

### Debug
- [X] Console output is displayed in the xsbug log pane
- [X] Sidebar output is displayed in the xsbug message pane
- [X] Sidebar output is displayed in the xsbug log pane
- [X] Display of selected property or complete message
- [X] Output to node status
- [X] Active
- [X] Relay to Node-RED Editor
- [ ] JSONata expression

### Function
- [X] "On Start" and "On Message" handlers
Expand Down Expand Up @@ -341,6 +344,7 @@ Function node implements support for calling `done()` if function's source code
### Status
- [X] "Report status from all nodes"
- [X] "Report status from selected nodes"
- [X] Status relayed to Node-RED Editor

### Complete
- [X] Implemented
Expand Down

0 comments on commit 93c9f67

Please sign in to comment.