253 lines
8.6 KiB
C#
253 lines
8.6 KiB
C#
namespace LaaProductionWeb.Blazor.Components.HeliumTester.Models
|
|
{
|
|
using Microsoft.AspNetCore.SignalR.Client;
|
|
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Reflection;
|
|
|
|
public class RecipesCollectionModel
|
|
{
|
|
private const string addToGroup = "AddToGroupAsync";
|
|
private const string recipes = nameof(recipes);
|
|
private HubConnection changeHub;
|
|
|
|
private readonly HeliumTesterService heliumTesterService;
|
|
private readonly List<HeliumRecipeModel> heliumRecieps;
|
|
private readonly List<WaterRecipeModel> waterRecieps;
|
|
private readonly MatchCollectionModel matchCollectionModel;
|
|
|
|
public RecipesCollectionModel(HeliumTesterService heliumTesterService, MatchCollectionModel matchCollectionModel)
|
|
{
|
|
this.heliumTesterService = heliumTesterService;
|
|
this.matchCollectionModel = matchCollectionModel;
|
|
|
|
this.VHeliumRecipe = new HeliumRecipeModel();
|
|
this.VWaterRecipe = new WaterRecipeModel();
|
|
this.heliumRecieps = [];
|
|
this.waterRecieps = [];
|
|
|
|
this.VHeliumRecipe.PropertyChanged += this.VHeliumRecipePropertyChanged;
|
|
this.VWaterRecipe.PropertyChanged += this.VWaterRecipePropertyChanged;
|
|
|
|
this.LoadRecipes();
|
|
|
|
this.changeHub = new HubConnectionBuilder()
|
|
#if DEBUG
|
|
.WithUrl("https://localhost:44354/changes")
|
|
#else
|
|
.WithUrl("http://sla12iis01.emea.sensus.net/LaaProductionWeb/Blazor/changes")
|
|
#endif
|
|
.Build();
|
|
|
|
this.changeHub.On<string>(nameof(this.NotifyChangedAsync), this.NotifyChangedAsync);
|
|
this.changeHub.StartAsync().Wait();
|
|
this.changeHub.SendAsync(addToGroup, recipes);
|
|
}
|
|
|
|
private void NotifyChangedAsync(string group)
|
|
{
|
|
if (group == recipes)
|
|
{
|
|
this.LoadRecipes();
|
|
this.RecipesChanged?.Invoke();
|
|
}
|
|
}
|
|
|
|
public event Action RecipesChanged;
|
|
|
|
public HeliumRecipeModel VHeliumRecipe { get; }
|
|
|
|
public WaterRecipeModel VWaterRecipe { get; }
|
|
|
|
public IEnumerable<HeliumRecipeModel> LoadHeliumRecieps()
|
|
{
|
|
var heliumRecieps = this.heliumRecieps.AsEnumerable();
|
|
|
|
if (heliumRecieps.Any())
|
|
{
|
|
var selectedRegionKeys = matchCollectionModel
|
|
.Regions
|
|
.Where(x => x.Selected)
|
|
.Select(x => x.Key);
|
|
|
|
if (selectedRegionKeys.Any())
|
|
{
|
|
heliumRecieps = heliumRecieps.Where(x => selectedRegionKeys.Any(r => r == x.MatchRegion));
|
|
}
|
|
|
|
var selectedMeterSizesKeys = matchCollectionModel
|
|
.MeterSizes
|
|
.Where(x => x.Selected)
|
|
.Select(x => x.Key);
|
|
|
|
if (selectedMeterSizesKeys.Any())
|
|
{
|
|
heliumRecieps = heliumRecieps.Where(x => selectedMeterSizesKeys.Any(r => r == x.MatchMeterSize));
|
|
}
|
|
|
|
var selectedBodyLengthKeys = matchCollectionModel
|
|
.BodyLengths
|
|
.Where(x => x.Selected)
|
|
.Select(x => x.Key);
|
|
|
|
if (selectedBodyLengthKeys.Any())
|
|
{
|
|
heliumRecieps = heliumRecieps.Where(x => selectedBodyLengthKeys.Any(r => r == x.MatchBodyLength));
|
|
}
|
|
|
|
var selectedPressureSensorKeys = matchCollectionModel
|
|
.PressureSensors
|
|
.Where(x => x.Selected)
|
|
.Select(x => x.Key);
|
|
|
|
if (selectedPressureSensorKeys.Any())
|
|
{
|
|
heliumRecieps = heliumRecieps.Where(x => selectedPressureSensorKeys.Any(r => r == x.MatchPressureSensor));
|
|
}
|
|
}
|
|
|
|
return heliumRecieps.ToList();
|
|
}
|
|
|
|
public IEnumerable<WaterRecipeModel> LoadWaterRecieps()
|
|
{
|
|
var waterRecieps = this.waterRecieps.AsEnumerable();
|
|
|
|
if (waterRecieps.Any())
|
|
{
|
|
var selectedRegionKeys = this.matchCollectionModel
|
|
.Regions
|
|
.Where(x => x.Selected)
|
|
.Select(x => x.Key);
|
|
|
|
if (selectedRegionKeys.Any())
|
|
{
|
|
waterRecieps = waterRecieps.Where(x => selectedRegionKeys.Any(r => r == x.MatchRegion));
|
|
}
|
|
|
|
var selectedMeterSizesKeys = matchCollectionModel
|
|
.MeterSizes
|
|
.Where(x => x.Selected)
|
|
.Select(x => x.Key);
|
|
|
|
if (selectedMeterSizesKeys.Any())
|
|
{
|
|
waterRecieps = waterRecieps.Where(x => selectedMeterSizesKeys.Any(r => r == x.MatchMeterSize));
|
|
}
|
|
|
|
var selectedBodyLengthKeys = matchCollectionModel
|
|
.BodyLengths
|
|
.Where(x => x.Selected)
|
|
.Select(x => x.Key);
|
|
|
|
if (selectedBodyLengthKeys.Any())
|
|
{
|
|
waterRecieps = waterRecieps.Where(x => selectedBodyLengthKeys.Any(r => r == x.MatchBodyLength));
|
|
}
|
|
|
|
var selectedPressureSensorKeys = matchCollectionModel
|
|
.PressureSensors
|
|
.Where(x => x.Selected)
|
|
.Select(x => x.Key);
|
|
|
|
if (selectedPressureSensorKeys.Any())
|
|
{
|
|
waterRecieps = waterRecieps.Where(x => selectedPressureSensorKeys.Any(r => r == x.MatchPressureSensor));
|
|
}
|
|
}
|
|
|
|
return waterRecieps.ToList();
|
|
}
|
|
|
|
public void LoadRecipes()
|
|
{
|
|
try
|
|
{
|
|
var heliumRecipes = this.heliumTesterService.LoadHeliumRecieps();
|
|
var waterRecieps = this.heliumTesterService.LoadWaterRecieps();
|
|
|
|
this.LoadHeliumRecipes(heliumRecipes);
|
|
this.LoadWaterRecieps(waterRecieps);
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
Console.WriteLine(e);
|
|
}
|
|
}
|
|
|
|
private void LoadHeliumRecipes(IEnumerable<HeliumRecipeModel> heliumRecieps)
|
|
{
|
|
this.heliumRecieps.Clear();
|
|
this.heliumRecieps.AddRange(heliumRecieps);
|
|
|
|
foreach (var heliumRecipe in this.heliumRecieps)
|
|
{
|
|
heliumRecipe.RecipeChanged += this.OnHeliumRecipeChanged;
|
|
}
|
|
}
|
|
|
|
private void LoadWaterRecieps(IEnumerable<WaterRecipeModel> waterRecieps)
|
|
{
|
|
this.waterRecieps.Clear();
|
|
this.waterRecieps.AddRange(waterRecieps);
|
|
|
|
foreach (var waterRecipe in this.waterRecieps)
|
|
{
|
|
waterRecipe.RecipeChanged += this.OnWaterRecipeChanged;
|
|
}
|
|
}
|
|
|
|
private void OnHeliumRecipeChanged(HeliumRecipeModel recipe)
|
|
{
|
|
this.heliumTesterService.UpdateRecipe(HeliumRecipeModel.ToEntity(recipe));
|
|
this.OnRecipeChanged();
|
|
}
|
|
|
|
|
|
private long previousTiks;
|
|
const long minTicks = 1000 * 10_000;
|
|
private void OnRecipeChanged()
|
|
{
|
|
var currentTicks = DateTime.UtcNow.Ticks;
|
|
var difference = currentTicks - previousTiks;
|
|
|
|
if (difference > minTicks)
|
|
{
|
|
this.changeHub.SendAsync(nameof(this.NotifyChangedAsync), recipes);
|
|
}
|
|
|
|
previousTiks = DateTime.UtcNow.Ticks;
|
|
}
|
|
|
|
private void OnWaterRecipeChanged(WaterRecipeModel recipe)
|
|
{
|
|
this.heliumTesterService.UpdateRecipe(WaterRecipeModel.ToEntity(recipe));
|
|
this.OnRecipeChanged();
|
|
}
|
|
|
|
private void VHeliumRecipePropertyChanged(string property)
|
|
{
|
|
var propertyInfo = typeof(HeliumRecipeModel).GetProperty(property, BindingFlags.Public | BindingFlags.Instance);
|
|
var value = propertyInfo.GetValue(this.VHeliumRecipe);
|
|
|
|
foreach (var heliumRecipe in this.LoadHeliumRecieps())
|
|
{
|
|
propertyInfo.SetValue(heliumRecipe, value);
|
|
}
|
|
}
|
|
|
|
private void VWaterRecipePropertyChanged(string property)
|
|
{
|
|
var propertyInfo = typeof(WaterRecipeModel).GetProperty(property, BindingFlags.Public | BindingFlags.Instance);
|
|
var value = propertyInfo.GetValue(this.VWaterRecipe);
|
|
|
|
foreach (var waterRecipe in this.LoadWaterRecieps())
|
|
{
|
|
propertyInfo.SetValue(waterRecipe, value);
|
|
}
|
|
}
|
|
}
|
|
}
|