/// <summary> /// Adds the given label to the <see cref="FloatQueues"/> dictionary if it is not present. /// </summary> /// <param name="label">The label to be added.</param> protected virtual void UpdateFloatDictionaries(string label) { lock (FloatLocks) { if (ContainsFloat(label)) { return; } FloatLocks.Add(label, new object()); FloatQueues.Add(label, new Queue <UdpValue>()); } }
/// <summary> /// Adds the given label and a "max_<see cref="label"/>" to the <see cref="GenericDeviceData.FloatQueues"/> dictionary if it is not already there. /// </summary> /// <param name="label">The label to be added.</param> protected override void UpdateFloatDictionaries(string label) { if (FloatQueues.ContainsKey(label)) { return; } FloatLocks.Add(label, new object()); FloatQueues.Add(label, new Queue <UdpValue>()); FloatQueues.Add("max_" + label, new Queue <UdpValue>()); FloatQueues["max_" + label].Enqueue(new UdpValue()); FloatLocks.Add("max_" + label, new object()); }
/// <summary> /// Returns the float value of the given label. Can be redefined in children classes to change the smoothing logic. Current smoothing logic is as follows:<para><example> <see cref="GenericDeviceController.Smoothing"/> ? <see cref="Smoother.Average(Queue{Vector3})"/> : queue.LastOrDefault()</example></para> /// </summary> /// <param name="label">The label to get the float value.</param> /// <returns>A float value of the given label.</returns> public virtual float GetFloat(string label) { if (FloatQueues.ContainsKey(label) == false) { return(0f); } var @lock = FloatLocks[label]; var queue = FloatQueues[label]; lock (@lock) { return(Controller.Smoothing ? Smoother.Average(queue) : queue.Count == 0 ? 0f : queue.Last().Value); } }
/// <summary> /// Adds the <see cref="value"/> to its respective queue with key as <see cref="label"/> and as "max_<see cref="label"/>. The second is to be used in the Specific <see cref="NeuroskyUnity"/> demo /// This function also updates the <see cref="GenericDeviceData.LastUpdate"/> property. /// </summary> /// <param name="label">The label to which this float value belongs to.</param> /// <param name="value">The float value for this label.</param> protected override void SetFloat(string label, float value) { LastUpdate = DateTime.Now; UpdateFloatDictionaries(label); if (FloatQueues.ContainsKey("max_" + label) == false || FloatLocks.ContainsKey("max_" + label) == false) { return; } var @lock = FloatLocks[label]; var queue = FloatQueues[label]; var queueMax = FloatQueues["max_" + label]; var maxLock = FloatLocks["max_" + label]; var newValue = new UdpValue { LastTimeUpdated = DateTime.Now, Value = value }; lock (@lock) { queue.Enqueue(newValue); while (queue.Count > 1) { queue.Dequeue(); } } lock (maxLock) { //Debug.Log("label: " + label + "count: " + queueMax.Count + " || comparison " + value + ">" + queueMax.Last().Value); if (queueMax.Count != 0 && value > queueMax.Last().Value) { queueMax.Clear(); queueMax.Enqueue(newValue); } } }
/// <summary> /// Checks if this set of data has any float value for the given label. /// </summary> /// <param name="label">The label to search for rotations</param> /// <returns>Returns true if the <see cref="RotationQueues"/> dictionary has any key equals to the given label.</returns> public bool ContainsFloat(string label) { return(FloatQueues.ContainsKey(label)); }
/// <summary> /// Removes all data from the dictionaries that are older than <see cref="GenericDeviceController.TimeToLive"/> /// </summary> private void CleanDictionaries() { //sleeps in the beggining to give time for the software to stabilize //Thread.Sleep(2000); while (true) { //Debug.Log("Cleaning Dictionary"); var keysToKill = new List <string>(); //Debug.Log("TimeToLive" + Controller.TimeToLive); #region floats foreach (var floatQueueEntry in FloatQueues) { if (floatQueueEntry.Key.Contains("max_")) { continue; } lock (FloatLocks[floatQueueEntry.Key]) { while (floatQueueEntry.Value.Count != 0 && (DateTime.Now - floatQueueEntry.Value.Peek().LastTimeUpdated).TotalSeconds >= Controller.TimeToLive) { floatQueueEntry.Value.Dequeue(); } if (floatQueueEntry.Value.Count == 0) { keysToKill.Add(floatQueueEntry.Key); } } } foreach (var key in keysToKill) { FloatQueues.Remove(key); FloatLocks.Remove(key); } //Debug.Log("Float count: " + FloatQueues.Count); #endregion floats keysToKill.Clear(); #region positions foreach (var positionQueueEntry in PositionQueues) { lock (PositionLocks[positionQueueEntry.Key]) { while (positionQueueEntry.Value.Count != 0 && (DateTime.Now - positionQueueEntry.Value.Peek().LastTimeUpdated).TotalSeconds >= Controller.TimeToLive) { positionQueueEntry.Value.Dequeue(); } if (positionQueueEntry.Value.Count == 0) { keysToKill.Add(positionQueueEntry.Key); } } } foreach (var key in keysToKill) { PositionQueues.Remove(key); PositionLocks.Remove(key); } //Debug.Log("Position count: " + PositionQueues.Count); #endregion positions keysToKill.Clear(); #region rotations foreach (var rotationsQueueEntry in RotationQueues) { lock (RotationLocks[rotationsQueueEntry.Key]) { while (rotationsQueueEntry.Value.Count != 0 && (DateTime.Now - rotationsQueueEntry.Value.Peek().LastTimeUpdated).TotalSeconds >= Controller.TimeToLive) { rotationsQueueEntry.Value.Dequeue(); } if (rotationsQueueEntry.Value.Count == 0) { keysToKill.Add(rotationsQueueEntry.Key); } } } foreach (var key in keysToKill) { RotationQueues.Remove(key); RotationLocks.Remove(key); } //Debug.Log("Rotation count: " + RotationQueues.Count); #endregion rotations keysToKill.Clear(); #region samples foreach (var sampleQueueEntry in SampleList) { lock (SampleLocks[sampleQueueEntry.Key]) { if ((DateTime.Now - sampleQueueEntry.Value.LastTimeUpdated).TotalSeconds >= Controller.TimeToLive) { keysToKill.Add(sampleQueueEntry.Key); } } } foreach (var key in keysToKill) { SampleList.Remove(key); SampleLocks.Remove(key); } //Debug.Log("Samples count: " + SampleList.Count); #endregion samples keysToKill.Clear(); #region booleans foreach (var booleanQueueEntry in BooleanProperties) { lock (BooleanLocks[booleanQueueEntry.Key]) { if ((DateTime.Now - booleanQueueEntry.Value.LastTimeUpdated).TotalSeconds >= Controller.TimeToLive) { keysToKill.Add(booleanQueueEntry.Key); } } } foreach (var key in keysToKill) { BooleanProperties.Remove(key); BooleanLocks.Remove(key); } //Debug.Log("Boolean count: " + BooleanProperties.Count); #endregion booleans //Debug.Log("cleaning dictionaries"); Thread.Sleep(50); } }