Skip to content

Commit

Permalink
add wot-server-td node which is for getting td on server side
Browse files Browse the repository at this point in the history
  • Loading branch information
hidetak committed Mar 6, 2024
1 parent a85fbbc commit 63cf1a5
Show file tree
Hide file tree
Showing 12 changed files with 369 additions and 9 deletions.
3 changes: 2 additions & 1 deletion node-red-node-wot/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@
"wot-server-end": "dist/wot-server-end.js",
"wot-server-property": "dist/wot-server-property.js",
"wot-server-action": "dist/wot-server-action.js",
"wot-server-event": "dist/wot-server-event.js"
"wot-server-event": "dist/wot-server-event.js",
"wot-server-td": "dist/wot-server-td.js"
}
},
"scripts": {
Expand Down
37 changes: 37 additions & 0 deletions node-red-node-wot/src/locales/en-US/wot-server-td.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<script type="text/html" data-help-name="wot-server-td">
<p>This is a node that outputs the Thing Description (TD) managed on the WoT server side.</p>
<p>The TD is output at the following times:</p>
<ul>
<li>When the server start is complete.</li>
<li>When there is an input to the node.</li>
</ul>

<h3>Inputs</h3>
<dl class="message-properties">
<dt>
Request for TD
<span class="property-type">-</span>
</dt>
<dd>Request a TD by making an input to the node. There is no need to include a value in the input.</dd>
</dl>

<h3>Outputs</h3>
<dl class="message-properties">
<dt>TD <span class="property-type">Object</span></dt>
<dd>Outputs the TD managed by the server.</dd>
</dl>

<h3>Details</h3>
<p>Specify the following items in the node's properties screen:</p>
<ul>
<li>Name: Specify the node name.</li>
<li>Server config: Specify the server that will publish the property.</li>
<li>Thing config: Specify the Thing that the property belongs to.</li>
<li>TD output after server start: Specify whether to output the TD after the server start.</li>
<li>TD destination: Specify where to output the TD.</li>
</ul>
<p>
This node uses <a href="https://github.com/eclipse-thingweb/node-wot">eclipse-thingweb/node-wot</a> for WoT
implementation.
</p>
</script>
18 changes: 18 additions & 0 deletions node-red-node-wot/src/locales/en-US/wot-server-td.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"editor": {
"paletteLabel": "Server-TD",
"nameLabel": "Name",
"serverConfigLabel": "Server config",
"thingConfigLabel": "Thing config",
"outputTDAfterServerStartFlagLabel": "TD output after server start",
"outParams1": {
"tabLabel": "Output TD",
"outputLabel": "TD",
"td": {
"label": "TD destination",
"description": "TD destination.",
"placeholder": "TD destination"
}
}
}
}
2 changes: 1 addition & 1 deletion node-red-node-wot/src/locales/ja/wot-server-property.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ <h3>Inputs</h3>
<dl class="message-properties">
<dt>
プロパティ変更の通知
<span class="property-type">any(プロパティの定義による)</span>
<span class="property-type">-</span>
</dt>
<dd>
サーバ側でプロパティに変更があった場合に入力を行います。入力を行うことで観測しているクライアントに通知されます。入力に値を含める必要はありません。
Expand Down
37 changes: 37 additions & 0 deletions node-red-node-wot/src/locales/ja/wot-server-td.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<script type="text/html" data-help-name="wot-server-td">
<p>WoTのサーバ側で管理するThing Description(TD)を出力するノードです。</p>
<p>TDは次のタイミングで出力します。</p>
<ul>
<li>サーバの起動が完了したタイミング。</li>
<li>ノードに対して入力があったタイミング。</li>
</ul>

<h3>Inputs</h3>
<dl class="message-properties">
<dt>
TDの要求
<span class="property-type">-</span>
</dt>
<dd>ノードに入力を行うことでTDを要求します。入力に値を含める必要はありません。</dd>
</dl>

<h3>Outputs</h3>
<dl class="message-properties">
<dt>TD <span class="property-type">Object</span></dt>
<dd>サーバで管理するTDを出力します。</dd>
</dl>

