Exemple #1
0
        /// <summary>
        /// 解析ETS文件
        /// </summary>
        /// <param name="etsProjectFile"></param>
        /// <exception cref="ArgumentNullException"></exception>
        /// <returns></returns>
        public static List <ImGroupAddr> ParseEtsProjectFile(string etsProjectFile, BackgroundWorker worker)
        {
            List <ImGroupAddr> listAddress = new List <ImGroupAddr>();

            // 如果文件存在
            if (File.Exists(etsProjectFile) == true)
            {
                worker.ReportProgress(0, ResourceMng.GetString("TextIsUnziping"));

                // 在当前位置解压文件
                string directoryName = Path.GetDirectoryName(etsProjectFile);
                ZipHelper.UnZipDir(etsProjectFile, directoryName);

                worker.ReportProgress(0, ResourceMng.GetString("TextIsCaluculating"));

                // 查找 0.xml 文件
                const string addressFileName = "0.xml";
                var          addressFiles    = Directory.GetFiles(directoryName, addressFileName, SearchOption.AllDirectories);

                if (addressFiles.Length > 0)
                {
                    string addressFile = addressFiles[0];
                    var    addressXDoc = XDocument.Load(addressFile);
                    addressXDoc.Element("KNX");
                    XNamespace xns = addressXDoc.Root.Name.Namespace;

                    // 从导入的ETS项目中获取group address
                    var groupAddress = from item in addressXDoc.Root.Descendants(xns + attrGroupAddress)
                                       select new ImGroupAddr
                    {
                        Id         = item.Attribute(attrId).Value,
                        Name       = item.Attribute(attrName).Value,
                        KnxAddress = item.Attribute(attrAddress).Value,
                        DPTName    = (null != item.Attribute(attrDatapointType)) ? item.Attribute(attrDatapointType).Value : "",
                    };

                    // 获取数据类型
                    var comObjectInstanceRef = (from item in addressXDoc.Descendants(xns + strSend)
                                                let xElement = item.Parent
                                                               where xElement != null
                                                               select new
                    {
                        GroupAddressRefId = item.Attribute(strGroupAddressRefId).Value,
                        ComObjectInstanceRefId = xElement.Parent.Attribute(strRefId).Value,
                    }).ToLookup(p => p.GroupAddressRefId, p => p.ComObjectInstanceRefId);

                    Dictionary <string, XDocument> xDocs = new Dictionary <string, XDocument>();

                    int   len = groupAddress.ToList().Count;
                    float i   = 0;

                    /* 获取组地址的大小 */
                    foreach (var address in groupAddress.ToList())
                    {
                        ImGroupAddr addr = new ImGroupAddr();
                        addr.Id         = address.Id;
                        addr.Name       = address.Name;
                        addr.KnxAddress = KNXAddressHelper.AddressToString(Convert.ToUInt16(address.KnxAddress));
                        addr.DPTName    = address.DPTName;
                        float f = i++ / len;

                        worker.ReportProgress((int)(f * 100), ResourceMng.GetString("TextIsImportingGroupAddress") + " " + address.Name + " " + addr.KnxAddress);

                        if (comObjectInstanceRef.Contains(address.Id))
                        {
                            var comObjectInstanceRefId = comObjectInstanceRef[address.Id].First <string>();

                            var index1 = comObjectInstanceRefId.IndexOf('_');
                            var index2 = comObjectInstanceRefId.IndexOf('_', index1 + 1);
                            var index3 = comObjectInstanceRefId.IndexOf('_', index2 + 1);
                            var comObjectInstanceFile = comObjectInstanceRefId.Substring(0, index2);
                            var comObjectInstanceId   = comObjectInstanceRefId.Substring(0, index3);

                            var manufacturerDataFiles = Directory.GetFiles(directoryName, comObjectInstanceFile + ".xml", SearchOption.AllDirectories);
                            if (manufacturerDataFiles.Length > 0)
                            {
                                string    manufacturerDataFile = manufacturerDataFiles[0];
                                XDocument manufacturerDataXDoc;
                                if (xDocs.ContainsKey(manufacturerDataFile))
                                {
                                    manufacturerDataXDoc = xDocs[manufacturerDataFile];
                                }
                                else
                                {
                                    manufacturerDataXDoc = XDocument.Load(manufacturerDataFile);
                                    xDocs.Add(manufacturerDataFile, manufacturerDataXDoc);
                                }

                                var comObject = (from p in manufacturerDataXDoc.Descendants(xns + strComObject)
                                                 where p.Attribute(attrId).Value == comObjectInstanceId
                                                 select p).FirstOrDefault();
                                parseComObject(addr, comObject);

                                var comObjectRef = (from p in manufacturerDataXDoc.Descendants(xns + strComObjectRef)
                                                    where p.Attribute(attrId).Value == comObjectInstanceRefId
                                                    select p).FirstOrDefault();
                                parseComObject(addr, comObjectRef);
                            }

                            parseDatapointType(addr);
                        }
                        else
                        {
                            parseDatapointType(addr);
                            addr.IsSelected = false;
                        }

                        bool isExsit = MyCache.AddressIsExsit(addr.KnxAddress);
                        if (isExsit)
                        {
                            addr.IsSelected = false;
                        }

                        listAddress.Add(addr);
                    }
                }
            }

            return(listAddress);
        }
