12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667 |
- 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<Exception, Task> reconnecting = ex => Task.Run(() =>
- {
- //_logger.Warning("Reconnecting to WebCoket...");
- });
- Func<string, Task> 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<string>("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<Dictionary<string, double>>(data);
- if (dictionary == null) return;
- double value;
- foreach (var kvp in dictionary)
- {
- value = kvp.Value;
- HardwareData[kvp.Key].Value = value;
- }
- });
- }
- public Dictionary<string,SimpleDataModel> HardwareData { get; set; }
- }
- }
|