object IGXDLMSBase.GetValue(GXDLMSSettings settings, ValueEventArgs e)
 {
     if (e.Index == 1)
     {
         return(GXCommon.LogicalNameToBytes(LogicalName));
     }
     if (e.Index == 2)
     {
         //If client set new value.
         if (!settings.IsServer && Scaler != 1 && Value != null)
         {
             DataType dt = base.GetDataType(2);
             if (dt == DataType.None && Value != null)
             {
                 dt = GXCommon.GetDLMSDataType(Value.GetType());
                 //If user has set initial value.
                 if (dt == DataType.String)
                 {
                     dt = DataType.None;
                 }
             }
             object tmp;
             tmp = Convert.ToDouble(Value) / Scaler;
             if (dt != DataType.None)
             {
                 tmp = Convert.ChangeType(tmp, GXCommon.GetDataType(dt));
             }
             return(tmp);
         }
         return(Value);
     }
     if (e.Index == 3)
     {
         GXByteBuffer data = new GXByteBuffer();
         data.SetUInt8((byte)DataType.Structure);
         data.SetUInt8(2);
         GXCommon.SetData(settings, data, DataType.Int8, scaler);
         GXCommon.SetData(settings, data, DataType.Enum, Unit);
         return(data.Array());
     }
     if (e.Index == 4)
     {
         return(Status);
     }
     if (e.Index == 5)
     {
         return(CaptureTime);
     }
     e.Error = ErrorCode.ReadWriteDenied;
     return(null);
 }
Ejemplo n.º 2
0
        /// <summary>
        /// Change value using DLMS data type.
        /// </summary>
        /// <param name="value">Value to convert.</param>
        /// <param name="type">DLMS data type.</param>
        /// <param name="isHex">Is value given as hex.</param>
        /// <returns>Converted value.</returns>
        public static object ChangeFromDLMSType(object value, DataType type, bool isHex)
        {
            Type t = GXCommon.GetDataType(type);

            if (isHex && value is string)
            {
                var dict = new Dictionary <Type, Func <string, object> >
                {
                    { typeof(byte), s => byte.Parse(s, NumberStyles.AllowHexSpecifier) },
                    { typeof(sbyte), s => sbyte.Parse(s, NumberStyles.AllowHexSpecifier) },
                    { typeof(short), s => short.Parse(s, NumberStyles.AllowHexSpecifier) },
                    { typeof(ushort), s => ushort.Parse(s, NumberStyles.AllowHexSpecifier) },
                    { typeof(int), s => int.Parse(s, NumberStyles.AllowHexSpecifier) },
                    { typeof(uint), s => uint.Parse(s, NumberStyles.AllowHexSpecifier) },
                    { typeof(long), s => long.Parse(s, NumberStyles.AllowHexSpecifier) },
                    { typeof(ulong), s => ulong.Parse(s, NumberStyles.AllowHexSpecifier) },
                };
                if (dict.ContainsKey(t))
                {
                    return(dict[t]((string)value));
                }
            }
            return(Convert.ChangeType(value, t));
        }
 object IGXDLMSBase.GetValue(GXDLMSSettings settings, ValueEventArgs e)
 {
     if (e.Index == 1)
     {
         return(GXCommon.LogicalNameToBytes(LogicalName));
     }
     if (e.Index == 2)
     {
         //If client set new value.
         if (!settings.IsServer && Scaler != 1 && CurrentAverageValue != null)
         {
             Type type = null;
             if (CurrentAverageValue != null)
             {
                 type = CurrentAverageValue.GetType();
             }
             object tmp;
             tmp = Convert.ToDouble(CurrentAverageValue) / Scaler;
             if (type != null)
             {
                 tmp = Convert.ChangeType(tmp, type);
             }
             return(tmp);
         }
         return(CurrentAverageValue);
     }
     if (e.Index == 3)
     {
         //If client set new value.
         if (!settings.IsServer && Scaler != 1 && LastAverageValue != null)
         {
             DataType dt = base.GetDataType(3);
             if (dt == DataType.None && LastAverageValue != null)
             {
                 dt = GXCommon.GetDLMSDataType(LastAverageValue.GetType());
             }
             object tmp;
             tmp = Convert.ToDouble(LastAverageValue) / Scaler;
             if (dt != DataType.None)
             {
                 tmp = Convert.ChangeType(tmp, GXCommon.GetDataType(dt));
             }
             return(tmp);
         }
         return(LastAverageValue);
     }
     if (e.Index == 4)
     {
         GXByteBuffer data = new GXByteBuffer();
         data.SetUInt8((byte)DataType.Structure);
         data.SetUInt8(2);
         GXCommon.SetData(settings, data, DataType.Int8, scaler);
         GXCommon.SetData(settings, data, DataType.Enum, Unit);
         return(data.Array());
     }
     if (e.Index == 5)
     {
         return(Status);
     }
     if (e.Index == 6)
     {
         return(CaptureTime);
     }
     if (e.Index == 7)
     {
         return(StartTimeCurrent);
     }
     if (e.Index == 8)
     {
         return(Period);
     }
     if (e.Index == 9)
     {
         return(NumberOfPeriods);
     }
     e.Error = ErrorCode.ReadWriteDenied;
     return(null);
 }