private void SetCurrentGroup()
        {
            m_CurrentOPCGroup = m_OPCGroups.FindLast(p => p.Type == (Type)comboBoxEvents.SelectedItem);

            if (m_CurrentOPCGroup == null)
            {
                CurrentStatus = WorkStatus.NoGroupExist;
            }
        }
 private void FillGroupDataFromControls(ref Group group)
 {
     group.Name = comboBoxGroups.Text;
     group.Location = textBoxGroupLocation.Text;
     group.Destination = textBoxGroupDestination.Text;
     group.Type = (Type)comboBoxEvents.SelectedItem;
     group.FilterPropertyName = string.IsNullOrEmpty((string)comboBoxGroupFilterPropertyName.SelectedItem) ? "" : (string)comboBoxGroupFilterPropertyName.SelectedItem;
     group.FilterPropertyValue = textBoxGroupFilterPropertyValue.Text;
     group.IsWriteble = checkBoxIsWriteble.Checked;
     group.Event = (BaseEvent)Activator.CreateInstance(group.Type);
 }
 private string GetNextGroupName(Group opcGroup)
 {
     int number;
     if (opcGroup == null)
     {
         return string.Format("PLC{0}1", ((Type)comboBoxEvents.SelectedItem).Name);
     }
     try
     {
         string eventNumber = opcGroup.Name.Substring(opcGroup.Name.IndexOf("Event"));
         number = int.Parse(eventNumber.Remove(0, "Event".Length));
         return opcGroup.Name.Replace(eventNumber, "Event" + (number + 1).ToString());
     }
     catch
     {
         number = 0;
         return string.Format("PLC{0}{1}", ((Type)comboBoxEvents.SelectedItem).Name, 1);
     }
 }
 private void comboBoxGroups_SelectedIndexChanged(object sender, EventArgs e)
 {
     m_CurrentOPCGroup = m_OPCGroups.Find(p => p.Name == (string)comboBoxGroups.SelectedItem);
     if (m_CurrentOPCGroup == null)
     {
         CurrentStatus = WorkStatus.NoGroupExist;
     }
     else
     {
         CurrentStatus = WorkStatus.EditGroup;
     }
 }
        private void comboBoxGroups_Leave(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(comboBoxEvents.Text) && !string.IsNullOrEmpty(textBoxGroupLocation.Text) &&
                !string.IsNullOrEmpty(textBoxGroupDestination.Text) && CurrentStatus == WorkStatus.AddGroup)
            {
                m_CurrentOPCGroup = new Group();
                FillGroupDataFromControls(ref m_CurrentOPCGroup);
                m_OPCGroups.Add(m_CurrentOPCGroup);
                comboBoxGroups.Items.Add(m_CurrentOPCGroup.Name);
                CurrentStatus = WorkStatus.EditGroup;
            }

            if (CurrentStatus == WorkStatus.EditGroup)
            {
                FillGroupDataFromControls(ref m_CurrentOPCGroup);
            }
        }
        private void buttonRemoveGroup_Click(object sender, EventArgs e)
        {
            if (CurrentStatus == WorkStatus.EditGroup || CurrentStatus == WorkStatus.EditProperty)
            {
                m_OPCGroups.Remove(m_CurrentOPCGroup);
                comboBoxGroups.Items.Remove(m_CurrentOPCGroup.Name);
            }

            m_CurrentOPCGroup = m_OPCGroups.FindLast(p => p.Type == (Type)comboBoxEvents.SelectedItem);

            if (m_CurrentOPCGroup == null)
            {
                CurrentStatus = WorkStatus.NoGroupExist;
            }
            else
            {
                comboBoxGroups.SelectedItem = m_CurrentOPCGroup.Name;
            }
        }
        private static ICollection<Group> GetGroupFromAttribytes(Type type)
        {
            List<Group> groups = new List<Group>();
            var data = type.GetCustomAttributes(false).Where(x => x.GetType().Name == "PLCGroup").ToArray();
            foreach (PLCGroup plcg in data)
            {
                if (plcg == null) continue;

                Group group = new Group();
                group.Name = plcg.Name;
                group.Destination = plcg.Destination;
                group.Location = plcg.Location;
                group.FilterPropertyName = plcg.FilterPropertyName;
                group.FilterPropertyValue = plcg.FilterPropertyValue;
                group.Type = type;

                group.Event = (BaseEvent)Activator.CreateInstance(group.Type);
                int handleClientPoint = 0;

                foreach (PropertyInfo property in type.GetProperties())
                {
                    PLCPoint plcp = (PLCPoint)property.GetCustomAttributes(false).Where(x => x.GetType().Name == "PLCPoint").FirstOrDefault();
                    if (plcp == null) continue;
                    OPC.Point newPoint = new OPC.Point();
                    newPoint.FieldName = property.Name;
                    newPoint.Location = plcp.Location;
                    newPoint.Encoding = plcp.Encoding;
                    newPoint.Type = property.PropertyType;
                    newPoint.IsBoolean = plcp.IsBoolean;
                    newPoint.BitNumber = plcp.BitNumber;
                    group.Points.Add(newPoint);
                    //group.ItemDefs.Add(new OPCItemDef("S7:[" + group.Location + "]" + newPoint.OPCLocation, true, handleClientPoint, VarEnum.VT_EMPTY));
                    group.ItemDefs.Add(new OPCItemDef(group.Location + "." + newPoint.Location.Replace(',', '.'), true, handleClientPoint, VarEnum.VT_EMPTY));
                    handleClientPoint++;
                }
                groups.Add(group);
            }

            return groups;
        }
        public static ICollection<Group> LoadGroupsFromXmlFile(string fileName)
        {
            // Создаем экземпляр класса
            XmlDocument xmlDoc = new XmlDocument();
            Type[] eventTypes = null;
            // Загружаем XML-документ из файла
            xmlDoc.Load(fileName);
            try
            {
                Assembly a = null;
                InstantLogger.log(string.Format(@"{0}\{1}", xmlDoc.DocumentElement.Attributes["Path"].Value, xmlDoc.DocumentElement.Attributes["Name"].Value));
                a = Assembly.LoadFrom(string.Format(@"{0}\{1}", xmlDoc.DocumentElement.Attributes["Path"].Value, xmlDoc.DocumentElement.Attributes["Name"].Value));
                eventTypes = BaseEvent.GetEvents();
            }
            catch
            {
                return new List<Group>();
            }
            InstantLogger.log(string.Format("eventtypes is null {0} count {1}", eventTypes == null, eventTypes.Count()));

            // Получаем всех детей корневого элемента
            // xmlDoc.DocumentElement - корневой элемент

            List<Group> resultGroups = new List<Group>();
            foreach (XmlNode table in xmlDoc.DocumentElement.ChildNodes)
            {
                // перебираем все атрибуты элемента
                Group group = new Group()
                {
                    Name = table.Attributes["Name"].Value,
                    Type = eventTypes.FirstOrDefault(p => p.Name == table.Attributes["Type"].Value),
                    Location = table.Attributes["Location"].Value,
                    Destination = table.Attributes["Destination"].Value,
                    FilterPropertyName = table.Attributes["FilterPropertyName"].Value,
                    FilterPropertyValue = table.Attributes["FilterPropertyValue"].Value,
                    IsWriteble = bool.Parse(table.Attributes["IsWriteble"].Value)
                };
                group.Event = (BaseEvent)Activator.CreateInstance(group.Type);
                int handleClientPoint = 0;
                // перебираем всех детей текущего узла
                foreach (XmlNode ch in table.ChildNodes)
                {
                    if (ch.Name == "Point")
                    {
                        Point newPoint = new Point();
                        newPoint.FieldName = ch.Attributes["Name"].Value;
                        newPoint.Location = ch.Attributes["Location"].Value;
                        newPoint.Encoding = ch.Attributes["Encoding"].Value;
                        newPoint.Type = (group.Type.GetProperties().Select(a => a.PropertyType).FirstOrDefault(p => p.Name == ch.Attributes["Type"].Value));
                        newPoint.IsBoolean = bool.Parse(ch.Attributes["IsBoolean"].Value);
                        newPoint.BitNumber = int.Parse(ch.Attributes["BitNumber"].Value);
                        group.Points.Add(newPoint);
                        //group.ItemDefs.Add(new OPCItemDef("S7:[" + group.Location + "]" + newPoint.OPCLocation, true, handleClientPoint, VarEnum.VT_EMPTY));
                        group.ItemDefs.Add(new OPCItemDef(group.Location + "." + newPoint.Location.Replace(',', '.'), true, handleClientPoint, VarEnum.VT_EMPTY));

                        handleClientPoint++;
                    }
                }
                resultGroups.Add(group);
            }
            return resultGroups;
        }