From fb808620195780365fc2b02b646ef72a9c390803 Mon Sep 17 00:00:00 2001 From: Lars Elgtvedt Susaas Date: Sat, 30 Nov 2024 22:08:39 +0100 Subject: [PATCH] completed test of eds -> protobuffer tests --- Tests/EDSMappingTests.cs | 37 ++++++++------- libEDSsharp/CanOpenEDSMapping.cs | 80 ++++++++++++++++++++++++++++---- 2 files changed, 90 insertions(+), 27 deletions(-) diff --git a/Tests/EDSMappingTests.cs b/Tests/EDSMappingTests.cs index 70ef3ec..3633c32 100644 --- a/Tests/EDSMappingTests.cs +++ b/Tests/EDSMappingTests.cs @@ -41,19 +41,6 @@ public void Test_ToProtobufferAssertConfig() MappingEDS.MapToProtobuffer(eds); } [Fact] - public void Test_FromProtobufferAssertConfig() - { - var d = new CanOpenDevice - { - FileInfo = new CanOpen_FileInfo(), - DeviceInfo = new CanOpen_DeviceInfo(), - DeviceCommissioning = new CanOpen_DeviceCommissioning() - }; - - //Assert is called inside the map function - MappingEDS.MapFromProtobuffer(d); - } - [Fact] public void Test_ToProtobufferFileInfo() { var eds = new EDSsharp @@ -150,7 +137,7 @@ public void Test_ToProtobufferDeviceCommissioning() [InlineData(OdObject.Types.ObjectType.Unspecified, ObjectType.DOMAIN)] [InlineData(OdObject.Types.ObjectType.Unspecified, ObjectType.NULL)] [InlineData(OdObject.Types.ObjectType.Unspecified, ObjectType.UNKNOWN)] - void Test_ToProtobufferODObject(OdObject.Types.ObjectType objTypeProto, ObjectType objTypeEDS) + public void Test_ToProtobufferODObject(OdObject.Types.ObjectType objTypeProto, ObjectType objTypeEDS) { var eds = new EDSsharp { @@ -194,7 +181,7 @@ void Test_ToProtobufferODObject(OdObject.Types.ObjectType objTypeProto, ObjectTy [InlineData(OdSubObject.Types.DataType.Unsigned48, DataType.UNSIGNED48)] [InlineData(OdSubObject.Types.DataType.Unsigned56, DataType.UNSIGNED56)] [InlineData(OdSubObject.Types.DataType.Unsigned64, DataType.UNSIGNED64)] - void Test_ToProtobufferSubODObjectDatatype(OdSubObject.Types.DataType datatypeProto, DataType datatypeEDS) + public void Test_ToProtobufferSubODObjectDatatype(OdSubObject.Types.DataType datatypeProto, DataType datatypeEDS) { var eds = new EDSsharp { @@ -216,15 +203,16 @@ void Test_ToProtobufferSubODObjectDatatype(OdSubObject.Types.DataType datatypePr var tmp = MappingEDS.MapToProtobuffer(eds); Assert.Equal(datatypeProto, tmp.Objects[od.Index.ToString()].SubObjects["0"].Type); } + [Theory] [InlineData(OdSubObject.Types.AccessPDO.Tr, OdSubObject.Types.AccessSDO.Rw, EDSsharp.AccessType.rw)] - [InlineData(OdSubObject.Types.AccessPDO.T, OdSubObject.Types.AccessSDO.Ro, EDSsharp.AccessType.ro)] - [InlineData(OdSubObject.Types.AccessPDO.R, OdSubObject.Types.AccessSDO.Wo, EDSsharp.AccessType.wo)] + [InlineData(OdSubObject.Types.AccessPDO.No, OdSubObject.Types.AccessSDO.Ro, EDSsharp.AccessType.ro)] + [InlineData(OdSubObject.Types.AccessPDO.No, OdSubObject.Types.AccessSDO.Wo, EDSsharp.AccessType.wo)] [InlineData(OdSubObject.Types.AccessPDO.T, OdSubObject.Types.AccessSDO.Rw, EDSsharp.AccessType.rwr)] [InlineData(OdSubObject.Types.AccessPDO.R, OdSubObject.Types.AccessSDO.Rw, EDSsharp.AccessType.rww)] [InlineData(OdSubObject.Types.AccessPDO.R, OdSubObject.Types.AccessSDO.Ro, EDSsharp.AccessType.@const)] [InlineData(OdSubObject.Types.AccessPDO.No, OdSubObject.Types.AccessSDO.No, EDSsharp.AccessType.UNKNOWN)] - void Test_ToProtobufferSubODObjectAccesstype(OdSubObject.Types.AccessPDO accessPDOProto, OdSubObject.Types.AccessSDO accessSDOProto, EDSsharp.AccessType datatypeEDS) + public void Test_ToProtobufferSubODObjectAccesstype(OdSubObject.Types.AccessPDO accessPDOProto, OdSubObject.Types.AccessSDO accessSDOProto, EDSsharp.AccessType datatypeEDS) { var eds = new EDSsharp { @@ -279,5 +267,18 @@ public void Test_ToProtobufferSubODObjectMembers() Assert.Equal(sub.LowLimit, tmp.Objects[od.Index.ToString()].SubObjects["0"].LowLimit); Assert.Equal(sub.defaultvalue, tmp.Objects[od.Index.ToString()].SubObjects["0"].DefaultValue); } + [Fact] + public void Test_FromProtobufferAssertConfig() + { + var d = new CanOpenDevice + { + FileInfo = new CanOpen_FileInfo(), + DeviceInfo = new CanOpen_DeviceInfo(), + DeviceCommissioning = new CanOpen_DeviceCommissioning() + }; + + //Assert is called inside the map function + MappingEDS.MapFromProtobuffer(d); + } } } diff --git a/libEDSsharp/CanOpenEDSMapping.cs b/libEDSsharp/CanOpenEDSMapping.cs index 93641aa..c9d870a 100644 --- a/libEDSsharp/CanOpenEDSMapping.cs +++ b/libEDSsharp/CanOpenEDSMapping.cs @@ -150,12 +150,14 @@ public static CanOpenDevice MapToProtobuffer(EDSsharp source) .ForMember(dest => dest.Type, opt => opt.MapFrom(src => src.objecttype)) .ForMember(dest => dest.CountLabel, opt => opt.MapFrom(src => src.Label)); cfg.CreateMap().ConvertUsing(); + cfg.CreateMap().ConvertUsing(); + cfg.CreateMap().ConvertUsing(); cfg.CreateMap() .ForMember(dest => dest.Name, opt => opt.MapFrom(src => src.parameter_name)) .ForMember(dest => dest.Alias, opt => opt.Ignore()) .ForMember(dest => dest.Type, opt => opt.MapFrom(src => src.datatype)) - .ForMember(dest => dest.Sdo, opt => opt.MapFrom(src => src.AccessSDO())) - .ForMember(dest => dest.Pdo, opt => opt.MapFrom(src => src.AccessPDO())) + .ForMember(dest => dest.Sdo, opt => opt.MapFrom(src => src.accesstype)) + .ForMember(dest => dest.Pdo, opt => opt.MapFrom(src => src.accesstype)) .ForMember(dest => dest.Srdo, opt => opt.Ignore()) .ForMember(dest => dest.StringLengthMin, opt => opt.MapFrom(src => src.Lengthofstring)); }); @@ -166,7 +168,6 @@ public static CanOpenDevice MapToProtobuffer(EDSsharp source) } } - /// /// Helper class to convert EDS date and time into datetime used in the protobuffer timestand (datetime) /// @@ -233,16 +234,16 @@ public Timestamp Resolve(FileInfo source, CanOpen_FileInfo destination, Timestam } /// - /// Helper class to convert Enum types + /// Helper class to convert object type enum /// /// Checkout AutoMapper.Extensions.EnumMapping when .net framework is gone public class ODTypeResolver : ITypeConverter { /// - /// Resolver to convert eds date and time into protobuffer timestamp (datetime) + /// Resolver to convert object types /// - /// source EDS fileinfo object - /// protobuffer fileinfo object + /// EDS object type object + /// protobuffer object type /// result object /// resolve context /// result @@ -264,7 +265,68 @@ public OdObject.Types.ObjectType Convert(ObjectType source, OdObject.Types.Objec default: return OdObject.Types.ObjectType.Unspecified; } - } } -} \ No newline at end of file + /// + /// Helper class to convert Enum types + /// + /// Checkout AutoMapper.Extensions.EnumMapping when .net framework is gone + public class ODSDOAccessTypeResolver : ITypeConverter + { + /// + /// Resolver to convert eds access into SDO access type + /// + /// EDS accesstype + /// protobuffer sdo access type + /// result object + /// resolve context + /// result + public OdSubObject.Types.AccessSDO Convert(EDSsharp.AccessType source, OdSubObject.Types.AccessSDO destination, ResolutionContext context) + { + switch (source) + { + case EDSsharp.AccessType.rw: + case EDSsharp.AccessType.rwr: + case EDSsharp.AccessType.rww: + return OdSubObject.Types.AccessSDO.Rw; + case EDSsharp.AccessType.ro: + case EDSsharp.AccessType.@const: + return OdSubObject.Types.AccessSDO.Ro; + case EDSsharp.AccessType.wo: + return OdSubObject.Types.AccessSDO.Wo; + case EDSsharp.AccessType.UNKNOWN: + default: + return OdSubObject.Types.AccessSDO.No; + } + } + } + public class ODPDOAccessTypeResolver : ITypeConverter + { + /// + /// Resolver to convert eds access into PDO access type + /// + /// EDS accesstype + /// protobuffer pdo access type + /// result object + /// resolve context + /// result + public OdSubObject.Types.AccessPDO Convert(EDSsharp.AccessType source, OdSubObject.Types.AccessPDO destination, ResolutionContext context) + { + switch (source) + { + case EDSsharp.AccessType.rw: + return OdSubObject.Types.AccessPDO.Tr; + case EDSsharp.AccessType.rwr: + return OdSubObject.Types.AccessPDO.T; + case EDSsharp.AccessType.rww: + case EDSsharp.AccessType.@const: + return OdSubObject.Types.AccessPDO.R; + case EDSsharp.AccessType.ro: + case EDSsharp.AccessType.wo: + case EDSsharp.AccessType.UNKNOWN: + default: + return OdSubObject.Types.AccessPDO.No; + } + } + } +}