示例#1
0
        public PackageData AddPackage(string archiveFileName)
        {
            // uncompress and verify package content
            string tempFolder = Path.Combine(Utility.GetTmpFolder(), Path.GetFileNameWithoutExtension(archiveFileName));

            Utility.FolderCleanUp(tempFolder);
            Utility.UncompressZip(archiveFileName, tempFolder);
            string packageJson = File.ReadAllText(Path.Combine(tempFolder, "package.json"));
            var    packageData = JsonConvert.DeserializeObject <PackageData>(packageJson);
            // TODO: Data normalization and check
            string programsDatabase = Path.Combine(tempFolder, "programs.xml");
            var    serializer1      = new XmlSerializer(typeof(List <ProgramBlock>));

            using (var reader = new StreamReader(programsDatabase))
            {
                // verify if package data checksum matches the one in programs.xml file
                var programs = (List <ProgramBlock>)serializer1.Deserialize(reader);
                foreach (var prg in packageData.Programs)
                {
                    // Get the program from programs.xml
                    var p1 = programs.Find((p) => p.Address.ToString() == prg.Hid);
                    if (p1 != null)
                    {
                        p1.PackageInfo.Checksum = Utility.GetObjectChecksum(new
                        {
                            setup  = p1.ScriptSetup,
                            source = p1.ScriptSource
                        });
                        if (p1.PackageInfo.Checksum != prg.Checksum)
                        {
                            //TODO: Integrity check failed, should abort package importing
                            //Console.WriteLine(p1.PackageInfo.Checksum);
                            //Console.WriteLine(prg.Checksum);
                        }
                    }
                }
            }
            // copy the extracted package folder to the repository folder
            string repositoryFolder = Path.Combine(Utility.GetDataFolder(), "packages", packageData.Repository);

            if (!Directory.Exists(repositoryFolder))
            {
                Directory.CreateDirectory(repositoryFolder);
            }
            string pkgFolder = Path.Combine(repositoryFolder, packageData.Id);

            if (Directory.Exists(pkgFolder))
            {
                Directory.Delete(pkgFolder, true);
            }
            Directory.Move(tempFolder, pkgFolder);
            return(packageData);
        }
示例#2
0
        public string CreatePackage(PackageData package)
        {
            string programsFile  = Path.Combine(Utility.GetTmpFolder(), "programs.xml");
            string modulesFile   = Path.Combine(Utility.GetTmpFolder(), "modules.xml");
            string groupsFile    = Path.Combine(Utility.GetTmpFolder(), "groups.xml");
            string schedulesFile = Path.Combine(Utility.GetTmpFolder(), "schedules.xml");
            string packageFile   = Path.Combine(Utility.GetTmpFolder(), "package.json");
            string bundleFile    = Path.Combine(Utility.GetTmpFolder(), package.Id + "-" + package.Version + ".zip");

            try
            {
                // Clean-up
                File.Delete(programsFile);
                File.Delete(packageFile);
                File.Delete(bundleFile);
                // collect programs and modules
                bool saveProgramsRequired = false;
                var  packagePrograms      = new List <ProgramBlock>();
                var  packageModules       = new List <Module>();
                foreach (var item in package.Programs)
                {
                    if (item.Repository != package.Repository || item.PackageId != package.Id)
                    {
                        // item is an external dependency belonging to some other repository/package
                        continue;
                    }
                    var program = homegenie.ProgramManager.ProgramGet(int.Parse(item.Hid));
                    if (program != null)
                    {
                        saveProgramsRequired = true;
                        //if (program.PackageInfo.Repository == null)
                        {
                            program.PackageInfo.Repository = package.Repository;
                        }
                        //if (program.PackageInfo.PackageId == null)
                        {
                            program.PackageInfo.PackageId = package.Id;
                        }
                        // update package version only if repository/package id match
                        if (program.PackageInfo.Repository == package.Repository && program.PackageInfo.PackageId == package.Id)
                        {
                            program.PackageInfo.PackageVersion = package.Version;
                        }
                        if (program.PackageInfo.Id == null)
                        {
                            program.PackageInfo.Id = item.Id;
                        }
                        program.PackageInfo.Version  = item.Version;
                        program.PackageInfo.Required = item.Required;
                        item.Checksum = program.PackageInfo.Checksum = Utility.GetObjectChecksum(new
                        {
                            setup  = program.ScriptSetup,
                            source = program.ScriptSource
                        });
                        packagePrograms.Add(program);
                        // lookup for modules belonging to this program
                        packageModules.AddRange(homegenie.Modules.FindAll((m) =>
                        {
                            var vm = Utility.ModuleParameterGet(m, Properties.VirtualModuleParentId);
                            return((m.Domain == Domains.HomeAutomation_HomeGenie && m.Address == program.Address.ToString()) ||
                                   (vm != null && vm.Value == program.Address.ToString()));
                        }));
                    }
                }
                Utility.UpdateXmlDatabase(packagePrograms, programsFile, null);
                Utility.UpdateXmlDatabase(packageModules, modulesFile, null);
                // collect control groups
                var packageGroups = new List <Group>();
                foreach (var item in package.Groups)
                {
                    var group = homegenie.GetGroups("Control").Find((g) => g.Name == item.Hid);
                    if (group != null)
                    {
                        packageGroups.Add(group);
                    }
                }
                Utility.UpdateXmlDatabase(packageGroups, groupsFile, null);
                // collect schedules
                var packageSchedules = new List <SchedulerItem>();
                foreach (var item in package.Schedules)
                {
                    var schedule = homegenie.ProgramManager.SchedulerService.Get(item.Hid);
                    if (schedule != null)
                    {
                        packageSchedules.Add(schedule);
                    }
                }
                Utility.UpdateXmlDatabase(packageSchedules, schedulesFile, null);
                // add files to zip bundle
                File.WriteAllText(packageFile, JsonConvert.SerializeObject(package));
                Utility.AddFileToZip(bundleFile, packageFile, "package.json");
                Utility.AddFileToZip(bundleFile, programsFile, "programs.xml");
                Utility.AddFileToZip(bundleFile, modulesFile, "modules.xml");
                Utility.AddFileToZip(bundleFile, groupsFile, "groups.xml");
                Utility.AddFileToZip(bundleFile, schedulesFile, "schedules.xml");
                // move files to package folder in data/packages
                string packageFolder = Path.Combine(Utility.GetDataFolder(), "packages", package.Repository, package.Id);
                Utility.FolderCleanUp(packageFolder);
                File.Move(packageFile, Path.Combine(packageFolder, "package.json"));
                File.Move(programsFile, Path.Combine(packageFolder, "programs.xml"));
                File.Move(modulesFile, Path.Combine(packageFolder, "modules.xml"));
                File.Move(groupsFile, Path.Combine(packageFolder, "groups.xml"));
                File.Move(schedulesFile, Path.Combine(packageFolder, "schedules.xml"));
                // update programs db if required
                if (saveProgramsRequired)
                {
                    homegenie.UpdateProgramsDatabase();
                }
            }
            catch (Exception e)
            {
                homegenie.RaiseEvent(
                    Domains.HomeGenie_System,
                    Domains.HomeGenie_PackageInstaller,
                    SourceModule.Master,
                    "HomeGenie Package Installer",
                    Properties.InstallProgressMessage,
                    "= Error: " + e.Message
                    );
                return(null);
            }
            // TODO: cleanup temp files and folders
            return(bundleFile);
        }