<h3>Details</h3>
<p>ノードのプロパティ画面では以下の項目を指定します。</p>
<ul>
<li>名前: ノード名を指定します。</li>
<li>サーバ設定: 取得するTDを管理するサーバを指定します。</li>
<li>Thing設定: 取得するTDのThingを指定します。</li>
<li>サーバ起動後のTD出力: サーバ起動後にTDを出力するかどうかを指定します。</li>
<li>TDの出力先: TDの出力先を指定します。</li>
</ul>
<p>
本ノードでは、WoTの実装として<a href="https://github.com/eclipse-thingweb/node-wot">eclipse-thingweb/node-wot</a
>を利用します。
</p>
</script>
18 changes: 18 additions & 0 deletions node-red-node-wot/src/locales/ja/wot-server-td.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"editor": {
"paletteLabel": "Server-TD",
"nameLabel": "名前",
"serverConfigLabel": "サーバ設定",
"thingConfigLabel": "Thing設定",
"outputTDAfterServerStartFlagLabel": "サーバ起動後のTD出力",
"outParams1": {
"tabLabel": "TD出力",
"outputLabel": "TD",
"td": {
"label": "TDの出力先",
"description": "取得したTDの出力先。",
"placeholder": "取得したTDの出力先"
}
}
}
}
4 changes: 2 additions & 2 deletions node-red-node-wot/src/wot-server-action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,12 @@ module.exports = function (RED) {
done()
})

const woTServerConfig = RED.nodes.getNode(config.woTServerConfig) //test
const woTServerConfig = RED.nodes.getNode(config.woTServerConfig)
woTServerConfig.addUserNode(node)
}
RED.nodes.registerType("wot-server-action", WoTServerAction, {
credentials: {
inParams_actionName: { type: "text" },
outParams1_actionArgs: { type: "text" },
},
})
}
11 changes: 10 additions & 1 deletion node-red-node-wot/src/wot-server-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,17 +176,26 @@ module.exports = function (RED) {
// make thing title list
const thingNamesObj = {}
for (const userNode of userNodes) {
if (userNode.type === "wot-server-td") {
continue
}
thingNamesObj[userNode.getThingProps().title] = true
}
const thingNames = Object.keys(thingNamesObj)
// Generate and Expose a Thing for each Thing title
for (const thingName of thingNames) {
const targetNodes = userNodes.filter((n) => n.getThingProps().title === thingName)
const targetNodes = userNodes.filter(
(n) => n.type !== "wot-server-td" && n.getThingProps().title === thingName
)
const thingProps = targetNodes[0]?.getThingProps() || {}
await createWoTScriptAndExpose(thingProps, servientWrapper, targetNodes)
}
node.running = true
userNodes.forEach((n) => {
if (n.type === "wot-server-td" && n.getOutputTDAfterServerStartFlag() === true) {
// send trigger to wot-server-td node for getting TD
n.receive({})
}
n.setServientStatus(node.running)
})
} catch (err) {
Expand Down
4 changes: 2 additions & 2 deletions node-red-node-wot/src/wot-server-event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,12 @@ module.exports = function (RED) {
done()
})

const woTServerConfig = RED.nodes.getNode(config.woTServerConfig) //test
const woTServerConfig = RED.nodes.getNode(config.woTServerConfig)
woTServerConfig?.addUserNode(node)
}
RED.nodes.registerType("wot-server-event", WoTServerEvent, {
credentials: {
inParams_propertyName: { type: "text" },
inParams_eventValue: { type: "text" },
},
})

Expand Down
4 changes: 2 additions & 2 deletions node-red-node-wot/src/wot-server-property.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,12 @@ module.exports = function (RED) {
done()
})

const woTServerConfig = RED.nodes.getNode(config.woTServerConfig) //test
const woTServerConfig = RED.nodes.getNode(config.woTServerConfig)
woTServerConfig?.addUserNode(node)
}
RED.nodes.registerType("wot-server-property", WoTServerProperty, {
credentials: {
inParams_propertyName: { type: "text" },
outParams2_writingValue: { type: "text" },
},
})

