Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: use detectProtocolSchemes from @thingweb/td-utils for check… #29

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion node-red-node-wot/package-lock.json

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

3 changes: 2 additions & 1 deletion node-red-node-wot/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@
"@node-wot/binding-mqtt": "0.8.14",
"@node-wot/binding-opcua": "0.8.14",
"@node-wot/binding-websockets": "0.8.14",
"@node-wot/core": "0.8.14"
"@node-wot/core": "0.8.14",
"@thingweb/td-utils": "^1.0.0"
},
"devDependencies": {
"@types/chai": "^4.3.12",
Expand Down
46 changes: 5 additions & 41 deletions node-red-node-wot/plugin-resources-src/node-wot-plugin-lib.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { detectProtocolSchemes } from "@thingweb/td-utils/build";

const PROTOCOLS = ["http", "ws", "coap", "mqtt", "opcua", "modbus"]
const DATATYPES = {
string: {
Expand Down Expand Up @@ -315,52 +317,14 @@ function generateId() {
}

function checkBinding(tdStr: string, binding: string) {
let td
try {
td = JSON.parse(tdStr)
} 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 as any).forms) return false
let checkForms = (item as any).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(detectProtocolSchemes(tdStr))

return bindings.includes(binding)
}

const THING_COMMON_TEMP = `[
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,4 @@
"prettier": "^2.3.2",
"pretty-quick": "^3.1.1"
}
}
}
Loading