Skip to content

Commit

Permalink
Merge pull request #2 from space928/dev-thomas
Browse files Browse the repository at this point in the history
Bug fixes & new properties:
  • Loading branch information
space928 authored Jun 29, 2024
2 parents 219b404 + 2a0bde0 commit 89281ec
Show file tree
Hide file tree
Showing 5 changed files with 164 additions and 36 deletions.
27 changes: 27 additions & 0 deletions WirelessMicSuiteServer/IWirelessMicReceiver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,10 @@ public interface IWirelessMic : INotifyPropertyChanged
/// </summary>
public abstract int? Gain { get; set; }
/// <summary>
/// Transmitter sensitivity in dB.
/// </summary>
public abstract int? Sensitivity { get; set; }
/// <summary>
/// Receiver output gain in dB.
/// </summary>
public abstract int? OutputGain { get; set; }
Expand All @@ -147,6 +151,10 @@ public interface IWirelessMic : INotifyPropertyChanged
/// </summary>
public abstract int? Channel { get; set; }
/// <summary>
/// The type of UI lock that is enabled on the wireless transmitter.
/// </summary>
public abstract LockMode? LockMode { get; set; }
/// <summary>
/// Transmitter model type identifier, ie: UR1, UR1H, UR2.
/// </summary>
public abstract string? TransmitterType { get; }
Expand Down Expand Up @@ -182,6 +190,21 @@ public enum DiversityIndicator
D = 1 << 3,
}

/// <summary>
/// Represents what type of UI lock is enabled on a wireless transmitter.
/// </summary>
[JsonConverter(typeof(JsonStringEnumConverter<LockMode>))]
[Flags]
public enum LockMode
{
None,
Mute = 1 << 0,
Power = 1 << 1,
Frequency = 1 << 2,
FrequencyPower = Power | Frequency,
All = Mute | Power | Frequency
}

/// <summary>
/// A data structure representing a single sample of metering data.
/// </summary>
Expand Down Expand Up @@ -271,6 +294,8 @@ public struct WirelessMicData(IWirelessMic other) : IWirelessMic
/// <inheritdoc />
[JsonInclude] public int? Gain { get; set; } = other.Gain;
/// <inheritdoc />
[JsonInclude] public int? Sensitivity { get; set; } = other.Sensitivity;
/// <inheritdoc />
[JsonInclude] public int? OutputGain { get; set; } = other.OutputGain;
/// <inheritdoc />
[JsonInclude] public bool? Mute { get; set; } = other.Mute;
Expand All @@ -281,6 +306,8 @@ public struct WirelessMicData(IWirelessMic other) : IWirelessMic
/// <inheritdoc />
[JsonInclude] public int? Channel { get; set; } = other.Channel;
/// <inheritdoc />
[JsonInclude] public LockMode? LockMode { get; set; } = other.LockMode;
/// <inheritdoc />
[JsonInclude] public string? TransmitterType { get; init; } = other.TransmitterType;
/// <inheritdoc />
[JsonInclude] public float? BatteryLevel { get; init; } = other.BatteryLevel;
Expand Down
96 changes: 88 additions & 8 deletions WirelessMicSuiteServer/ShureUHFR/ShureWirelessMic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,13 @@ public class ShureWirelessMic : IWirelessMic
private readonly uint uid;
private string? name;
private int? gain;
private int? sensitivity;
private int? outputGain;
private bool? mute;
private ulong? frequency;
private int? group;
private int? channel;
private LockMode? lockMode;
private string? transmitterType;
private float? batteryLevel;

Expand All @@ -46,17 +48,26 @@ public int? Gain
get => gain;
set
{
if (value != null && value >= 0 && value <= 32)
SetAsync("TX_IR_GAIN", value.Value.ToString());
if (value != null && value >= -10 && value <= 20)
SetAsync("TX_IR_GAIN", Math.Abs(value.Value+10).ToString());
}
}
public int? Sensitivity
{
get => sensitivity;
set
{
if (value != null && value >= -10 && value <= 15)
SetAsync("TX_IR_TRIM", value.Value.ToString());
}
}
public int? OutputGain
{
get => outputGain;
set
{
if (value != null && value >= 0 && value <= 32)
SetAsync("AUDIO_GAIN", value.Value.ToString());
if (value != null && value >= -32 && value <= 0)
SetAsync("AUDIO_GAIN", Math.Abs(value.Value).ToString());
}
}
public bool? Mute
Expand All @@ -77,6 +88,33 @@ public ulong? Frequency
SetAsync("FREQUENCY", (value.Value / 1000).ToString("000000"));
}
}
public LockMode? LockMode
{
get => lockMode;
set
{
if (value != null)
{
switch (value)
{
case WirelessMicSuiteServer.LockMode.None:
SetAsync("TX_IR_LOCK", "UNLOCK");
break;
case WirelessMicSuiteServer.LockMode.Power:
SetAsync("TX_IR_LOCK", "POWER");
break;
case WirelessMicSuiteServer.LockMode.Frequency:
SetAsync("TX_IR_LOCK", "FREQ");
break;
case WirelessMicSuiteServer.LockMode.FrequencyPower:
SetAsync("TX_IR_LOCK", "FREQ_AND_POWER");
break;
default:
break;
}
}
}
}
public int? Group
{
get => group;
Expand Down Expand Up @@ -142,6 +180,9 @@ private void SendStartupCommands()
receiver.Send($"* GET {receiverNo} TX_BAT *");
receiver.Send($"* GET {receiverNo} TX_BAT_MINS *");
receiver.Send($"* GET {receiverNo} TX_POWER *");
receiver.Send($"* GET {receiverNo} TX_GAIN *");
receiver.Send($"* GET {receiverNo} TX_TRIM *");
receiver.Send($"* GET {receiverNo} TX_LOCK *");
}

