Skip to content

Instantly share code, notes, and snippets.

@joshooaj
Created September 3, 2019 19:50
Show Gist options
  • Save joshooaj/c97b22afac150498e88a7cc4e7285be9 to your computer and use it in GitHub Desktop.
Save joshooaj/c97b22afac150498e88a7cc4e7285be9 to your computer and use it in GitHub Desktop.
MilestonePSTools Get-PlaybackInfo
using System;
using System.Linq;
using System.Management.Automation;
using VideoOS.Platform;
using VideoOS.Platform.ConfigurationItems;
using VideoOS.Platform.Data;
namespace MilestonePSTools.SnapshotCommands
{
/// <summary>
/// <para type="synopsis">Gets the timestamps of the beginning and end of the media database for a given camera</para>
/// <para type="description">Gets the timestamps of the beginning and end of the media database for a given camera
/// including a retention calculation provided as a Timespan object. The implementation of this command retrieves
/// the first and last image from the media database so it can take a long time to complete if you're piping all
/// cameras to this command.</para>
/// <example>
/// <code>C:\PS> Get-Hardware | Get-Camera | Get-PlaybackInfo -UseLocalTime</code>
/// <para>Gets the Begin datetime, End datetime, and Retention timespan values for all cameras, converting the
/// timestamps to localtime based on the region settings of the computer where this command is executed.</para>
/// <para/><para/><para/>
/// </example>
/// <para type="link" uri="https://doc.developer.milestonesys.com/html/index.html">MIP SDK Documentation</para>
/// </summary>
[Cmdlet(VerbsCommon.Get, "PlaybackInfo")]
[OutputType(typeof(PSCustomObject))]
public class GetPlaybackInfo : ConfigApiCmdlet
{
/// <summary>
/// <para type="description">Specifies a camera object - typically the output of a Get-Camera command.</para>
/// </summary>
[Parameter(ValueFromPipeline = true)]
public Camera Camera { get; set; }
/// <summary>
/// <para type="description">Specifies the Guid value of a Camera object.</para>
/// </summary>
[Parameter(Position = 1)]
public Guid CameraId { get; set; }
/// <summary>
/// <para type="description">Convert the UTC timestamps from the Recording Server(s) to local time using the region settings of the current session.</para>
/// </summary>
[Parameter(Position = 2)]
public SwitchParameter UseLocalTime { get; set; }
/// <summary>
///
/// </summary>
protected override void BeginProcessing()
{
base.BeginProcessing();
VideoOS.Platform.SDK.Export.Environment.Initialize();
}
/// <summary>
///
/// </summary>
protected override void ProcessRecord()
{
ValidateParameters();
SwitchToAssemblyPathAndExecute(() =>
{
RawVideoSource src = null;
try
{
var cameraId = Camera != null ? new Guid(Camera.Id) : CameraId;
var item = Configuration.Instance.GetItem(Connection.CurrentSite.FQID.ServerId, cameraId, Kind.Camera);
if (item == null)
{
WriteWarning(
$"Configuration not available for camera with ID {cameraId}. It might be disabled.");
return;
}
src = new RawVideoSource(item);
src.Init();
RawVideoSourceDataList begin = null;
RawVideoSourceDataList end = null;
try
{
var any = src.GoToWithResult(DateTime.UtcNow, "Any");
if (!any)
{
throw new InvalidOperationException($"No video in database for {item.Name} ({cameraId})");
}
begin = src.GetBegin();
end = src.GetEnd();
var beginTimestamp = DateTime.SpecifyKind(begin.List.OrderBy(r => r.DateTime).First().DateTime, DateTimeKind.Utc);
var endTimestamp = DateTime.SpecifyKind(end.List.OrderBy(r => r.DateTime).Last().DateTime, DateTimeKind.Utc);
var obj = new PSObject();
obj.Members.Add(new PSNoteProperty("Begin", UseLocalTime ? beginTimestamp.ToLocalTime() : beginTimestamp));
obj.Members.Add(new PSNoteProperty("End", UseLocalTime ? endTimestamp.ToLocalTime() : endTimestamp));
obj.Members.Add(new PSNoteProperty("Retention", DateTime.UtcNow - beginTimestamp));
WriteObject(obj);
}
finally
{
begin?.Dispose(true);
end?.Dispose(true);
}
}
catch (CommunicationMIPException)
{
WriteWarning($"Unable to connect to {src?.Item.Name} ({src?.Item.FQID.ObjectId})");
}
catch (Exception ex)
{
WriteError(
new ErrorRecord(
ex,
ex.Message,
ErrorCategory.ReadError,
src));
}
finally
{
src?.Close();
}
});
}
private void ValidateParameters()
{
if (Camera == null && CameraId == Guid.Empty)
{
ThrowTerminatingError(
new ErrorRecord(
new ArgumentException(nameof(Camera)),
"Supply Camera or valid CameraId parameter",
ErrorCategory.InvalidArgument,
null));
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment