Skip to content

Commit

Permalink
Version 0.1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Marakusa committed Jul 31, 2023
1 parent a55ef5d commit 752f3da
Show file tree
Hide file tree
Showing 27 changed files with 577 additions and 331 deletions.
30 changes: 30 additions & 0 deletions CutOverlay/CutOverlay/App/CutOverlayApp.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using Newtonsoft.Json;

namespace CutOverlay.App;

public class CutOverlayApp
{
public async Task Start()
{
Dictionary<string, string?>? configurations = await FetchConfigurationsAsync();

Spotify spotify = new();
await spotify.Start(configurations);
Pulsoid pulsoid = new();
await pulsoid.Start(configurations);
}

private static async Task<Dictionary<string, string?>?> FetchConfigurationsAsync()
{
HttpResponseMessage configurationResponse =
await new HttpClient().GetAsync($"http://localhost:{Globals.Port}/configuration");

Console.WriteLine("Fetching configurations...");
string configurationJson = await configurationResponse.Content.ReadAsStringAsync();

if (configurationResponse.IsSuccessStatusCode)
return JsonConvert.DeserializeObject<Dictionary<string, string?>>(configurationJson);
Console.WriteLine($"ERROR: {configurationJson}");
return new Dictionary<string, string?>();
}
}
42 changes: 42 additions & 0 deletions CutOverlay/CutOverlay/App/OverlayApp.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using Newtonsoft.Json;

namespace CutOverlay.App;

public class OverlayApp : IDisposable
{
private protected readonly HttpClient? HttpClient;

public OverlayApp()
{
HttpClient = new HttpClient();
}

public void Dispose()
{
Unload();
}

public virtual Task Start(Dictionary<string, string?>? configurations)
{
return Task.CompletedTask;
}

private protected async Task<Dictionary<string, string?>?> FetchConfigurationsAsync()
{
HttpResponseMessage configurationResponse =
await HttpClient!.GetAsync($"http://localhost:{Globals.Port}/configuration");

Console.WriteLine("Fetching configurations...");
string configurationJson = await configurationResponse.Content.ReadAsStringAsync();

if (configurationResponse.IsSuccessStatusCode)
return JsonConvert.DeserializeObject<Dictionary<string, string?>>(configurationJson);
Console.WriteLine($"ERROR: {configurationJson}");
return new Dictionary<string, string?>();
}

public virtual void Unload()
{
throw new NotImplementedException();
}
}
113 changes: 113 additions & 0 deletions CutOverlay/CutOverlay/App/Pulsoid.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
using System.Net.WebSockets;
using System.Text;
using CutOverlay.Models;
using Newtonsoft.Json;

namespace CutOverlay.App;

public class Pulsoid : OverlayApp
{
internal static Pulsoid? Instance;

private readonly string _dataFolder = $"{Globals.GetAppDataPath()}data\\";
private string? _pulsoidApiToken;
private int _reconnectInterval = 1000;
private ClientWebSocket? _socket;

public Pulsoid()
{
if (Instance != null)
{
Dispose();
return;
}

Instance = this;

_socket = null;
}

public override Task Start(Dictionary<string, string?>? configurations)
{
Console.WriteLine("Pulsoid app starting...");

if (configurations == null ||
!configurations.ContainsKey("pulsoidAccessToken") ||
string.IsNullOrEmpty(configurations["pulsoidAccessToken"]))
{
Console.WriteLine("Pulsoid access token missing");
return Task.CompletedTask;
}

_pulsoidApiToken = configurations["pulsoidAccessToken"];
_ = SetupWebSocket();

Console.WriteLine("Pulsoid app started!");
return Task.CompletedTask;
}

private async Task SetupWebSocket()
{
Console.WriteLine("Pulsoid web socket setting up...");

string url = $"wss://dev.pulsoid.net/api/v1/data/real_time?access_token={_pulsoidApiToken}";

using ClientWebSocket webSocket = new();
try
{
await webSocket.ConnectAsync(new Uri(url), CancellationToken.None);
_socket = webSocket;

while (_socket.State == WebSocketState.Open) await ReceiveMessage(_socket);
}
catch (Exception ex)
{
Console.WriteLine("WebSocket error: " + ex.Message);
// Attempt reconnection after a certain interval
await Task.Delay(_reconnectInterval);
// Increase the reconnect interval for the next attempt
_reconnectInterval = Math.Min(_reconnectInterval * 2, 60000); // Max 1 minute
_ = SetupWebSocket();
}
}

private async Task ReceiveMessage(WebSocket? webSocket)
{
byte[] buffer = new byte[1024];
WebSocketReceiveResult result =
await webSocket?.ReceiveAsync(new ArraySegment<byte>(buffer), CancellationToken.None)!;
if (result.MessageType == WebSocketMessageType.Text)
{
string data = Encoding.UTF8.GetString(buffer, 0, result.Count);
// Parse JSON data and call the updateHeartRate method with the data
UpdateHeartRate(data);
}
}

private void UpdateHeartRate(string data)
{
PulsoidResponse? response = JsonConvert.DeserializeObject<PulsoidResponse>(data);

if (!Directory.Exists(_dataFolder)) Directory.CreateDirectory(_dataFolder);
string statusFile = $"{_dataFolder}pulsoid.txt";

File.WriteAllText(statusFile, response == null ? "" : response.Data.HeartRate.ToString());
}

public override void Unload()
{
try
{
if (!Directory.Exists(_dataFolder)) Directory.CreateDirectory(_dataFolder);
string statusFile = $"{_dataFolder}pulsoid.txt";
File.WriteAllText(statusFile, "");
}
catch
{
// ignored
}

_socket?.Dispose();
HttpClient?.Dispose();
}
}
Loading

0 comments on commit 752f3da

Please sign in to comment.