public void PortInputFormatSingleEncoder_Decode(string dataAsString, byte expectedPortId, byte expectedMode, uint expectedDeltaInterval, bool expectedNotificationEnabled) { // arrange var data = BytesStringUtil.StringToData(dataAsString); // act var message = MessageEncoder.Decode(data, null) as PortInputFormatSingleMessage; // assert Assert.Equal(expectedPortId, message.PortId); Assert.Equal(expectedMode, message.ModeIndex); Assert.Equal(expectedDeltaInterval, message.DeltaInterval); Assert.Equal(expectedNotificationEnabled, message.NotificationEnabled); // reverse var reverseMessage = new PortInputFormatSingleMessage(expectedPortId, expectedMode, expectedDeltaInterval, expectedNotificationEnabled) { HubId = 0, }; var reverseData = MessageEncoder.Encode(reverseMessage, null); var reverseDataAsString = BytesStringUtil.DataToString(reverseData); Assert.Equal(dataAsString, reverseDataAsString); }
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); }
public void Encode(PortInputFormatSingleMessage message, in Span<byte> data)
public void ReceiveMessage(IMessage message) { if (message.MessageType == MessageType.Hub__Attached_IO) { var attachedIOMessage = new AttachedIOMessage(message.Bytes.ToArray()); switch (attachedIOMessage.Event) { case IOEvent.Attached_IO: switch (attachedIOMessage.DeviceType) { case IODeviceType.TechnicMotorL: ConnectedDevices[attachedIOMessage.Port] = new TechnicMotorL(this, attachedIOMessage.Port); break; case IODeviceType.TechnicMotorXL: ConnectedDevices[attachedIOMessage.Port] = new TechnicMotorXL(this, attachedIOMessage.Port); break; } break; case IOEvent.Attached_Virtual_IO: switch (attachedIOMessage.DeviceType) { case IODeviceType.LED_Light: ConnectedDevices[attachedIOMessage.Port] = new LED(this, attachedIOMessage.Port); break; } break; } } else if (message.MessageType == MessageType.Port_Information) { var portInformationMessage = new PortInformationMessage(message.Bytes.ToArray()); if (ConnectedDevices.ContainsKey(portInformationMessage.Port)) { ConnectedDevices[portInformationMessage.Port].ReceiveMessage(portInformationMessage); } } else if (message.MessageType == MessageType.Port_Mode_Information) { var portModeInformationMessage = new PortModeInformationMessage(message.Bytes.ToArray()); if (ConnectedDevices.ContainsKey(portModeInformationMessage.Port)) { ConnectedDevices[portModeInformationMessage.Port].ReceiveMessage(portModeInformationMessage); } } else if (message.MessageType == MessageType.Port_Input_Format__Single) { var portInputFormatMessage = new PortInputFormatSingleMessage(message.Bytes.ToArray()); if (ConnectedDevices.ContainsKey(portInputFormatMessage.Port)) { ConnectedDevices[portInputFormatMessage.Port].ReceiveMessage(portInputFormatMessage); } } else if (message.MessageType == MessageType.Port_Input_Format__Combined_Mode) { var portInputFormatMessage = new PortInputFormatCombinedMessage(message.Bytes.ToArray()); if (ConnectedDevices.ContainsKey(portInputFormatMessage.Port)) { ConnectedDevices[portInputFormatMessage.Port].ReceiveMessage(portInputFormatMessage); } } else if (message.MessageType == MessageType.Port_Value__Single) { var portValueMessage = new PortValueSingleMessage(message.Bytes.ToArray()); if (ConnectedDevices.ContainsKey(portValueMessage.Port)) { ConnectedDevices[portValueMessage.Port].ReceiveMessage(portValueMessage); } } else if (message.MessageType == MessageType.Port_Value__Combined_Mode) { var portValueMessage = new PortValueCombinedMessage(message.Bytes.ToArray()); if (ConnectedDevices.ContainsKey(portValueMessage.Port)) { ConnectedDevices[portValueMessage.Port].ReceiveMessage(portValueMessage); } } else { Messages.Enqueue(message); } }