private void OnPropertyChanged([CallerMemberName] string? propertyName = null)
Expand Down Expand Up @@ -231,6 +272,21 @@ internal void ParseCommand(ShureCommandType type, ReadOnlySpan<char> cmd, ReadOn
else
CommandError(fullMsg, "Couldn't parse transmitter gain, or gain was out of the range -10:20.");
break;
case "TX_IR_TRIM":
case "TX_TRIM":
if (int.TryParse(args, out int ntrim) && ntrim is >= -10 and <= 15)
{
sensitivity = ntrim;
OnPropertyChanged(nameof(Sensitivity));
}
else if (args.SequenceEqual("UNKNOWN"))
{
sensitivity = null;
OnPropertyChanged(nameof(Sensitivity));
}
else
CommandError(fullMsg, "Couldn't parse transmitter gain, or gain was out of the range -10:20.");
break;
case "SQUELCH":
if (int.TryParse(args, out int nsquelch))
{
Expand Down Expand Up @@ -295,10 +351,33 @@ internal void ParseCommand(ShureCommandType type, ReadOnlySpan<char> cmd, ReadOn
transmitterType = args.ToString();
OnPropertyChanged(nameof(TransmitterType));
break;
case "FRONT_PANEL_LOCK":
case "TX_IR_LOCK":
case "TX_LOCK":
switch(args)
{
case "UNLOCK":
lockMode = WirelessMicSuiteServer.LockMode.None;
OnPropertyChanged(nameof(LockMode));
break;
case "POWER":
lockMode = WirelessMicSuiteServer.LockMode.Power;
OnPropertyChanged(nameof(LockMode));
break;
case "FREQ":
lockMode = WirelessMicSuiteServer.LockMode.Frequency;
OnPropertyChanged(nameof(LockMode));
break;
case "FREQ_AND_POWER":
lockMode = WirelessMicSuiteServer.LockMode.FrequencyPower;
OnPropertyChanged(nameof(LockMode));
break;
case "NOCHANGE":
default:
break;
}
break;
case "FRONT_PANEL_LOCK":
case "TX_IR_POWER":
case "TX_IR_TRIM":
case "TX_IR_BAT_TYPE":
case "TX_IR_CUSTOM_GPS":
case "AUDIO_INDICATOR":
Expand All @@ -307,8 +386,6 @@ internal void ParseCommand(ShureCommandType type, ReadOnlySpan<char> cmd, ReadOn
case "TX_POWER":
case "TX_CHANGE_BAT":
case "TX_EXT_DC":
case "TX_TRIM":
case "TX_LOCK":
// Unimplemented for now
break;
default:
Expand Down Expand Up @@ -391,6 +468,9 @@ private void ParseSampleCommand(ReadOnlySpan<char> args, ReadOnlySpan<char> full

private void ParseRFLevelCommand(ReadOnlySpan<char> nargs, ReadOnlySpan<char> args, ReadOnlySpan<char> fullMsg)
{
if (rfScanInProgress == null)
return;

// "* RFLEVEL n 10 578000 100 578025 100 578050 100 578075 100 578100 100 578125 100 578150 100 578175 100 578200 100 578225 100 *"
// * RFLEVEL n numSamples [freq level]... *
// level: is in - dBm
Expand Down
11 changes: 10 additions & 1 deletion WirelessMicSuiteServer/WebSocketAPI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ public class WebSocketAPIManager : IDisposable
private readonly List<WebSocketAPI> clients;
private readonly Timer meteringTimer;
private bool isSendingMeteringMsg;
private readonly JsonSerializerOptions jsonSerializerOptions;

public WebSocketAPIManager(WirelessMicManager micManager, int meterInterval)
{
Expand All @@ -150,6 +151,13 @@ public WebSocketAPIManager(WirelessMicManager micManager, int meterInterval)
propCache = [];
BuildPropCache();

jsonSerializerOptions = new()
{
IncludeFields = false,
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
WriteIndented = false,
};

((INotifyCollectionChanged)micManager.Receivers).CollectionChanged += (o, e) =>
{
switch (e.Action)
Expand Down Expand Up @@ -205,6 +213,7 @@ private void MeteringTimer_Elapsed(object? sender, ElapsedEventArgs e)
foreach (var client in clients)
client.SendMessage(json);
}
catch { }
finally
{
isSendingMeteringMsg = false;
Expand Down Expand Up @@ -251,7 +260,7 @@ private void OnPropertyChanged(object? target, PropertyChangedEventArgs args)
object? val = cached.prop.GetValue(target);

var propNotif = new PropertyChangeNotification(cached.name, val, uid);
var json = JsonSerializer.SerializeToUtf8Bytes(propNotif);
var json = JsonSerializer.SerializeToUtf8Bytes(propNotif, jsonSerializerOptions);

foreach (var client in clients)
client.SendMessage(json);
Expand Down
64 changes: 38 additions & 26 deletions WirelessMicSuiteServer/WirelessMicManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,16 @@ public class WirelessMicManager : IDisposable
//public ObservableCollection<IWirelessMicReceiver> Receivers { get; init; }
public ReadOnlyObservableCollection<IWirelessMicReceiver> Receivers { get; init; }
// TODO: There's a race condition where if receivers get added or removed while this is being enumerated an exception is thrown.
public IEnumerable<IWirelessMic> WirelessMics => new WirelessMicEnumerator(Receivers);
public IEnumerable<IWirelessMic> WirelessMics
{
get
{
lock (receivers)
{
return new WirelessMicEnumerator(Receivers);
}
}
}

public WirelessMicManager(IEnumerable<IWirelessMicReceiverManager>? receiverManagers)
{
Expand All @@ -25,32 +34,35 @@ public WirelessMicManager(IEnumerable<IWirelessMicReceiverManager>? receiverMana
// Attempt to synchronise the observable collections
rm.Receivers.CollectionChanged += (o, e) =>
{
switch (e.Action)
lock (receivers)
{
case System.Collections.Specialized.NotifyCollectionChangedAction.Add:
if (e.NewItems != null)
foreach (IWirelessMicReceiver obj in e.NewItems)
receivers.Add(obj);
break;
case System.Collections.Specialized.NotifyCollectionChangedAction.Remove:
if (e.OldItems != null)
foreach (IWirelessMicReceiver obj in e.OldItems)
receivers.Remove(obj);
break;
case System.Collections.Specialized.NotifyCollectionChangedAction.Replace:
if (e.OldItems != null && e.NewItems != null && e.OldItems.Count == e.NewItems.Count)
for (int i = 0; i < e.OldItems.Count; i++)
{
receivers.Remove((IWirelessMicReceiver)e.OldItems[i]!);
receivers.Add((IWirelessMicReceiver)e.NewItems[i]!);
}
break;
case System.Collections.Specialized.NotifyCollectionChangedAction.Move:
break;
case System.Collections.Specialized.NotifyCollectionChangedAction.Reset:
throw new NotSupportedException();
default:
throw new InvalidOperationException();
switch (e.Action)
{
case System.Collections.Specialized.NotifyCollectionChangedAction.Add:
if (e.NewItems != null)
foreach (IWirelessMicReceiver obj in e.NewItems)
receivers.Add(obj);
break;
case System.Collections.Specialized.NotifyCollectionChangedAction.Remove:
if (e.OldItems != null)
foreach (IWirelessMicReceiver obj in e.OldItems)
receivers.Remove(obj);
break;
case System.Collections.Specialized.NotifyCollectionChangedAction.Replace:
if (e.OldItems != null && e.NewItems != null && e.OldItems.Count == e.NewItems.Count)
for (int i = 0; i < e.OldItems.Count; i++)
{
receivers.Remove((IWirelessMicReceiver)e.OldItems[i]!);
receivers.Add((IWirelessMicReceiver)e.NewItems[i]!);
}
break;
case System.Collections.Specialized.NotifyCollectionChangedAction.Move:
break;
case System.Collections.Specialized.NotifyCollectionChangedAction.Reset:
throw new NotSupportedException();
default:
throw new InvalidOperationException();
}
}
};
}
Expand Down
2 changes: 1 addition & 1 deletion WirelessMicSuiteServer/WirelessMicSuiteServer.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<Title>Wireless Mic Suite Server</Title>
<Version>1.2.1</Version>
<Version>1.3.1</Version>
<Authors>Thomas Mathieson</Authors>
<Copyright>Copyright Thomas Mathieson 2024</Copyright>
<PackageProjectUrl>https://github.com/space928/WirelessMicSuiteServer</PackageProjectUrl>
Expand Down

0 comments on commit 89281ec

Please sign in to comment.