GetDataType() public method

Returns device data type of selected attribute index.
public GetDataType ( int index ) : DataType
index int Attribute index of the object.
return DataType
Exemplo n.º 1
26
 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 void Write(GXDLMSObject obj, object target, int index, List<object> UpdatedObjects)
 {
     for (int it = 1; it != (obj as IGXDLMSBase).GetAttributeCount() + 1; ++it)
     {
         object value;
         if (obj.GetDirty(it, out value))
         {
             //Read DLMS data type if not known.
             DataType type = obj.GetDataType(it);
             if (type == DataType.None)
             {
                 byte[] data = m_Cosem.Read(obj.Name, obj.ObjectType, it)[0];
                 data = ReadDataBlock(data, "Write object type " + obj.ObjectType);
                 type = m_Cosem.GetDLMSDataType(data);
                 if (type == DataType.None)
                 {
                     throw new Exception("Failed to write value. Data type not set.");
                 }
                 obj.SetDataType(it, type);
             }
             try
             {
                 foreach (byte[] tmp in m_Cosem.Write(obj.Name, value, type, obj.ObjectType, it))
                 {
                     ReadDataBlock(tmp, "Write object");
                 }
                 obj.ClearDirty(it);
                 //Read data once again to make sure it is updated.
                 byte[] data = m_Cosem.Read(obj.Name, obj.ObjectType, it)[0];
                 data = ReadDataBlock(data, "Read object " + obj.ObjectType);
                 value = m_Cosem.GetValue(data);
                 obj.SetValue(it, value);
             }
             catch (GXDLMSException ex)
             {
                 if (ex.ErrorCode == 3)
                 {
                     throw new Exception("Read/Write Failed.");
                 }
                 else
                 {
                     throw ex;
                 }
             }
         }
     }
 }
 public void ReadValue(GXDLMSObject it, int attributeOrdinal)
 {
     GXReplyData reply = new GXReplyData();
     ReadDataBlock(Read(it, attributeOrdinal), "Read object", reply);
     //If data type is unknown
     if (it.GetDataType(attributeOrdinal) == DataType.None)
     {
         it.SetDataType(attributeOrdinal, reply.DataType);
     }
     client.UpdateValue(it, attributeOrdinal, reply.Value);
 }
Exemplo n.º 5
0
 /// <summary>
 /// Generates a write message.
 /// </summary>
 /// <param name="item">object to write.</param>
 /// <param name="index">Attribute index where data is write.</param>
 /// <returns></returns>
 public byte[][] Write(GXDLMSObject item, int index)
 {
     if (item == null || index < 1)
     {
         throw new GXDLMSException("Invalid parameter");
     }
     Object value = (item as IGXDLMSBase).GetValue(index, 0, null);
     DataType type = item.GetDataType(index);
     if (type == DataType.None)
     {
         type = Gurux.DLMS.Internal.GXCommon.GetValueType(value);
     }
     return Write(item.Name, value, type, item.ObjectType, index);
 }
Exemplo n.º 6
0
 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 attribute value.
 /// </summary>
 public object Read(GXDLMSObject it, int attributeIndex)
 {
     byte[] reply = ReadDataBlock(Client.Read(it.Name, it.ObjectType, attributeIndex)[0]);
     //Update data type.
     if (it.GetDataType(attributeIndex) == DataType.None)
     {
         it.SetDataType(attributeIndex, Client.GetDLMSDataType(reply));
     }
     return Client.UpdateValue(reply, it, attributeIndex);
 }
Exemplo n.º 8
0
 /// <summary>
 /// Add value of COSEM object to byte buffer.
 /// </summary>
 /// <param name="obj">COSEM object.</param>
 /// <param name="index">Attribute index.</param>
 /// <param name="buff">Byte buffer.</param>
 /// <remarks>
 /// AddData method can be used with GetDataNotificationMessage -method.
 /// DLMS spesification do not specify the structure of Data-Notification body.
 /// So each manufacture can sent different data.
 /// </remarks>
 /// <seealso cref="GetDataNotificationMessages"/>
 internal static void AddData(GXDLMSSettings settings, GXDLMSObject obj, int index, GXByteBuffer buff)
 {
     DataType dt;
     object value = (obj as IGXDLMSBase).GetValue(settings, new ValueEventArgs(settings, obj, index, 0, null));
     dt = obj.GetDataType(index);
     if (dt == DataType.None && value != null)
     {
         dt = GXCommon.GetValueType(value);
     }
     GXCommon.SetData(settings, buff, dt, value);
 }