Expand Down
161 changes: 161 additions & 0 deletions node-red-node-wot/src/wot-server-td.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
<script type="text/javascript">
;(() => {
RED.nodes.registerType("wot-server-td", {
category: "Web of Things",
color: "#a2dea0",
defaults: {
name: { value: "" },
outParams1_tdType: {
value: "msg",
},
outParams1_tdConstValue: {
value: "payload",
},
woTServerConfig: {
type: "wot-server-config",
value: "",
required: true,
},
woTThingConfig: {
type: "wot-thing-config",
value: "",
required: true,
},
outputTDAfterServerStartFlag: {
value: true,
required: true,
},
},
credentials: {
outParams1_td: { type: "text" },
},
inputs: 1,
outputs: 1,
icon: "arrow.png",
label: function () {
return this.name || this._("editor.paletteLabel") || "wot-server-td"
},
labelStyle: function () {
return this.name ? "node_label_italic" : ""
},
paletteLabel: function () {
return this._("editor.paletteLabel") || "node"
},
onpaletteadd: function () {},
oneditprepare: function () {
console.log("node oneditprepare")
// Tab
const tabs = RED.tabs.create({
id: "red-tabs",
onchange(tab) {
$("#tabs-content").children().hide()
$("#" + tab.id).show()
$("#red-tabs").resize()
},
})
tabs.addTab({
id: "tab-outParams1-settings",
label: this._("editor.outParams1.tabLabel"),
})

prepareInOutParamSetting(
"outParams1",
{
name: "td",
types: ["msg"],
defaultType: "msg",
defaultValue: "payload",
required: true,
},
this
)
},
oneditsave: function () {
saveInOutParamSetting(
"outParams1",
{
name: "td",
types: ["msg"],
defaultType: "msg",
defaultValue: "payload",
required: true,
},
this
)
},
})
const prepareInOutParamSetting = (inOrOutParams, params, _this) => {
const { name, types, defaultType, defaultValue, required } = params
const varName = `${inOrOutParams}_${name}`
if (!_this[`${varName}Type`]) {
_this[`${varName}Type`] = defaultType
}
if (this[`${varName}Type`] == "str") {
$(`#node-input-${varName}ConstValue`).val("")
} else {
if (_this[`${varName}ConstValue`] == "") {
$(`#node-input-${varName}ConstValue`).val(_this.credentials[varName])
} else {
_this.credentials[varName] = _this[`${varName}ConstValue`]
$(`#node-input-${varName}`).val(_this.credentials[`${varName}`])
}
}
$(`#node-input-${varName}Type`).val(_this[`${varName}Type`])
$(`#node-input-${varName}`).typedInput({
default: defaultType,
typeField: $(`#node-input-${varName}Type`),
types: types,
})
$(`#node-input-${varName}`).typedInput("type", _this[`${varName}Type`])
}
const saveInOutParamSetting = (inOrOutParams, params, _this) => {
const varName = `${inOrOutParams}_${params.name}`
if ($(`#node-input-${varName}Type`).val() != "str") {
_this[`${varName}ConstValue`] = $(`#node-input-${varName}`).val()
$(`#node-input-${varName}ConstValue`).val(_this[`${varName}ConstValue`])
} else {
$(`#node-input-${varName}ConstValue`).val("")
_this[`${varName}ConstValue`] = ""
}
}
})()
</script>

<script type="text/html" data-template-name="wot-server-td">

<div class="form-row">
<label for="node-input-name"><span data-i18n="editor.nameLabel"></span></label>
<input type="text" id="node-input-name" data-i18n="[placeholder]editor.nameLabel">
</div>
<div class='form-row'>
<label for='node-input-woTServerConfig' style='font-size: 95%;'><span data-i18n="editor.serverConfigLabel"></label>
<input required type='url' id='node-input-woTServerConfig' placeholder='[placeholder]editor.serverConfigLabel'>
</div>
<div class='form-row'>
<label for='node-input-woTThingConfig' style='font-size: 95%;'><span data-i18n="editor.thingConfigLabel"></label>
<input required type='url' id='node-input-woTThingConfig' placeholder='[placeholder]editor.thingConfigLabel'>
</div>
<div class="form-row">
<label for="node-input-outputTDAfterServerStartFlag"><span data-i18n="editor.outputTDAfterServerStartFlagLabel"></span></label>
<input type="checkbox" id="node-input-outputTDAfterServerStartFlag">
</div>

<!-- Tab -->
<div class="form-row">
<ul style="min-width: 500px; margin-bottom: 20px;" id="red-tabs">
</ul>
</div>
<!-- Tab contents -->
<div id="tabs-content" style="min-height:250px;">
<div id="tab-outParams1-settings">
<div class='form-row'>
<label for='node-input-outParams1_td'><span data-i18n="editor.outParams1.td.label"></span></label>
<input required type='text' id='node-input-outParams1_td' data-i18n="[placeholder]editor.outParams1.td.placeholder">
<input type='hidden' id='node-input-outParams1_tdType'>
<input type='hidden' id='node-input-outParams1_tdConstValue'>
</div>
</div>
</div>
</script>

<style></style>
Loading

0 comments on commit 63cf1a5

Please sign in to comment.