Exemplo n.º 1
0
        private void OnHubAttachedIOMessage(HubAttachedIOMessage hubAttachedIO)
        {
            Port port = null;

            switch (hubAttachedIO)
            {
            case HubAttachedIOForAttachedDeviceMessage attachedDeviceMessage:
                port = Port(attachedDeviceMessage.PortId);

                var device = _deviceFactory.CreateConnected(attachedDeviceMessage.IOTypeId, Protocol, attachedDeviceMessage.HubId, attachedDeviceMessage.PortId);

                port.AttachDevice(device, attachedDeviceMessage.IOTypeId);
                break;

            case HubAttachedIOForDetachedDeviceMessage detachedDeviceMessage:
                port = Port(detachedDeviceMessage.PortId);

                port.DetachDevice();

                if (port.IsVirtual)
                {
                    _ports.Remove(port.PortId, out var _);
                }

                break;
                // case HubAttachedIOForAttachedVirtualDeviceMessage attachedVirtualDeviceMessage:
                //     OnHubAttachedVirtualIOMessage(attachedVirtualDeviceMessage);

                //     break;
            }
        }
Exemplo n.º 2
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);
        }
Exemplo n.º 3
0
        private void OnHubAttachedIOMessage(HubAttachedIOMessage hubAttachedIO)
        {
            Port port = null;

            switch (hubAttachedIO)
            {
            case HubAttachedIOForAttachedDeviceMessage attachedDeviceMessage:
                port = Port(attachedDeviceMessage.PortId);
                if (port == null)
                {
                    _logger?.LogInformation($"Hub sent notification of attached device with port id '{hubAttachedIO.PortId}' but hub type '{GetType().Name}' is not configured for this port");
                    return;
                }
                var device = _deviceFactory.CreateConnected(attachedDeviceMessage.IOTypeId, Protocol, attachedDeviceMessage.HubId, attachedDeviceMessage.PortId);

                port.AttachDevice(device, attachedDeviceMessage.IOTypeId);
                break;

            case HubAttachedIOForDetachedDeviceMessage detachedDeviceMessage:
                port = Port(detachedDeviceMessage.PortId);
                if (port == null)
                {
                    _logger?.LogInformation($"Hub sent notification of detached device with port id '{hubAttachedIO.PortId}' but hub type '{GetType().Name}' is not configured for this port");
                    return;
                }

                port.DetachDevice();

                if (port.IsVirtual)
                {
                    _ports.Remove(port.PortId, out var _);
                }

                break;

                // Note - HubAttachedIOForAttachedVirtualDeviceMessage is handled directly in OnHubChange not here
            }
        }