Exemplo n.º 1
0
        /// <summary>
        /// Exports the configuration to the specified archive.
        /// </summary>
        public void ExportToArchive(string destFileName, ScadaProject project, Instance instance,
                                    UploadSettings uploadSettings)
        {
            if (destFileName == null)
            {
                throw new ArgumentNullException("destFileName");
            }
            if (project == null)
            {
                throw new ArgumentNullException("project");
            }
            if (instance == null)
            {
                throw new ArgumentNullException("instance");
            }
            if (uploadSettings == null)
            {
                throw new ArgumentNullException("transferSettings");
            }

            FileStream fileStream = null;
            ZipArchive zipArchive = null;

            try
            {
                fileStream = new FileStream(destFileName, FileMode.Create, FileAccess.Write, FileShare.Read);
                zipArchive = new ZipArchive(fileStream, ZipArchiveMode.Create);

                List <int> objNums       = uploadSettings.ObjNums;
                bool       filterByObj   = objNums.Count > 0;
                bool       ignoreRegKeys = uploadSettings.IgnoreRegKeys;

                // add the configuration database to the archive
                if (uploadSettings.IncludeBase)
                {
                    foreach (IBaseTable srcTable in project.ConfigBase.AllTables)
                    {
                        string          entryName  = "BaseDAT/" + srcTable.Name.ToLowerInvariant() + ".dat";
                        ZipArchiveEntry tableEntry = zipArchive.CreateEntry(entryName, CompressionLevel.Fastest);

                        using (Stream entryStream = tableEntry.Open())
                        {
                            // filter the source table by objects if needed
                            IBaseTable baseTable = srcTable;

                            if (filterByObj)
                            {
                                if (srcTable.ItemType == typeof(InCnl))
                                {
                                    baseTable = GetFilteredTable <InCnl>(srcTable, objNums);
                                }
                                else if (srcTable.ItemType == typeof(CtrlCnl))
                                {
                                    baseTable = GetFilteredTable <CtrlCnl>(srcTable, objNums);
                                }
                                else if (srcTable.ItemType == typeof(Interface))
                                {
                                    baseTable = GetFilteredTable <Interface>(srcTable, objNums);
                                }
                            }

                            // convert the table to DAT format
                            BaseAdapter baseAdapter = new BaseAdapter()
                            {
                                Stream = entryStream
                            };
                            baseAdapter.Update(baseTable);
                        }
                    }
                }

                // add the interface files to the archive
                if (uploadSettings.IncludeInterface)
                {
                    string interfaceDir = project.Interface.InterfaceDir;
                    string entryPrefix  = DirectoryBuilder.GetDirectory(ConfigParts.Interface, '/');

                    if (filterByObj)
                    {
                        PackFiles(zipArchive, interfaceDir,
                                  GetInterfaceFiles(project.ConfigBase.InterfaceTable, interfaceDir, objNums),
                                  entryPrefix);
                    }
                    else
                    {
                        PackDirectory(zipArchive, interfaceDir, entryPrefix, ignoreRegKeys);
                    }
                }

                // add the Server settings to the archive
                if (uploadSettings.IncludeServer && instance.ServerApp.Enabled)
                {
                    PackDirectory(zipArchive, instance.ServerApp.AppDir,
                                  DirectoryBuilder.GetDirectory(ConfigParts.Server, '/'), ignoreRegKeys);
                }

                // add the Communicator settings to the archive
                if (uploadSettings.IncludeComm && instance.CommApp.Enabled)
                {
                    PackDirectory(zipArchive, instance.CommApp.AppDir,
                                  DirectoryBuilder.GetDirectory(ConfigParts.Comm, '/'), ignoreRegKeys);
                }

                // add the Webstation settings to the archive
                if (uploadSettings.IncludeWeb && instance.WebApp.Enabled)
                {
                    PackDirectory(zipArchive, Path.Combine(instance.WebApp.AppDir, "config"),
                                  DirectoryBuilder.GetDirectory(ConfigParts.Web, AppFolder.Config, '/'), ignoreRegKeys);

                    if (!uploadSettings.IgnoreWebStorage)
                    {
                        PackDirectory(zipArchive, Path.Combine(instance.WebApp.AppDir, "storage"),
                                      DirectoryBuilder.GetDirectory(ConfigParts.Web, AppFolder.Storage, '/'), ignoreRegKeys);
                    }
                }

                // add an information entry to the archive
                using (Stream entryStream =
                           zipArchive.CreateEntry(ProjectInfoEntryName, CompressionLevel.Fastest).Open())
                {
                    using (StreamWriter writer = new StreamWriter(entryStream, Encoding.UTF8))
                    {
                        writer.Write(project.GetInfo());
                    }
                }
            }
            catch (Exception ex)
            {
                throw new ScadaException(AdminPhrases.ExportToArchiveError, ex);
            }
            finally
            {
                zipArchive?.Dispose();
                fileStream?.Dispose();
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Compresses the configuration into the specified file.
        /// </summary>
        private void CompressConfig(string destFileName)
        {
            transferControl.ThrowIfCancellationRequested();
            transferControl.WriteLine();
            transferControl.WriteMessage(ExtensionPhrases.CompressConfig);

            // archive path separator is '/'
            FileStream fileStream = null;
            ZipArchive zipArchive = null;

            try
            {
                fileStream = new FileStream(destFileName, FileMode.Create, FileAccess.Write, FileShare.Read);
                zipArchive = new ZipArchive(fileStream, ZipArchiveMode.Create);

                bool ignoreRegKeys = uploadOptions.IgnoreRegKeys;
                bool filterByObj   = uploadOptions.ObjectFilter.Count > 0;

                // archive the configuration database
                if (uploadOptions.IncludeBase)
                {
                    transferControl.ThrowIfCancellationRequested();
                    transferControl.WriteMessage(ExtensionPhrases.CompressBase);

                    foreach (IBaseTable srcTable in project.ConfigDatabase.AllTables)
                    {
                        transferControl.ThrowIfCancellationRequested();

                        // filter source table by objects if needed
                        IBaseTable baseTable = srcTable;

                        if (filterByObj)
                        {
                            if (srcTable.ItemType == typeof(Cnl))
                            {
                                baseTable = GetFilteredTable <Cnl>(srcTable, uploadOptions.ObjectFilter);
                            }
                            else if (srcTable.ItemType == typeof(View))
                            {
                                baseTable = GetFilteredTable <View>(srcTable, uploadOptions.ObjectFilter);
                            }
                        }

                        // convert table to DAT format
                        using Stream entryStream =
                                  zipArchive.CreateEntry("BaseDAT/" + srcTable.FileNameDat, CompressionLevel.Fastest).Open();
                        BaseTableAdapter baseAdapter = new() { Stream = entryStream };
                        baseAdapter.Update(baseTable);
                    }
                }

                // archive views
                if (uploadOptions.IncludeView)
                {
                    transferControl.ThrowIfCancellationRequested();
                    transferControl.WriteMessage(ExtensionPhrases.CompressViews);

                    if (filterByObj)
                    {
                        PackFiles(zipArchive, project.Views.ViewDir, GetFilteredViews(project.ConfigDatabase.ViewTable,
                                                                                      project.Views.ViewDir, uploadOptions.ObjectFilter), "Views/");
                    }
                    else
                    {
                        PackDirectory(zipArchive, project.Views.ViewDir, "Views/", ignoreRegKeys);
                    }
                }

                // archive Server configuration
                if (uploadOptions.IncludeServer && instance.ServerApp.Enabled)
                {
                    transferControl.ThrowIfCancellationRequested();
                    transferControl.WriteMessage(string.Format(ExtensionPhrases.CompressAppConfig,
                                                               CommonPhrases.ServerAppName));
                    PackDirectory(zipArchive, instance.ServerApp.AppDir, "ScadaServer/", ignoreRegKeys);
                }

                // archive Communicator configuration
                if (uploadOptions.IncludeComm && instance.CommApp.Enabled)
                {
                    transferControl.ThrowIfCancellationRequested();
                    transferControl.WriteMessage(string.Format(ExtensionPhrases.CompressAppConfig,
                                                               CommonPhrases.CommAppName));
                    PackDirectory(zipArchive, instance.CommApp.AppDir, "ScadaComm/", ignoreRegKeys);
                }

                // archive Webstation configuration
                if (uploadOptions.IncludeWeb && instance.WebApp.Enabled)
                {
                    transferControl.ThrowIfCancellationRequested();
                    transferControl.WriteMessage(string.Format(ExtensionPhrases.CompressAppConfig,
                                                               CommonPhrases.WebAppName));
                    PackDirectory(zipArchive, instance.WebApp.AppDir, "ScadaWeb/", ignoreRegKeys);
                }

                // add project information
                transferControl.ThrowIfCancellationRequested();
                transferControl.WriteMessage(ExtensionPhrases.AddProjectInfo);

                using (Stream entryStream =
                           zipArchive.CreateEntry(AgentConst.ProjectInfoEntry, CompressionLevel.Fastest).Open())
                {
                    using StreamWriter writer = new(entryStream, Encoding.UTF8);
                    writer.Write(project.GetInfo());
                }

                // add transfer options
                transferControl.ThrowIfCancellationRequested();
                transferControl.WriteMessage(ExtensionPhrases.AddTransferOptions);

                using (Stream entryStream =
                           zipArchive.CreateEntry(AgentConst.UploadOptionsEntry, CompressionLevel.Fastest).Open())
                {
                    uploadOptions.Save(entryStream);
                }

                progressTracker.TaskIndex++;
            }
            finally
            {
                zipArchive?.Dispose();
                fileStream?.Dispose();
            }
        }