Beispiel #1
0
        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);
        }
Beispiel #2
0
        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);
            }
        }
    private static void DefaultAsserts(byte expectedPort, byte expectedMode, PortModeInformationType expectedType, PortModeInformationMessage message)
    {
        Assert.NotNull(message);

        Assert.Equal(expectedPort, message.PortId);
        Assert.Equal(expectedMode, message.Mode);
        Assert.Equal(expectedType, message.InformationType);
    }