this package provides an implementation of the OPCUA File Transfer specification as per OPCUA 1.0.4 part 5 Annex C (page 99)
$ npm install node-opcua
$ npm install node-opcua-file-transfer
import { OPCUAServer, UAFile } from "node-opcua";
import { installFileType } from "node-opcua-file-transfer";
const server = new OPCUAServer({
});
await server.initialize();
// let say we want to create a access to this file:
const my_data_filename = "/data/someFile.txt";
await promisify(fs.writeFile)(my_data_filename, "some content", "utf8");
// now add a file object in the address Space
const addressSpace = server.engine.addressSpace;
// retrieve the FileType UAObjectType
const fileType = addressSpace.findObjectType("FileType")!;
// create a instance of FileType
const myFile = fileType.instantiate({
nodeId: "s=MyFile",
browseName: "MyFile",
organizedBy: addressSpace.rootFolder.objects
}) as UAFile;
// now bind the opcuaFile object with our file
installFileType(opcuaFile, {
filename: my_data_filename
});
We assume that we have a valid OPCUA Client Session
import { OPCUAClient } from "node-opcua";
import { ClientFile } from "node-opcua-file-transfer";
const client = OPCUAClient.create({});
await client.connect();
const session = await client.createSession();
Let's assume that the nodeId of the file object is "ns=1;s=MyFile"
import {ClientFile,OpenFileMode } from "node-opcua-file-transfer";
//
const fileNodeId = "ns=1;s=MyFile";
// let's create a client file object from the session and nodeId
const clientFile = new ClientFile(session, fileNodeId);
// let's open the file
const mode = OpenFileMode.ReadWriteAppend;
await clientFile.open(mode);
// ... do some reading or writing
// don't forget to close the file when done
await clientFile.close();
You can read the size of the file at any time.
const clientFile = new ClientFile(session, fileNodeId);
const size = await clientFile.size();
console.log("the current file size is : ",size," bytes);
const mode = OpenFileMode.ReadWriteAppend;
await clientFile.open(mode);
| Mode | Description |
|---|---|
| Read | The file is opened for reading. The Write method cannot be executed. |
| Write | The file is opened for writing. The Read cannot be executed. |
| ReadWrite | The file is opened for reading and writing |
| WriteEraseExisting | File is open for writing only. The existing content of the file is erased and an empty file is provided. |
| WriteAppend | File is open for writing only and the position is set at the end of the file |
| ReadWriteAppend | File is opened for reading and writing and the position is set at the end of the file |
| } |
const mode = OpenFileMode.ReadWriteAppend;
await clientFile.open(mode);
// ... do some reading or writing
// don't forget to close the file when done
await clientFile.close();
Once the file is opened, the position for reading or writing can be set using setPosition.
const clientFile = new ClientFile(session, fileNodeId);
const mode = OpenFileMode.ReadWriteAppend;
await clientFile.open(mode);
await client.setPosition([0,32]);
Once the file is opened, the position for reading or writing can be retrieved using getPosition.
const clientFile = new ClientFile(session, fileNodeId);
const mode = OpenFileMode.ReadWriteAppend;
await clientFile.open(mode);
await client.setPosition(32);
Data can be writing to the file at the current cursor position. Data must be passed in a buffer.
const dataToWrite = Buffer.from("Some data");
const mode = OpenFileMode.ReadWriteAppend;
await clientFile.open(mode);
await clientFile.write(dataToWrite);
Data can be written to the file at the current cursor position. Data must be passed in a buffer.
const mode = OpenFileMode.ReadWriteAppend;
await clientFile.open(mode);
// read 200 bytes from position 32
await clientFile.setPosition([0,32]);
const data: Buffer = await clientFile.read(200);
UInt64
| FileType | :white_check_mark: | |
| FileDirectoryType | :new_moon: | (sponsor wanted) |
| FileTransferStateMachineType | :new_moon: | (sponsor wanted) |
| TemporaryFileTransferType | :new_moon: | (sponsor wanted) |
| AddressSpaceFileType | :new_moon: | (sponsor wanted) |
We appreciate that, once you have evaluated the software you consider supporting our effort by applying to our sponsor program:
Grants ensure the following:
Technical Support is exclusively provided to registered members of the NodeOPCUA Subscription (https://support.sterfive.com) or through dedicated Professional Services.
Node-OPCUA is made available to you under the MIT open source license.
See LICENSE for details.
Contact Sterfive SAS (mailto:contact@sterfive.com) for additional companion modules that are available to registered members:
Copyright (c) 2022-2024 Sterfive SAS - https://www.sterfive.com
Copyright (c) 2014-2022 Etienne Rossignon