using System; using System.Collections.Generic; using System.Threading.Tasks; using Haoliang.Models.Device; using Haoliang.Models.DataCollection; namespace Haoliang.Core.Services { public interface IDeviceCollectionService { Task CollectAllDevicesAsync(); Task CollectDeviceAsync(int deviceId); Task CollectDeviceDataAsync(int deviceId); Task PingDeviceAsync(string ipAddress); Task GetDeviceDataAsync(string httpUrl); Task ProcessCollectedDataAsync(CollectionResult result); Task> GetCollectionHistoryAsync(int deviceId, DateTime startDate, DateTime endDate); Task GetCollectionStatisticsAsync(DateTime date); Task GetCollectionHealthAsync(); Task RestartFailedCollectionsAsync(); Task TestConnectionAsync(int deviceId); } public interface IPingService { Task PingAsync(string ipAddress); Task GetPingTimeAsync(string ipAddress); Task IsDeviceOnlineAsync(string ipAddress); Task GetPingResultAsync(string ipAddress); } public interface IDataParserService { Task ParseDeviceDataAsync(string rawJson, int deviceId); Task ParseTagDataAsync(object tagValue, string dataType); Task ValidateDeviceDataAsync(DeviceCurrentStatus data); Task ConvertDataFormatAsync(object value, string dataType); } public interface IDataStorageService { Task SaveCollectionResultAsync(CollectionResult result); Task SaveDeviceStatusAsync(DeviceCurrentStatus status); Task SaveRawDataAsync(int deviceId, string rawJson, bool isSuccess, string errorMessage = null); Task LogCollectionAsync(int deviceId, LogLevel logLevel, string message, string data = null); Task ArchiveOldDataAsync(int daysToKeep = 30); } public class PingResult { public bool IsSuccess { get; set; } public int PingTime { get; set; } public string ErrorMessage { get; set; } public DateTime PingTime { get; set; } } public interface IRetryService { Task ExecuteWithRetryAsync(Func> operation, int maxRetries = 3, int delayMs = 30000); Task ExecuteWithRetryAsync(Func operation, int maxRetries = 3, int delayMs = 30000); bool ShouldRetry(Exception ex, int attemptNumber); } public class CollectionException : Exception { public int DeviceId { get; set; } public string DeviceCode { get; set; } public CollectionErrorType ErrorType { get; set; } public CollectionException(int deviceId, string deviceCode, string message, CollectionErrorType errorType) : base(message) { DeviceId = deviceId; DeviceCode = deviceCode; ErrorType = errorType; } } public enum CollectionErrorType { DeviceOffline, NetworkError, DataParseError, DatabaseError, Unknown } }