DLMS Converter is used to get string value for enumeration types.
Esempio n. 1
0
        ///<summary>
        /// Handle action request.
        ///</summary>
        public static void HandleMethodRequest(GXDLMSSettings settings, GXDLMSServer server, GXByteBuffer data, GXDLMSConnectionEventArgs connectionInfo, GXByteBuffer replyData, GXDLMSTranslatorStructure xml)
        {
            ErrorCode    error = ErrorCode.Ok;
            GXByteBuffer bb    = new GXByteBuffer();
            // Get type.
            ActionRequestType type = (ActionRequestType)data.GetUInt8();
            // Get invoke ID and priority.
            byte invokeId = data.GetUInt8();

            settings.UpdateInvokeId(invokeId);
            // CI
            ObjectType ci = (ObjectType)data.GetUInt16();

            byte[] ln = new byte[6];
            data.Get(ln);
            // Attribute Id
            byte id = data.GetUInt8();
            // Get parameters.
            object parameters = null;
            byte   selection  = data.GetUInt8();

            if (xml != null)
            {
                xml.AppendStartTag(Command.MethodRequest);
                if (type == ActionRequestType.Normal)
                {
                    xml.AppendStartTag(Command.MethodRequest, ActionRequestType.Normal);
                    xml.AppendLine(TranslatorTags.InvokeId, "Value", xml.IntegerToHex(invokeId, 2));
                    AppendMethodDescriptor(xml, (int)ci, ln, id);
                    if (selection != 0)
                    {
                        //MethodInvocationParameters
                        xml.AppendStartTag(TranslatorTags.MethodInvocationParameters);
                        GXDataInfo di = new GXDataInfo();
                        di.xml = xml;
                        GXCommon.GetData(settings, data, di);
                        xml.AppendEndTag(TranslatorTags.MethodInvocationParameters);
                    }
                    xml.AppendEndTag(Command.MethodRequest, ActionRequestType.Normal);
                }
                xml.AppendEndTag(Command.MethodRequest);
                return;
            }
            if (selection != 0)
            {
                GXDataInfo info = new GXDataInfo();
                parameters = GXCommon.GetData(settings, data, info);
            }

            GXDLMSObject obj = settings.Objects.FindByLN(ci, GXCommon.ToLogicalName(ln));

            if (settings.Connected == ConnectionState.None && !settings.CanAccess() && (ci != ObjectType.AssociationLogicalName || id != 1))
            {
                replyData.Set(GXDLMSServer.GenerateConfirmedServiceError(ConfirmedServiceError.InitiateError,
                                                                         ServiceError.Service, (byte)Service.Unsupported));
                return;
            }

            if (obj == null)
            {
                obj = server.NotifyFindObject(ci, 0, GXCommon.ToLogicalName(ln));
            }
            if (obj == null)
            {
                // Device reports a undefined object.
                error = ErrorCode.UndefinedObject;
            }
            else
            {
                ValueEventArgs e = new ValueEventArgs(server, obj, id, 0, parameters);
                e.InvokeId = invokeId;
                if (server.NotifyGetMethodAccess(e) == MethodAccessMode.NoAccess)
                {
                    error = ErrorCode.ReadWriteDenied;
                }
                else
                {
                    server.NotifyAction(new ValueEventArgs[] { e });
                    byte[] actionReply;
                    if (e.Handled)
                    {
                        actionReply = (byte[])e.Value;
                    }
                    else
                    {
                        actionReply = (obj as IGXDLMSBase).Invoke(settings, e);
                        server.NotifyPostAction(new ValueEventArgs[] { e });
                    }
                    //Set default action reply if not given.
                    if (actionReply != null && e.Error == 0)
                    {
                        //Add return parameters
                        bb.SetUInt8(1);
                        //Add parameters error code.
                        bb.SetUInt8(0);
                        GXCommon.SetData(settings, bb, GXDLMSConverter.GetDLMSDataType(actionReply), actionReply);
                    }
                    else
                    {
                        error = e.Error;
                        //Add return parameters
                        bb.SetUInt8(0);
                    }
                }
                invokeId = (byte)e.InvokeId;
            }

            GXDLMSLNParameters p = new GXDLMSLNParameters(null, settings, invokeId, Command.MethodResponse, 1, null, bb, (byte)error);

            GXDLMS.GetLNPdu(p, replyData);
            //If High level authentication fails.
            if (obj is GXDLMSAssociationLogicalName && id == 1)
            {
                if ((obj as GXDLMSAssociationLogicalName).AssociationStatus == Objects.Enums.AssociationStatus.Associated)
                {
                    server.NotifyConnected(connectionInfo);
                    settings.Connected |= ConnectionState.Dlms;
                }
                else
                {
                    server.NotifyInvalidConnection(connectionInfo);
                    settings.Connected &= ~ConnectionState.Dlms;
                }
            }
        }
        static public object ChangeType(object value, DataType type, CultureInfo cultureInfo)
        {
            object ret;

            if (type == DataType.OctetString)
            {
                if (value is byte[])
                {
                    ret = value;
                }
                else
                {
                    ret = GXDLMSTranslator.HexToBytes((string)value);
                }
            }
            else if (type == DataType.DateTime)
            {
                if (value is GXDateTime)
                {
                    ret = value;
                }
                else
                {
                    ret = new GXDateTime((string)value, CultureInfo.InvariantCulture);
                }
            }
            else if (type == DataType.Date)
            {
                if (value is GXDateTime)
                {
                    ret = value;
                }
                else
                {
                    ret = new GXDate((string)value, CultureInfo.InvariantCulture);
                }
            }
            else if (type == DataType.Time)
            {
                if (value is GXDateTime)
                {
                    ret = value;
                }
                else
                {
                    ret = new GXTime((string)value, CultureInfo.InvariantCulture);
                }
            }
            else if (type == DataType.Enum)
            {
                if (value is GXEnum)
                {
                    ret = value;
                }
                else
                {
                    ret = new GXEnum((byte)Convert.ChangeType(value, typeof(byte)));
                }
            }
            else if (type == DataType.Structure || type == DataType.Array)
            {
                ret = GXDLMSTranslator.XmlToValue((string)value);
            }
            else if (type == DataType.BitString)
            {
                ret = new GXBitString((string)value);
            }
            else if (type == DataType.None && value is string && string.IsNullOrEmpty((string)value))
            {
                ret = null;
            }
            else
            {
                ret = Convert.ChangeType(value, GXDLMSConverter.GetDataType(type), cultureInfo);
            }
            return(ret);
        }
