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

opcua: nsu in nodeId (fixing #1334) #1335

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
5,503 changes: 2,104 additions & 3,399 deletions package-lock.json

Large diffs are not rendered by default.

43 changes: 21 additions & 22 deletions packages/binding-opcua/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,29 +21,28 @@
"@node-wot/core": "0.8.17",
"ajv": "^8.11.0",
"ajv-formats": "^2.1.1",
"node-opcua": "2.113.0",
"node-opcua-address-space": "2.113.0",
"node-opcua-basic-types": "2.113.0",
"node-opcua-binary-stream": "2.110.0",
"node-opcua-buffer-utils": "2.110.0",
"node-opcua-client": "2.113.0",
"node-opcua-constants": "2.98.1",
"node-opcua-data-model": "2.113.0",
"node-opcua-data-value": "2.113.0",
"node-opcua-date-time": "2.113.0",
"node-opcua-debug": "2.113.0",
"node-opcua-extension-object": "2.113.0",
"node-opcua-factory": "2.113.0",
"node-opcua": "2.138.1",
"node-opcua-address-space": "2.138.1",
"node-opcua-basic-types": "2.134.0",
"node-opcua-binary-stream": "2.133.0",
"node-opcua-buffer-utils": "2.133.0",
"node-opcua-client": "2.138.1",
"node-opcua-constants": "2.125.0",
"node-opcua-data-model": "2.137.0",
"node-opcua-data-value": "2.137.0",
"node-opcua-date-time": "2.134.0",
"node-opcua-debug": "2.133.0",
"node-opcua-extension-object": "2.137.0",
"node-opcua-factory": "2.137.0",
"node-opcua-json": "0.50.0",
"node-opcua-nodeid": "2.113.0",
"node-opcua-numeric-range": "2.113.0",
"node-opcua-pseudo-session": "2.113.0",
"node-opcua-pubsub-client": "0.19.1",
"node-opcua-service-browse": "2.113.0",
"node-opcua-service-translate-browse-path": "2.113.0",
"node-opcua-status-code": "2.110.0",
"node-opcua-types": "2.113.0",
"node-opcua-variant": "2.113.0",
"node-opcua-nodeid": "2.133.0",
"node-opcua-numeric-range": "2.137.0",
"node-opcua-pseudo-session": "2.138.0",
"node-opcua-service-browse": "2.137.0",
"node-opcua-service-translate-browse-path": "2.137.0",
"node-opcua-status-code": "2.133.0",
"node-opcua-types": "2.137.0",
"node-opcua-variant": "2.137.0",
"rxjs": "5.5.11"
},
"scripts": {
Expand Down
29 changes: 22 additions & 7 deletions packages/binding-opcua/src/opcua-protocol-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ import {
Variant,
VariantOptions,
} from "node-opcua-client";
import { ArgumentDefinition, getBuiltInDataType } from "node-opcua-pseudo-session";
import { ArgumentDefinition, getBuiltInDataType, readNamespaceArray } from "node-opcua-pseudo-session";

import { makeNodeId, NodeId, NodeIdLike, NodeIdType, resolveNodeId } from "node-opcua-nodeid";
import { AttributeIds, BrowseDirection, makeResultMask } from "node-opcua-data-model";
Expand Down Expand Up @@ -81,6 +81,7 @@ interface OPCUAConnection {
session: ClientSession;
client: OPCUAClient;
subscription: ClientSubscription;
namespaceArray?: string[];
}

type Resolver = (...arg: [...unknown[]]) => void;
Expand Down Expand Up @@ -211,6 +212,15 @@ export class OPCUAProtocolClient implements ProtocolClient {
});
}

private async _getNamespaceArray(form: OPCUAForm): Promise<string[]> {
return this._withConnection(form, async (c: OPCUAConnection) => {
if (!c.namespaceArray) {
c.namespaceArray = await readNamespaceArray(c.session);
}
return c.namespaceArray;
});
}

private async _withSubscription<T>(
form: OPCUAForm,
next: (session: ClientSession, subscription: ClientSubscription) => Promise<T>
Expand All @@ -220,13 +230,18 @@ export class OPCUAProtocolClient implements ProtocolClient {
});
}

private async _resolveNodeId3(form: OPCUAForm, nodeId: NodeIdLike): Promise<NodeId> {
const namespaceArray = await this._getNamespaceArray(form);
return resolveNodeId(nodeId, { namespaceArray });
}

