void UpdateObject(GXDLMSObject it, GXDLMSCategory item) { item.ShortName = (UInt16)it.ShortName; item.LogicalName = it.LogicalName; if (it.ShortName != 0) { item.Name = string.Format("0x{0} {1} {2}", it.ShortName.ToString("X4"), it.LogicalName, it.Description); } else { item.Name = it.LogicalName + " " + it.Description; } //Update description. item.Description = it.Description; //Update atribute index. for (int pos = 2; pos != (it as IGXDLMSBase).GetAttributeCount() + 1; ++pos) { string name = (it as IGXDLMSBase).GetNames()[pos - 1]; object value = it.GetValues()[pos - 1]; GXDLMSProperty prop; if (((pos == 2 || pos == 3) && it.ObjectType == ObjectType.DemandRegister) || (pos == 2 && it.ObjectType == ObjectType.ExtendedRegister)) { prop = new GXDLMSRegister(); prop.ObjectType = it.ObjectType; prop.LogicalName = it.LogicalName; prop.ShortName = it.ShortName; prop.Name = name; prop.AttributeOrdinal = pos; } else { prop = new GXDLMSProperty(it.ObjectType, it.LogicalName, it.ShortName, name, pos); } item.Properties.Add(prop); prop.DLMSType = it.GetDataType(pos); //Update scaler and unit. if ((pos == 4 && it.ObjectType == ObjectType.DemandRegister) || (pos == 3 && it.ObjectType == ObjectType.ExtendedRegister)) { prop.ValueType = DataType.String; } else { if (value is Enum) { prop.ForcePresetValues = true; foreach (object val in Enum.GetValues(value.GetType())) { prop.Values.Add(new GXValueItem(val.ToString(), (int)val)); } } else { prop.ValueType = it.GetUIDataType(pos); if (prop.ValueType == DataType.None) { prop.ValueType = it.GetDataType(pos); } } } prop.AccessMode = (Gurux.Device.AccessMode)it.GetAccess(pos); } }
public void Write(GXDLMSObject obj, object target, int index, List<object> UpdatedObjects) { object value; GXReplyData reply = new GXReplyData(); for (int it = 1; it != (obj as IGXDLMSBase).GetAttributeCount() + 1; ++it) { reply.Clear(); if (obj.GetDirty(it, out value)) { //Read DLMS data type if not known. DataType type = obj.GetDataType(it); if (type == DataType.None) { byte[] data = client.Read(obj, it)[0]; ReadDataBlock(data, "Write object type " + obj.ObjectType, reply); type = reply.DataType; if (type == DataType.None) { throw new Exception("Failed to write value. Data type not set."); } obj.SetDataType(it, type); } try { foreach (byte[] tmp in client.Write(obj.Name, value, type, obj.ObjectType, it)) { ReadDataBlock(tmp, "Write object", reply); } obj.ClearDirty(it); //Read data once again to make sure it is updated. byte[] data = client.Read(obj, it)[0]; ReadDataBlock(data, "Read object " + obj.ObjectType, reply); value = reply.Value; if (value is byte[] && (type = obj.GetUIDataType(it)) != DataType.None) { value = GXDLMSClient.ChangeType((byte[])value, type); } client.UpdateValue(obj, it, value); } catch (GXDLMSException ex) { if (ex.ErrorCode == 3) { throw new Exception("Read/Write Failed."); } else { throw ex; } } } } }
public object ReadElementContentAsObject(string name, object defaultValue, GXDLMSObject obj, byte index) { GetNext(); if (string.Compare(name, reader.Name, true) == 0) { object ret; DataType uiType; DataType dt = (DataType)Enum.Parse(typeof(DataType), reader.GetAttribute(0)); if (obj != null) { obj.SetDataType(index, dt); } if (reader.AttributeCount > 1) { uiType = (DataType)Enum.Parse(typeof(DataType), reader.GetAttribute(1)); } else { uiType = dt; } if (obj != null && obj.GetUIDataType(index) == DataType.None) { obj.SetUIDataType(index, uiType); } if (dt == DataType.Array || dt == DataType.Structure) { reader.Read(); GetNext(); ret = ReadArray(); ReadEndElement(name); return(ret); } else { string str = reader.ReadElementContentAsString(); ret = GXDLMSConverter.ChangeType(str, uiType, CultureInfo.InvariantCulture); } while (!(reader.NodeType == XmlNodeType.Element || reader.NodeType == XmlNodeType.EndElement)) { reader.Read(); } return(ret); } return(defaultValue); }
/// <summary> /// Read object. /// </summary> /// <param name="InitialValues"></param> /// <param name="obj"></param> /// <param name="attribute"></param> public void Read(object sender, GXDLMSObject obj, int attribute) { GXReplyData reply = new GXReplyData(); foreach (int it in (obj as IGXDLMSBase).GetAttributeIndexToRead()) { reply.Clear(); if (obj is GXDLMSProfileGeneric && it == 2) { if (OnBeforeRead != null) { OnBeforeRead(obj, it); } try { CurrentProfileGeneric = obj as GXDLMSProfileGeneric; OnDataReceived += new GXDLMSCommunicator.DataReceivedEventHandler(this.OnProfileGenericDataReceived); if (CurrentProfileGeneric.AccessSelector == AccessRange.Range || CurrentProfileGeneric.AccessSelector == AccessRange.Last) { byte[][] tmp = client.ReadRowsByRange(CurrentProfileGeneric, Convert.ToDateTime(CurrentProfileGeneric.From), Convert.ToDateTime(CurrentProfileGeneric.To)); ReadDataBlock(tmp[0], "Reading profile generic data", 1, reply); } else if (CurrentProfileGeneric.AccessSelector == AccessRange.Entry) { byte[][] tmp = client.ReadRowsByEntry(CurrentProfileGeneric, Convert.ToInt32(CurrentProfileGeneric.From), Convert.ToInt32(CurrentProfileGeneric.To)); ReadDataBlock(tmp[0], "Reading profile generic data " + CurrentProfileGeneric.Name, 1, reply); } else //Read all. { byte[] tmp = client.Read(CurrentProfileGeneric, 2)[0]; ReadDataBlock(tmp, "Reading profile generic data " + CurrentProfileGeneric.Name, 1, reply); } } finally { if (OnAfterRead != null) { OnAfterRead(obj, it); } OnDataReceived -= new GXDLMSCommunicator.DataReceivedEventHandler(OnProfileGenericDataReceived); } continue; } else { if (OnBeforeRead != null) { OnBeforeRead(obj, it); } byte[] data = client.Read(obj.Name, obj.ObjectType, it)[0]; try { ReadDataBlock(data, "Read object type " + obj.ObjectType + " index: " + it, reply); } catch (GXDLMSException ex) { if (ex.ErrorCode == 3 || //If read is denied. ex.ErrorCode == 4 || // Undefined object. ex.ErrorCode == 13) //Actaris returns access violation error. { obj.SetAccess(it, AccessMode.NoAccess); continue; } else { throw ex; } } if (obj is IGXDLMSBase) { object value = reply.Value; DataType type; if (value is byte[] && (type = obj.GetUIDataType(it)) != DataType.None) { value = GXDLMSClient.ChangeType((byte[])value, type); } client.UpdateValue(obj, it, value); } if (OnAfterRead != null) { OnAfterRead(obj, it); } obj.SetLastReadTime(it, DateTime.Now); //If only selected attribute is read. if (attribute != 0) { break; } } } }
/// <summary> /// Get Value from byte array received from the meter. /// </summary> /// <param name="data">Reply byte array received from the meter.</param> /// <param name="target">Read COSEM Object.</param> /// <param name="attributeIndex"></param> /// <returns></returns> /// <seealso cref="TryGetValue"/> public object GetValue(byte[] data, GXDLMSObject target, int attributeIndex) { DataType type = target.GetUIDataType(attributeIndex); Object value = GetValue(data); if (value is byte[] && type != DataType.None) { return GXDLMSClient.ChangeType((byte[]) value, type); } return value; }
private byte[] GetValue(object name, GXDLMSObject item, int index, int selector, object parameters) { IGXDLMSBase tmp = item as IGXDLMSBase; object value = null; if (tmp != null) { value = tmp.GetValue(index, selector, parameters); } else { object[] values = item.GetValues(); if (index <= values.Length) { value = values[index - 1]; } } Gurux.DLMS.DataType tp = item.GetDataType(index); if (tp == DataType.None) { tp = Gurux.DLMS.Internal.GXCommon.GetValueType(value); } //If data is shown as string, but it's OCTECT String. if (tp == DataType.OctetString && value is string && item.GetUIDataType(index) == DataType.String) { value = ASCIIEncoding.ASCII.GetBytes((string)value); } if (tp != DataType.None || (value == null && tp == DataType.None)) { SendData.AddRange(ReadReply(name, item.ObjectType, index, value, tp)); return SendData[FrameIndex]; } //Return HW error. throw new ArgumentOutOfRangeException(); }
/// <summary> /// Read object. /// </summary> /// <param name="InitialValues"></param> /// <param name="obj"></param> /// <param name="attribute"></param> public void Read(object sender, GXDLMSObject obj, int attribute) { foreach (int it in (obj as IGXDLMSBase).GetAttributeIndexToRead()) { if (obj is GXDLMSProfileGeneric && it == 2) { if (OnBeforeRead != null) { OnBeforeRead(obj, it); } try { CurrentProfileGeneric = obj as GXDLMSProfileGeneric; OnDataReceived += new GXDLMSCommunicator.DataReceivedEventHandler(this.OnProfileGenericDataReceived); if (CurrentProfileGeneric.AccessSelector != AccessRange.Entry) { GXDLMSObject obj2 = CurrentProfileGeneric.CaptureObjects[0].Key; byte[] tmp = m_Cosem.ReadRowsByRange(CurrentProfileGeneric.Name, obj2.LogicalName, obj2.ObjectType, obj2.Version, Convert.ToDateTime(CurrentProfileGeneric.From), Convert.ToDateTime(CurrentProfileGeneric.To)); ReadDataBlock(tmp, "Reading profile generic data", 1); } else { byte[] tmp = m_Cosem.ReadRowsByEntry(CurrentProfileGeneric.Name, Convert.ToInt32(CurrentProfileGeneric.From), Convert.ToInt32(CurrentProfileGeneric.To)); ReadDataBlock(tmp, "Reading profile generic data " + CurrentProfileGeneric.Name, 1); } } finally { if (OnAfterRead != null) { OnAfterRead(obj, it); } OnDataReceived -= new GXDLMSCommunicator.DataReceivedEventHandler(OnProfileGenericDataReceived); } continue; } else { if (OnBeforeRead != null) { OnBeforeRead(obj, it); } byte[] data = m_Cosem.Read(obj.Name, obj.ObjectType, it)[0]; try { data = ReadDataBlock(data, "Read object type " + obj.ObjectType + " index: " + it); } catch (GXDLMSException ex) { if (ex.ErrorCode == 3 || //If read is denied. ex.ErrorCode == 4 || // Undefined object. ex.ErrorCode == 13) //Actaris returns access violation error. { obj.SetAccess(it, AccessMode.NoAccess); continue; } else { throw ex; } } if (obj is IGXDLMSBase) { object value = m_Cosem.GetValue(data); DataType type; if (value is byte[] && (type = obj.GetUIDataType(it)) != DataType.None) { value = GXDLMS.Common.GXHelpers.ConvertFromDLMS(value, obj.GetDataType(it), type, true); } (obj as IGXDLMSBase).SetValue(it, value); } if (OnAfterRead != null) { OnAfterRead(obj, it); } obj.SetLastReadTime(it, DateTime.Now); //If only selected attribute is read. if (attribute != 0) { break; } } } }
void UpdateObject(GXDLMSObject it, GXDLMSProperty item, int index) { //Update name. item.AttributeOrdinal = index; item.ShortName = it.ShortName; item.LogicalName = it.LogicalName; if (it.ShortName != 0) { item.Name = string.Format("0x{0} {1} {2}", it.ShortName.ToString("X4"), it.LogicalName, it.Description); } else { item.Name = it.LogicalName + " " + it.Description; } //Update description. item.Description = it.Description; //Update access mode. item.DLMSType = it.GetDataType(index); if (item.DLMSType == DataType.Enum) { item.ForcePresetValues = true; object value = it.GetValues()[index - 1]; foreach (object val in Enum.GetValues(value.GetType())) { item.Values.Add(new GXValueItem(val.ToString(), (int)val)); } } else { item.ValueType = it.GetUIDataType(index); if (item.ValueType == DataType.None) { item.ValueType = it.GetDataType(index); } } item.AccessMode = (Gurux.Device.AccessMode)it.GetAccess(index); }