-
Notifications
You must be signed in to change notification settings - Fork 18
Local Overrides Sample: PTZ on Group Streams
bp2008 edited this page Nov 25, 2018
·
4 revisions
This script will enable PTZ controls to be used while viewing a camera group. Near the top of the script, it is configurable which camera will be controlled when viewing each group stream.
This script requires a minor API change that was made in UI3 version 63.
To learn more about ui3-local-overrides, see: Local Overrides Scripts and Styles
(function ()
{
// This extension enables PTZ controls on group streams (the first camera in the group that has PTZ will be the one that is controlled).
var overrideGroupPtzMap = [
{ group: "Index", cam: "backyardptz" },
{ group: "frontyard", cam: "frontyardptz" }
];
var unmappedGroupsInheritFirstPtz = true;
// (Part 1) Override camera list loading so we can modify groups to enable ptz on them.
BI_CustomEvent.AddListener("ExecJSON_Success", function (e)
{
if (e.args.cmd === "camlist" && e.data.result === "success")
{
var allStreams = e.data.data;
var ptzCams = [];
var groups = [];
var i;
for (i = 0; i < allStreams.length; i++)
{
if (allStreams[i].group)
groups.push(allStreams[i]);
else if (allStreams[i].ptz)
ptzCams[allStreams[i].optionValue] = allStreams[i];
}
for (i = 0; i < groups.length; i++)
InheritPTZ(ptzCams, groups[i]);
}
});
function InheritPTZ(ptzCams, group)
{
var i;
var cam;
// Choose a camera to inherit PTZ control from.
if (overrideGroupPtzMap)
{
// Look in the list of manual overrides first.
for (i = 0; i < overrideGroupPtzMap.length; i++)
{
if (group.optionValue === overrideGroupPtzMap[i].group)
{
cam = ptzCams[overrideGroupPtzMap[i].cam];
if (cam && cam.ptz)
{
group.ptz = true;
group.extPtzId = cam.optionValue; // Add a new field to this group specifying the ID of the inherited PTZ camera.
return;
}
}
}
}
if (unmappedGroupsInheritFirstPtz)
{
// Pick the first camera that has ptz.
for (i = 0; i < group.group.length; i++)
{
cam = ptzCams[group.group[i]];
if (cam && cam.ptz)
{
group.ptz = true;
group.extPtzId = cam.optionValue; // Add a new field to this group specifying the ID of the inherited PTZ camera.
return;
}
}
}
}
// (Part 2) Override ptz thumbnail URL function to ensure it happens for the right camera
ptzPresetThumbLoader.UrlForPreset = function (cameraId, presetNumber, overrideCache)
{
if (presetNumber < 1 || presetNumber > 20)
return "";
var sessionArg = currentServer.GetAPISessionArg("?");
var cacheArg = overrideCache ? ((sessionArg ? "&" : "?") + "cache=" + Date.now()) : "";
////// OVERRIDE CAMERA ID //////
var cam = cameraListLoader.GetCameraWithId(cameraId);
if (cam && cam.group && cam.ptz && cam.extPtzId) // Check if this is a group that has inherited a PTZ camera.
cameraId = cam.extPtzId;
////////////////////////////////
return currentServer.remoteBaseURL + "image/" + cameraId + "/preset_" + presetNumber + ".jpg" + sessionArg + cacheArg;
};
// (Part 3) Override "ptz" commands to ensure that commands sent to a group are redirected to the appropriate camera.
BI_CustomEvent.AddListener("ExecJSON_Start", function (e)
{
if (e.args.cmd === "ptz")
{
var cam = cameraListLoader.GetCameraWithId(e.args.camera);
if (cam && cam.group && cam.ptz && cam.extPtzId) // Check if this is a group that has inherited a PTZ camera.
e.args.camera = cam.extPtzId; // Change the camera ID to the one this group inherited from.
}
});
})();