-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
27 changed files
with
577 additions
and
331 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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?>(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
Oops, something went wrong.