Ejemplo n.º 1
0
        /// <summary>
        /// Loads the configuration from the specified file.
        /// </summary>
        public bool Load(string fileName, out string errMsg)
        {
            try
            {
                SetToDefault();

                if (!File.Exists(fileName))
                {
                    throw new FileNotFoundException(string.Format(CommonPhrases.NamedFileNotFound, fileName));
                }

                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.Load(fileName);
                Culture = xmlDoc.DocumentElement.GetChildAsString("Culture");

                errMsg = "";
                return(true);
            }
            catch (Exception ex)
            {
                errMsg = ScadaUtils.BuildErrorMessage(ex, Locale.IsRussian ?
                                                      "Ошибка при загрузке конфигурации экземпляра" :
                                                      "Error loading instance configuration");
                return(false);
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Saves all modified tables of the configuration database.
        /// </summary>
        public bool Save(out string errMsg)
        {
            try
            {
                Directory.CreateDirectory(BaseDir);

                foreach (IBaseTable baseTable in AllTables)
                {
                    if (baseTable.Modified)
                    {
                        try
                        {
                            string fileName = Path.Combine(BaseDir, baseTable.FileName);
                            baseTable.Save(fileName);
                        }
                        catch (Exception ex)
                        {
                            throw new ScadaException(string.Format(
                                                         AdminPhrases.SaveBaseTableError, baseTable.Title), ex);
                        }
                    }
                }

                errMsg = "";
                return(true);
            }
            catch (Exception ex)
            {
                errMsg = ScadaUtils.BuildErrorMessage(ex, AdminPhrases.SaveConfigBaseError);
                return(false);
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Saves the state to the specified file.
        /// </summary>
        public bool Save(string fileName, out string errMsg)
        {
            try
            {
                XmlDocument    xmlDoc  = new();
                XmlDeclaration xmlDecl = xmlDoc.CreateXmlDeclaration("1.0", "utf-8", null);
                xmlDoc.AppendChild(xmlDecl);

                XmlElement rootElem = xmlDoc.CreateElement("ScadaAdminState");
                xmlDoc.AppendChild(rootElem);

                MainFormState.SaveToXml(rootElem.AppendElem("MainFormState"));
                rootElem.AppendElem("ProjectDir", ProjectDir);

                XmlElement recentProjectsElem = rootElem.AppendElem("RecentProjects");
                foreach (string path in RecentProjects)
                {
                    recentProjectsElem.AppendElem("Path", path);
                }

                xmlDoc.Save(fileName);
                errMsg = "";
                return(true);
            }
            catch (Exception ex)
            {
                errMsg = ScadaUtils.BuildErrorMessage(ex, AppPhrases.SaveAppStateError);
                return(false);
            }
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Renames the instance.
        /// </summary>
        public bool Rename(string newName, out string errMsg)
        {
            try
            {
                if (string.IsNullOrEmpty(newName))
                {
                    throw new ArgumentException(AdminPhrases.InstanceNameEmpty, nameof(newName));
                }

                if (!AdminUtils.NameIsValid(newName))
                {
                    throw new ArgumentException(AdminPhrases.InstanceNameInvalid, nameof(newName));
                }

                DirectoryInfo directoryInfo  = new(InstanceDir);
                string        newInstanceDir = Path.Combine(directoryInfo.Parent.FullName, newName);
                directoryInfo.MoveTo(newInstanceDir);
                InstanceDir = newInstanceDir;
                Name        = newName;

                errMsg = "";
                return(true);
            }
            catch (Exception ex)
            {
                errMsg = ScadaUtils.BuildErrorMessage(ex, AdminPhrases.RenameInstanceError);
                return(false);
            }
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Writes the error to the log and displays a error message.
        /// </summary>
        public void ProcError(Exception ex, string text = "", params object[] args)
        {
            string msg = ScadaUtils.BuildErrorMessage(ex, text, args);

            ErrLog.WriteMessage(msg, LogMessageType.Exception);
            ScadaUiUtils.ShowError(msg);
        }
Ejemplo n.º 6
0
 /// <summary>
 /// Creates configuration files required for the application.
 /// </summary>
 public virtual bool CreateConfigFiles(out string errMsg)
 {
     try
     {
         Directory.CreateDirectory(GetConfigDir());
         return(SaveConfig(out errMsg));
     }
     catch (Exception ex)
     {
         errMsg = ScadaUtils.BuildErrorMessage(ex, AdminPhrases.CreateAppConfigError, AppName);
         return(false);
     }
 }
Ejemplo n.º 7
0
 /// <summary>
 /// Saves the project to the specified file.
 /// </summary>
 public bool Save(string fileName, out string errMsg)
 {
     try
     {
         Save(fileName);
         errMsg = "";
         return(true);
     }
     catch (Exception ex)
     {
         errMsg = ScadaUtils.BuildErrorMessage(ex, AdminPhrases.SaveProjectError);
         return(false);
     }
 }
Ejemplo n.º 8
0
 /// <summary>
 /// Deletes configuration files of the application.
 /// </summary>
 public virtual bool DeleteConfigFiles(out string errMsg)
 {
     try
     {
         Directory.Delete(AppDir, true);
         errMsg = "";
         return(true);
     }
     catch (Exception ex)
     {
         errMsg = ScadaUtils.BuildErrorMessage(ex, AdminPhrases.DeleteAppConfigError, AppName);
         return(false);
     }
 }
Ejemplo n.º 9
0
 /// <summary>
 /// Saves the specified table of the configuration database.
 /// </summary>
 public bool SaveTable(IBaseTable baseTable, out string errMsg)
 {
     try
     {
         Directory.CreateDirectory(BaseDir);
         string fileName = Path.Combine(BaseDir, baseTable.FileName);
         baseTable.Save(fileName);
         errMsg = "";
         return(true);
     }
     catch (Exception ex)
     {
         errMsg = ScadaUtils.BuildErrorMessage(ex, AdminPhrases.SaveBaseTableError, baseTable.Title);
         return(false);
     }
 }
Ejemplo n.º 10
0
 /// <summary>
 /// Loads a registration key.
 /// </summary>
 private void LoadRegKey()
 {
     try
     {
         if (File.Exists(regKeyFileName))
         {
             XmlDocument xmlDoc = new();
             xmlDoc.Load(regKeyFileName);
             txtRegKey.Text = xmlDoc.DocumentElement.Name == "RegKey" ?
                              xmlDoc.DocumentElement.InnerText.Trim() : "";
         }
     }
     catch (Exception ex)
     {
         ScadaUiUtils.ShowError(ScadaUtils.BuildErrorMessage(ex, AdminPhrases.LoadRegKeyError));
     }
 }
Ejemplo n.º 11
0
 /// <summary>
 /// Loads the project description from the specified project file.
 /// </summary>
 public static bool LoadDescription(string fileName, out string description, out string errMsg)
 {
     try
     {
         XmlDocument xmlDoc = new();
         xmlDoc.Load(fileName);
         description = xmlDoc.DocumentElement.GetChildAsString("Description");
         errMsg      = "";
         return(true);
     }
     catch (Exception ex)
     {
         description = "";
         errMsg      = ScadaUtils.BuildErrorMessage(ex, AdminPhrases.LoadProjectDescrError);
         return(false);
     }
 }
Ejemplo n.º 12
0
        /// <summary>
        /// Deletes all project files of the instance.
        /// </summary>
        public bool DeleteInstanceFiles(out string errMsg)
        {
            try
            {
                if (Directory.Exists(InstanceDir))
                {
                    Directory.Delete(InstanceDir, true);
                }

                errMsg = "";
                return(true);
            }
            catch (Exception ex)
            {
                errMsg = ScadaUtils.BuildErrorMessage(ex, AdminPhrases.DeleteInstanceFilesError);
                return(false);
            }
        }
Ejemplo n.º 13
0
        /// <summary>
        /// Saves the registration key.
        /// </summary>
        private bool SaveRegKey()
        {
            try
            {
                XmlDocument    xmlDoc  = new XmlDocument();
                XmlDeclaration xmlDecl = xmlDoc.CreateXmlDeclaration("1.0", "utf-8", null);
                xmlDoc.AppendChild(xmlDecl);

                XmlElement xmlElem = xmlDoc.CreateElement("RegKey");
                xmlDoc.AppendChild(xmlElem);
                xmlElem.InnerText = txtRegKey.Text.Trim();

                xmlDoc.Save(regKeyFileName);
                return(true);
            }
            catch (Exception ex)
            {
                ScadaUiUtils.ShowError(ScadaUtils.BuildErrorMessage(ex, AdminPhrases.SaveRegKeyError));
                return(false);
            }
        }
Ejemplo n.º 14
0
        /// <summary>
        /// Loads the state from the specified file.
        /// </summary>
        public bool Load(string fileName, out string errMsg)
        {
            try
            {
                SetToDefault();

                if (File.Exists(fileName))
                {
                    XmlDocument xmlDoc = new();
                    xmlDoc.Load(fileName);
                    XmlElement rootElem = xmlDoc.DocumentElement;

                    if (rootElem.SelectSingleNode("MainFormState") is XmlNode mainFormStateNode)
                    {
                        MainFormState.LoadFromXml(mainFormStateNode);
                    }

                    ProjectDir = rootElem.GetChildAsString("ProjectDir");

                    if (rootElem.SelectSingleNode("RecentProjects") is XmlNode recentProjectsNode)
                    {
                        XmlNodeList pathNodeList = recentProjectsNode.SelectNodes("Path");
                        foreach (XmlNode pathNode in pathNodeList)
                        {
                            RecentProjects.Add(pathNode.InnerText);
                        }
                    }
                }

                errMsg = "";
                return(true);
            }
            catch (Exception ex)
            {
                errMsg = ScadaUtils.BuildErrorMessage(ex, AppPhrases.LoadAppStateError);
                return(false);
            }
        }
Ejemplo n.º 15
0
        /// <summary>
        /// Creates all project files required for the instance.
        /// </summary>
        public bool CreateInstanceFiles(out string errMsg)
        {
            try
            {
                Directory.CreateDirectory(InstanceDir);

                foreach (ProjectApp app in AllApps)
                {
                    if (app.Enabled && !app.CreateConfigFiles(out errMsg))
                    {
                        return(false);
                    }
                }

                errMsg = "";
                return(true);
            }
            catch (Exception ex)
            {
                errMsg = ScadaUtils.BuildErrorMessage(ex, AdminPhrases.CreateInstanceFilesError);
                return(false);
            }
        }
Ejemplo n.º 16
0
        /// <summary>
        /// Saves the configuration to the specified file.
        /// </summary>
        public bool Save(string fileName, out string errMsg)
        {
            try
            {
                XmlDocument    xmlDoc  = new XmlDocument();
                XmlDeclaration xmlDecl = xmlDoc.CreateXmlDeclaration("1.0", "utf-8", null);
                xmlDoc.AppendChild(xmlDecl);

                XmlElement rootElem = xmlDoc.CreateElement("ScadaServerConfig");
                xmlDoc.AppendChild(rootElem);

                GeneralOptions.SaveToXml(rootElem.AppendElem("GeneralOptions"));
                ListenerOptions.SaveToXml(rootElem.AppendElem("ListenerOptions"));
                PathOptions.SaveToXml(rootElem.AppendElem("PathOptions"));

                XmlElement modulesElem = rootElem.AppendElem("Modules");
                foreach (string moduleCode in ModuleCodes)
                {
                    modulesElem.AppendElem("Module").SetAttribute("code", moduleCode);
                }

                XmlElement archivesElem = rootElem.AppendElem("Archives");
                foreach (ArchiveConfig archiveConfig in Archives)
                {
                    archiveConfig.SaveToXml(archivesElem.AppendElem("Archive"));
                }

                xmlDoc.Save(fileName);
                errMsg = "";
                return(true);
            }
            catch (Exception ex)
            {
                errMsg = ScadaUtils.BuildErrorMessage(ex, CommonPhrases.SaveAppConfigError);
                return(false);
            }
        }
Ejemplo n.º 17
0
        /// <summary>
        /// Loads the configuration database if needed.
        /// </summary>
        public bool Load(out string errMsg)
        {
            try
            {
                if (!Loaded)
                {
                    foreach (IBaseTable baseTable in AllTables)
                    {
                        string fileName = Path.Combine(BaseDir, baseTable.FileName);

                        if (File.Exists(fileName))
                        {
                            try
                            {
                                baseTable.Load(fileName);
                            }
                            catch (Exception ex)
                            {
                                throw new ScadaException(string.Format(
                                                             AdminPhrases.LoadBaseTableError, baseTable.Title), ex);
                            }
                        }
                    }

                    Loaded = true;
                }

                errMsg = "";
                return(true);
            }
            catch (Exception ex)
            {
                errMsg = ScadaUtils.BuildErrorMessage(ex, AdminPhrases.LoadConfigBaseError);
                return(false);
            }
        }
Ejemplo n.º 18
0
        /// <summary>
        /// Renames the project.
        /// </summary>
        public bool Rename(string newName, out string errMsg)
        {
            try
            {
                if (string.IsNullOrEmpty(newName))
                {
                    throw new ArgumentException(AdminPhrases.ProjectNameEmpty, nameof(newName));
                }

                if (!AdminUtils.NameIsValid(newName))
                {
                    throw new ArgumentException(AdminPhrases.ProjectNameInvalid, nameof(newName));
                }

                string        projectDir    = ProjectDir;
                DirectoryInfo directoryInfo = new(projectDir);
                string        newProjectDir = Path.Combine(directoryInfo.Parent.FullName, newName);

                if (Directory.Exists(newProjectDir))
                {
                    throw new ScadaException(AdminPhrases.ProjectDirectoryExists);
                }

                File.Move(FileName, GetProjectFileName(projectDir, newName));
                Directory.Move(projectDir, newProjectDir);
                FileName = GetProjectFileName(newProjectDir, newName);

                errMsg = "";
                return(true);
            }
            catch (Exception ex)
            {
                errMsg = ScadaUtils.BuildErrorMessage(ex, AdminPhrases.RenameProjectError);
                return(false);
            }
        }
Ejemplo n.º 19
0
        /// <summary>
        /// Saves the configuration to the specified file.
        /// </summary>
        public bool Save(string fileName, out string errMsg)
        {
            try
            {
                XmlDocument    xmlDoc  = new XmlDocument();
                XmlDeclaration xmlDecl = xmlDoc.CreateXmlDeclaration("1.0", "utf-8", null);
                xmlDoc.AppendChild(xmlDecl);

                XmlElement rootElem = xmlDoc.CreateElement("ScadaInstanceConfig");
                xmlDoc.AppendChild(rootElem);
                rootElem.AppendElem("Culture", Culture);

                xmlDoc.Save(fileName);
                errMsg = "";
                return(true);
            }
            catch (Exception ex)
            {
                errMsg = ScadaUtils.BuildErrorMessage(ex, Locale.IsRussian ?
                                                      "Ошибка при сохранении конфигурации экземпляра" :
                                                      "Error saving instance configuration");
                return(false);
            }
        }
Ejemplo n.º 20
0
        /// <summary>
        /// Loads the configuration from the specified file.
        /// </summary>
        public bool Load(string fileName, out string errMsg)
        {
            try
            {
                SetToDefault();

                if (!File.Exists(fileName))
                {
                    throw new FileNotFoundException(string.Format(CommonPhrases.NamedFileNotFound, fileName));
                }

                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.Load(fileName);
                XmlElement rootElem = xmlDoc.DocumentElement;

                if (rootElem.SelectSingleNode("GeneralOptions") is XmlNode generalOptionsNode)
                {
                    GeneralOptions.LoadFromXml(generalOptionsNode);
                }

                if (rootElem.SelectSingleNode("ListenerOptions") is XmlNode listenerOptionsNode)
                {
                    ListenerOptions.LoadFromXml(listenerOptionsNode);
                }

                if (rootElem.SelectSingleNode("PathOptions") is XmlNode pathOptionsNode)
                {
                    PathOptions.LoadFromXml(pathOptionsNode);
                }

                HashSet <string> moduleCodes = new HashSet <string>();

                if (rootElem.SelectSingleNode("Modules") is XmlNode modulesNode)
                {
                    foreach (XmlElement moduleElem in modulesNode.SelectNodes("Module"))
                    {
                        string moduleCode = ScadaUtils.RemoveFileNameSuffixes(moduleElem.GetAttribute("code"));

                        if (moduleCodes.Add(moduleCode.ToLowerInvariant())) // check uniqueness
                        {
                            ModuleCodes.Add(moduleCode);
                        }
                    }
                }

                if (rootElem.SelectSingleNode("Archives") is XmlNode archivesNode)
                {
                    foreach (XmlElement archiveElem in archivesNode.SelectNodes("Archive"))
                    {
                        ArchiveConfig archiveConfig = new ArchiveConfig();
                        archiveConfig.LoadFromXml(archiveElem);
                        Archives.Add(archiveConfig);

                        if (archiveConfig.Active && moduleCodes.Add(archiveConfig.Module.ToLowerInvariant()))
                        {
                            ModuleCodes.Add(archiveConfig.Module);
                        }
                    }
                }

                errMsg = "";
                return(true);
            }
            catch (Exception ex)
            {
                errMsg = ScadaUtils.BuildErrorMessage(ex, CommonPhrases.LoadAppConfigError);
                return(false);
            }
        }
Ejemplo n.º 21
0
        /// <summary>
        /// Creates a new project with the specified parameters.
        /// </summary>
        public static bool Create(string name, string location, string template,
                                  out ScadaProject project, out string errMsg)
        {
            try
            {
                // validate arguments
                if (string.IsNullOrEmpty(name))
                {
                    throw new ArgumentException(AdminPhrases.ProjectNameEmpty, nameof(name));
                }

                if (!AdminUtils.NameIsValid(name))
                {
                    throw new ArgumentException(AdminPhrases.ProjectNameInvalid, nameof(name));
                }

                // define project directory and file
                string projectDir      = Path.Combine(location, name);
                string projectFileName = GetProjectFileName(projectDir, name);

                if (Directory.Exists(projectDir))
                {
                    throw new ScadaException(AdminPhrases.ProjectDirectoryExists);
                }

                // copy template
                if (File.Exists(template))
                {
                    FileInfo templateFileInfo = new(template);
                    CopyDirectory(templateFileInfo.Directory, new DirectoryInfo(projectDir));
                    File.Move(Path.Combine(projectDir, templateFileInfo.Name), projectFileName);
                }

                // create project
                project = new ScadaProject {
                    Name = name
                };

                if (File.Exists(projectFileName))
                {
                    // load from template
                    project.Load(projectFileName);
                    project.Description = "";
                }
                else
                {
                    Directory.CreateDirectory(projectDir);
                    project.FileName = projectFileName;
                }

                project.Save(project.FileName);

                // create necessary directories
                Directory.CreateDirectory(project.ConfigBase.BaseDir);
                Directory.CreateDirectory(project.Views.ViewDir);

                errMsg = "";
                return(true);
            }
            catch (Exception ex)
            {
                project = null;
                errMsg  = ScadaUtils.BuildErrorMessage(ex, AdminPhrases.CreateProjectError);
                return(false);
            }
        }