void RegisterChannel(int antChannel, PowerChannel c) { c.OnSearchEnded += ChannelSearchEnded; c.OnAccumPowerReceived += ChannelAccumPowerReceived; c.OnClosed += ChannelClosed; lock (this) { channels.Add((byte)antChannel, c); c.Open(); } }
void ChannelAccumPowerReceived(PowerChannel sender, int accumPower, int count) { foreach (var kvp in channels) { if (kvp.Value == sender) { OnAccumPowerReceived?.Invoke(this, kvp.Key, accumPower, count); return; } } throw new Exception("channels inconsistency"); }
void ChannelSearchEnded(PowerChannel sender, bool isConnected, int deviceNumber) { foreach (var kvp in channels) { if (kvp.Value == sender) { OnSearchEnded?.Invoke(this, kvp.Key, isConnected, deviceNumber); return; } } throw new Exception("channels inconsistency"); }
void ChannelClosed(PowerChannel sender) { lock (this) { byte i = 255; foreach (var kvp in channels) { if (kvp.Value == sender) { i = kvp.Key; break; } } if (i == 255) { throw new Exception("channels inconsistency"); } channels.Remove(i); } }