// -----------------------------------------------------------------------
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// -----------------------------------------------------------------------
namespace Microsoft.Samples.Kinect.Webserver.Sensor
{
using System.Collections.Generic;
using Microsoft.Kinect;
///
/// Helper class used to measure user activity.
///
internal class UserActivityMeter
{
private readonly Dictionary activityRecords = new Dictionary();
private int totalUpdatesSoFar;
///
/// Clears all user activity metrics.
///
public void Clear()
{
this.activityRecords.Clear();
}
///
/// Update user activity metrics with data from a collection of skeletons.
///
///
/// Collection of skeletons to use to update activity metrics.
///
///
/// Time when skeleton array was received for processing.
///
///
/// UserActivityMeter assumes that this method is called regularly, e.g.: once
/// per skeleton frame received by application, so if a user whose activity was
/// previously measured is now absent, activity record will be removed.
///
public void Update(ICollection skeletons, long timestamp)
{
foreach (var skeleton in skeletons)
{
UserActivityRecord record;
if (this.activityRecords.TryGetValue(skeleton.TrackingId, out record))
{
record.Update(skeleton.Position, this.totalUpdatesSoFar, timestamp);
}
else
{
record = new UserActivityRecord(skeleton.Position, this.totalUpdatesSoFar, timestamp);
this.activityRecords[skeleton.TrackingId] = record;
}
}
// Remove activity records corresponding to users that are no longer being tracked
var idsToRemove = new List();
foreach (var record in this.activityRecords)
{
if (record.Value.LastUpdateId != this.totalUpdatesSoFar)
{
idsToRemove.Add(record.Key);
}
}
foreach (var id in idsToRemove)
{
this.activityRecords.Remove(id);
}
++this.totalUpdatesSoFar;
}
///
/// Gets the activity record associated with the specified user.
///
///
/// Skeleton tracking Id of user associated with the activity record to
/// retrieve.
///
///
/// [out] When this method returns, contains the record associated with the
/// specified user tracking Id, if the appropriate activity record is found.
/// This parameter is passed uninitialized.
///
///
/// true
if the UserActivityMeter contains an activity record
/// for the specified user tracking Id; otherwise, false
.
///
public bool TryGetActivityRecord(int userTrackingId, out UserActivityRecord record)
{
return this.activityRecords.TryGetValue(userTrackingId, out record);
}
}
}