Ejemplo n.º 1
0
        /// <summary>
        /// Parses the raw text input and generates a decimal sensor log record
        /// </summary>
        /// <param name="timestampInputValue">The timestamp string value from the input log record</param>
        /// <param name="decimalInputValue">The decimal string value from the input log record</param>
        /// <exception cref="ArgumentException">timestampInputValue is null or whitespace</exception>
        /// <exception cref="ArgumentException">decimalInputValue is null or whitespace</exception>
        /// <exception cref="ArgumentException">timestampInputValue cannot be parsed to a DateTime</exception>
        /// <exception cref="ArgumentException">decimalInputValue cannot be parsed to a decimal</exception>
        /// <returns>Integer sensor log record</returns>
        private DecimalReadingModel HandleDecimalReading(string timestampInputValue, string decimalInputValue)
        {
            if (string.IsNullOrWhiteSpace(timestampInputValue))
            {
                _logger.LogError($"{Messages.NullWhitespaceString} (Parameter '{nameof(timestampInputValue)}')");
                throw new ArgumentException(Messages.NullWhitespaceString, nameof(timestampInputValue));
            }

            if (string.IsNullOrWhiteSpace(decimalInputValue))
            {
                _logger.LogError($"{Messages.NullWhitespaceString} (Parameter '{nameof(decimalInputValue)}')");
                throw new ArgumentException(Messages.NullWhitespaceString, nameof(decimalInputValue));
            }

            if (!DateTime.TryParse(timestampInputValue, out DateTime timestamp))
            {
                _logger.LogError($"{Messages.InvalidDateTimeValue} (Parameter '{nameof(timestampInputValue)}')");
                throw new ArgumentException(Messages.InvalidDateTimeValue, nameof(timestampInputValue));
            }

            if (!decimal.TryParse(decimalInputValue, out decimal value))
            {
                _logger.LogError($"{Messages.InvalidDecimalValue} (Parameter '{nameof(decimalInputValue)}')");
                throw new ArgumentException(Messages.InvalidDecimalValue, nameof(decimalInputValue));
            }

            DecimalReadingModel readingModel = new DecimalReadingModel
            {
                Timestamp = timestamp,
                Value     = value
            };

            return(readingModel);
        }
        public void HandleDecimalReadingTest(string timestampInputValue, string decimalInputValue, DecimalReadingModel expected)
        {
            // Arrange
            MethodInfo methodInfo = _methodInfos
                                    .Where(x => x.Name.Equals("HandleDecimalReading") && x.IsPrivate)
                                    .First();

            // Act
            DecimalReadingModel actual = (DecimalReadingModel)methodInfo.Invoke(_logFile, new object[] { timestampInputValue, decimalInputValue });

            // Assert
            actual
            .Should()
            .NotBeNull()
            .And
            .Equals(expected);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Generates a sensor log model from the sensor log input text file
        /// </summary>
        /// <param name="logContentsStr">The sensor log input text file</param>
        /// <exception cref="ArgumentException">logContentStr is null or whitespace</exception>
        /// <exception cref="NotImplementedException">Invalid record type is entered</exception>
        /// <returns>The sensor log model representing the sensors and reading log records</returns>
        private SensorLogModel ParseInputLogFile(string logContentsStr)
        {
            if (string.IsNullOrWhiteSpace(logContentsStr))
            {
                _logger.LogError($"{Messages.NullWhitespaceString} (Parameter '{nameof(logContentsStr)}')");
                throw new ArgumentException(Messages.NullWhitespaceString, nameof(logContentsStr));
            }

            SensorLogModel model = new SensorLogModel();

            string[] input          = logContentsStr.Split(new string[] { Environment.NewLine }, StringSplitOptions.None);
            Regex    timeValueRegex = new Regex("[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])T(2[0-3]|[01][0-9]):[0-5][0-9]");

            for (int i = 0; i < input.Length; i++)
            {
                string[] line = input[i].Split(' ');

                switch (line[0])
                {
                case "reference":
                    model.ReferenceValues = HandleReferenceValuesLogRecord(line[1], line[2], line[3]);
                    break;

                case "thermometer":
                    ThermometerModel thermometerModel = new ThermometerModel
                    {
                        Name = line[1]
                    };

                    while (true)
                    {
                        i++;
                        line = input[i].Split(' ');

                        if (timeValueRegex.IsMatch(line[0]))
                        {
                            DecimalReadingModel readingModel = HandleDecimalReading(line[0], line[1]);
                            thermometerModel.Readings.Add(readingModel);
                        }
                        else
                        {
                            i--;
                            break;
                        }

                        if (i + 1 >= input.Length)
                        {
                            break;
                        }
                    }

                    model.ThermometerReadings.Add(thermometerModel);
                    break;

                case "humidity":
                    HumidityModel humidityModel = new HumidityModel
                    {
                        Name = line[1]
                    };

                    while (true)
                    {
                        i++;
                        line = input[i].Split(' ');

                        if (timeValueRegex.IsMatch(line[0]))
                        {
                            DecimalReadingModel readingModel = HandleDecimalReading(line[0], line[1]);
                            humidityModel.Readings.Add(readingModel);
                        }
                        else
                        {
                            i--;
                            break;
                        }

                        if (i + 1 >= input.Length)
                        {
                            break;
                        }
                    }

                    model.HumidityReadings.Add(humidityModel);
                    break;

                case "monoxide":
                    MonoxideModel monoxideModel = new MonoxideModel
                    {
                        Name = line[1]
                    };

                    while (true)
                    {
                        i++;
                        line = input[i].Split(' ');

                        if (timeValueRegex.IsMatch(line[0]))
                        {
                            IntegerReadingModel readingModel = HandleIntegerReading(line[0], line[1]);
                            monoxideModel.Readings.Add(readingModel);
                        }
                        else
                        {
                            i--;
                            break;
                        }

                        if (i + 1 >= input.Length)
                        {
                            break;
                        }
                    }

                    model.MonoxideReadings.Add(monoxideModel);
                    break;

                default:
                    _logger.LogError($"Sensor type {line[0]} has not been implemented yet.");
                    throw new NotImplementedException($"Sensor type {line[0]} has not been implemented yet.");
                }
            }

            return(model);
        }