| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121 |
- 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<string, object> 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;
- }
- }
|