Esempio n. 1
0
        public static List <ImGroupAddr> ParseOpcFile(string opcFile, BackgroundWorker worker)
        {
            List <ImGroupAddr> listAddr = new List <ImGroupAddr>();

            string[] lines = File.ReadAllLines(opcFile, System.Text.Encoding.Default);
            foreach (string line in lines)
            {
                string[] elements = line.Split('\t');
                if (elements.Length >= 4)
                {
                    ImGroupAddr addr = new ImGroupAddr();
                    addr.Id = Guid.NewGuid().ToString();
                    addr.IsCommunication = true;
                    addr.IsRead          = true;
                    addr.IsTransmit      = true;
                    addr.IsUpgrade       = true;
                    addr.IsWrite         = true;

                    string[] group = elements[0].Split('.');
                    if (group.Length >= 3)
                    {
                        addr.KnxAddress = group[2];
                    }

                    addr.Name = elements[1];

                    worker.ReportProgress(0, addr.Name);

                    int start = elements[2].IndexOf('(');
                    int end   = elements[2].IndexOf(')');
                    if ((start > 0) && (end > start))
                    {
                        string type = elements[2].Substring(start + 1, end - (start + 1));
                        addr.DPTName = DPTHelper.GetDPTMainName(KNXAddressHelper.GetKnxDataType(type));
                        if (addr.DPTName.Length > 0)
                        {
                            //addr.IsSelected = true;
                            addr.DPTNameIsDetermined = false;
                        }
                    }

                    var formName = typeof(FrmGroupAddressMgt).Name;
                    if (Application.OpenForms[formName] != null)
                    {
                        var frm = Application.OpenForms[formName] as FrmGroupAddressMgt;
                        if (frm != null)
                        {
                            if (frm.AddressIsExsit(addr.KnxAddress))
                            {
                                addr.IsSelected = false;
                            }
                            else
                            {
                                addr.IsSelected = true;
                            }
                        }
                    }

                    addr.Priority = KNXAddressHelper.GetKNXPriority(elements[3]);

                    listAddr.Add(addr);
                }
            }

            return(listAddr);
        }
Esempio n. 2
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, UIResMang.GetString("TextIsUnziping"));

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

                worker.ReportProgress(0, UIResMang.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,
                        ComObjectInstance = xElement.Parent,
                        //ComObjectInstanceRefId = xElement.Parent.Attribute(strRefId).Value,
                    }).ToLookup(
                        p => p.GroupAddressRefId,
                        p => p.ComObjectInstance
                        //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;
                        parseDatapointType(addr);

                        float f = i++ / len;
                        worker.ReportProgress((int)(f * 100), UIResMang.GetString("TextIsImportingGroupAddress") + " " + address.Name + " " + addr.KnxAddress);

                        if (comObjectInstanceRef.Contains(address.Id))
                        {
                            XElement comObjectInstance = comObjectInstanceRef[address.Id].First <XElement>();
                            if ((null == addr.DPTName) || (addr.DPTName.Trim().Length <= 0) || (addr.DPTName.Contains("*")))
                            {
                                if (null != comObjectInstance.Attribute(attrDatapointType))
                                {
                                    addr.DPTName = comObjectInstance.Attribute(attrDatapointType).Value;
                                    parseDatapointType(addr);
                                }
                            }
                            var comObjectInstanceRefId = comObjectInstance.Attribute(strRefId).Value;

                            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);
                            var formName = typeof(FrmGroupAddressMgt).Name;
                            if (Application.OpenForms[formName] != null)
                            {
                                var frm = Application.OpenForms[formName] as FrmGroupAddressMgt;
                                if (frm != null)
                                {
                                    if (frm.AddressIsExsit(addr.KnxAddress))
                                    {
                                        addr.IsSelected = false;
                                    }
                                }
                            }
                            //if (ProjResManager.AddressIsExsit(addr.KnxAddress))
                            //{
                            //    addr.IsSelected = false;
                            //}
                            listAddress.Add(addr);
                        }
                        else
                        {
                            //parseDatapointType(addr);
                            //addr.IsSelected = false;
                        }

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

                        //listAddress.Add(addr);
                    }
                }
            }

            return(listAddress);
        }
Esempio n. 3
0
        public static void parseComObject(ImGroupAddr address, XElement comObject)
        {
            var refId             = comObject.Attribute(attrRefId);
            var text              = comObject.Attribute(attrText);
            var priority          = comObject.Attribute(attrPriority);
            var objectSize        = comObject.Attribute(attrObjectSize);
            var readFlag          = comObject.Attribute(attrReadFlag);
            var writeFlag         = comObject.Attribute(attrWriteFlag);
            var communicationFlag = comObject.Attribute(attrCommunicationFlag);
            var transmitFlag      = comObject.Attribute(attrTransmitFlag);
            var updateFlag        = comObject.Attribute(attrUpdateFlag);
            var datapointType     = comObject.Attribute(attrDatapointType);

            //
            //if (null != text)
            //{
            //address.Name = text.Value;
            //}

            if (null != priority)
            {
                address.Priority = KNXAddressHelper.GetKNXPriority(priority.Value);
            }

            if (null != datapointType && null != datapointType.Value)
            {
                if ((null == address.DPTName) || (address.DPTName.Trim().Length <= 0) || (address.DPTName.Contains("*")))
                {
                    address.DPTName = datapointType.Value;
                    parseDatapointType(address);
                }
            }

            if (objectSize != null)
            {
                if ((null == address.DPTName) || (address.DPTName.Trim().Length <= 0) || (address.DPTName.Contains("*")))
                {
                    // 数据类型
                    address.DPTName = DPTHelper.GetDPTMainName(KNXAddressHelper.GetKnxDataType(objectSize.Value));
                    if (address.DPTName.Length > 0)
                    {
                        address.IsSelected          = true;
                        address.DPTNameIsDetermined = false;
                    }
                }
            }

            if (null != readFlag && null != readFlag.Value)
            {
                if ("Disabled" == readFlag.Value)
                {
                    address.IsRead = false;
                }
                else if ("Enabled" == readFlag.Value)
                {
                    address.IsRead = true;
                }
            }

            if (null != writeFlag && null != writeFlag.Value)
            {
                if ("Disabled" == writeFlag.Value)
                {
                    address.IsWrite = false;
                }
                else if ("Enabled" == writeFlag.Value)
                {
                    address.IsWrite = true;
                }
            }

            if (null != communicationFlag && null != communicationFlag.Value)
            {
                if ("Disabled" == communicationFlag.Value)
                {
                    address.IsCommunication = false;
                }
                else if ("Enabled" == communicationFlag.Value)
                {
                    address.IsCommunication = true;
                }
            }

            if (null != transmitFlag && null != transmitFlag.Value)
            {
                if ("Disabled" == transmitFlag.Value)
                {
                    address.IsTransmit = false;
                }
                else if ("Enabled" == transmitFlag.Value)
                {
                    address.IsTransmit = true;
                }
            }

            if (null != updateFlag && null != updateFlag.Value)
            {
                if ("Disabled" == updateFlag.Value)
                {
                    address.IsUpgrade = false;
                }
                else if ("Enabled" == updateFlag.Value)
                {
                    address.IsUpgrade = true;
                }
            }
        }