IndexViewModel.cs 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. using Microsoft.AspNetCore.SignalR.Client;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Data.Common;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Text.Json;
  8. using System.Threading.Tasks;
  9. using VeloeMonitorApp.Models;
  10. namespace VeloeMonitorApp.ViewModels
  11. {
  12. public class IndexViewModel : ViewModelBase
  13. {
  14. HubConnection hubConnection;
  15. public IndexViewModel()
  16. {
  17. HardwareData = new()
  18. {
  19. {"cpuload", new SimpleDataModel() {Title = "CPU"} },
  20. {"ramload", new SimpleDataModel() {Title = "RAM"} },
  21. {"ramavailable", new SimpleDataModel() {Title = "RAM Available"} },
  22. {"ramused", new SimpleDataModel() {Title = "RAM Used"} },
  23. {"root_directory", new SimpleDataModel() {Title = "SSD"} },
  24. {"_mnt_nas", new SimpleDataModel() {Title = "HDD"} },
  25. };
  26. hubConnection = new HubConnectionBuilder()
  27. .WithUrl("https://monitor.veloe.link/hubs/data")
  28. .WithAutomaticReconnect()
  29. .Build();
  30. Func<Exception, Task> reconnecting = ex => Task.Run(() =>
  31. {
  32. //_logger.Warning("Reconnecting to WebCoket...");
  33. });
  34. Func<string, Task> reconnected = str => Task.Run(() =>
  35. {
  36. //_logger.Warning("Reconnected to WebCoket.");
  37. //foreach (var server in serverInfo)
  38. //{
  39. // _connection.InvokeAsync("ConnectToGroup", server.Key);
  40. //}
  41. });
  42. hubConnection.Reconnecting += reconnecting;
  43. hubConnection.Reconnected += reconnected;
  44. hubConnection.On<string>("Update", (data) =>
  45. {
  46. //{"cpuload":5.239773,"ramavailable":11.503387,"ramused":10.2684555,"ramload":89.264626,"root_directory":0.23512806,"_mnt_nas":0.761549}
  47. var dictionary = JsonSerializer.Deserialize<Dictionary<string, double>>(data);
  48. if (dictionary == null) return;
  49. double value;
  50. foreach (var kvp in dictionary)
  51. {
  52. value = kvp.Value;
  53. HardwareData[kvp.Key].Value = value;
  54. }
  55. });
  56. }
  57. public Dictionary<string,SimpleDataModel> HardwareData { get; set; }
  58. }
  59. }