Exemplo n.º 1
0
		/// <summary>
		/// Imports a GXDevice from a stream.
		/// </summary>
        /// <param name="usingDeviceProfileName">When template is exported to DC file name is changed to device profile name.</param>
        private static GXDeviceProfile Import(IWin32Window parent, ZipInputStream s, string target)
        {
            string TempPath = System.IO.Path.GetTempPath() + Guid.NewGuid().ToString();
            Directory.CreateDirectory(TempPath);
            //Extract to windows temp directory
            ZipEntry theEntry;
            ArrayList FilePaths = new ArrayList();
            string DevicePath = "";
            bool oldWay = false;
            while ((theEntry = s.GetNextEntry()) != null)
            {
                string FileName = theEntry.Name;
                FileName = Path.Combine(TempPath , FileName);
                if (string.Compare(Path.GetExtension(theEntry.Name), ".gxp", true) == 0 ||
                    string.Compare(Path.GetExtension(theEntry.Name), ".gxt", true) == 0)
                {
                    oldWay = string.Compare(Path.GetExtension(theEntry.Name), ".gxt", true) == 0;
                    if (oldWay)
                    {
                        FileName = Path.GetFileNameWithoutExtension(theEntry.Name) + ".gxp";
                        FileName = Path.Combine(TempPath, FileName);
                    }
                    DevicePath = FileName; //Save .gxp file name for opening
                }
				FilePaths.Add(FileName);
				WriteFile(s, FileName);
            }
            s.Close();

            if (DevicePath.Trim().Length == 0)
            {
                throw new Exception(Resources.DeviceFileDoesNotExistInThePacket);
            }
            if (!File.Exists(DevicePath)) //Shouldn't happen
            {
                throw new Exception(Resources.CouldNotFindUnpackedDeviceFile);
            }

            GXDeviceList.Update(TempPath);
            GXDeviceProfile type;
            if (GXJsonParser.IsJSONFile(DevicePath))
            {
                type = GXDevice.Load(DevicePath);
            }
            else
            {
                bool preset = false;
                string protocol = null, profileName = null;                
                Guid guid;
                type = new GXDeviceProfile();
                GXDevice.GetProtocolInfo(DevicePath, out preset, out protocol, out profileName, out guid);
                type.Protocol = protocol;
                type.Name = profileName;
                type.Guid = guid;
            }                            
            string DeviceFilePath;
            string protocolPath;
            if (string.IsNullOrEmpty(target))
            {
                DeviceFilePath = GXDevice.GetDeviceProfilePath(type.Protocol, type.Guid);
                protocolPath = Path.GetDirectoryName(DeviceFilePath);
            }
            else
            {
                if (string.IsNullOrEmpty(Path.GetFileName(target)))
                {
                    DeviceFilePath = target + Path.GetFileName(DevicePath);
                    protocolPath = Path.GetDirectoryName(target);
                }
                else
                {
                    DeviceFilePath = target;
                    protocolPath = Path.GetDirectoryName(DeviceFilePath);
                    target = null;
                }
            }
            if (!string.IsNullOrEmpty(protocolPath) && !Directory.Exists(protocolPath))
            {
                Directory.CreateDirectory(protocolPath);
            }
                       
            //Ask to overwrite if exists
            if (File.Exists(DeviceFilePath))
            {
				DialogResult retval = GXCommon.ShowQuestion(parent, Resources.DoYouWantToReplaceExistingDevice);
                if (retval != DialogResult.Yes)
                {
                    throw new Exception(Resources.DeviceInstallationFailed);
                }
            }
            bool restart = false;
            GXDevice Device = null;            
            //Copy to the final directory
            foreach (string FilePath in FilePaths)
            {
                FileInfo info = new FileInfo(FilePath);
                string FileName = info.Name;                
                if (string.Compare(info.Extension, ".gxp", true) == 0 ||
                    string.Compare(info.Extension, ".gxt", true) == 0)
                {
                    if (!string.IsNullOrEmpty(Path.GetFileName(target)))
                    {
                        FileName = Path.GetFileName(target);
                    }
                    string tmp = Path.Combine(protocolPath, FileName);
                    File.Copy(FilePath, tmp, true);
                    Gurux.Common.GXFileSystemSecurity.UpdateFileSecurity(tmp);
                }
                else if (string.Compare(info.Extension, ".dll", true) == 0) // AddIn protocol file for GXDeviceEditor
                {                    
                    string targetPath;
                    if (string.IsNullOrEmpty(target))
                    {
                        targetPath = Path.Combine(GXCommon.ProtocolAddInsPath, Path.GetFileName(FilePath));
                    }
                    else
                    {
                        targetPath = Path.Combine(Path.GetDirectoryName(target), Path.GetFileName(FilePath));
                    } 
                    string existsVersion = "0.0.0.0", newVersion = "0.0.0.0";
                    if (File.Exists(targetPath))
                    {
                        System.Diagnostics.FileVersionInfo vInfo = System.Diagnostics.FileVersionInfo.GetVersionInfo(targetPath);
						existsVersion = vInfo.FileVersion;
                    }
                    System.Diagnostics.FileVersionInfo vInfo2 = System.Diagnostics.FileVersionInfo.GetVersionInfo(FilePath);
                    newVersion = vInfo2.FileVersion;
					if (IsGreaterVersion(newVersion, existsVersion))
                    {
                        string DllPath = Directory.GetParent(targetPath).FullName;
                        if (!Directory.Exists(DllPath))
                        {
                            Directory.CreateDirectory(DllPath);
                        }                        
                        try
                        {
                            File.Copy(FilePath, targetPath, true);
                            Gurux.Common.GXFileSystemSecurity.UpdateFileSecurity(targetPath);
                        }
                        //If file is in use.
                        catch (System.IO.IOException)
                        {
                            DllPath = Path.Combine(DllPath, "cached");
                            if (!Directory.Exists(DllPath))
                            {
                                Directory.CreateDirectory(DllPath);
                            }
                            File.Copy(FilePath, Path.Combine(DllPath, Path.GetFileName(targetPath)), true);
                            restart = true;
                        }
                    }
                }
                else
                {
                    throw new Exception(string.Format(Resources.UnknownDataInExportedFile0, FileName));
                }
            }
            //Register device
            if (string.IsNullOrEmpty(target))
            {
                GXDeviceList.Update();
                Device = GXDevice.Load(DeviceFilePath);
                Device.Register();
                if (Device.m_AddIn != null)
                {
                    Device.m_AddIn.InitializeAfterImport(Device);
                }                          
            }
            return type;
        }
            public List <object[]> Import(string path, string target, out byte[] data, out string assemblyName, out string addInType)
            {
                data            = null;
                TargetDirectory = target;
                AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(CurrentDomain_AssemblyResolve);
                GXDeviceProfile type = GXZip.Import(null, path, target);

                GXDeviceList.Update(target);
                string filename = Path.Combine(target, type.DeviceGuid + ".gxp");

                using (FileStream s = File.OpenRead(filename))
                {
                    long size = s.Length;
                    size = 0;
                }
                GXDevice device = GXDevice.Load(filename);

                addInType    = device.AddIn.ToString();
                assemblyName = device.AddIn.GetType().Assembly.FullName;
                GXAmiDeviceProfile dt = new GXAmiDeviceProfile();

                dt.Guid           = device.Guid;
                dt.Protocol       = device.ProtocolName;
                dt.Profile        = device.DeviceProfile;
                dt.PresetName     = device.PresetName;
                dt.Manufacturer   = device.Manufacturer;
                dt.Model          = device.Model;
                dt.Version        = device.Version;
                dt.UpdateInterval = device.UpdateInterval;
                dt.WaitTime       = device.WaitTime;
                dt.ResendCount    = device.ResendCount;
                dt.Added          = DateTime.Now.ToUniversalTime();
                List <GXAmiMediaType> list   = new List <GXAmiMediaType>();
                List <string>         medias = new List <string>(Gurux.Communication.GXClient.GetAvailableMedias());

                foreach (GXMediaType it in device.AllowedMediaTypes)
                {
                    GXAmiMediaType mt = new GXAmiMediaType();
                    mt.Name = it.Name;
                    //If default media settings are not given, ask them from the parser.
                    if (string.IsNullOrEmpty(it.DefaultMediaSettings) && medias.Contains(it.Name))
                    {
                        Gurux.Common.IGXMedia m = device.GXClient.SelectMedia(it.Name);
                        mt.Settings = m.Settings;
                    }
                    else
                    {
                        mt.Settings = it.DefaultMediaSettings;
                    }
                    list.Add(mt);
                }
                dt.AllowedMediaTypes = list.ToArray();
                List <object[]> items = new List <object[]>();

                items.Add(new object[] { dt, dt });
                ImportParameters(device, device.ID, dt, items);
                foreach (GXCategory cat in device.Categories)
                {
                    GXAmiCategoryTemplate ct = new GXAmiCategoryTemplate();
                    items.Add(new object[] { dt, ct });
                    ct.Name = cat.Name;
                    ct.Id   = cat.ID;
                    ImportParameters(cat, cat.ID, ct, items);
                    foreach (GXProperty prop in cat.Properties)
                    {
                        GXAmiPropertyTemplate pt = new GXAmiPropertyTemplate();
                        pt.Id       = prop.ID;
                        pt.ParentID = cat.ID;
                        pt.Name     = prop.Name;
                        pt.Unit     = prop.Unit;
                        if (prop.ValueType != null)
                        {
                            pt.TypeAsString = prop.ValueType.ToString();
                        }
                        pt.AccessMode = prop.AccessMode;
                        items.Add(new object[] { dt, pt });
                        ImportParameters(prop, prop.ID, pt, items);
                    }
                }

                foreach (GXTable table in device.Tables)
                {
                    GXAmiDataTableTemplate tt = new GXAmiDataTableTemplate();
                    items.Add(new object[] { dt, tt });
                    tt.Name = table.Name;
                    tt.Id   = table.ID;
                    ImportParameters(table, table.ID, tt, items);
                    foreach (GXProperty prop in table.Columns)
                    {
                        GXAmiPropertyTemplate pt = new GXAmiPropertyTemplate();
                        pt.Id       = prop.ID;
                        pt.ParentID = table.ID;
                        pt.Name     = prop.Name;
                        pt.Unit     = prop.Unit;
                        if (prop.ValueType != null)
                        {
                            pt.TypeAsString = prop.ValueType.ToString();
                        }
                        pt.AccessMode = prop.AccessMode;
                        items.Add(new object[] { dt, pt });
                        ImportParameters(prop, prop.ID, pt, items);
                    }
                }
                return(items);
            }