using Microsoft.AspNetCore.SignalR.Client; using System; using System.Collections.Generic; using System.Data.Common; using System.Linq; using System.Text; using System.Text.Json; using System.Threading.Tasks; using VeloeMonitorApp.Models; namespace VeloeMonitorApp.ViewModels { public class IndexViewModel : ViewModelBase { HubConnection hubConnection; public IndexViewModel() { HardwareData = new() { {"cpuload", new SimpleDataModel() {Title = "CPU"} }, {"ramload", new SimpleDataModel() {Title = "RAM"} }, {"ramavailable", new SimpleDataModel() {Title = "RAM Available"} }, {"ramused", new SimpleDataModel() {Title = "RAM Used"} }, {"root_directory", new SimpleDataModel() {Title = "SSD"} }, {"_mnt_nas", new SimpleDataModel() {Title = "HDD"} }, }; hubConnection = new HubConnectionBuilder() .WithUrl("https://monitor.veloe.link/hubs/data") .WithAutomaticReconnect() .Build(); Func reconnecting = ex => Task.Run(() => { //_logger.Warning("Reconnecting to WebCoket..."); }); Func reconnected = str => Task.Run(() => { //_logger.Warning("Reconnected to WebCoket."); //foreach (var server in serverInfo) //{ // _connection.InvokeAsync("ConnectToGroup", server.Key); //} }); hubConnection.Reconnecting += reconnecting; hubConnection.Reconnected += reconnected; hubConnection.On("Update", (data) => { //{"cpuload":5.239773,"ramavailable":11.503387,"ramused":10.2684555,"ramload":89.264626,"root_directory":0.23512806,"_mnt_nas":0.761549} var dictionary = JsonSerializer.Deserialize>(data); if (dictionary == null) return; double value; foreach (var kvp in dictionary) { value = kvp.Value; HardwareData[kvp.Key].Value = value; } }); } public Dictionary HardwareData { get; set; } } }