本文整理匯總了TypeScript中node-opcua-client.ClientSession類的典型用法代碼示例。如果您正苦於以下問題:TypeScript ClientSession類的具體用法?TypeScript ClientSession怎麽用?TypeScript ClientSession使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
在下文中一共展示了ClientSession類的5個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的TypeScript代碼示例。
示例1: test1
async function test1() {
try {
await client.connect("opc.tcp://opcuademo.sterfive.com:26543");
const session: ClientSession = await client.createSession({
type: UserTokenType.UserName,
password: "password1",
userName: "user1"
});
const subscription = await session.createSubscription2({
maxNotificationsPerPublish: 1000,
publishingEnabled: true,
requestedLifetimeCount: 100,
requestedMaxKeepAliveCount: 10,
requestedPublishingInterval: 1000
});
subscription.on("raw_notification", (n) => {
console.log(n.toString());
});
const parameters1: MonitoringParametersOptions = {
discardOldest: true,
queueSize: 100,
samplingInterval: 100,
filter: new DataChangeFilter({
deadbandType: DeadbandType.Absolute,
deadbandValue: 0.1,
trigger: DataChangeTrigger.StatusValueTimestamp
})
};
const itemToMonitor1: ReadValueIdLike = {
attributeId: AttributeIds.Value,
nodeId: "ns=1;s=FanSpeed"
};
const item1 = await subscription.monitor(itemToMonitor1, parameters1, TimestampsToReturn.Both);
console.log(" Item1 = ", item1.result!.statusCode.toString());
item1.on("changed", (dataValue: DataValue) => {
console.log(" Value1 has changed : ", dataValue.toString());
});
const itemToMonitor2: ReadValueIdLike = {
attributeId: AttributeIds.EventNotifier,
nodeId: "i=2258"
};
const parameters2: MonitoringParametersOptions = {
discardOldest: true,
filter: new EventFilter({}),
queueSize: 100,
samplingInterval: 0
};
const item2 = subscription.monitor(itemToMonitor2, parameters2, TimestampsToReturn.Both);
await timeout(20000);
await subscription.terminate();
await client.disconnect();
console.log(" Done!");
} catch (e) {
// Deal with the fact the chain failed
console.log(chalk.red("Error !"), e);
process.exit(-1);
}
}
示例2: describe
//.........這裏部分代碼省略.........
};
const server = new OPCUAServer({
port: 2010,
nodeset_filename: nodesets.standard,
userManager: mockUserManager,
serverCertificateManager: certificateManager,
userCertificateManager: certificateManager
});
await server.initialize();
verifyServer(server);
await installPushCertificateManagementOnServer(server);
debugLog("private key location = ", server.serverCertificateManager.privateKey);
verifyServer(server);
// Given that the server is configured to trust client certificate
const clientCertificatePEM = await promisify(fs.readFile)(clientCertificateFile, "utf8");
const clientCertificateDER = convertPEMtoDER(clientCertificatePEM);
await server.serverCertificateManager.trustCertificate(clientCertificateDER);
// also trusted newest certificate
return server;
}
let onGoingClient: OPCUAClient;
let onGoingSession: ClientSession;
let count = 0;
async function testWithSimpleClient(endpointUri: string) {
const client = OPCUAClient.create({
certificateFile: clientCertificateFile,
privateKeyFile: clientPrivateKeyFile,
securityMode: MessageSecurityMode.SignAndEncrypt,
securityPolicy: SecurityPolicy.Basic256
});
try {
await client.connect(endpointUri);
const session = await client.createSession();
await session.close();
} catch (err) {
errorLog("Cannot reconnect a client !!!! ", err.message, "\n", err);
} finally {
await client.disconnect();
}
}
async function startOnGoingConnection(endpointUri: string) {
onGoingClient = OPCUAClient.create({
certificateFile: clientCertificateFile,
privateKeyFile: clientPrivateKeyFile,
securityMode: MessageSecurityMode.SignAndEncrypt,
securityPolicy: SecurityPolicy.Basic256
});
onGoingClient.on("start_reconnection", () => {
示例3: callback
session.read(nodeToRead, (err: Error | null, dataValue?: DataValue) => {
// istanbul ignore next
if (err) {
setImmediate(() => {
callback(err);
});
return;
}
dataValue = dataValue!;
let dataType: DataType;
// istanbul ignore next
if (dataValue.statusCode !== StatusCodes.Good) {
dataType = DataType.Null;
setImmediate(() => {
callback(null, dataType);
});
return;
}
const dataTypeName = dataValue.value.value;
if (dataTypeId.namespace === 0 && DataType[dataTypeId.value as number]) {
dataType = (DataType as any)[dataTypeId.value as number] as DataType;
setImmediate(() => {
callback(null, dataType);
});
return;
}
/// example => Duration (i=290) => Double (i=11)
// read subTypeOf
const nodeToBrowse = {
browseDirection: BrowseDirection.Inverse,
includeSubtypes: false,
nodeId: dataTypeId,
// BrowseDescription
referenceTypeId: makeRefId("HasSubtype"),
// xx nodeClassMask: makeNodeClassMask("ObjectType"),
resultMask
};
// tslint:disable:no-shadowed-variable
session.browse(nodeToBrowse, (err: Error | null, browseResult?: BrowseResult) => {
// istanbul ignore next
if (err) {
return callback(err);
}
const references = browseResult!.references;
if (!references || references.length !== 1) {
return callback(new Error("cannot find SuperType of " + dataTypeName.toString()));
}
const nodeId = references[0].nodeId;
return convertNodeIdToDataTypeAsync(session, nodeId, callback);
});
});
示例4: stopOnGoingConnection
async function stopOnGoingConnection() {
debugLog("stopping");
await new Promise((resolve) => setTimeout(resolve, 5000));
debugLog("stopOnGoingConnection - Server certificate is now ",
makeSHA1Thumbprint(onGoingClient.serverCertificate!).toString("base64"));
await onGoingSession.close();
await onGoingClient.disconnect();
}
示例5: startOnGoingConnection
async function startOnGoingConnection(endpointUri: string) {
onGoingClient = OPCUAClient.create({
certificateFile: clientCertificateFile,
privateKeyFile: clientPrivateKeyFile,
securityMode: MessageSecurityMode.SignAndEncrypt,
securityPolicy: SecurityPolicy.Basic256
});
onGoingClient.on("start_reconnection", () => {
debugLog(chalk.bgWhite.red(" !!!!!!!!!!!!!!!!!!!!!!!! Starting Reconnection !!!!!!!!!!!!!!!!!!!"));
});
onGoingClient.on("backoff", (retry: number, delay: number) => {
debugLog(chalk.bgWhite.yellow("backoff attempt #"), retry, " retrying in ", delay / 1000.0, " seconds");
});
onGoingClient.on("connection_reestablished", () => {
debugLog(chalk.bgWhite.red(" !!!!!!!!!!!!!!!!!!!!!!!! CONNECTION RE-ESTABLISHED !!!!!!!!!!!!!!!!!!!"));
debugLog(" Server certificate is now ", makeSHA1Thumbprint(onGoingClient.serverCertificate!).toString("base64"));
});
onGoingClient.on("connection_lost", () => {
debugLog(chalk.bgWhite.red("Client has lost connection ..."));
debugLog(chalk.bgWhite.red(" Server certificate was ", makeSHA1Thumbprint(onGoingClient.serverCertificate!).toString("base64")));
});
onGoingClient.on("close", () => {
debugLog(chalk.bgWhite.red("client has CLOOOOOOOOOOSSSSSED"));
});
await onGoingClient.connect(endpointUri);
onGoingSession = await onGoingClient.createSession();
const subscription = await onGoingSession.createSubscription2({
requestedPublishingInterval: 500,
maxNotificationsPerPublish: 1000,
publishingEnabled: true,
requestedLifetimeCount: 100,
requestedMaxKeepAliveCount: 10
});
const monitoredItem = await subscription.monitor({
attributeId: AttributeIds.Value,
nodeId: "i=2258" // Current Time
},
{ samplingInterval: 500 }, TimestampsToReturn.Both);
count = 0;
monitoredItem.on("changed", (dataValue: DataValue) => {
debugLog(" ", chalk.cyan(dataValue.value.toString()));
count++;
});
await new Promise((resolve) => setTimeout(resolve, 1500));
}