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

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

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

            if (!int.TryParse(integerInputValue, out int value))
            {
                _logger.LogError($"{Messages.InvalidIntegerValue} (Parameter '{nameof(integerInputValue)}')");
                throw new ArgumentException(Messages.InvalidIntegerValue, nameof(integerInputValue));
            }

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

            return(readingModel);
        }
        public void HandleIntegerReadingTest(string timestampInputValue, string integerInputValue, IntegerReadingModel expected)
        {
            // Arrange
            MethodInfo methodInfo = _methodInfos
                                    .Where(x => x.Name.Equals("HandleIntegerReading") && x.IsPrivate)
                                    .First();

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

            // Assert
            actual
            .Should()
            .NotBeNull()
            .And
            .Equals(expected);
        }
Example #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);
        }