/** * @module node-opcua-server * * What serves the compact namespaces of a server: the compact address space of this thread, or, * in a front thread, the shared columns of the engine thread's store plus messages to the engine. * The accessor routes every NodeId of these namespaces here; the rest goes to the node objects. */ import type { CompactAddressSpace, ContinuationData, ISessionContext } from "node-opcua-address-space"; import { CompactAddressSpaceServices, historyReadThrough } from "node-opcua-address-space"; import type { DataValue, TimestampsToReturn } from "node-opcua-data-value"; import { getCurrentClock } from "node-opcua-date-time"; import { type NodeId, type NodeIdLike, resolveNodeId } from "node-opcua-nodeid"; import { type HistoryReadDetails, HistoryReadResult, type HistoryReadValueId } from "node-opcua-service-history"; import { coerceStatusCode, type StatusCode, StatusCodes } from "node-opcua-status-code"; import type { BrowseDescription, BrowsePath, BrowsePathResult, BrowseResult, CallMethodRequest, CallMethodResultOptions, ReadValueIdOptions, ReferenceDescription, WriteValue } from "node-opcua-types"; import type { FoundNode } from "./monitorable_node.js"; export interface ICompactBackend { /** the namespace indexes served here */ readonly namespaces: ReadonlySet; /** one item of a Read, synchronously */ read( context: ISessionContext | null, nodeToRead: ReadValueIdOptions, maxAge: number, timestampsToReturn?: TimestampsToReturn ): DataValue; /** the items of a Write that fall in these namespaces, in their order */ write(context: ISessionContext | null, nodesToWrite: WriteValue[]): Promise; browse(context: ISessionContext | null, description: BrowseDescription): Promise; /** the references these namespaces add to a node of another namespace (a folder of the base namespace) */ references(context: ISessionContext | null, nodeId: NodeId, description: BrowseDescription): Promise; /** a path followed in these namespaces from its start; null when it leads nowhere here */ translate(browsePath: BrowsePath, context?: ISessionContext | null): Promise; /** a call of a Method of these namespaces */ call?(context: ISessionContext | null, request: CallMethodRequest): Promise; /** a HistoryRead of a Variable of these namespaces */ historyRead?( context: ISessionContext, nodeToRead: HistoryReadValueId, historyReadDetails: HistoryReadDetails, continuationData: ContinuationData ): Promise; /** the node a monitored item watches; undefined where monitored items are not served yet */ findNode?(nodeId: NodeIdLike): FoundNode | null; } /** the compact address space of this thread */ export class LocalCompactBackend implements ICompactBackend { readonly #space: CompactAddressSpace; readonly #services: CompactAddressSpaceServices; public readonly namespaces: ReadonlySet; constructor(space: CompactAddressSpace, namespaces: ReadonlySet) { this.#space = space; this.namespaces = namespaces; this.#services = new CompactAddressSpaceServices(space); } public read( context: ISessionContext | null, nodeToRead: ReadValueIdOptions, maxAge: number, timestampsToReturn?: TimestampsToReturn ): DataValue { return this.#services.read(context, nodeToRead, maxAge, timestampsToReturn); } public async write(context: ISessionContext | null, nodesToWrite: WriteValue[]): Promise { // one clock for the whole Write (the accessor has read it into the context already) const now = (context?.currentTime ?? getCurrentClock()).timestamp.getTime(); return nodesToWrite.map((writeValue) => coerceStatusCode(this.#services.write(context, writeValue, now))); } public async browse(context: ISessionContext | null, description: BrowseDescription): Promise { return this.#services.browse(context, description); } public async references( context: ISessionContext | null, nodeId: NodeId, description: BrowseDescription ): Promise { const node = this.#space.store.find(nodeId); if (node < 0) { return []; } const referenceType = description.referenceTypeId && description.referenceTypeId.value !== 0 ? description.referenceTypeId : undefined; return this.#services.references(context, node, description, referenceType, this.namespaces); } public async translate(browsePath: BrowsePath, context?: ISessionContext | null): Promise { const result = this.#services.translate(browsePath, context ?? null); return result.statusCode.isGood() ? result : null; } public call(context: ISessionContext | null, request: CallMethodRequest): Promise { return this.#services.call(context, request); } public async historyRead( context: ISessionContext, nodeToRead: HistoryReadValueId, historyReadDetails: HistoryReadDetails, continuationData: ContinuationData ): Promise { const space = this.#space; const index = space.store.find(resolveNodeId(nodeToRead.nodeId)); if (index < 0 || space.store.nodes.isDeleted(index)) { return new HistoryReadResult({ statusCode: StatusCodes.BadNodeIdUnknown }); } const historian = space.historians.get(index); if (!historian) { // not historized: what a node object without history answers return new HistoryReadResult({ statusCode: StatusCodes.BadNotReadable }); } const view = space.viewOf(index); return historyReadThrough( { nodeId: view.nodeId, browseName: view.browseName, varHistorian: historian, canUserReadHistory: (c) => space.permissions.historyReadStatus(c, index) === StatusCodes.Good.value }, context, historyReadDetails as Parameters[2], nodeToRead.indexRange ?? null, nodeToRead.dataEncoding ?? null, continuationData ); } public findNode(nodeId: NodeIdLike): FoundNode | null { return this.#space.findNode(resolveNodeId(nodeId)) as unknown as FoundNode | null; } }