/
client_file.ts
371 lines (336 loc) · 14.1 KB
/
client_file.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
/**
* @module node-opcua-file-transfer
*/
import { Byte, coerceInt32, Int32, Int64, UInt16, UInt32, UInt64 } from "node-opcua-basic-types";
import { AttributeIds } from "node-opcua-data-model";
import { NodeId, resolveNodeId } from "node-opcua-nodeid";
import { IBasicSession } from "node-opcua-pseudo-session";
import { ReadValueIdOptions } from "node-opcua-service-read";
import { BrowsePath, makeBrowsePath } from "node-opcua-service-translate-browse-path";
import { StatusCodes } from "node-opcua-status-code";
import { DataType, VariantArrayType } from "node-opcua-variant";
import { MethodIds } from "node-opcua-constants";
import { checkDebugFlag, make_debugLog, make_errorLog } from "node-opcua-debug";
const debugLog = make_debugLog("FileType");
const errorLog = make_errorLog("FileType");
const doDebug = checkDebugFlag("FileType");
import { OpenFileMode } from "../open_mode";
export { OpenFileMode } from "../open_mode";
export interface IClientFile {
fileHandle: number;
open(mode: OpenFileMode): Promise<number>;
close(): Promise<void>;
getPosition(): Promise<UInt64>;
setPosition(position: UInt64 | UInt32): Promise<void>;
read(bytesToRead: UInt32 | Int32 | Int64 | UInt64): Promise<Buffer>;
write(data: Buffer): Promise<void>;
openCount(): Promise<UInt16>;
size(): Promise<UInt64>;
}
export interface IClientFilePriv extends IClientFile {
readonly fileNodeId: NodeId;
openMethodNodeId?: NodeId;
closeMethodNodeId?: NodeId;
setPositionNodeId?: NodeId;
getPositionNodeId?: NodeId;
readNodeId?: NodeId;
writeNodeId?: NodeId;
openCountNodeId?: NodeId;
sizeNodeId?: NodeId;
ensureInitialized(): Promise<void>;
}
/**
*
*
*/
export class ClientFile implements IClientFile {
public static useGlobalMethod = false;
public fileHandle = 0;
protected session: IBasicSession;
protected readonly fileNodeId: NodeId;
private openMethodNodeId?: NodeId;
private closeMethodNodeId?: NodeId;
private setPositionNodeId?: NodeId;
private getPositionNodeId?: NodeId;
private readNodeId?: NodeId;
private writeNodeId?: NodeId;
private openCountNodeId?: NodeId;
private sizeNodeId?: NodeId;
constructor(session: IBasicSession, nodeId: NodeId) {
this.session = session;
this.fileNodeId = nodeId;
}
public async open(mode: OpenFileMode): Promise<number> {
if (mode === null || mode === undefined) {
throw new Error("expecting a validMode " + OpenFileMode[mode]);
}
if (this.fileHandle) {
throw new Error("File has already be opened");
}
await this.ensureInitialized();
const result = await this.session.call({
inputArguments: [{ dataType: DataType.Byte, value: mode as Byte }],
methodId: this.openMethodNodeId,
objectId: this.fileNodeId
});
if (result.statusCode.isNotGood()) {
debugLog("Cannot open file : ");
throw new Error("cannot open file statusCode = " + result.statusCode.toString() + " mode = " + OpenFileMode[mode]);
}
this.fileHandle = result.outputArguments![0].value;
return this.fileHandle;
}
public async close(): Promise<void> {
if (!this.fileHandle) {
throw new Error("File has not been opened yet");
}
await this.ensureInitialized();
const result = await this.session.call({
inputArguments: [{ dataType: DataType.UInt32, value: this.fileHandle }],
methodId: this.closeMethodNodeId,
objectId: this.fileNodeId
});
if (result.statusCode.isNotGood()) {
debugLog("Cannot close file : ");
throw new Error("cannot close file statusCode = " + result.statusCode.toString());
}
this.fileHandle = 0;
}
public async getPosition(): Promise<UInt64> {
await this.ensureInitialized();
if (!this.fileHandle) {
throw new Error("File has not been opened yet");
}
const result = await this.session.call({
inputArguments: [{ dataType: DataType.UInt32, value: this.fileHandle }],
methodId: this.getPositionNodeId,
objectId: this.fileNodeId
});
if (result.statusCode.isNotGood()) {
throw new Error("Error " + result.statusCode.toString());
}
return result.outputArguments![0].value as UInt64;
}
public async setPosition(position: UInt64 | UInt32): Promise<void> {
await this.ensureInitialized();
if (!this.fileHandle) {
throw new Error("File has not been opened yet");
}
if (typeof position === "number") {
position = [0, position];
}
const result = await this.session.call({
inputArguments: [
{ dataType: DataType.UInt32, value: this.fileHandle },
{
arrayType: VariantArrayType.Scalar,
dataType: DataType.UInt64,
value: position
}
],
methodId: this.setPositionNodeId,
objectId: this.fileNodeId
});
if (result.statusCode.isNotGood()) {
throw new Error("Error " + result.statusCode.toString());
}
return;
}
public async read(bytesToRead: UInt32 | Int32 | Int64 | UInt64): Promise<Buffer> {
await this.ensureInitialized();
if (!this.fileHandle) {
throw new Error("File has not been opened yet");
}
const result = await this.session.call({
inputArguments: [
{ dataType: DataType.UInt32, value: this.fileHandle },
{
arrayType: VariantArrayType.Scalar,
dataType: DataType.Int32,
value: coerceInt32(bytesToRead)
}
],
methodId: this.readNodeId,
objectId: this.fileNodeId
});
if (result.statusCode.isNotGood()) {
throw new Error("Error " + result.statusCode.toString());
}
if (!result.outputArguments || result.outputArguments[0].dataType !== DataType.ByteString) {
throw new Error("Error invalid output");
}
return result.outputArguments![0].value as Buffer;
}
public async write(data: Buffer): Promise<void> {
await this.ensureInitialized();
if (!this.fileHandle) {
throw new Error("File has not been opened yet");
}
const result = await this.session.call({
inputArguments: [
{ dataType: DataType.UInt32, value: this.fileHandle },
{
arrayType: VariantArrayType.Scalar,
dataType: DataType.ByteString,
value: data
}
],
methodId: this.writeNodeId,
objectId: this.fileNodeId
});
if (result.statusCode.isNotGood()) {
throw new Error("Error " + result.statusCode.toString());
}
return;
}
public async openCount(): Promise<UInt16> {
await this.ensureInitialized();
const nodeToRead: ReadValueIdOptions = { nodeId: this.openCountNodeId!, attributeId: AttributeIds.Value };
const dataValue = await this.session.read(nodeToRead);
if (doDebug) {
debugLog(" OpenCount ", nodeToRead.nodeId!.toString(), dataValue.toString());
}
return dataValue.value.value;
}
public async size(): Promise<UInt64> {
await this.ensureInitialized();
const nodeToRead = { nodeId: this.sizeNodeId, attributeId: AttributeIds.Value };
const dataValue = await this.session.read(nodeToRead);
return dataValue.value.value;
}
// eslint-disable-next-line max-statements
protected async extractMethodsIds(): Promise<void> {
if (ClientFile.useGlobalMethod) {
debugLog("Using GlobalMethodId");
this.openMethodNodeId = resolveNodeId(MethodIds.FileType_Open);
this.closeMethodNodeId = resolveNodeId(MethodIds.FileType_Close);
this.setPositionNodeId = resolveNodeId(MethodIds.FileType_SetPosition);
this.getPositionNodeId = resolveNodeId(MethodIds.FileType_GetPosition);
this.writeNodeId = resolveNodeId(MethodIds.FileType_Write);
this.readNodeId = resolveNodeId(MethodIds.FileType_Read);
const browsePaths: BrowsePath[] = [
makeBrowsePath(this.fileNodeId, "/OpenCount"),
makeBrowsePath(this.fileNodeId, "/Size")
];
const results = await this.session.translateBrowsePath(browsePaths);
if (results[0].statusCode.isNotGood()) {
throw new Error("fileType object does not expose mandatory OpenCount Property");
}
if (results[1].statusCode.isNotGood()) {
throw new Error("fileType object does not expose mandatory Size Property");
}
this.openCountNodeId = results[0].targets![0].targetId;
this.sizeNodeId = results[1].targets![0].targetId;
return;
}
const browsePaths: BrowsePath[] = [
makeBrowsePath(this.fileNodeId, "/Open"),
makeBrowsePath(this.fileNodeId, "/Close"),
makeBrowsePath(this.fileNodeId, "/SetPosition"),
makeBrowsePath(this.fileNodeId, "/GetPosition"),
makeBrowsePath(this.fileNodeId, "/Write"),
makeBrowsePath(this.fileNodeId, "/Read"),
makeBrowsePath(this.fileNodeId, "/OpenCount"),
makeBrowsePath(this.fileNodeId, "/Size")
];
const results = await this.session.translateBrowsePath(browsePaths);
if (results[0].statusCode.isNotGood()) {
throw new Error("fileType object does not expose mandatory Open Method");
}
if (results[1].statusCode.isNotGood()) {
throw new Error("fileType object does not expose mandatory Close Method");
}
if (results[2].statusCode.isNotGood()) {
throw new Error("fileType object does not expose mandatory SetPosition Method");
}
if (results[3].statusCode.isNotGood()) {
throw new Error("fileType object does not expose mandatory GetPosition Method");
}
if (results[4].statusCode.isNotGood()) {
throw new Error("fileType object does not expose mandatory Write Method");
}
if (results[5].statusCode.isNotGood()) {
throw new Error("fileType object does not expose mandatory Read Method");
}
if (results[6].statusCode.isNotGood()) {
throw new Error("fileType object does not expose mandatory OpenCount Variable");
}
if (results[7].statusCode.isNotGood()) {
throw new Error("fileType object does not expose mandatory Size Variable");
}
if (false && doDebug) {
results.map((x: any) => debugLog(x.toString()));
}
this.openMethodNodeId = results[0].targets![0].targetId;
this.closeMethodNodeId = results[1].targets![0].targetId;
this.setPositionNodeId = results[2].targets![0].targetId;
this.getPositionNodeId = results[3].targets![0].targetId;
this.writeNodeId = results[4].targets![0].targetId;
this.readNodeId = results[5].targets![0].targetId;
this.openCountNodeId = results[6].targets![0].targetId;
this.sizeNodeId = results[7].targets![0].targetId;
}
protected async ensureInitialized(): Promise<void> {
if (!this.openMethodNodeId) {
await this.extractMethodsIds();
}
}
}
export async function readFile(clientFile: IClientFile): Promise<Buffer> {
await clientFile.open(OpenFileMode.Read);
try {
const fileSize = coerceInt32(await clientFile.size());
/**
* Read file
*/
const data = await clientFile.read(fileSize);
if (data.length >= fileSize) {
// everything has been read
return data;
}
// wee need to loop to complete the read
const chunks = [data];
let remaining = fileSize - data.length;
while (remaining > 0) {
const buf = await clientFile.read(remaining);
chunks.push(buf);
remaining -= buf.length;
}
return Buffer.concat(chunks);
} finally {
await clientFile.close();
}
}
export async function readOPCUAFile(clientFile: IClientFile): Promise<Buffer> {
return await readFile(clientFile);
}
/**
* 5.2.10 UserRolePermissions
*
* The optional UserRolePermissions Attribute specifies the Permissions that apply to a Node for
* all Roles granted to current Session. The value of the Attribute is an array of
* RolePermissionType Structures (see Table 8).
* Clients may determine their effective Permissions by logically ORing the Permissions for each
* Role in the array.
* The value of this Attribute is derived from the rules used by the Server to map Sessions to
* Roles. This mapping may be vendor specific or it may use the standard Role model defined in 4.8.
* This Attribute shall not be writeable.
* If not specified, the value of DefaultUserRolePermissions Property from the Namespace
* Metadata Object associated with the Node is used instead. If the NamespaceMetadata Object
* does not define the Property or does not exist, then the Server does not publish any information
* about Roles mapped to the current Session.
*
*
* 5.2.11 AccessRestrictions
* The optional AccessRestrictions Attribute specifies the AccessRestrictions that apply to a Node.
* Its data type is defined in 8.56. If a Server supports AccessRestrictions for a particular
* Namespace it adds the DefaultAccessRestrictions Property to the NamespaceMetadata Object
* for that Namespace (see Figure 8). If a particular Node in the Namespace needs to override
* the default value the Server adds the AccessRestrictions Attribute to the Node.
* If a Server implements a vendor specific access restriction model for a Namespace, it does not
* add the DefaultAccessRestrictions Property to the NamespaceMetadata Object.
*
*
* DefaultAccessRestrictions
*
*/