BindModel and FileModel

Questions regarding the use of the .NET SDK 2.0 for Server or Client development or integration into customer products ...

Moderator: uasdknet

Post Reply
SirTobi
Full Member
Full Member
Posts: 7
Joined: 20 Dec 2023, 14:05

BindModel and FileModel

Post by SirTobi »

Hello,

I want to provide a file via OPC-UA from my file system. With the UaModeler I have created a custom object which has a component FileType to provide the file.
In the code I'm using the BindModel on the custom object and set the FileModel like this:

Code: Select all


	var fileModel = new FileModel
            {
                FileOnDisk = new FileInfo(file),
                Writable = false,
                UserWritable = false
            };

            if (File.Exists(file))
            {
                fileModel.Size = (ulong)fileModel.FileOnDisk.Length;
            }
File = fileModel;

From time to time the file in the file system will be changed and the OPC-UA node must be updated with the correct size. Currently I am creating a new file model everytime the file changes, but the size property in the OPC-UA node is not updated.
I also tried to call File.FileOnDisk.Refresh(), but it was not updated.

However after I used UaExpert to save to local file, then the size property is updated and shown correctly in UaExpert.

What is the correct way to update the file?

User avatar
Support Team
Hero Member
Hero Member
Posts: 3072
Joined: 18 Mar 2011, 15:09

Re: BindModel and FileModel

Post by Support Team »

Hi,

please, do not try to exchange child UA objects, i.e. model child instances, when using BindModel. That does not work. What you want to do is changing the properties/variables of the object. The FileModel implementation offers the FileOnDisk option for persistence and large files that do not need to be kept in memory. It was not intended to be used from outside via file access. Nonetheless, this can work. The only thing you have to setup is a monitor watching the file. So the model gets informed when the file was changed.

Without having tested it, the following should work:

Code: Select all

            var monitor = new FileSystemWatcher(fileModel.FileOnDisk.FullName);
            monitor.Changed += (s, e) =>
            {
                if (e.FullPath == fileModel.FileOnDisk.FullName)
                {
                    synchronizationContext.Post(_ =>
                    {
                        fileModel.FileOnDisk.Refresh();
                        fileModel.Size = (ulong)fileModel.FileOnDisk.Length;
                        fileModel.LastModifiedTime = fileModel.FileOnDisk.LastWriteTimeUtc;
                    }, null);
                }
            };
            monitor.NotifyFilter = NotifyFilters.LastWrite | NotifyFilters.Size;
            monitor.EnableRaisingEvents = true;
This will setup a FileSystemWatcher. When ever something was been written to the file or the file size was changed, the model properties for the size and the LastModifiedTime will be updated.

Note:
  • The FileSystemWatcher is disposable. So you have to clean it up, once you no longer use it.
  • This example does not handle the case when the file was removed entierly from the file system.
Best regards
Unified Automation Support Team

SirTobi
Full Member
Full Member
Posts: 7
Joined: 20 Dec 2023, 14:05

Re: BindModel and FileModel

Post by SirTobi »

Hello,

thank you for your reply. I updated the properties in my fileModel and now they are displayed correctly in the OPC-UA server.
Thank you very much!

Post Reply