-
Notifications
You must be signed in to change notification settings - Fork 0
/
PTZControl.cs
94 lines (82 loc) · 3.3 KB
/
PTZControl.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
using System;
using System.Net;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.Timers;
using OnvifMedia10;
using OnvifPTZService;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.StartPanel;
namespace ONVIFCelestialTracker
{
public class PTZControl
{
private MediaClient mediaClient;
private PTZClient ptzClient;
private Profile profile;
private PTZConfigurationOptions options;
private bool initialized = false;
/// <summary>
/// Initialize the PTZ controls for this camera.
/// </summary>
/// <param name="camera"></param>
public PTZControl(Camera camera)
{
var uri = new Uri(camera.Addresses[0]);
var baseUri = uri.GetLeftPart(System.UriPartial.Authority);
var messageElement = new TextMessageEncodingBindingElement()
{
MessageVersion = MessageVersion.CreateVersion(EnvelopeVersion.Soap12, AddressingVersion.None)
};
HttpTransportBindingElement httpBinding = new HttpTransportBindingElement()
{
AuthenticationScheme = AuthenticationSchemes.Digest
};
CustomBinding bind = new CustomBinding(messageElement, httpBinding);
mediaClient = new MediaClient(bind, new EndpointAddress($"{baseUri}/onvif/Media"));
mediaClient.ClientCredentials.HttpDigest.ClientCredential.UserName = camera.User;
mediaClient.ClientCredentials.HttpDigest.ClientCredential.Password = camera.Password;
ptzClient = new PTZClient(bind, new EndpointAddress($"{baseUri}/onvif/PTZ"));
ptzClient.ClientCredentials.HttpDigest.ClientCredential.UserName = camera.User;
ptzClient.ClientCredentials.HttpDigest.ClientCredential.Password = camera.Password;
var profs = mediaClient.GetProfiles();
profile = mediaClient.GetProfile(profs[0].token);
var configs = ptzClient.GetConfigurations();
options = ptzClient.GetConfigurationOptions(configs[0].token);
}
/// <summary>
/// Move to this point, synchronously.
/// </summary>
/// <param name="x">-1.0 to 1.0</param>
/// <param name="y">-1.0 to 1.0</param>
public void MoveTo(float x, float y)
{
OnvifPTZService.PTZSpeed velocity = new OnvifPTZService.PTZSpeed()
{
PanTilt = new OnvifPTZService.Vector2D()
{
x = 0,
y = 0,
space = options.Spaces.RelativePanTiltTranslationSpace[0].URI
},
Zoom = new OnvifPTZService.Vector1D()
{
space = options.Spaces.RelativeZoomTranslationSpace[0].URI
}
};
PTZVector vector = new PTZVector()
{
PanTilt = new OnvifPTZService.Vector2D()
{
x = x,
y = y,
space = options.Spaces.RelativePanTiltTranslationSpace[0].URI
}
};
ptzClient.AbsoluteMove(profile.token, vector, velocity);
}
public void Stop()
{
ptzClient.Stop(profile.token, true, true);
}
}
}