/// <summary> /// Initialize server. /// </summary> /// <remarks> /// This must call after server objects are set. /// </remarks> public void Initialize() { if (Authentications.Count == 0) { throw new Exception("Authentications is not set."); } 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; pg.Parent.Parent = this; 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. { System.Diagnostics.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); } } } }
/// <summary> /// Initialize server. /// </summary> /// <remarks> /// This must call after server objects are set. /// </remarks> public void Initialize() { bool association = false; Initialized = true; 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; 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.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) { if ((it as GXDLMSAssociationShortName).ObjectList.Count == 0) { (it as GXDLMSAssociationShortName).ObjectList.AddRange(this.Items); } association = true; } else if (it is GXDLMSAssociationLogicalName && this.UseLogicalNameReferencing) { if ((it as GXDLMSAssociationLogicalName).ObjectList.Count == 0) { (it as GXDLMSAssociationLogicalName).ObjectList.AddRange(this.Items); } association = true; } else if (it is GXDLMSHdlcSetup) { hdlcSetup = it as GXDLMSHdlcSetup; } 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. int sn = 0xA0; int offset, count; if (!this.UseLogicalNameReferencing) { foreach (GXDLMSObject it in Items) { //Generate Short Name if not given. if (it.ShortName == 0) { it.ShortName = (ushort)sn; //Add method index addresses. GXDLMS.GetActionInfo(it.ObjectType, out offset, out count); if (count != 0) { sn += offset + (8 * count); } else //If there are no methods. { //Add attribute index addresses. sn += 8 * (it as IGXDLMSBase).GetAttributeCount(); } } } } }