Exemple #2
0
        private void OpenKnxUiPrject()
        {
            Cursor = Cursors.WaitCursor;

            if (Saved == false)
            {
                var result = MessageBox.Show(ResourceMng.GetString("Message7"), ResourceMng.GetString("Message4"), MessageBoxButtons.YesNoCancel,
                                             MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1);

                if (DialogResult.Yes == result)
                {
                    SaveKnxUiProject(ProjectFile);
                    Cursor = Cursors.Default;
                    return;
                }
                else if (DialogResult.No == result)
                {
                }
                else if (DialogResult.Cancel == result)
                {
                    Cursor = Cursors.Default;
                    return;
                }
            }

            try
            {
                using (var ofd = new OpenFileDialog())
                {
                    ofd.InitialDirectory = MyCache.DefaultKnxProjectFolder;
                    ofd.Filter           = KnxFilter;
                    ofd.FilterIndex      = 1;
                    ofd.DefaultExt       = MyConst.KnxUiEditorFileExt;
                    ofd.RestoreDirectory = true;

                    if (ofd.ShowDialog(this) == DialogResult.OK)
                    {
                        // 新建项目文件夹
                        CreateProjectFolder();
                        var projectFile = ofd.FileName;
                        Debug.WriteLine(projectFile);
                        Debug.WriteLine(MyCache.ProjectFolder);

                        ZipHelper.UnZipDir(projectFile, MyCache.ProjectFolder, MyConst.MyKey);

                        var app = AppStorage.Load();

                        if (app != null)
                        {
                            // 导入所有节点
                            AppNode appNode = FrmMainHelp.ImportNode(app);
                            SetProjectOutline(appNode);

                            ProjectFile = ofd.FileName;
                            ShowProjectFile(ProjectFile);

                            //
                            //ToolBarStatus status = new ToolBarStatus { collapse = true, expand = true, searchBox = true, importKnx = true };
                            //SetButtonStatus(status);
                            SetToolStripButtonStatus(false);
                            SetToolStripButtonKNXAddrStatus(true);
                            SetToolStripButtonSaveStatus(true);

                            ResetParameter();
                            CreateCommandManager();
                            CloseAllTabPages();

                            Saved = true;
                        }
                        else
                        {
                            throw new ApplicationException(ResourceMng.GetString("Message8"));
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                string errorMsg = ResourceMng.GetString("Message8");
                MessageBox.Show(errorMsg, ResourceMng.GetString("Message6"), MessageBoxButtons.OK, MessageBoxIcon.Error);
                Log.Error(errorMsg + LogHelper.Format(ex));
            }
            finally
            {
                Cursor = Cursors.Default;
            }
        }