Esempio n. 3
0
     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;
 }
 public GXDLMSObjectCollection GetObjects()
 {
     GXLogWriter.WriteLog("--- Collecting objects. ---");
     GXReplyData reply = new GXReplyData(){Peek = false};
     try
     {
         ReadDataBlock(client.GetObjectsRequest(), "Collecting objects", 3, reply);
     }
     catch (Exception Ex)
     {
         throw new Exception("GetObjects failed. " + Ex.Message);
     }
     GXDLMSObjectCollection objs = client.ParseObjects(reply.Data, true);
     //Update OBIS code description.
     GXDLMSConverter c = new GXDLMSConverter();
     c.UpdateOBISCodeInformation(objs);
     GXLogWriter.WriteLog("--- Collecting " + objs.Count.ToString() + " objects. ---");
     return objs;
 }
Esempio n. 5
0
        static public object ChangeType(object value, DataType type, CultureInfo cultureInfo)
        {
            object ret;

            if (type == DataType.OctetString)
            {
                if (value is byte[])
                {
                    ret = value;
                }
                else
                {
                    ret = GXDLMSTranslator.HexToBytes((string)value);
                }
            }
            else if (type == DataType.DateTime)
            {
                if (value is GXDateTime)
                {
                    ret = value;
                }
                else
                {
                    ret = new GXDateTime((string)value, CultureInfo.InvariantCulture);
                }
            }
            else if (type == DataType.Date)
            {
                if (value is GXDateTime)
                {
                    ret = value;
                }
                else
                {
                    ret = new GXDate((string)value, CultureInfo.InvariantCulture);
                }
            }
            else if (type == DataType.Time)
            {
                if (value is GXDateTime)
                {
                    ret = value;
                }
                else
                {
                    ret = new GXTime((string)value, CultureInfo.InvariantCulture);
                }
            }
            else if (type == DataType.Enum)
            {
                if (value is GXEnum)
                {
                    ret = value;
                }
                else
                {
                    ret = new GXEnum((byte)Convert.ChangeType(value, typeof(byte)));
                }
            }
            else
            {
                ret = Convert.ChangeType(value, GXDLMSConverter.GetDataType(type));
            }
            return(ret);
        }