Exemplo n.º 1
0
        public bool WidgetImport(string archiveFile, string importPath)
        {
            bool          success        = false;
            string        widgetInfoFile = "widget.info";
            List <string> extractedFiles = Utility.UncompressZip(archiveFile, importPath);

            if (File.Exists(Path.Combine(importPath, widgetInfoFile)))
            {
                // Read "widget.info" and, if a mapping is present, add it to "html/pages/control/widgets/configuration.json"
                var mapping = File.ReadAllText(Path.Combine(importPath, widgetInfoFile));
                if (mapping.StartsWith("["))
                {
                    AddWidgetMapping(mapping);
                }
                foreach (string f in extractedFiles)
                {
                    // copy only files contained in sub-folders, avoid copying zip-root files
                    if (Path.GetDirectoryName(f) != "")
                    {
                        string destFolder = Path.Combine(widgetBasePath, Path.GetDirectoryName(f));
                        if (!Directory.Exists(destFolder))
                        {
                            Directory.CreateDirectory(destFolder);
                        }
                        File.Copy(Path.Combine(importPath, f), Path.Combine(widgetBasePath, f), true);
                    }
                }
                success = true;
            }
            return(success);
        }
Exemplo n.º 2
0
        public ProgramBlock ProgramImport(int newPid, string archiveName, string groupName)
        {
            ProgramBlock newProgram;

            char[] signature = new char[2];
            using (var reader = new StreamReader(archiveName))
            {
                reader.Read(signature, 0, 2);
            }
            if (signature[0] == 'P' && signature[1] == 'K')
            {
                // Read and uncompress zip file content (arduino program bundle)
                string zipFileName = archiveName.Replace(".hgx", ".zip");
                if (File.Exists(zipFileName))
                {
                    File.Delete(zipFileName);
                }
                File.Move(archiveName, zipFileName);
                string destFolder = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, Utility.GetTmpFolder(), "import");
                if (Directory.Exists(destFolder))
                {
                    Directory.Delete(destFolder, true);
                }
                Utility.UncompressZip(zipFileName, destFolder);
                string bundleFolder = Path.Combine("programs", "arduino", newPid.ToString());
                if (Directory.Exists(bundleFolder))
                {
                    Directory.Delete(bundleFolder, true);
                }
                if (!Directory.Exists(Path.Combine("programs", "arduino")))
                {
                    Directory.CreateDirectory(Path.Combine("programs", "arduino"));
                }
                Directory.Move(Path.Combine(destFolder, "src"), bundleFolder);
                archiveName = Path.Combine(destFolder, "program.hgx");
            }

            // TODO: Deprecate Compat
            Compat_526.FixProgramsDatabase(archiveName);

            using (var reader = new StreamReader(archiveName))
            {
                var serializer = new XmlSerializer(typeof(ProgramBlock));
                newProgram = (ProgramBlock)serializer.Deserialize(reader);
            }
            newProgram.Address = newPid;
            newProgram.Group   = groupName;
            homegenie.ProgramManager.ProgramAdd(newProgram);

            newProgram.IsEnabled    = false;
            newProgram.ScriptErrors = "";
            newProgram.Engine.SetHost(homegenie);

            if (newProgram.Type.ToLower() != "arduino")
            {
                homegenie.ProgramManager.CompileScript(newProgram);
            }
            return(newProgram);
        }
Exemplo n.º 3
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);
        }
Exemplo n.º 4
0
        public List <string> DownloadAndUncompress(ReleaseInfo releaseInfo)
        {
            if (ArchiveDownloadUpdate != null)
            {
                ArchiveDownloadUpdate(this, new ArchiveDownloadEventArgs(releaseInfo, ArchiveDownloadStatus.DOWNLOADING));
            }
            //
            string destinationFolder = Path.Combine(updateFolder, "files");
            string archiveName       = Path.Combine(updateFolder, "archives", "hg_update_" + releaseInfo.Version.Replace(" ", "_").Replace(".", "_") + ".zip");

            if (!Directory.Exists(Path.GetDirectoryName(archiveName)))
            {
                Directory.CreateDirectory(Path.GetDirectoryName(archiveName));
            }
            using (var client = new WebClient())
            {
                client.Headers.Add("user-agent", "HomeGenieUpdater/1.0 (compatible; MSIE 7.0; Windows NT 6.0)");
                try
                {
                    client.DownloadFile(releaseInfo.DownloadUrl, archiveName);
                }
                catch (Exception)
                {
                    if (ArchiveDownloadUpdate != null)
                    {
                        ArchiveDownloadUpdate(this, new ArchiveDownloadEventArgs(releaseInfo, ArchiveDownloadStatus.ERROR));
                    }
                    return(null);
                    //                throw;
                }
                finally
                {
                    client.Dispose();
                }
            }

            // Unarchive (unzip)
            if (ArchiveDownloadUpdate != null)
            {
                ArchiveDownloadUpdate(this, new ArchiveDownloadEventArgs(releaseInfo, ArchiveDownloadStatus.DECOMPRESSING));
            }

            bool errorOccurred = false;
            var  files         = Utility.UncompressZip(archiveName, destinationFolder);

            errorOccurred = (files.Count == 0);

            if (ArchiveDownloadUpdate != null)
            {
                if (errorOccurred)
                {
                    ArchiveDownloadUpdate(this, new ArchiveDownloadEventArgs(releaseInfo, ArchiveDownloadStatus.ERROR));
                }
                else
                {
                    ArchiveDownloadUpdate(this, new ArchiveDownloadEventArgs(releaseInfo, ArchiveDownloadStatus.COMPLETED));
                }
            }

            return(files);
        }
