private void SetupOnHubChange() { var disposable = Protocol.UpstreamMessages .Where(msg => msg switch { PortValueSingleMessage x => false, PortValueCombinedModeMessage x => false, PortInformationMessage x => false, PortOutputCommandFeedbackMessage x => false, PortInputFormatCombinedModeMessage x => false, _ => true, })
public Device(ILegoWirelessProtocol protocol, byte hubId, byte portId) { _protocol = protocol ?? throw new ArgumentNullException(nameof(protocol)); _hubId = hubId; _portId = portId; _portValueObservable = _protocol.UpstreamMessages .Where(msg => msg.HubId == _hubId) .SelectMany(msg => msg switch { PortValueSingleMessage pvsm => pvsm.Data, PortValueCombinedModeMessage pvcmm => pvcmm.Data, _ => Array.Empty <PortValueData>(), })
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 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); } }
public override string ToString() => $"Port Values (Combined Mode) - " + PortValueSingleMessage.FormatPortValueDataArray(HubId, Data);