/// <summary> /// Auto update the streamgroup /// </summary> /// <param name="streamingGroup"></param> /// <param name="cancellationToken"></param> /// <param name="frequency"></param> /// <param name="onlySendDirtyStates">Only send light states that have been changed since last update</param> public Task AutoUpdate(StreamingGroup streamingGroup, CancellationToken cancellationToken, int frequency = 50, bool onlySendDirtyStates = false) { if (!_simulator) { int groupCount = (streamingGroup.Count / 10) + 1; frequency = frequency / groupCount; } else { onlySendDirtyStates = false; //Simulator does not understand partial updates } var waitTime = TimeSpan.FromMilliseconds(TimeSpan.FromSeconds(1).TotalMilliseconds / frequency); return(Task.Run(() => { int missedMessages = 0; #if DEBUG int lastSecond = 0; int msgPerSecondCount = 0; #endif while (!cancellationToken.IsCancellationRequested) { var sw = Stopwatch.StartNew(); IEnumerable <IEnumerable <StreamingLight> > chunks = streamingGroup.GetChunksForUpdate(forceUpdate: !onlySendDirtyStates); if (chunks != null) { Send(chunks); } else { missedMessages++; if (missedMessages > frequency) { //If there are no updates, still send updates to keep connection open chunks = streamingGroup.GetChunksForUpdate(forceUpdate: true); Send(chunks); missedMessages = 0; } } sw.Stop(); //#if DEBUG // //Debug.WriteLine("Elasped: " + sw.ElapsedMilliseconds); // msgPerSecondCount++; // if (DateTime.Now.Second != lastSecond) // { // Debug.WriteLine("Msg per second: " + msgPerSecondCount); // msgPerSecondCount = 0; // lastSecond = DateTime.Now.Second; // } //#endif if (sw.Elapsed < waitTime) { Thread.Sleep(waitTime - sw.Elapsed); //Better performance than Task.Delay } } })); }
/// <summary> /// Can be used if you dont want to use the AutoUpdate, but need the same logic for sending updated to the bridge /// </summary> /// <param name="streamingGroup"></param> /// <param name="onlySendDirtyStates"></param> public void ManualUpdate(StreamingGroup streamingGroup, bool onlySendDirtyStates = false) { IEnumerable <IEnumerable <StreamingLight> > chunks = streamingGroup.GetChunksForUpdate(forceUpdate: !onlySendDirtyStates); if (chunks != null) { Send(chunks); } }
/// <summary> /// Auto update the streamgroup /// </summary> /// <param name="entGroup"></param> /// <param name="frequency"></param> /// <param name="onlySendDirtyStates">Only send light states that have been changed since last update</param> /// <param name="cancellationToken"></param> public void AutoUpdate(StreamingGroup entGroup, CancellationToken cancellationToken, int frequency = 50, bool onlySendDirtyStates = false) { if (!_simulator) { int groupCount = (entGroup.Count / 10) + 1; frequency = frequency / groupCount; } else { onlySendDirtyStates = false; //Simulator does not understand partial updates } var waitTime = (int)TimeSpan.FromSeconds(1).TotalMilliseconds / frequency; Task.Run(async() => { int missedMessages = 0; while (!cancellationToken.IsCancellationRequested) { IEnumerable <IEnumerable <StreamingLight> > chunks = entGroup.GetChunksForUpdate(forceUpdate: !onlySendDirtyStates); if (chunks != null) { Send(chunks); } else { missedMessages++; if (missedMessages > frequency) { //If there are no updates, still send updates to keep connection open chunks = entGroup.GetChunksForUpdate(forceUpdate: true); Send(chunks); missedMessages = 0; } } await Task.Delay(waitTime, cancellationToken).ConfigureAwait(false); } }); }