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

Merged
merged 1 commit into from
Feb 26, 2025
Merged
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
7,726 changes: 2,059 additions & 5,667 deletions package-lock.json

Large diffs are not rendered by default.

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@
"build:podman": "podman build -t wot-servient .",
"clean:dist": "npm exec --workspaces -- npx rimraf tsconfig.tsbuildinfo dist",
"update:wot-typescript-definitions": "npx npm-check-updates -u -f \"wot-typescript-definitions\" --deep",
"link": "npm link -ws"
"link": "npm link -ws",
"ncu:opcua": "npx -y npm-check-updates -u --deep --dep=dev,prod -f \"node-opcua*\" -t newest"
},
"workspaces": [
"./packages/td-tools",
Expand All @@ -51,7 +52,7 @@
"@types/chai-as-promised": "^7.1.4",
"@types/chai-spies": "^1.0.4",
"@types/mocha": "^9.0.0",
"@types/node": "^18.19.71",
"@types/node": "22.13.1",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this change necessary? @relu91 ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we still support Node 18, so the maximum upgrade that we can do here is to move the minor. See https://github.com/eclipse-thingweb/node-wot/blob/master/.github/workflows/ci.yaml#L22

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should not cause any problem and will put you ahead of the curven but we can downgrade,

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I noticed that the CI runs on Node 18.x and 20.x were fine. @relu91 any opinion?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I noticed that the CI runs on Node 18.x and 20.x were fine. @relu91 any opinion?

They might run fine now, but we would notice only late if Node 22.x introduces any breaking changes in the API. This is something very rare but it can happen. Since we don't need any new feature of Node 22 (yet), I'd prefer to stick with 18.x. Plus the scope of this PR is something else and this change is not strictly related.

"@types/readable-stream": "^4.0.15",
"@types/sinon": "10.0.2",
"@typescript-eslint/eslint-plugin": "^6.7.3",
Expand Down
42 changes: 21 additions & 21 deletions packages/binding-opcua/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,28 +21,28 @@
"@node-wot/core": "0.9.0",
"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.143.0",
"node-opcua-address-space": "2.143.0",
"node-opcua-basic-types": "2.139.0",
"node-opcua-binary-stream": "2.139.0",
"node-opcua-buffer-utils": "2.139.0",
"node-opcua-client": "2.143.0",
"node-opcua-constants": "2.139.0",
"node-opcua-data-model": "2.139.0",
"node-opcua-data-value": "2.142.0",
"node-opcua-date-time": "2.139.0",
"node-opcua-debug": "2.139.0",
"node-opcua-extension-object": "2.139.0",
"node-opcua-factory": "2.139.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-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.139.0",
"node-opcua-numeric-range": "2.139.0",
"node-opcua-pseudo-session": "2.143.0",
"node-opcua-service-browse": "2.143.0",
"node-opcua-service-translate-browse-path": "2.143.0",
"node-opcua-status-code": "2.139.0",
"node-opcua-types": "2.143.0",
"node-opcua-variant": "2.142.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();
}
});
});
5 changes: 4 additions & 1 deletion packages/binding-opcua/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@
"extends": "../../tsconfig.json",
"compilerOptions": {
"outDir": "dist",
"rootDir": "src"
"rootDir": "src",
"skipLibCheck": true,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we run into issues if we do the checking?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would use skipLibCheck only as a last resort.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"skipLibCheck": true required only for binding-opcua

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok then let's leave it, @erossignon any plan for fixing this in the future for opc-ua? do you have a tracking issue?

"module": "Node16",
"moduleResolution": "Node16"
Comment on lines +7 to +8
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think those 2 statements are necessary since the root tsconfig.json defines them already like this.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes they could be moved, and modulResolution is also redundant when using Node16 as module. But we can keep both in the main tsconfig.json for documentation.

},
"include": ["./src/**/*"],
"references": [{ "path": "../td-tools" }, { "path": "../core" }]
Expand Down
2 changes: 1 addition & 1 deletion packages/examples/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"extends": "../../tsconfig.json",
"compilerOptions": {
"outDir": "dist",
"module": "commonjs",
"module": "Node16",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@relu91 you kept "commonjs" because of scrambled output, right? Do you mind changing it?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, this should be kept, any reason for changing it @erossignon ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Examples use node-opcua which now requires Node16

"rootDir": "src",
"target": "ES2021",
"sourceMap": false,
Expand Down
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"target": "ES2021",
"skipLibCheck": false,
"module": "Node16",
"moduleResolution": "Node16",
"outDir": "dist",
"strict": true,
"composite": true,
Expand Down