public void HubActionsEncoder_Encode(HubAction action, string expectedData) { // arrange var message = new HubActionMessage(action); // act var data = MessageEncoder.Encode(message, null); // assert Assert.Equal(expectedData, BytesStringUtil.DataToString(data)); }
public static byte[] Encode(LegoWirelessMessage message, ProtocolKnowledge knowledge) { var messageType = message switch { HubPropertyMessage msg => MessageType.HubProperties, HubActionMessage msg => MessageType.HubActions, HubAlertMessage msg => MessageType.HubAlerts, HubAttachedIOMessage msg => MessageType.HubAttachedIO, GenericErrorMessage msg => MessageType.GenericErrorMessages, PortInformationRequestMessage msg => MessageType.PortInformationRequest, PortModeInformationRequestMessage msg => MessageType.PortModeInformationRequest, PortInputFormatSetupSingleMessage msg => MessageType.PortInputFormatSetupSingle, PortInputFormatSetupCombinedModeMessage msg => MessageType.PortInputFormatSetupCombinedMode, PortInformationMessage msg => MessageType.PortInformation, PortModeInformationMessage msg => MessageType.PortModeInformation, PortValueSingleMessage msg => MessageType.PortValueSingle, PortValueCombinedModeMessage msg => MessageType.PortValueCombinedMode, PortInputFormatSingleMessage msg => MessageType.PortInputFormatSingle, PortInputFormatCombinedModeMessage msg => MessageType.PortInputFormatCombinedMode, VirtualPortSetupMessage msg => MessageType.VirtualPortSetup, PortOutputCommandMessage msg => MessageType.PortOutputCommand, PortOutputCommandFeedbackMessage msg => MessageType.PortOutputCommandFeedback, _ => throw new NotImplementedException(), }; var encoder = CreateEncoder(messageType, knowledge); var contentLength = encoder.CalculateContentLength(message); var commonHeaderLength = (contentLength + 3 > 127) ? 4 : 3; byte[] data = new byte[commonHeaderLength + contentLength]; CommonMessageHeaderEncoder.Encode(contentLength, message.HubId, messageType, data.AsSpan().Slice(0, commonHeaderLength)); encoder.Encode(message, data.AsSpan().Slice(commonHeaderLength)); return(data); }