namespace EEMSUIClient.Services; public class ClientService( AddressInfo address, RunningData runningData, IClientBaseProvider provider) { private IClientCaller? clientCaller; private Timer? _timer; public bool Initialize() { if (this.clientCaller is not null) return false; if (string.IsNullOrEmpty(address.Ip) || address.Port < 0 || string.IsNullOrEmpty(address.HubName)) return false; ClientCaller temp = new(provider); if (!temp.Initialize()) return false; if (!temp.Open(address.Ip, address.Port, address.HubName, 5)) return false; this.clientCaller = temp; return true; } public Guid RegisterDevice(DeviceInfo deviceInfo) { if (clientCaller is null) return Guid.Empty; return clientCaller.RegisterDevice(deviceInfo).Result; } public bool UpdateRealTimeData(Dictionary realtimeData) { if (clientCaller is null) return false; return clientCaller.UpdateRealTimeData(realtimeData).Result; } public bool StartDataCollectionService() { _fakeRecipe.DeviceId = runningData.DeviceInfo.Guid ?? Guid.Empty; _timer?.Dispose(); _timer = new(TimerCallback, null, 0, 1000 * interval); //TimerCallback(null); return true; } Recipe _fakeRecipe = new() { DeviceId = runningData.DeviceInfo.Guid ?? Guid.Empty, RecipeInfo = [], CurrentStepName = $"Step {coutner}", NextStepName = $"Step {coutner + 1}", CurrentStepRemainTime = 0, CurrentStepTotalTime = max, TotalRemainTime = 0, TotalTime = max * cycle }; static int interval = 1; static int coutner = 1; static int max = 10; static int cycle = 3; private void TimerCallback(object? state) { if (clientCaller is null) return; _fakeRecipe.CurrentStepRemainTime += interval; _fakeRecipe.TotalRemainTime += interval; clientCaller.UpdateRecipeInfo(_fakeRecipe); if (_fakeRecipe.CurrentStepRemainTime >= max) { coutner++; _fakeRecipe.CurrentStepRemainTime = 0; _fakeRecipe.CurrentStepName = $"Step {coutner}"; if (coutner == 3) _fakeRecipe.NextStepName = "Finish"; else _fakeRecipe.NextStepName = $"Step {coutner + 1}"; } if (_fakeRecipe.TotalRemainTime >= max * cycle) { _timer?.Dispose(); coutner = 1; _fakeRecipe = new() { DeviceId = runningData.DeviceInfo.Guid ?? Guid.Empty, RecipeInfo = [], CurrentStepName = $"Step {coutner}", NextStepName = $"Step {coutner + 1}", CurrentStepRemainTime = 0, CurrentStepTotalTime = max, TotalRemainTime = 0, TotalTime = max * cycle }; return; } } public void Dispose() { clientCaller?.Dispose(); clientCaller = null; } }