Skip to content

Commit

Permalink
refactor: usertoken in constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
AndreasHeine committed Sep 16, 2024
1 parent a753599 commit 74eca47
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 10 deletions.
7 changes: 4 additions & 3 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { UserTokenType } from "node-opcua";
import { OpcUaDeviceProxyClass } from "./ua-device";

const UaDevice = new OpcUaDeviceProxyClass("opc.tcp://opcua.umati.app:4843")
// const UaDevice = new OpcUaDeviceProxyClass("opc.tcp://opcua.umati.app:4840")
// const UaDevice = new OpcUaDeviceProxyClass("opc.tcp://127.0.0.1:4840")
// const UaDevice = new OpcUaDeviceProxyClass("opc.tcp://opcua.umati.app:4843", { type: UserTokenType.Anonymous })
// const UaDevice = new OpcUaDeviceProxyClass("opc.tcp://opcua.umati.app:4840", { type: UserTokenType.Anonymous })
const UaDevice = new OpcUaDeviceProxyClass("opc.tcp://127.0.0.1:4840", { type: UserTokenType.Anonymous })

;(async () => {
function shutdown() {
Expand Down
12 changes: 5 additions & 7 deletions src/ua-device.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import {
ClientSession,
CreateSubscriptionRequestOptions,
UserIdentityInfo,
UserTokenType,
DataValue,
StatusCode,
AttributeIds,
Expand Down Expand Up @@ -127,17 +126,15 @@ const createSubscriptionRequest: CreateSubscriptionRequestOptions = {
// priority: 1,
}

let userIdentityInfo: UserIdentityInfo = {
type: UserTokenType.Anonymous
}

export class OpcUaDeviceProxyClass {

readonly endpoint: string
readonly client: OPCUAClient
readonly userIdentityInfo: UserIdentityInfo

private session: ClientSession | undefined
private subscription: ClientSubscription | undefined

private monitoredItemValueMap: Map<string, ClientMonitoredItem> = new Map()
private namespaceArray: string[] = []
private serverProfileArray: string[] = []
Expand All @@ -158,8 +155,9 @@ export class OpcUaDeviceProxyClass {
private findMachinesOnServerInterval: NodeJS.Timeout | undefined
private updateSummeryInterval: NodeJS.Timeout | undefined

constructor (endpoint: string) {
constructor (endpoint: string, userIdentityInfo: UserIdentityInfo) {
this.endpoint = endpoint
this.userIdentityInfo = userIdentityInfo
this.client = OPCUAClient.create(optionsInitial)
this.client.on("backoff", (retry: number, delay: number) => {
console.warn(`OPC UA Client: unable to connect to the OPC UA Device @ '${endpoint}' - attempt '${retry}' retrying in '${delay / 1000.0}' seconds`)
Expand Down Expand Up @@ -316,7 +314,7 @@ export class OpcUaDeviceProxyClass {

async initialize() {
await this.client.connect(this.endpoint)
await this.createSession(userIdentityInfo)
await this.createSession(this.userIdentityInfo)
await this.readServerState()
if (this.serverState > 0) {
console.error(`OPC UA Client: OPC UA Device @ '${this.endpoint}' has invalid ServerState '${this.serverState}'`)
Expand Down

0 comments on commit 74eca47

Please sign in to comment.