Exemplo n.º 1
0
        /// <summary>
        /// Creates a new controller file.
        /// </summary>
        private ServiceResult OnCreateController(
            ISystemContext context,
            MethodState method,
            NodeId objectId,
            string name,
            ref NodeId nodeId)
        {
            nodeId = null;

            // get the system to use.
            FileSystemMonitor system = context.SystemHandle as FileSystemMonitor;

            if (system == null)
            {
                return(StatusCodes.BadOutOfService);
            }

            // get the current dierctory.
            DirectoryInfo directory = GetDirectory(context, this.NodeId);

            if (directory == null || !directory.Exists)
            {
                return(StatusCodes.BadOutOfService);
            }

            // build the file path.
            StringBuilder filePath = new StringBuilder();

            filePath.Append(directory.FullName);
            filePath.Append('\\');
            filePath.Append(name);
            filePath.Append(".csv");

            if (File.Exists(filePath.ToString()))
            {
                return(StatusCodes.BadNodeIdExists);
            }

            // write a dummy configuration file.
            try
            {
                using (StreamWriter writer = new StreamWriter(filePath.ToString()))
                {
                    writer.WriteLine("Temperature, Double, 15");
                    writer.WriteLine("TemperatureSetPoint, Double, 15");
                }
            }
            catch (Exception e)
            {
                return(new ServiceResult(e, StatusCodes.BadUnexpectedError));
            }

            // return the node id.
            nodeId = system.CreateNodeIdFromFilePath(ObjectTypes.ControllerType, filePath.ToString());

            return(ServiceResult.Good);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Initializes a controller from a file.
        /// </summary>
        public ControllerState(ISystemContext context, FileInfo fileInfo) : base(null)
        {
            fileInfo.Refresh();

            // extract the display name from the original file path.
            string name = fileInfo.Name;

            // need to read the correct casing from the file system.
            if (fileInfo.Exists)
            {
                FileInfo[] files = fileInfo.Directory.GetFiles(name);

                if (files != null && files.Length > 0)
                {
                    name = files[0].Name;
                }
            }

            int index = name.LastIndexOf('.');

            if (index >= 0)
            {
                name = name.Substring(0, index);
            }

            // get the system to use.
            FileSystemMonitor system = context.SystemHandle as FileSystemMonitor;

            if (system != null)
            {
                this.NodeId     = system.CreateNodeIdFromFilePath(ObjectTypes.ControllerType, fileInfo.FullName);
                this.BrowseName = new QualifiedName(name, system.NamespaceIndex);
                this.OnValidate = system.ValidateController;
            }

            this.DisplayName      = new LocalizedText(name);
            this.EventNotifier    = EventNotifiers.None;
            this.TypeDefinitionId = GetDefaultTypeDefinitionId(context.NamespaceUris);
        }