private async _resolveNodeId2(form: OPCUAForm, fNodeId: NodeIdLike | NodeByBrowsePath): Promise<NodeId> {
if (fNodeId instanceof NodeId) {
return fNodeId;
} else if ((<NodeByBrowsePath>fNodeId).root != null) {
const f = <NodeByBrowsePath>fNodeId;
const r: NodeIdLike = f.root;
const rootNodeId = resolveNodeId(r);
const rootNodeId = await this._resolveNodeId3(form, r);
const nodeId = this._withSession<NodeId>(form, async (session) => {
const path = makeBrowsePath(rootNodeId, f.path);
const result = await session.translateBrowsePath(path);
Expand All @@ -241,7 +256,7 @@ export class OPCUAProtocolClient implements ProtocolClient {
});
return nodeId;
} else {
return resolveNodeId(fNodeId as NodeIdLike);
return await this._resolveNodeId3(form, fNodeId as NodeIdLike);
}
}

Expand All @@ -262,10 +277,10 @@ export class OPCUAProtocolClient implements ProtocolClient {
throw new Error("form must expose a 'opcua:nodeId'");
}
const nodeId = await this._resolveNodeId2(form, fNodeId);
return await this._withSession<DataType>(form, async (session: IBasicSession) => {
const dataTypeOrNull = await promisify(getBuiltInDataType)(session, nodeId);
if (dataTypeOrNull !== null) {
return dataTypeOrNull as DataType;
return await this._withSession<DataType>(form, async (session) => {
const dataTypeOrNull = await getBuiltInDataType(session, nodeId);
if (dataTypeOrNull !== undefined && dataTypeOrNull !== DataType.Null) {
return dataTypeOrNull;
}
throw new Error("cannot predict dataType for nodeId " + nodeId.toString());
});
Expand Down
13 changes: 13 additions & 0 deletions packages/binding-opcua/test/fixture/basic-opcua-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,19 @@ export async function startServer(): Promise<OPCUAServer> {
throw new Error("cannot find DI namespace");
}

const namespaceSpecial = addressSpace.registerNamespace("http://example.org/SpecialNamespace/");
const uaSpecialObject = namespaceSpecial.addObject({
browseName: "SpecialObject",
organizedBy: addressSpace.rootFolder.objects,
});
const uaSpecialVariable = namespaceSpecial.addVariable({
browseName: "SpecialVariable",
nodeId: "s=SpecialVariable",
dataType: "Double",
componentOf: uaSpecialObject,
});
uaSpecialVariable.setValueFromSource({ dataType: DataType.Double, value: 42.0 });

const deviceType = addressSpace.findObjectType("DeviceType", nsDI);
if (!deviceType) {
throw new Error("cannot find DeviceType");
Expand Down
30 changes: 30 additions & 0 deletions packages/binding-opcua/test/full-opcua-thing-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,23 @@ const thingDescription: WoT.ThingDescription = {
},
],
},

specialVariable: {
description: "a special variable",
observable: true,
readOnly: true,
unit: "°C",
type: "number",
"opcua:nodeId": "nsu=http://example.org/SpecialNamespace/;s=SpecialVariable",
forms: [
{
href: "/", // endpoint,
op: ["readproperty", "observeproperty"],
"opcua:nodeId": "nsu=http://example.org/SpecialNamespace/;s=SpecialVariable",
contentType: "application/json",
},
],
},
},
actions: {
setTemperatureSetPoint: {
Expand Down Expand Up @@ -617,4 +634,17 @@ describe("Full OPCUA Thing Test", () => {
await servient.shutdown();
}
});

it("Z11 - should read a variable with nodeid containing a NSU namespace", async () => {
const { thing, servient } = await makeThing();

try {
const content = await thing.readProperty("specialVariable");
const value = await content.value();
debug(`specialVariable = ${value}`);
expect(value).to.eql(42);
} finally {
await servient.shutdown();
}
});
});
2 changes: 1 addition & 1 deletion packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"@thingweb/thing-model": "^1.0.1",
"ajv": "^8.11.0",
"commander": "^9.1.0",
"dotenv": "^8.6.0",
"dotenv": "^16.4.5",
"lodash": "^4.17.21",
"vm2": "3.9.18"
},
Expand Down
2 changes: 1 addition & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
"uritemplate": "0.3.4",
"url-toolkit": "2.1.6",
"uuid": "^7.0.3",
"web-streams-polyfill": "^3.0.1"
"web-streams-polyfill": "^4.0.0"
},
"scripts": {
"build": "tsc -b",
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/content-serdes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import OctetstreamCodec from "./codecs/octetstream-codec";
import { DataSchema, DataSchemaValue } from "wot-typescript-definitions";
import { Readable } from "stream";
import { ProtocolHelpers } from "./core";
import { ReadableStream } from "web-streams-polyfill/ponyfill/es2018";
import { ReadableStream } from "web-streams-polyfill";
import { createLoggers } from "./logger";

const { debug, warn } = createLoggers("core", "content-serdes");
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/exposed-thing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import Helpers from "./helpers";
import { InteractionOutput } from "./interaction-output";
import { Readable } from "stream";
import ProtocolHelpers from "./protocol-helpers";
import { ReadableStream as PolyfillStream } from "web-streams-polyfill/ponyfill/es2018";
import { ReadableStream as PolyfillStream } from "web-streams-polyfill";
import { Content, ContentSerdes, PropertyContentMap } from "./core";
import ContentManager from "./content-serdes";
import {
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/protocol-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

import { Form, ThingInteraction } from "./thing-description";
import { Readable } from "stream";
import { ReadableStream as PolyfillStream } from "web-streams-polyfill/ponyfill/es2018";
import { ReadableStream as PolyfillStream } from "web-streams-polyfill";
import { ActionElement, EventElement, PropertyElement } from "wot-thing-description-types";
import { createLoggers } from "./logger";

Expand Down
2 changes: 1 addition & 1 deletion packages/core/test/ProtocolHelpersStreamTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import { suite, test } from "@testdeck/mocha";
import { expect, should } from "chai";
import { once, Readable } from "stream";
import { ReadableStream } from "web-streams-polyfill/ponyfill/es2018";
import { ReadableStream } from "web-streams-polyfill";
import ProtocolHelpers from "../src/protocol-helpers";

should();
Expand Down
4 changes: 2 additions & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"target": "es6",
"lib": ["dom"],
"skipLibCheck": false,
"module": "commonjs",
"module": "Node16",
"outDir": "dist",
"strict": true,
"composite": true,
Expand All @@ -16,7 +16,7 @@
"experimentalDecorators": true,
"declaration": true,
"esModuleInterop": true,
"moduleResolution": "node" /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */,
"moduleResolution": "Node16" /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */,
"resolveJsonModule": false,
"skipDefaultLibCheck": false,
"allowJs": false,
Expand Down