示例#1
0
        public void ShouldParseSetProbeTemp()
        {
            // Arrange
            var bytes = Encoding.ASCII.GetBytes("UF300!");

            // Act
            var parsed = RequestFactory.CreateFromBytes(bytes);

            // Assert
            Assert.Equal(RequestCodes.SET_PROBE_TEMP, parsed.Code);
        }
示例#2
0
        public void ShouldParsePowerOffGrill()
        {
            // Arrange
            var bytes = Encoding.ASCII.GetBytes(RequestCodes.POWER_OFF);

            // Act
            var parsed = RequestFactory.CreateFromBytes(bytes);

            // Assert
            Assert.Equal(RequestCodes.POWER_OFF, parsed.Code);
        }
示例#3
0
        public void ShouldParseSendGrillFirmware()
        {
            // Arrange
            var bytes = Encoding.ASCII.GetBytes(RequestCodes.GRILL_FIRMWARE);

            // Act
            var parsed = RequestFactory.CreateFromBytes(bytes);

            // Assert
            Assert.Equal(RequestCodes.GRILL_FIRMWARE, parsed.Code);
        }
示例#4
0
        public void Listen(CancellationToken token = default(CancellationToken))
        {
            var address = string.IsNullOrWhiteSpace(options.Address) ||
                          string.Equals("*", options.Address, StringComparison.OrdinalIgnoreCase)
                ? IPAddress.Any
                : IPAddress.Parse(options.Address);

            var endpoint = new IPEndPoint(address, options.Port);
            var client   = new UdpClient(endpoint);
            var sender   = new IPEndPoint(IPAddress.Any, 0);

            Log.Logger.Information("Now listening for traffic on at {address}:{port}",
                                   address, options.Port);

            while (!token.IsCancellationRequested)
            {
                var requestBytes = client.Receive(ref sender);
                var requestTxt   = Encoding.ASCII.GetString(requestBytes);
                Log.Logger.Information("Recieved Request: [{ip}] -> {requestTxt}", sender, requestTxt);

                var request = RequestFactory.CreateFromBytes(requestBytes);
                if (request == null)
                {
                    Log.Logger.Warning("Invalid Request: [{ip}] -> [{requestTxt}] not supported!", sender, requestTxt);
                    continue;
                }

                Log.Logger.Information("Parsed Request: [{ip}] -> {name} ({type})",
                                       sender, request.GetType().Name.ToWords(), request.ToString());

                var response      = emulator.HandleRequest(request);
                var responseBytes = response.ToBytes();

                client.Send(responseBytes, responseBytes.Length, sender);
                Log.Logger.Information("Sent Response: [{ip}] -> {name} ({data})",
                                       sender, response.GetType().Name.ToWords(), response.ToString());
            }
        }