Ejemplo n.º 1
0
 private async void SerialPortCommunicationService_DataReceived(
     object sender,
     SerialPortDataReceivedEventArgs e)
 {
     var communicationService = ((ISerialPortCommunicationService)sender);
     var webSocket            = (WebSocket)communicationService.State;
     await webSocket.SendAsync(
         new ArraySegment <byte>(e.Data, 0, e.Data.Length),
         WebSocketMessageType.Text,
         true,
         CancellationToken.None);
 }
Ejemplo n.º 2
0
 public void TestDataReceived(SerialPortDataReceivedEventArgs args)
 {
     DataReceived?.Invoke(
         this,
         args);
 }
Ejemplo n.º 3
0
        public async Task GivenHttpContext_AndSerialPortMonitorService_AndRequestCorrect_AndPortDisconnectedDuringCommsLoop_WhenInvoke_ThenRXData_ThenPortDisconnected_AndTaskCancelled()
        {
            //Arrange
            var mockSerialPortConnectionManager    = new Mock <ISerialPortConnectionManager>();
            var mockSerialPortCommunicationService = new Mock <ISerialPortCommunicationService>();
            var mockWebSocketConnectionService     = new Mock <IWebSocketConnectionService>();
            var mockWebSocketTerminalService       = new Mock <IWebSocketTerminalService>();
            var mockRequestDelegate          = new Mock <RequestDelegate>();
            var mockSerialPortMonitorService = new Mock <ISerialPortMonitorService>();
            var mockHttpContext      = new Mock <HttpContext>();
            var mockWebSocket        = new Mock <WebSocket>();
            var mockWebSocketManager = new Mock <WebSocketManager>();
            var mockHttpRequest      = new Mock <HttpRequest>();
            var mockSerialPort       = new Mock <ISerialPort>();
            var sut = new TerminalMiddleware(
                mockRequestDelegate.Object,
                mockSerialPortConnectionManager.Object,
                mockWebSocketConnectionService.Object,
                Mock.Of <ILogger <TerminalMiddleware> >());

            mockWebSocket.Setup(x => x.SendAsync(
                                    It.IsAny <ArraySegment <byte> >(),
                                    It.IsAny <WebSocketMessageType>(),
                                    It.IsAny <bool>(),
                                    It.IsAny <CancellationToken>()))
            .Returns(Task.FromResult(true));
            mockWebSocketManager.Setup(x => x.AcceptWebSocketAsync())
            .Returns(Task.FromResult(mockWebSocket.Object));
            mockWebSocketManager.SetupGet(x => x.IsWebSocketRequest)
            .Returns(true);
            mockHttpContext.SetupGet(x => x.WebSockets)
            .Returns(mockWebSocketManager.Object);
            mockHttpContext.SetupGet(x => x.Request)
            .Returns(mockHttpRequest.Object);
            mockHttpRequest.SetupGet(x => x.Path)
            .Returns("/terminal?portName=COM1&baudRate=9600&parity=None&dataBits=8&stopBits=One&Handshake=None");
            mockWebSocketConnectionService.Setup(x => x.Create(
                                                     It.IsAny <HttpContext>()))
            .ReturnsAsync(mockWebSocketTerminalService.Object);
            mockRequestDelegate.Setup(x => x.Invoke(
                                          It.IsAny <HttpContext>()));

            mockSerialPortCommunicationService.SetupGet(x => x.PortName)
            .Returns("COM1");

            mockSerialPortConnectionManager.Setup(x => x.GetOrOpen(
                                                      It.IsAny <HttpRequest>()))
            .Returns(mockSerialPortCommunicationService.Object);

            mockWebSocketTerminalService.Setup(x => x.RunCommsLoopAsync(
                                                   It.IsAny <ISerialPortCommunicationService>(),
                                                   It.IsAny <CancellationToken>()))
            .Returns((ISerialPortCommunicationService commsService, CancellationToken ct) =>
            {
                return(Task.Delay(50000, ct));
            });

            mockSerialPortCommunicationService.SetupGet(x => x.State)
            .Returns(mockWebSocket.Object);

            mockSerialPortCommunicationService.SetupGet(x => x.SerialPort)
            .Returns(mockSerialPort.Object);

            mockSerialPort.SetupGet(x => x.Encoding)
            .Returns(Encoding.ASCII);

            var dataString = "Hello World!";
            var dataBytes  = Encoding.ASCII.GetBytes(dataString);
            var dataBuffer = new ArraySegment <byte>(dataBytes, 0, dataBytes.Length);

            var eventArgs = new SerialPortDataReceivedEventArgs
            {
                SerialPort = mockSerialPort.Object,
                Data       = dataBuffer.Array
            };

            // Act
            var invokeTask = sut.Invoke(
                mockHttpContext.Object,
                mockSerialPortMonitorService.Object);
            await Task.Delay(500).ConfigureAwait(false);

            mockSerialPortCommunicationService.Raise(x => x.DataReceived += null, new object[] { mockSerialPortCommunicationService.Object, eventArgs });
            await Task.Delay(500).ConfigureAwait(false);

            mockWebSocket.Verify(x => x.SendAsync(
                                     It.Is <ArraySegment <byte> >(y => Convert.ToBase64String(y.Array) == Convert.ToBase64String(dataBuffer.Array)),
                                     It.Is <WebSocketMessageType>(y => y == WebSocketMessageType.Text),
                                     It.Is <bool>(y => y == true),
                                     It.Is <CancellationToken>(y => y == CancellationToken.None)), Times.Once);
            mockSerialPortMonitorService.Raise(x => x.PortsDisconnected += null, new PortsDisconnectedEventArgs()
            {
                SerialPorts = new string[] { "COM1" }
            });
            await Task.Delay(500).ConfigureAwait(false);

            Assert.True(invokeTask.IsCanceled);
        }