public async Task SendBinaryPulseForXSeconds(double numberOfSeconds, double flashInterval) { await LogInfo($"Sending pulses of white light for {numberOfSeconds.ToString()}s with {flashInterval.ToString()}s intervals"); var stopwatch = new Stopwatch(); stopwatch.Start(); bool isOn = false; while (stopwatch.Elapsed.TotalSeconds < numberOfSeconds) { var shouldBeOn = (int)(stopwatch.Elapsed.TotalSeconds / flashInterval) % 2 == 0; if ((isOn & !shouldBeOn) || (!isOn & shouldBeOn)) { var status = shouldBeOn ? "on" : "off"; await LogInfo($"Time elapsed: {Math.Round(stopwatch.Elapsed.TotalSeconds, 2).ToString()}s. Toggling pulse to be {status}"); } isOn = shouldBeOn; var pulse = ConstructPulse(30, isOn); limiter.Limit(() => Sessions.Broadcast(pulse)); } stopwatch.Stop(); }
protected override void OnMessage(MessageEventArgs e) { limiter.Limit(() => Sessions.Broadcast(e.Data)); }