ParsePush(Object[] data) { int index; GXDLMSObject obj; object value; DataType dt; List <KeyValuePair <GXDLMSObject, int> > items = new List <KeyValuePair <GXDLMSObject, int> >(); if (data != null) { GXDLMSConverter c = new GXDLMSConverter(); GXDLMSObjectCollection objects = new GXDLMSObjectCollection(); foreach (Object it in (Object[])data[0]) { Object[] tmp = (Object[])it; int classID = ((UInt16)(tmp[0])) & 0xFFFF; if (classID > 0) { GXDLMSObject comp; comp = this.Objects.FindByLN((ObjectType)classID, GXCommon.ToLogicalName(tmp[1] as byte[])); if (comp == null) { comp = GXDLMSClient.CreateDLMSObject(classID, 0, 0, tmp[1], null); c.UpdateOBISCodeInformation(comp); objects.Add(comp); } if ((comp is IGXDLMSBase)) { items.Add(new KeyValuePair <GXDLMSObject, int>(comp, (sbyte)tmp[2])); } else { System.Diagnostics.Debug.WriteLine(string.Format("Unknown object : {0} {1}", classID, GXCommon.ToLogicalName((byte[])tmp[1]))); } } } for (int pos = 0; pos < data.Length; ++pos) { obj = items[pos].Key as GXDLMSObject; value = data[pos]; index = items[pos].Value; if (value is byte[] && (dt = obj.GetUIDataType(index)) != DataType.None) { value = GXDLMSClient.ChangeType(value as byte[], dt, Settings.UseUtc2NormalTime); } ValueEventArgs e = new ValueEventArgs(Settings, obj, index, 0, null); e.Value = value; (obj as IGXDLMSBase).SetValue(Settings, e); } } return(items); }
/// <summary> /// Returns collection of push objects. /// </summary> /// <param name="data">Received data.</param> /// <returns>Array of objects and called indexes.</returns> public List <KeyValuePair <GXDLMSObject, int> > ParsePushObjects(GXByteBuffer data) { int index; GXDLMSObject obj; object value; DataType dt; GXReplyData reply = new GXReplyData(); reply.Data = data; List <KeyValuePair <GXDLMSObject, int> > items = new List <KeyValuePair <GXDLMSObject, int> >(); GXDLMS.GetValueFromData(Settings, reply); Object[] list = (Object[])reply.Value; GXDLMSObjectCollection objects = new GXDLMSObjectCollection(); foreach (Object it in (Object[])list[0]) { Object[] tmp = (Object[])it; int classID = ((UInt16)(tmp[0])) & 0xFFFF; if (classID > 0) { GXDLMSObject comp; comp = this.Objects.FindByLN((ObjectType)classID, GXDLMSObject.ToLogicalName(tmp[1] as byte[])); if (comp == null) { comp = GXDLMSClient.CreateDLMSObject(classID, 0, 0, tmp[1], null); objects.Add(comp); } if ((comp is IGXDLMSBase)) { items.Add(new KeyValuePair <GXDLMSObject, int>(comp, (sbyte)tmp[2])); } else { System.Diagnostics.Debug.WriteLine(string.Format("Unknown object : {0} {1}", classID, GXDLMSObject.ToLogicalName((byte[])tmp[1]))); } } } for (int pos = 0; pos < list.Length; ++pos) { obj = items[pos].Key as GXDLMSObject; value = list[pos]; index = items[pos].Value; if (value is byte[] && (dt = obj.GetUIDataType(index)) != DataType.None) { value = GXDLMSClient.ChangeType(value as byte[], dt); } (obj as IGXDLMSBase).SetValue(Settings, index, value); } return(items); }
/// <summary> /// Reserved for internal use. /// </summary> GXDLMSObjectCollection ParseSNObjects(byte[] buff, bool onlyKnownObjects) { int index = 0; //Get array tag. byte size = buff[index++]; //Check that data is in the array if (size != 0x01) { throw new GXDLMSException("Invalid response."); } GXDLMSObjectCollection items = new GXDLMSObjectCollection(this); long cnt = GXCommon.GetObjectCount(buff, ref index); int total, count; for (long objPos = 0; objPos != cnt; ++objPos) { DataType dt = DataType.None; int cachePosition = 0; object[] objects = (object[])GXCommon.GetData(buff, ref index, ActionType.None, out total, out count, ref dt, ref cachePosition); if (index == -1) { throw new OutOfMemoryException(); } if (objects.Length != 4) { throw new GXDLMSException("Invalid structure format."); } int ot = Convert.ToInt16(objects[1]); int baseName = Convert.ToInt32(objects[0]) & 0xFFFF; if (!onlyKnownObjects || GXDLMS.AvailableObjectTypes.ContainsKey((ObjectType)ot)) { if (baseName > 0) { GXDLMSObject comp = CreateDLMSObject(ot, objects[2], baseName, objects[3], null); if (comp != null) { items.Add(comp); } } } else { System.Diagnostics.Debug.WriteLine(string.Format("Unknown object : {0} {1}", ot, baseName)); } } return items; }
/// <summary> /// Parse data columns. /// </summary> /// <param name="data"></param> /// <returns></returns> public List<GXKeyValuePair<GXDLMSObject, GXDLMSCaptureObject>> ParseColumns(byte[] data) { if (data == null) { throw new GXDLMSException("Invalid parameter."); } int index = 0; byte size = data[index++]; //Check that data is in the array. if (size != 0x01) { throw new GXDLMSException("Invalid response."); } //get object count int cnt = GXCommon.GetObjectCount(data, ref index); int objectCnt = 0; GXDLMSObjectCollection items = new GXDLMSObjectCollection(); int total, count; List<GXKeyValuePair<GXDLMSObject, GXDLMSCaptureObject>> list = new List<GXKeyValuePair<GXDLMSObject, GXDLMSCaptureObject>>(); while (index != data.Length && cnt != objectCnt) { DataType type = DataType.None; int cachePosition = 0; object[] objects = (object[])GXCommon.GetData(data, ref index, ActionType.None, out total, out count, ref type, ref cachePosition); if (index == -1) { throw new OutOfMemoryException(); } if (objects.Length != 4) { throw new GXDLMSException("Invalid structure format."); } ++objectCnt; GXDLMSObject comp = CreateDLMSObject(Convert.ToInt16(objects[0]), null, 0, objects[1], 0); int attributeIndex = Convert.ToInt16(objects[2]); if (comp != null) { items.Add(comp); list.Add(new GXKeyValuePair<GXDLMSObject, GXDLMSCaptureObject>(comp, new GXDLMSCaptureObject(attributeIndex, Convert.ToInt16(objects[3])))); } //Update data type and scaler unit if register. if (Objects != null) { GXDLMSObject tmp = Objects.FindByLN(comp.ObjectType, comp.LogicalName); if (tmp != null) { if (comp is GXDLMSRegister) { //Some meters return zero. if (attributeIndex == 0) { attributeIndex = 2; } comp.SetUIDataType(attributeIndex, tmp.GetUIDataType(attributeIndex)); (comp as GXDLMSRegister).Scaler = (tmp as GXDLMSRegister).Scaler; (comp as GXDLMSRegister).Unit = (tmp as GXDLMSRegister).Unit; } } } } UpdateOBISCodes(items); return list; }
/// <summary> /// Reserved for internal use. /// </summary> GXDLMSObjectCollection ParseLNObjects(byte[] buff, bool onlyKnownObjects) { int index = 0; byte size = buff[index++]; //Check that data is in the array. if (size != 0x01) { throw new GXDLMSException("Invalid response."); } //get object count int cnt = GXCommon.GetObjectCount(buff, ref index); int objectCnt = 0; GXDLMSObjectCollection items = new GXDLMSObjectCollection(this); int total, count; //Some meters give wrong item count. while (index != buff.Length && cnt != objectCnt) { DataType dt = DataType.None; int cachePosition = 0; object[] objects = (object[])GXCommon.GetData(buff, ref index, ActionType.None, out total, out count, ref dt, ref cachePosition); if (index == -1) { throw new OutOfMemoryException(); } if (objects.Length != 4) { throw new GXDLMSException("Invalid structure format."); } ++objectCnt; int ot = Convert.ToInt16(objects[0]); if (!onlyKnownObjects || GXDLMS.AvailableObjectTypes.ContainsKey((ObjectType) ot)) { GXDLMSObject comp = CreateDLMSObject(ot, objects[1], 0, objects[2], objects[3]); items.Add(comp); } else { System.Diagnostics.Debug.WriteLine(string.Format("Unknown object : {0} {1}", ot, GXDLMSObject.toLogicalName((byte[])objects[2]))); } } return items; }
/// <summary> /// Delete selected object. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void DeleteMnu_Click(object sender, EventArgs e) { try { if (ObjectTree.Focused) { TreeNode node = ObjectTree.SelectedNode; if (node != null) { object obj = node.Tag; if (obj is GXDLMSDevice) { if (MessageBox.Show(this, GXDLMSDirector.Properties.Resources.RemoveDeviceConfirmation, GXDLMSDirector.Properties.Resources.GXDLMSDirectorTxt, MessageBoxButtons.YesNoCancel) != DialogResult.Yes) { return; } RemoveDevice(obj as GXDLMSDevice); } else if (obj is GXDLMSObject) { if (MessageBox.Show(this, GXDLMSDirector.Properties.Resources.RemoveObjectConfirmation, GXDLMSDirector.Properties.Resources.GXDLMSDirectorTxt, MessageBoxButtons.YesNoCancel) != DialogResult.Yes) { return; } RemoveObject(obj); } else if (obj is GXDLMSObjectCollection) { if (MessageBox.Show(this, GXDLMSDirector.Properties.Resources.RemoveObjectConfirmation, GXDLMSDirector.Properties.Resources.GXDLMSDirectorTxt, MessageBoxButtons.YesNoCancel) != DialogResult.Yes) { return; } RemoveObject(obj); } //Find selected device from the device list. else if (obj is GXDLMSDeviceCollection) { if (MessageBox.Show(this, GXDLMSDirector.Properties.Resources.RemoveDeviceConfirmation, GXDLMSDirector.Properties.Resources.GXDLMSDirectorTxt, MessageBoxButtons.YesNoCancel) != DialogResult.Yes) { return; } foreach (ListViewItem it in DeviceList.SelectedItems) { RemoveDevice(it.Tag as GXDLMSDevice); } } } } else if (ObjectList.Focused && ObjectList.SelectedItems.Count != 0) { if (MessageBox.Show(this, GXDLMSDirector.Properties.Resources.RemoveObjectConfirmation, GXDLMSDirector.Properties.Resources.GXDLMSDirectorTxt, MessageBoxButtons.YesNoCancel) != DialogResult.Yes) { return; } GXDLMSObjectCollection items = new GXDLMSObjectCollection(); foreach (ListViewItem it in ObjectList.SelectedItems) { items.Add(it.Tag as GXDLMSObject); } foreach (GXDLMSObject it in items) { RemoveObject(it); } } SetDirty(true); } catch (Exception Ex) { GXDLMS.Common.Error.ShowError(this, Ex); } }
/// <summary> /// Update object tree. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void refreshToolStripMenuItem_Click(object sender, EventArgs e) { if (tabControl1.SelectedIndex == 0) { TransactionWork = new GXAsyncWork(this, OnAsyncStateChange, Refresh, new object[] { ObjectTree.SelectedNode.Tag }); } else { GXDLMSObjectCollection items = new GXDLMSObjectCollection(); foreach (ListViewItem it in ObjectList.SelectedItems) { items.Add(it.Tag as GXDLMSObject); } TransactionWork = new GXAsyncWork(this, OnAsyncStateChange, Refresh, new object[] { items }); } TransactionWork.Start(); }
/// <summary> /// Read selected DLMS object. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void ReadMnu_Click(object sender, EventArgs e) { if (tabControl1.SelectedIndex == 0) { //Set focus to the tree or Read count do not updated. this.ObjectTree.Focus(); if (this.ObjectTree.SelectedNode != null) { TransactionWork = new GXAsyncWork(this, OnAsyncStateChange, Read, new object[] { this.ObjectTree.SelectedNode.Tag, SelectedView }); TransactionWork.Start(); } } else { if (this.ObjectList.SelectedItems.Count != 0) { GXDLMSObjectCollection items = new GXDLMSObjectCollection(); foreach (ListViewItem it in this.ObjectList.SelectedItems) { items.Add(it.Tag as GXDLMSObject); } TransactionWork = new GXAsyncWork(this, OnAsyncStateChange, Read, new object[] { items, SelectedView }); TransactionWork.Start(); } } }
ParsePush(Object[] data) { int index; GXDLMSObject obj; object value; DataType dt; List<KeyValuePair<GXDLMSObject, int>> items = new List<KeyValuePair<GXDLMSObject, int>>(); if (data != null) { GXDLMSConverter c = new GXDLMSConverter(); GXDLMSObjectCollection objects = new GXDLMSObjectCollection(); foreach (Object it in (Object[])data[0]) { Object[] tmp = (Object[])it; int classID = ((UInt16)(tmp[0])) & 0xFFFF; if (classID > 0) { GXDLMSObject comp; comp = this.Objects.FindByLN((ObjectType)classID, GXDLMSObject.ToLogicalName(tmp[1] as byte[])); if (comp == null) { comp = GXDLMSClient.CreateDLMSObject(classID, 0, 0, tmp[1], null); c.UpdateOBISCodeInformation(comp); objects.Add(comp); } if ((comp is IGXDLMSBase)) { items.Add(new KeyValuePair<GXDLMSObject, int>(comp, (sbyte)tmp[2])); } else { System.Diagnostics.Debug.WriteLine(string.Format("Unknown object : {0} {1}", classID, GXDLMSObject.ToLogicalName((byte[])tmp[1]))); } } } for (int pos = 0; pos < data.Length; ++pos) { obj = items[pos].Key as GXDLMSObject; value = data[pos]; index = items[pos].Value; if (value is byte[] && (dt = obj.GetUIDataType(index)) != DataType.None) { value = GXDLMSClient.ChangeType(value as byte[], dt); } ValueEventArgs e = new ValueEventArgs(Settings, obj, index, 0, null); e.Value = value; (obj as IGXDLMSBase).SetValue(Settings, e); } } return items; }
/// <summary> /// After UpdateObjects call objects can be read using Objects property. /// </summary> public void UpdateObjects() { try { GXDLMSObjectCollection objs = Comm.GetObjects(); objs.Tag = this; int pos = 0; foreach (GXDLMSObject it in objs) { //Profile Generic objects are added later. if (it.ObjectType == ObjectType.ProfileGeneric) { continue; } if (it.GetType() == typeof(GXDLMSObject)) { continue; } ++pos; NotifyProgress(this, "Creating object " + it.LogicalName, pos, objs.Count); m_Objects.Add(it); } GXLogWriter.WriteLog("--- Created " + m_Objects.Count.ToString() + " objects. ---"); int objPos = 0; //Read registers units and scalers. int cnt = Objects.Count; GXLogWriter.WriteLog("--- Reading scalers and units. ---"); for (pos = 0; pos != cnt; ++pos) { GXDLMSObject it = Objects[pos]; it.UpdateDefaultValueItems(); this.OnProgress(this, "Reading scalers and units.", pos + 1, cnt); if (it is GXDLMSRegister) { object data = it.ShortName; if (it.ShortName == 0) { data = it.LogicalName; } //Read scaler first. DataType type = DataType.None; try { data = Comm.ReadValue(data, it.ObjectType, 3, ref type); object tmp = GXHelpers.ConvertFromDLMS(data, DataType.None, DataType.None, false); //Actaris ACE 6000 is returning wrong value here. if (tmp is object[]) { object[] scalerUnit = (object[])tmp; ((GXDLMSRegister)it).Scaler = Math.Pow(10, Convert.ToInt32(scalerUnit.GetValue(0))); ((GXDLMSRegister)it).Unit = (Unit)Convert.ToInt32(scalerUnit.GetValue(1)); } } catch (Exception ex) { GXLogWriter.WriteLog(ex.Message); UpdateError(it, 3, ex); if (ex is GXDLMSException) { continue; } throw ex; } } if (it is GXDLMSDemandRegister) { object name = it.ShortName; object data; if (it.ShortName == 0) { name = it.LogicalName; } //Read scaler first. DataType type = DataType.None; byte attributeOrder = 4; try { data = Comm.ReadValue(name, it.ObjectType, attributeOrder, ref type); Array scalerUnit = (Array)GXHelpers.ConvertFromDLMS(data, DataType.None, DataType.None, false); ((GXDLMSDemandRegister)it).Scaler = Math.Pow(10, Convert.ToInt32(scalerUnit.GetValue(0))); ((GXDLMSDemandRegister)it).Unit = (Unit)Convert.ToInt32(scalerUnit.GetValue(1)); //Read Period data = Comm.ReadValue(name, it.ObjectType, 8, ref type); ((GXDLMSDemandRegister)it).Period = Convert.ToUInt64(data); //Read number of periods data = Comm.ReadValue(name, it.ObjectType, 9, ref type); ((GXDLMSDemandRegister)it).NumberOfPeriods = Convert.ToUInt32(data); } catch (Exception ex) { GXLogWriter.WriteLog(ex.Message); UpdateError(it, 3, ex); if (ex is GXDLMSException) { continue; } throw ex; } } } GXLogWriter.WriteLog("--- Reading scalers and units end. ---"); /* TODO: * if (!m.UseLogicalNameReferencing) * { * GXLogWriter.WriteLog("--- Reading Access rights. ---"); * try * { * foreach (GXDLMSAssociationShortName sn in dev.Objects.GetObjects(ObjectType.AssociationShortName)) * { * dev.Comm.Read(sn, 3); * } * } * catch (Exception ex) * { * GXLogWriter.WriteLog(ex.Message); * } * GXLogWriter.WriteLog("--- Reading Access rights end. ---"); * } * */ this.OnProgress(this, "Reading scalers and units.", cnt, cnt); foreach (Gurux.DLMS.Objects.GXDLMSProfileGeneric it in objs.GetObjects(ObjectType.ProfileGeneric)) { ++pos; NotifyProgress(this, "Creating object " + it.LogicalName, pos, objs.Count); //Read Profile Generic Columns. try { NotifyProgress(this, "Get profile generic columns", ++objPos, objs.Count); UpdateColumns(it, Manufacturers.FindByIdentification(Manufacturer)); if (it.CaptureObjects == null || it.CaptureObjects.Count == 0) { continue; } } catch { GXLogWriter.WriteLog(string.Format("Failed to read Profile Generic {0} columns.", it.LogicalName)); continue; } m_Objects.Add(it); } } finally { NotifyProgress(this, "", 0, 0); } }
/// <summary> /// After UpdateObjects call objects can be read using Objects property. /// </summary> public void UpdateObjects() { try { GXDLMSObjectCollection objs = Comm.GetObjects(); objs.Tag = this; int pos = 0; foreach (GXDLMSObject it in objs) { ++pos; NotifyProgress(this, "Creating object " + it.LogicalName, pos, objs.Count); m_Objects.Add(it); } GXLogWriter.WriteLog("--- Created " + m_Objects.Count.ToString() + " objects. ---"); //Read registers units and scalers. int cnt = Objects.Count; GXLogWriter.WriteLog("--- Reading scalers and units. ---"); for (pos = 0; pos != cnt; ++pos) { GXDLMSObject it = Objects[pos]; this.OnProgress(this, "Reading scalers and units.", cnt + pos + 1, 3 * cnt); if (it is GXDLMSRegister) { //Read scaler first. try { Comm.ReadValue(it, 3); } catch (Exception ex) { GXLogWriter.WriteLog(ex.Message); UpdateError(it, 3, ex); if (ex is GXDLMSException) { continue; } throw ex; } } if (it is GXDLMSDemandRegister) { try { //Read scaler first. Comm.ReadValue(it, 4); //Read Period Comm.ReadValue(it, 8); //Read number of periods Comm.ReadValue(it, 9); } catch (Exception ex) { GXLogWriter.WriteLog(ex.Message); UpdateError(it, 3, ex); if (ex is GXDLMSException) { continue; } throw ex; } } } GXLogWriter.WriteLog("--- Reading scalers and units end. ---"); if (!UseLogicalNameReferencing) { GXLogWriter.WriteLog("--- Reading Access rights. ---"); try { foreach (GXDLMSAssociationShortName sn in Objects.GetObjects(ObjectType.AssociationShortName)) { Comm.ReadValue(sn, 3); } } catch (Exception ex) { GXLogWriter.WriteLog(ex.Message); } GXLogWriter.WriteLog("--- Reading Access rights end. ---"); } this.OnProgress(this, "Reading profile generic columns.", cnt, cnt); foreach (Gurux.DLMS.Objects.GXDLMSProfileGeneric it in objs.GetObjects(ObjectType.ProfileGeneric)) { ++pos; //Read Profile Generic Columns. try { NotifyProgress(this, "Get profile generic columns", (2 * cnt) + pos, 3 * objs.Count); UpdateColumns(it, Manufacturers.FindByIdentification(Manufacturer)); if (it.CaptureObjects == null || it.CaptureObjects.Count == 0) { continue; } } catch { GXLogWriter.WriteLog(string.Format("Failed to read Profile Generic {0} columns.", it.LogicalName)); continue; } } } finally { NotifyProgress(this, "", 0, 0); } }