Skip to content

Commit

Permalink
add update td node
Browse files Browse the repository at this point in the history
  • Loading branch information
hidetak committed Mar 10, 2024
1 parent 61394ed commit b1fdca6
Show file tree
Hide file tree
Showing 12 changed files with 673 additions and 156 deletions.
6 changes: 3 additions & 3 deletions node-red-node-wot/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions node-red-node-wot/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"wot-property": "dist/wot-property.js",
"wot-action": "dist/wot-action.js",
"wot-event": "dist/wot-event.js",
"wot-update-td": "dist/wot-update-td.js",
"wot-server-config": "dist/wot-server-config.js",
"wot-thing-config": "dist/wot-thing-config.js",
"wot-server-end": "dist/wot-server-end.js",
Expand All @@ -33,7 +34,7 @@
"wot-server-event": "dist/wot-server-event.js",
"wot-server-td": "dist/wot-server-td.js"
},
"version":">=2.0.0"
"version": ">=2.0.0"
},
"scripts": {
"build": "npm run copy:src2dist && tsc",
Expand All @@ -52,7 +53,7 @@
"@node-wot/core": "0.8.12"
},
"devDependencies": {
"@types/chai": "^4.3.11",
"@types/chai": "^4.3.12",
"@types/chai-as-promised": "^7.1.8",
"@types/mocha": "^10.0.6",
"@types/node-red": "^1.3.4",
Expand Down
53 changes: 35 additions & 18 deletions node-red-node-wot/src/wot-action.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ module.exports = function (RED) {
function invokeActionNode(config) {
RED.nodes.createNode(this, config)
let node = this
let consumedThing

this.status({})

if (!config.thing) {
this.status({ fill: "red", shape: "dot", text: "Error: Thing undefined" })
Expand All @@ -17,33 +20,47 @@ module.exports = function (RED) {
return
}

const thingNode = RED.nodes.getNode(config.thing)
thingNode.addUpdateTDListener(async (_consumedThing) => {
consumedThing = _consumedThing
})

this.on("input", function (msg, send, done) {
RED.nodes.getNode(config.thing).consumedThing.then((consumedThing) => {
const uriVariables = config.uriVariables ? JSON.parse(config.uriVariables) : undefined
consumedThing
.invokeAction(config.action, msg.payload, {
uriVariables: uriVariables,
})
.then(async (resp) => {
const payload = resp ? await resp.value() : ""
if (!consumedThing) {
node.error("[error] consumedThing is not defined.")
done("consumedThing is not defined.")
return
}
const uriVariables = config.uriVariables ? JSON.parse(config.uriVariables) : undefined
consumedThing
.invokeAction(config.action, msg.payload, {
uriVariables: uriVariables,
})
.then(async (resp) => {
let payload
try {
payload = await resp.value()
node.send({ payload: payload, topic: config.topic })
node.status({
fill: "green",
shape: "dot",
text: "invoked",
})
done()
} catch (err) {
console.error(`[error] failed to get return value. err: `, err)
done(`[error] failed to get return value. err: ${err.toString()}`)
}
})
.catch((err) => {
node.warn(err)
node.status({
fill: "red",
shape: "ring",
text: err.message,
})
.catch((err) => {
node.warn(err)
node.status({
fill: "red",
shape: "ring",
text: err.message,
})
done(err)
})
})
done(err)
})
})

this.on("close", function (removed, done) {
Expand Down
148 changes: 69 additions & 79 deletions node-red-node-wot/src/wot-event.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ module.exports = function (RED) {
function subscribeEventNode(config) {
RED.nodes.createNode(this, config)
let node = this
let consumedThing
let subscription

this.subscription = undefined
this.status({})

if (!config.thing) {
Expand All @@ -16,95 +17,84 @@ module.exports = function (RED) {
return
}

RED.nodes
.getNode(config.thing)
.consumedThing.then(async (consumedThing) => {
let subscription
// Repeat until event subscription succeeds.
try {
while (true) {
subscription = await consumedThing
.subscribeEvent(
config.event,
async (resp) => {
if (resp) {
let payload
try {
payload = await resp.value()
} catch (err) {
node.error(`[error] failed to get event. err: ${err.toString()}`)
console.error(`[error] failed to get event. err: `, err)
}
node.send({ payload, topic: config.topic })
const thingNode = RED.nodes.getNode(config.thing)
thingNode.addUpdateTDListener(async (_consumedThing) => {
if (subscription) {
// Stop if already subscribed
await subscription.stop()
}
consumedThing = _consumedThing
// Repeat until event subscription succeeds.
try {
while (true) {
subscription = await consumedThing
.subscribeEvent(
config.event,
async (resp) => {
if (resp) {
let payload
try {
payload = await resp.value()
} catch (err) {
node.error(`[error] failed to get event. err: ${err.toString()}`)
console.error(`[error] failed to get event. err: `, err)
}
node.status({
fill: "green",
shape: "dot",
text: "Subscribed",
})
},
(err) => {
console.error("[error] subscribe events.", err)
node.error(`[error] subscribe events. err: ${err.toString()}`)
node.status({
fill: "red",
shape: "ring",
text: "Subscription error",
})
},
() => {
console.error("[warn] Subscription ended.")
node.warn("[warn] Subscription ended.")
node.status({})
node.subscription = undefined
node.send({ payload, topic: config.topic })
}
)
.catch((err) => {
console.warn("[warn] event subscribe error. try again. error: " + err)
})
if (subscription) {
break
}
await (() => {
return new Promise((resolve) => {
setTimeout(() => {
resolve()
}, 500)
})
})()
node.status({
fill: "green",
shape: "dot",
text: "Subscribed",
})
},
(err) => {
console.error("[error] subscribe events.", err)
node.error(`[error] subscribe events. err: ${err.toString()}`)
node.status({
fill: "red",
shape: "ring",
text: "Subscription error",
})
},
() => {
console.error("[warn] Subscription ended.")
node.warn("[warn] Subscription ended.")
node.status({})
subscription = undefined
}
)
.catch((err) => {
console.warn("[warn] event subscribe error. try again. error: " + err)
})
if (subscription) {
break
}
} catch (err) {
node.status({
fill: "red",
shape: "ring",
text: "Subscription error",
await new Promise((resolve) => {
setTimeout(resolve, 500)
})
node.error(`[error] failed to subscribe events. error: ${err.toString()}`)
}
node.subscription = subscription

if (node.subscription) {
node.status({
fill: "green",
shape: "dot",
text: "Subscribed",
})
}
})
.catch((err) => {
} catch (err) {
node.status({
fill: "red",
shape: "ring",
text: "Subscription error",
})
node.error(`[error] Failed to create consumed thing for enents. err: ${err.toString()}`)
})
node.error(`[error] failed to subscribe events. error: ${err.toString()}`)
}

if (subscription) {
node.status({
fill: "green",
shape: "dot",
text: "Subscribed",
})
}
})

this.on("close", function (removed, done) {
if (removed) {
// This node has been deleted
} else {
// This node is being restarted
this.on("close", async function (removed, done) {
if (subscription) {
// Stop if already subscribed
await subscription.stop()
}
done()
})
Expand Down
Loading

0 comments on commit b1fdca6

Please sign in to comment.