Exemplo n.º 9
0
 /// <summary>
 /// Read attribute value.
 /// </summary>
 public object Read(GXDLMSObject it, int attributeIndex)
 {
     GXReplyData reply = new GXReplyData();
     if (!ReadDataBlock(Client.Read(it, attributeIndex), reply))
     {
         reply.Clear();
         Thread.Sleep(1000);
         if (!ReadDataBlock(Client.Read(it, attributeIndex), reply))
         {
             if (reply.Error != 0)
             {
                 throw new GXDLMSException(reply.Error);
             }
         }
     }
     //Update data type.
     if (it.GetDataType(attributeIndex) == DataType.None)
     {
         it.SetDataType(attributeIndex, reply.DataType);
     }
     return Client.UpdateValue(it, attributeIndex, reply.Value);
 }
Exemplo n.º 10
0
 /// <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;
             }
         }
     }
 }
Exemplo n.º 11
0
 /// <summary>
 /// Update categories data.
 /// </summary>
 /// <param name="trace"></param>
 /// <param name="progressbar"></param>
 /// <param name="media"></param>
 /// <param name="Device"></param>
 /// <param name="wt"></param>
 /// <param name="cosem"></param>
 /// <param name="man"></param>
 /// <param name="objs"></param>
 /// <param name="dataItems"></param>
 /// <param name="registers"></param>
 private object UpdateData(Gurux.Common.IGXMedia media, GXDLMSDevice Device, int wt, GXDLMSClient cosem, GXManufacturer man, GXDLMSObject it, GXCategory dataItems, GXCategory registers)
 {
     GXObisCode code = man.ObisCodes.FindByLN(it.ObjectType, it.LogicalName, null);            
     if (it.ObjectType == ObjectType.Register)
     {
         GXDLMSRegister prop = new GXDLMSRegister();
         UpdateObject(it, prop, 2);
         registers.Properties.Add(prop);
         return prop;
     }
     else if (it.ObjectType == Gurux.DLMS.ObjectType.Data)
     {
         GXDLMSProperty prop = new GXDLMSProperty();
         prop.ObjectType = ObjectType.Data;
         UpdateObject(it, prop, 2);
         dataItems.Properties.Add(prop);
         return prop;
     }
     else if (it.ObjectType == Gurux.DLMS.ObjectType.ProfileGeneric)
     {
         GXDLMSProfileGeneric pg = it as GXDLMSProfileGeneric;
         GXDLMSTable table = new GXDLMSTable();
         table.Name = it.LogicalName + " " + it.Description;
         table.ShortName = it.ShortName;
         table.LogicalName = it.LogicalName;
         table.AccessMode = Gurux.Device.AccessMode.Read;
         foreach(var it2 in pg.CaptureObjects)
         {
             GXDLMSProperty prop;
             if (it2.Key is Gurux.DLMS.Objects.GXDLMSRegister)
             {
                 Gurux.DLMS.Objects.GXDLMSRegister tmp = it2.Key as Gurux.DLMS.Objects.GXDLMSRegister;
                 GXDLMSRegister r = new GXDLMSRegister();
                 prop = r;
                 r.Scaler = tmp.Scaler;
                 r.Unit = tmp.Unit.ToString();                    
             }
             else
             {
                 prop = new GXDLMSProperty();
             }
             int index = it2.Value.AttributeIndex;
             prop.Name = it2.Key.LogicalName + " " + it2.Key.Description;
             prop.ObjectType = it2.Key.ObjectType;
             prop.AttributeOrdinal = index;
             prop.LogicalName = it2.Key.LogicalName;
             table.Columns.Add(prop);
             prop.DLMSType = it.GetDataType(index);                    
             prop.ValueType = it2.Key.GetUIDataType(index);
         }
         Device.Tables.Add(table);
         return table; 
     }
     GXDLMSCategory cat = new GXDLMSCategory();
     cat.ObjectType = it.ObjectType;
     UpdateObject(it, cat);
     Device.Categories.Add(cat);
     return cat;            
 }
Exemplo n.º 12
0
 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);
 }
Exemplo n.º 13
0
 internal static void AppendData(GXDLMSSettings settings, GXDLMSObject obj, int index, GXByteBuffer bb, Object value)
 {
     DataType tp = obj.GetDataType(index);
     if (tp == DataType.Array)
     {
         if (value is byte[])
         {
             bb.Set((byte[])value);
             return;
         }
         else if (value is GXByteBuffer)
         {
             bb.Set((GXByteBuffer)value);
             return;
         }
     }
     else
     {
         if (tp == DataType.None)
         {
             tp = GXCommon.GetValueType(value);
         }
     }
     GXCommon.SetData(settings, bb, tp, value);
 }
Exemplo n.º 14
0
 private byte[] GetValue(object name, GXDLMSObject item, int index)
 {            
     object[] values = item.GetValues();
     if (index <= values.Length)
     {
         object value = values[index - 1];
         Gurux.DLMS.DataType tp = item.GetDataType(index);
         if (tp == DataType.None)
         {
             tp = Gurux.DLMS.Internal.GXCommon.GetValueType(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.
     SendData.Add(ServerReportError(1, 5, 3));
     return SendData[FrameIndex];
 }