private void ScheduleEdit_Click(object sender, EventArgs e)
 {
     try
     {
         GXDeviceTemplate       meter     = (GXDeviceTemplate)TemplatesView.SelectedItems[0].Tag;
         GXDeviceTemplate[]     templates = ami.templates;
         DBDevicePropertiesForm dlg       = new DBDevicePropertiesForm(templates, meter);
         if (dlg.ShowDialog(panel1.Parent) == DialogResult.OK)
         {
             ami.UpdateDeviceTemplates(new GXDeviceTemplate[] { meter });
             TemplatesView.SelectedItems[0].Text = meter.Name;
         }
     }
     catch (Exception Ex)
     {
         MessageBox.Show(panel1.Parent, Ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
     }
 }
 private void TemplatesAdd_Click(object sender, EventArgs e)
 {
     try
     {
         OpenFileDialog dlg = new OpenFileDialog();
         dlg.Multiselect      = false;
         dlg.InitialDirectory = Directory.GetCurrentDirectory();
         dlg.Filter           = Properties.Resources.FilterTxt;
         dlg.DefaultExt       = ".gdr";
         dlg.ValidateNames    = true;
         if (dlg.ShowDialog(panel1.Parent) == DialogResult.OK)
         {
             if (File.Exists(dlg.FileName))
             {
                 List <GXDLMSDevice> devices;
                 using (XmlReader reader = XmlReader.Create(dlg.FileName))
                 {
                     List <Type> types = new List <Type>(Gurux.DLMS.GXDLMSClient.GetObjectTypes());
                     types.Add(typeof(GXDLMSAttributeSettings));
                     types.Add(typeof(GXDLMSAttribute));
                     //Version is added to namespace.
                     XmlSerializer x = new XmlSerializer(typeof(List <GXDLMSDevice>), null, types.ToArray(), null, "Gurux1");
                     if (!x.CanDeserialize(reader))
                     {
                         x = new XmlSerializer(typeof(List <GXDLMSDevice>), types.ToArray());
                     }
                     devices = (List <GXDLMSDevice>)x.Deserialize(reader);
                     reader.Close();
                 }
                 GXDeviceTemplate        m         = new GXDeviceTemplate();
                 List <GXDeviceTemplate> templates = new List <GXDeviceTemplate>();
                 foreach (GXDLMSDevice it in devices)
                 {
                     GXDeviceTemplate t = new GXDeviceTemplate();
                     GXDevice.Copy(t, it);
                     if (!string.IsNullOrEmpty(it.Password))
                     {
                         t.Password = ASCIIEncoding.ASCII.GetString(CryptHelper.Decrypt(it.Password, Password.Key));
                     }
                     else if (it.HexPassword != null)
                     {
                         t.Password = GXDLMSTranslator.ToHex(CryptHelper.Decrypt(it.HexPassword, Password.Key));
                     }
                     List <GXObjectTemplate> list = new List <GXObjectTemplate>();
                     foreach (GXDLMSObject value in it.Objects)
                     {
                         string[]         names = ((IGXDLMSBase)value).GetNames();
                         GXObjectTemplate obj   = new GXObjectTemplate();
                         obj.LogicalName = value.LogicalName;
                         obj.ObjectType  = (int)value.ObjectType;
                         obj.Name        = value.Description;
                         obj.Version     = value.Version;
                         obj.ShortName   = value.ShortName;
                         list.Add(obj);
                         for (int pos = 2; pos <= ((IGXDLMSBase)value).GetAttributeCount(); ++pos)
                         {
                             GXAttributeTemplate a = new GXAttributeTemplate();
                             a.Index       = pos;
                             a.AccessLevel = (int)value.GetAccess(pos);
                             a.DataType    = (int)((IGXDLMSBase)value).GetDataType(pos);
                             a.UIDataType  = (int)((GXDLMSObject)value).GetUIDataType(pos);
                             if (value.GetStatic(pos))
                             {
                                 a.ExpirationTime = 0xFFFFFFFF;
                             }
                             //Profile generic capture object read is not allowed.
                             if (value is GXDLMSProfileGeneric && pos == 3)
                             {
                                 a.ExpirationTime = 0xFFFFFFFF;
                             }
                             obj.Attributes.Add(a);
                         }
                         t.Objects = list;
                     }
                     templates.Add(t);
                 }
                 DBDevicePropertiesForm dlg2 = new DBDevicePropertiesForm(templates.ToArray(), m);
                 if (dlg2.ShowDialog(panel1.Parent) == DialogResult.OK)
                 {
                     {
                         ami.AddDeviceTemplates(new GXDLMSMeterBase[] { m });
                         ListViewItem li = TemplatesView.Items.Add(m.Name);
                         li.Tag      = m;
                         li.Selected = true;
                     }
                 }
             }
         }
     }
     catch (Exception Ex)
     {
         MessageBox.Show(panel1.Parent, Ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
     }
 }