/// <summary> /// Initialize server. /// </summary> /// <remarks> /// This must call after server objects are set. /// </remarks> public void Initialize() { bool association = false; Initialized = true; if (SortedItems.Count != Items.Count) { for (int pos = 0; pos != Items.Count; ++pos) { GXDLMSObject it = Items[pos]; if (this.UseLogicalNameReferencing && (string.IsNullOrEmpty(it.LogicalName) || it.LogicalName.Split('.').Length != 6)) { throw new Exception("Invalid Logical Name."); } if (it is GXDLMSProfileGeneric) { GXDLMSProfileGeneric pg = it as GXDLMSProfileGeneric; if (pg.ProfileEntries < 1) { throw new Exception("Invalid Profile Entries. Profile entries tells amount of rows in the table."); } foreach (var obj in pg.CaptureObjects) { if (obj.Value.AttributeIndex < 1) { throw new Exception("Invalid attribute index. SelectedAttributeIndex is not set for " + obj.Key.Name); } } if (pg.ProfileEntries < 1) { throw new Exception("Invalid Profile Entries. Profile entries tells amount of rows in the table."); } if (pg.CapturePeriod != 0) { GXProfileGenericUpdater p = new GXProfileGenericUpdater(this, pg); Thread thread = new Thread(new ThreadStart(p.UpdateProfileGenericData)); thread.IsBackground = true; thread.Start(); } } else if ((it is GXDLMSAssociationShortName && !this.UseLogicalNameReferencing) || (it is GXDLMSAssociationLogicalName && this.UseLogicalNameReferencing)) { association = true; } else if (!(it is IGXDLMSBase))//Remove unsupported items. { Debug.WriteLine(it.ObjectType.ToString() + " not supported."); Items.RemoveAt(pos); --pos; } } if (!association) { if (UseLogicalNameReferencing) { GXDLMSAssociationLogicalName it = new GXDLMSAssociationLogicalName(); it.ObjectList = this.Items; Items.Add(it); } else { GXDLMSAssociationShortName it = new GXDLMSAssociationShortName(); it.ObjectList = this.Items; Items.Add(it); } } //Arrange items by Short Name. ushort sn = 0xA0; if (!this.UseLogicalNameReferencing) { SortedItems.Clear(); foreach (GXDLMSObject it in Items) { if (it is GXDLMSAssociationShortName) { it.ShortName = 0xFA00; } //Generate Short Name if not given. if (it.ShortName == 0) { do { it.ShortName = sn; sn += 0xA0; }while (SortedItems.ContainsKey(it.ShortName)); } SortedItems.Add(it.ShortName, it); } } } }