Skip to content

Commit

Permalink
add findMethodId
Browse files Browse the repository at this point in the history
  • Loading branch information
erossignon committed Nov 30, 2023
1 parent 9eeb81a commit 8166185
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ import fs from "fs";
import should from "should";
import { nodesets } from "node-opcua-nodesets";
import { checkDebugFlag, make_debugLog } from "node-opcua-debug";
import { resolveNodeId } from "node-opcua-nodeid";

import { extractFields, simpleBrowsePathsToString} from "node-opcua-pseudo-session";

import { generateAddressSpace } from "../nodeJS";
import { AddressSpace, PseudoSession } from "..";
import { resolveNodeId } from "node-opcua-nodeid";

const debugLog = make_debugLog("TEST");
const doDebug = checkDebugFlag("TEST");
Expand Down
1 change: 1 addition & 0 deletions packages/node-opcua-alarm-condition/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"dependencies": {
"node-opcua-assert": "2.105.0",
"node-opcua-basic-types": "2.118.0",
"node-opcua-constants":"2.114.0",
"node-opcua-data-model": "2.118.0",
"node-opcua-debug": "2.118.0",
"node-opcua-nodeid": "2.118.0",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { DataValue, TimestampsToReturn } from "node-opcua-data-value";
import { MonitoringMode } from "node-opcua-types";
import { ReadValueIdOptions, MonitoringParametersOptions, CreateSubscriptionRequestOptions } from "node-opcua-types";
import { IBasicSessionAsync } from "./basic_session_interface";

Expand All @@ -13,7 +14,12 @@ export interface IBasicSubscription {
monitoringParameters: MonitoringParametersOptions,
timestampsToReturn: TimestampsToReturn
): IBasicMonitoredItem;

monitor(
itemToMonitor: ReadValueIdOptions,
requestedParameters: MonitoringParametersOptions,
timestampsToReturn: TimestampsToReturn,
monitoringMode?: MonitoringMode
): Promise<IBasicMonitoredItem>;
terminate(): Promise<void>;
}

Expand Down
92 changes: 92 additions & 0 deletions packages/node-opcua-pseudo-session/source/find_method_id.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
import { NodeId, NodeIdLike } from "node-opcua-nodeid";
import { BrowsePath, makeBrowsePath } from "node-opcua-service-translate-browse-path";
import { BrowseDirection, QualifiedNameLike, coerceQualifiedName } from "node-opcua-data-model";
import { IBasicSessionAsync } from "./basic_session_interface";

export async function findInTypeOrSuperType(
session: IBasicSessionAsync,
browsePath: BrowsePath
): Promise<{ nodeId: NodeId } | { nodeId: null; err: Error }> {
const nodeId = browsePath.startingNode;
const result = await session.translateBrowsePath(browsePath);
if (result.statusCode.isGood()) {
return { nodeId: result.targets![0].targetId as NodeId };
}
// cannot be found here, go one step up
const br = await session.browse({
nodeId,
referenceTypeId: "HasSubtype",
browseDirection: BrowseDirection.Inverse,
includeSubtypes: true,
nodeClassMask: 0,
resultMask: 0
});
if (br.statusCode.isNotGood()) {
// cannot find typeDefinition
return { nodeId: null, err: new Error("cannot find typeDefinition") };
}
const typeDefinition = br.references![0].nodeId;
browsePath = new BrowsePath({
startingNode: typeDefinition,
relativePath: browsePath.relativePath
});
return await findInTypeOrSuperType(session, browsePath);
}

/**
*
* find a MethodId in a object or in its super type
*
* note:
* - methodName is a browse name and may therefore be prefixed with a namespace index.
* - if method is not found on the object specified by nodeId, then the findMethodId will
* recursively browse up the hierarchy of object typeDefinition Node
* until it reaches the root type. and try to find the first method that matches the
* provided name.
*
* @param session
* @param nodeId the nodeId of the object to find
* @param methodName the method name to find prefixed with a namespace index (unless ns=0)
* ( e.g "Add" or "Add" or "1:BumpCounter" )
*/
export async function findMethodId(
session: IBasicSessionAsync,
nodeId: NodeIdLike,
methodName: QualifiedNameLike
): Promise<{ methodId: NodeId } | { methodId: null; err: Error }> {
const browsePath = makeBrowsePath(nodeId, "/" + coerceQualifiedName(methodName).toString());
const result = await session.translateBrowsePath(browsePath);
if (result.statusCode.isNotGood()) {
const br = await session.browse({
nodeId,
referenceTypeId: "HasTypeDefinition",
browseDirection: BrowseDirection.Forward,
includeSubtypes: true,
nodeClassMask: 0,
resultMask: 0
});
if (br.statusCode.isNotGood()) {
// cannot find typeDefinition
return { methodId: null, err: new Error("cannot find typeDefinition") };
}
const typeDefinition = br.references![0].nodeId;
// need to find method on objectType
const browsePath = makeBrowsePath(typeDefinition, "/" + methodName);
const result = await findInTypeOrSuperType(session, browsePath);
if (!result.nodeId) {
return { err: result.err, methodId: null };
}
return { methodId: result.nodeId };
}
result.targets = result.targets || [];

// istanbul ignore else
if (result.targets.length > 0) {
const methodId = result.targets[0].targetId as NodeId;
return { methodId };
} else {
// cannot find objectWithMethodNodeId
const err = new Error(" cannot find " + methodName + " Method");
return { methodId: null, err };
}
}
5 changes: 3 additions & 2 deletions packages/node-opcua-samples/bin/client_example_ts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import { types } from "util";
import yargs from "yargs/yargs";
import chalk from "chalk";
import Table from "easy-table";
import { asTree, TreeObject } from "treeify";

import {
ApplicationType,
Expand Down Expand Up @@ -43,10 +42,12 @@ import {
VariableIds,
Variant
} from "node-opcua";
import { Certificate, toPem } from "node-opcua-crypto";

import { Certificate, toPem } from "node-opcua-crypto";
import { NodeCrawler } from "node-opcua-client-crawler";

const { asTree, TreeObject } = require("treeify");

function w(str: string, l: number): string {
return str.padEnd(l).substring(0, l);
}
Expand Down
16 changes: 0 additions & 16 deletions pnpm-lock.yaml

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

0 comments on commit 8166185

Please sign in to comment.