private static T GetDataCommand <T>(Datagram datagram) where T : class, IDataCommand { var dataCommandType = Assembly.GetExecutingAssembly().GetTypes().FirstOrDefault(x => x.GetCustomAttribute <DataCommandAttribute>(false) != null && (byte)(x.GetCustomAttribute <DataCommandAttribute>(false) as DataCommandAttribute).DataCommandIdentity == datagram.Command && x.GetInterface(typeof(T).Name) != null); if (dataCommandType == null) { return(null); } if (datagram.Content == null || datagram.Content.Length == 0) { var dataCommand = Activator.CreateInstance(dataCommandType) as T; dataCommand.SerialNumber = datagram.SerialNumber; return(dataCommand); } else { var dataCommand = Activator.CreateInstance(dataCommandType) as T; dataCommand.SerialNumber = datagram.SerialNumber; dataCommand.PayloadObject = BinEncoder.Decode(datagram.Content, dataCommandType.GetCustomAttribute <DataCommandAttribute>().EntityType); return(dataCommand); } }
public static Datagram Create(IDataCommandResponse dataCommandResponse) { return(Create((byte)dataCommandResponse.Id, 0x00, SerialNumber, BinEncoder.Encode(dataCommandResponse.PayloadObject))); }
public static Datagram Create(IDataCommandRequest dataCommandRequest) { return(Create((byte)dataCommandRequest.Id, dataCommandRequest.NeedResponse ? (byte)(DatagramFlag.Request | DatagramFlag.NeedResponse) : (byte)DatagramFlag.Request, SerialNumber, BinEncoder.Encode(dataCommandRequest.PayloadObject))); }