Skip to content

Commit

Permalink
update checkBinding in wot-thing.html
Browse files Browse the repository at this point in the history
Signed-off-by: Hasan Eroglu <[email protected]>
  • Loading branch information
hasanheroglu committed Jul 29, 2024
1 parent 0a34468 commit c29daa2
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ function checkBinding(tdStr: string, binding: string) {

const bindings = Object.keys(detectProtocolSchemes(tdStr))

return bindings.some(b => b === binding || b === binding + 's');
return bindings.some(b => b === binding || b === binding + 's')
}

const THING_COMMON_TEMP = `[
Expand Down
3 changes: 2 additions & 1 deletion node-red-node-wot/plugin/node-wot-plugin.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<script type="module" src="resources/@thingweb/node-red-node-wot/@thingweb/td-utils/dist/web-bundle.min.js"></script>
<!-- import tdUtils -->
<script type="module" src="/resources/@thingweb/node-red-node-wot/@thingweb/td-utils/dist/web-bundle.min.js"></script>
<script type="module" src="/resources/@thingweb/node-red-node-wot/node-wot-plugin-lib.js" ></script>
<script type="module">
import {createClientFlowUsingDashboard} from "/resources/@thingweb/node-red-node-wot/node-wot-plugin-lib.js"
Expand Down
46 changes: 5 additions & 41 deletions node-red-node-wot/src/wot-thing.html
Original file line number Diff line number Diff line change
@@ -1,55 +1,19 @@
<!-- import tdUtils -->
<script type="module" src="/resources/@thingweb/node-red-node-wot/@thingweb/td-utils/dist/web-bundle.min.js"></script>
<script type="text/javascript">
// List of supported protocols
// Secure ones (like https) are automatically inferred
const PROTOCOLS = ["http", "ws", "coap", "mqtt", "opcua", "modbus"]

function checkBinding(td_str, binding) {
let td
try {
td = JSON.parse(td_str)
} catch (err) {
return false
}

const affordances = ["properties", "actions", "events"]
// In case of OPC UA we look for opc.tcp in href not opcua
binding = binding === "opcua" ? "opc.tcp" : binding
// Also different for Modbus
binding = binding === "modbus" ? "modbus+tcp" : binding

// Check base URL if present and then possibly affordances
if (td["base"]) {
let base = new URL(td["base"]).protocol
if (base) {
// remove trailing ':'
base = base.slice(0, -1)
const checkBase = base === binding || base === binding + "s"
if (checkBase) return true
}
}

for (const affordance of affordances) {
if (td[affordance]) {
// item is either a property or an action or an event
for (const item of Object.values(td[affordance])) {
if (!item.forms) return false
let checkForms = item.forms.some((form) => {
try {
const parsed = new URL(form.href)
if (parsed.protocol !== null) {
// remove trailing ':'
const scheme = parsed.protocol.slice(0, -1)
return scheme === binding || scheme === binding + "s"
}
} catch (e) {
return false
}
})
if (checkForms) return true
}
}
}
return false
const bindings = Object.keys(tdUtils.detectProtocolSchemes(td_str))

return bindings.some(b => b === binding || b === binding + 's')
}

function containsID(td_str) {
Expand Down

0 comments on commit c29daa2

Please sign in to comment.