Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
3.5 kB
2
Indexable
Never
using System;
using System.Collections.Generic;

public class ErrorInfo
{
    public string ErrorType { get; }
    public double Min { get; }
    public double Max { get; }
    public string Result { get; }

    public ErrorInfo(string errorType, double min, double max, string result)
    {
        ErrorType = errorType;
        Min = min;
        Max = max;
        Result = result;
    }
}

public class ErrorInfoManager
{
    private Dictionary<string, ErrorInfo> errorInfoMap = new Dictionary<string, ErrorInfo>();
    private List<(string ErrorType, DateTime ExpiryTime)> activeWarnings = new List<(string, DateTime)>();

    public void AddErrorInfo(ErrorInfo errorInfo)
    {
        errorInfoMap[errorInfo.ErrorType] = errorInfo;
    }

    public string CheckWarning(double value)
    {
        foreach (var kvp in errorInfoMap)
        {
            string errorType = kvp.Key;
            var errorInfo = kvp.Value;

            if (value >= errorInfo.Min && value <= errorInfo.Max)
            {
                // Check if there is an active warning for this error type
                if (!IsWarningActive(errorType))
                {
                    // Add a new active warning with the expiry time (current time + 10 seconds)
                    activeWarnings.Add((errorType, DateTime.Now.AddSeconds(10)));

                    // Return the warning result
                    return $"{errorInfo.Result} (Error Type: {errorType})";
                }
                else
                {
                    // There is an active warning; check if it has expired
                    if (DateTime.Now > GetExpiryTime(errorType))
                    {
                        // Remove the expired warning
                        activeWarnings.RemoveAll(warning => warning.ErrorType == errorType);
                    }
                    else
                    {
                        // Return the warning result
                        return $"{errorInfo.Result} (Error Type: {errorType})";
                    }
                }
            }
        }

        return "OK"; // No warnings; return OK
    }

    private bool IsWarningActive(string errorType)
    {
        // Check if there is an active warning for the specified error type
        return activeWarnings.Exists(warning => warning.ErrorType == errorType);
    }

    private DateTime GetExpiryTime(string errorType)
    {
        // Get the expiry time for the active warning of the specified error type
        return activeWarnings.Find(warning => warning.ErrorType == errorType).ExpiryTime;
    }
}

class Program
{
    static void Main()
    {
        var errorInfoManager = new ErrorInfoManager();

        // Add error information to the manager
        errorInfoManager.AddErrorInfo(new ErrorInfo("Warning1", 10.5, 20.5, "Warning 1"));
        errorInfoManager.AddErrorInfo(new ErrorInfo("Error1", 30.5, 40.5, "Error 1"));
        errorInfoManager.AddErrorInfo(new ErrorInfo("Warning2", 50.5, 60.5, "Warning 2"));

        // Simulate processing data every 50 ms for a certain value
        for (int i = 0; i < 10; i++)
        {
            double valueToCheck = 15.5 + i * 5; // Simulating different values
            string result = errorInfoManager.CheckWarning(valueToCheck);
            Console.WriteLine($"Value: {valueToCheck}, Result: {result}");

            // Simulate waiting 50 ms for the next data point
            System.Threading.Thread.Sleep(50);
        }
    }
}