Exemplo n.º 5
0
        public bool InstallPackage(string pkgFolderUrl, string tempFolderPath)
        {
            string  installFolder = Path.Combine(tempFolderPath, "pkg");
            dynamic pkgData       = null;
            bool    success       = true;

            // Download package specs
            homegenie.RaiseEvent(
                Domains.HomeGenie_System,
                Domains.HomeGenie_PackageInstaller,
                SourceModule.Master,
                "HomeGenie Package Installer",
                Properties.InstallProgressMessage,
                "= Downloading: package.json"
                );
            using (var client = new WebClient())
            {
                try
                {
                    string pkgJson = "[" + client.DownloadString(pkgFolderUrl + "/package.json") + "]";
                    pkgData = (JsonConvert.DeserializeObject(pkgJson) as JArray)[0];
                }
                catch (Exception e)
                {
                    homegenie.RaiseEvent(
                        Domains.HomeGenie_System,
                        Domains.HomeGenie_PackageInstaller,
                        SourceModule.Master,
                        "HomeGenie Package Installer",
                        Properties.InstallProgressMessage,
                        "= ERROR: '" + e.Message + "'"
                        );
                    success = false;
                }
                client.Dispose();
            }
            // Download and install package files
            if (success && pkgData != null)
            {
                // Import Automation Programs in package
                foreach (var program in pkgData.programs)
                {
                    homegenie.RaiseEvent(
                        Domains.HomeGenie_System,
                        Domains.HomeGenie_PackageInstaller,
                        SourceModule.Master,
                        "HomeGenie Package Installer",
                        Properties.InstallProgressMessage,
                        "= Downloading: " + program.file.ToString()
                        );
                    Utility.FolderCleanUp(installFolder);
                    string programFile = Path.Combine(installFolder, program.file.ToString());
                    if (File.Exists(programFile))
                    {
                        File.Delete(programFile);
                    }
                    using (var client = new WebClient())
                    {
                        try
                        {
                            client.DownloadFile(pkgFolderUrl + "/" + program.file.ToString(), programFile);
                        }
                        catch (Exception e)
                        {
                            homegenie.RaiseEvent(
                                Domains.HomeGenie_System,
                                Domains.HomeGenie_PackageInstaller,
                                SourceModule.Master,
                                "HomeGenie Package Installer",
                                Properties.InstallProgressMessage,
                                "= ERROR: '" + e.Message + "'"
                                );
                            success = false;
                        }
                        client.Dispose();
                    }
                    if (success)
                    {
                        homegenie.RaiseEvent(
                            Domains.HomeGenie_System,
                            Domains.HomeGenie_PackageInstaller,
                            SourceModule.Master,
                            "HomeGenie Package Installer",
                            Properties.InstallProgressMessage,
                            "= Installing: " + program.name.ToString()
                            );
                        int pid = int.Parse(program.uid.ToString());
                        // by default enable package programs after installing them
                        var enabled    = true;
                        var oldProgram = homegenie.ProgramManager.ProgramGet(pid);
                        if (oldProgram != null)
                        {
                            homegenie.RaiseEvent(
                                Domains.HomeGenie_System,
                                Domains.HomeGenie_PackageInstaller,
                                SourceModule.Master,
                                "HomeGenie Package Installer",
                                Properties.InstallProgressMessage,
                                "= Replacing: '" + oldProgram.Name + "' with pid " + pid
                                );
                            // if the program was already installed, inherit IsEnabled
                            enabled = oldProgram.IsEnabled;
                            homegenie.ProgramManager.ProgramRemove(oldProgram);
                        }
                        var programBlock = ProgramImport(pid, programFile, program.group.ToString());
                        if (programBlock != null)
                        {
                            string groupName = programBlock.Group;
                            if (!String.IsNullOrWhiteSpace(groupName))
                            {
                                // Add automation program group if does not exist
                                Group newGroup = new Group()
                                {
                                    Name = groupName
                                };
                                if (homegenie.AutomationGroups.Find(g => g.Name == newGroup.Name) == null)
                                {
                                    homegenie.AutomationGroups.Add(newGroup);
                                    homegenie.UpdateGroupsDatabase("Automation");
                                }
                            }
                            programBlock.IsEnabled = enabled;
                            homegenie.RaiseEvent(
                                Domains.HomeGenie_System,
                                Domains.HomeGenie_PackageInstaller,
                                SourceModule.Master,
                                "HomeGenie Package Installer",
                                Properties.InstallProgressMessage,
                                "= Installed: '" + program.name.ToString() + "' as pid " + pid
                                );
                        }
                        else
                        {
                            // TODO: report error and stop the package install procedure
                            success = false;
                        }
                    }
                }
                // Import Widgets in package
                foreach (var widget in pkgData.widgets)
                {
                    homegenie.RaiseEvent(
                        Domains.HomeGenie_System,
                        Domains.HomeGenie_PackageInstaller,
                        SourceModule.Master,
                        "HomeGenie Package Installer",
                        Properties.InstallProgressMessage,
                        "= Downloading: " + widget.file.ToString()
                        );
                    Utility.FolderCleanUp(installFolder);
                    string widgetFile = Path.Combine(installFolder, widget.file.ToString());
                    if (File.Exists(widgetFile))
                    {
                        File.Delete(widgetFile);
                    }
                    using (var client = new WebClient())
                    {
                        try
                        {
                            client.DownloadFile(pkgFolderUrl + "/" + widget.file.ToString(), widgetFile);
                        }
                        catch (Exception e)
                        {
                            homegenie.RaiseEvent(
                                Domains.HomeGenie_System,
                                Domains.HomeGenie_PackageInstaller,
                                SourceModule.Master,
                                "HomeGenie Package Installer",
                                Properties.InstallProgressMessage,
                                "= ERROR: '" + e.Message + "'"
                                );
                            success = false;
                        }
                        client.Dispose();
                    }
                    if (success && WidgetImport(widgetFile, installFolder))
                    {
                        homegenie.RaiseEvent(
                            Domains.HomeGenie_System,
                            Domains.HomeGenie_PackageInstaller,
                            SourceModule.Master,
                            "HomeGenie Package Installer",
                            Properties.InstallProgressMessage,
                            "= Installed: '" + widget.name.ToString() + "'"
                            );
                    }
                    else
                    {
                        // TODO: report error and stop the package install procedure
                        success = false;
                    }
                }
                // Import MIG Interfaces in package
                foreach (var migface in pkgData.interfaces)
                {
                    homegenie.RaiseEvent(
                        Domains.HomeGenie_System,
                        Domains.HomeGenie_PackageInstaller,
                        SourceModule.Master,
                        "HomeGenie Package Installer",
                        Properties.InstallProgressMessage,
                        "= Downloading: " + migface.file.ToString()
                        );
                    Utility.FolderCleanUp(installFolder);
                    string migfaceFile = Path.Combine(installFolder, migface.file.ToString());
                    if (File.Exists(migfaceFile))
                    {
                        File.Delete(migfaceFile);
                    }
                    using (var client = new WebClient())
                    {
                        try
                        {
                            client.DownloadFile(pkgFolderUrl + "/" + migface.file.ToString(), migfaceFile);
                            Utility.UncompressZip(migfaceFile, installFolder);
                            File.Delete(migfaceFile);
                        }
                        catch (Exception e)
                        {
                            homegenie.RaiseEvent(
                                Domains.HomeGenie_System,
                                Domains.HomeGenie_PackageInstaller,
                                SourceModule.Master,
                                "HomeGenie Package Installer",
                                Properties.InstallProgressMessage,
                                "= ERROR: '" + e.Message + "'"
                                );
                            success = false;
                        }
                        client.Dispose();
                    }
                    if (success && InterfaceInstall(installFolder))
                    {
                        homegenie.RaiseEvent(
                            Domains.HomeGenie_System,
                            Domains.HomeGenie_PackageInstaller,
                            SourceModule.Master,
                            "HomeGenie Package Installer",
                            Properties.InstallProgressMessage,
                            "= Installed: '" + migface.name.ToString() + "'"
                            );
                    }
                    else
                    {
                        // TODO: report error and stop the package install procedure
                        success = false;
                    }
                }
            }
            else
            {
                success = false;
            }
            if (success)
            {
                pkgData.folder_url   = pkgFolderUrl;
                pkgData.install_date = DateTime.UtcNow;
                AddInstalledPackage(pkgData);
                homegenie.RaiseEvent(
                    Domains.HomeGenie_System,
                    Domains.HomeGenie_PackageInstaller,
                    SourceModule.Master,
                    "HomeGenie Package Installer",
                    Properties.InstallProgressMessage,
                    "= Status: Package Install Successful"
                    );
            }
            else
            {
                homegenie.RaiseEvent(
                    Domains.HomeGenie_System,
                    Domains.HomeGenie_PackageInstaller,
                    SourceModule.Master,
                    "HomeGenie Package Installer",
                    Properties.InstallProgressMessage,
                    "= Status: Package Install Error"
                    );
            }

            return(success);
        }