Add http action type and Sonar mute toggle (chatCapture, media channels)
This commit is contained in:
@@ -6,53 +6,92 @@ var configJson = File.ReadAllText(configPath);
|
||||
var config = JsonSerializer.Deserialize<Config>(configJson, new JsonSerializerOptions { PropertyNameCaseInsensitive = true })
|
||||
?? new Config();
|
||||
|
||||
var sonarAddress = await SonarAddressResolver.GetSonarAddressAsync();
|
||||
Console.WriteLine($"Adresse Sonar résolue : {sonarAddress ?? "AUCUNE"}");
|
||||
|
||||
var actions = new Dictionary<int, IAction>();
|
||||
foreach (var (key, binding) in config.Bindings)
|
||||
{
|
||||
var action = ActionFactory.Create(binding);
|
||||
if (binding.Target.Contains("{sonar}") && sonarAddress is not null)
|
||||
binding.Target = binding.Target.Replace("{sonar}", sonarAddress);
|
||||
|
||||
var action = ActionFactory.Create(binding, sonarAddress);
|
||||
if (action is not null && int.TryParse(key, out var bit))
|
||||
actions[bit] = action;
|
||||
}
|
||||
|
||||
var device = DeviceList.Local.GetHidDevices(vendorID: 0x2341, productID: 0x8036).FirstOrDefault();
|
||||
if (device is null)
|
||||
{
|
||||
Console.WriteLine("Pad non trouvé. Vérifie qu'il est branché.");
|
||||
return;
|
||||
}
|
||||
Console.WriteLine($"{actions.Count} binding(s) chargé(s) :");
|
||||
foreach (var (bit, _) in actions)
|
||||
Console.WriteLine($" bit {bit} -> {config.Bindings.First(b => int.Parse(b.Key) == bit).Value.Type}");
|
||||
Console.WriteLine();
|
||||
|
||||
using var stream = device.Open();
|
||||
stream.ReadTimeout = Timeout.Infinite;
|
||||
|
||||
var buffer = new byte[device.GetMaxInputReportLength()];
|
||||
ushort lastMask = 0;
|
||||
|
||||
Console.WriteLine("En écoute...\n");
|
||||
const ushort VendorId = 0x2341;
|
||||
const ushort ProductId = 0x8036;
|
||||
|
||||
while (true)
|
||||
{
|
||||
int count;
|
||||
try { count = stream.Read(buffer, 0, buffer.Length); }
|
||||
catch (TimeoutException) { continue; }
|
||||
var device = DeviceList.Local.GetHidDevices(vendorID: VendorId, productID: ProductId).FirstOrDefault();
|
||||
|
||||
int offset = count > 2 ? 1 : 0;
|
||||
ushort mask = (ushort)(buffer[offset] | (buffer[offset + 1] << 8));
|
||||
|
||||
if (mask != lastMask)
|
||||
if (device is null)
|
||||
{
|
||||
ushort changed = (ushort)(mask ^ lastMask);
|
||||
for (int bit = 0; bit < 12; bit++)
|
||||
{
|
||||
bool bitChanged = (changed & (1 << bit)) != 0;
|
||||
bool nowPressed = (mask & (1 << bit)) != 0;
|
||||
Console.WriteLine("Pad non trouvé, nouvelle tentative dans 2s...");
|
||||
Thread.Sleep(2000);
|
||||
continue;
|
||||
}
|
||||
|
||||
// On déclenche seulement au moment où le bouton est pressé, pas relâché
|
||||
if (bitChanged && nowPressed && actions.TryGetValue(bit, out var action))
|
||||
Console.WriteLine($"Pad connecté : {device.DevicePath}");
|
||||
|
||||
try
|
||||
{
|
||||
ListenToDevice(device, actions);
|
||||
}
|
||||
catch (Exception ex) when (ex is IOException or TimeoutException or ObjectDisposedException)
|
||||
{
|
||||
Console.WriteLine("Pad déconnecté. En attente de reconnexion...");
|
||||
}
|
||||
}
|
||||
|
||||
void ListenToDevice(HidDevice device, Dictionary<int, IAction> actions)
|
||||
{
|
||||
using var stream = device.Open();
|
||||
stream.ReadTimeout = Timeout.Infinite;
|
||||
|
||||
var buffer = new byte[device.GetMaxInputReportLength()];
|
||||
ushort lastMask = 0;
|
||||
|
||||
while (true)
|
||||
{
|
||||
int count = stream.Read(buffer, 0, buffer.Length);
|
||||
if (count == 0) throw new IOException("Rapport vide, device probablement déconnecté");
|
||||
|
||||
int offset = count > 2 ? 1 : 0;
|
||||
ushort mask = (ushort)(buffer[offset] | (buffer[offset + 1] << 8));
|
||||
|
||||
Console.WriteLine($"Rapport reçu, mask={Convert.ToString(mask, 2).PadLeft(12, '0')}");
|
||||
|
||||
if (mask != lastMask)
|
||||
{
|
||||
ushort changed = (ushort)(mask ^ lastMask);
|
||||
for (int bit = 0; bit < 12; bit++)
|
||||
{
|
||||
Console.WriteLine($"Bouton {bit} → exécution de l'action");
|
||||
action.Execute();
|
||||
bool bitChanged = (changed & (1 << bit)) != 0;
|
||||
bool nowPressed = (mask & (1 << bit)) != 0;
|
||||
|
||||
if (bitChanged && nowPressed && actions.TryGetValue(bit, out var action))
|
||||
{
|
||||
Console.WriteLine($"Bouton {bit} → exécution de l'action");
|
||||
try
|
||||
{
|
||||
action.Execute();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"Erreur lors de l'exécution : {ex.Message}");
|
||||
}
|
||||
}
|
||||
}
|
||||
lastMask = mask;
|
||||
}
|
||||
lastMask = mask;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user