|
1 | | -using Aio.Onvif.Connector.Ptz.Demo; |
| 1 | +using System.CommandLine; |
| 2 | +using Aio.Onvif.Connector.Ptz.Demo; |
2 | 3 | using Azure.Iot.Operations.Mqtt.Session; |
| 4 | +using Azure.Iot.Operations.Protocol; |
3 | 5 | using Azure.Iot.Operations.Protocol.Models; |
4 | | -using PTZ.dtmi_onvif_ptz__1; |
5 | | - |
6 | | -Console.Write("Mqtt Broker Host: "); |
7 | | -var host = Console.ReadLine(); |
8 | | -if (string.IsNullOrWhiteSpace(host)) |
| 6 | +using MediaClient.Media; |
| 7 | +using PtzClient.Ptz; |
| 8 | + |
| 9 | +var hostOption = new Option<string>(["--mqtt-host", "-h"], description: "The Hostname or IP of the MQTT Listener the demo connects to", getDefaultValue: () => "localhost"); |
| 10 | +var portOption = new Option<int>(["--mqtt-port", "-p"], description: "The port of the MQTT Listener the demo connects to", getDefaultValue: () => 1883); |
| 11 | +var namespaceOption = new Option<string>(["--namespace", "-n"], description: "The Kubernetes namespace AIO is deployed to", getDefaultValue: () => "azure-iot-operations"); |
| 12 | +var ptzAssetOption = new Option<string>(["--ptz-asset", "-pa"], description: "The name of the PTZ asset") { IsRequired = true }; |
| 13 | +var mediaAssetOption = new Option<string>(["--media-asset", "-ma"], description: "The name of the media asset") { IsRequired = true }; |
| 14 | +var modeOption = new Option<string>(["--mode", "-m"], description: "The method that should be used to move the camera", getDefaultValue: () => "relative").FromAmong("relative", "continuous"); |
| 15 | + |
| 16 | +var rootCommand = new RootCommand("AIO ONVIF Connector Demo"); |
| 17 | +rootCommand.AddOption(hostOption); |
| 18 | +rootCommand.AddOption(portOption); |
| 19 | +rootCommand.AddOption(namespaceOption); |
| 20 | +rootCommand.AddOption(ptzAssetOption); |
| 21 | +rootCommand.AddOption(mediaAssetOption); |
| 22 | +rootCommand.AddOption(modeOption); |
| 23 | + |
| 24 | +rootCommand.SetHandler(async (host, port, @namespace, ptzAsset, mediaAsset, mode) => |
9 | 25 | { |
10 | | - Console.Error.WriteLine("Invalid host"); |
11 | | - Environment.Exit(1); |
12 | | -} |
| 26 | + Console.WriteLine($"MQTT Host: {host}"); |
| 27 | + Console.WriteLine($"MQTT Port: {port}"); |
| 28 | + Console.WriteLine($"Namespace: {@namespace}"); |
| 29 | + Console.WriteLine($"PTZ Asset: {ptzAsset}"); |
| 30 | + Console.WriteLine($"Media Asset: {mediaAsset}"); |
| 31 | + Console.WriteLine($"Mode: {mode}"); |
13 | 32 |
|
14 | | -Console.Write("Mqtt Broker Port: "); |
15 | | -if (!int.TryParse(Console.ReadLine(), out var port)) |
16 | | -{ |
17 | | - Console.Error.WriteLine("Invalid port number"); |
18 | | - Environment.Exit(1); |
19 | | -} |
| 33 | + var mqttClientTcpOptions = new MqttClientTcpOptions(host, port); |
20 | 34 |
|
21 | | -Console.Write("AIO Namespace: "); |
22 | | -var aioNamespace = Console.ReadLine(); |
23 | | -if (string.IsNullOrWhiteSpace(aioNamespace)) |
24 | | -{ |
25 | | - Console.Error.WriteLine("Invalid AIO namespace"); |
26 | | - Environment.Exit(1); |
27 | | -} |
| 35 | + var mqttClientOptions = new MqttClientOptions(mqttClientTcpOptions) { SessionExpiryInterval = 60 }; |
28 | 36 |
|
29 | | -Console.Write("Asset Name: "); |
30 | | -var assetName = Console.ReadLine(); |
31 | | -if (string.IsNullOrWhiteSpace(assetName)) |
32 | | -{ |
33 | | - Console.Error.WriteLine("Invalid asset name"); |
34 | | - Environment.Exit(1); |
35 | | -} |
36 | | - |
37 | | -Console.Write("Profile Token: "); |
38 | | -var profileToken = Console.ReadLine(); |
39 | | -if (string.IsNullOrWhiteSpace(profileToken)) |
40 | | -{ |
41 | | - Console.Error.WriteLine("Invalid profile token"); |
42 | | - Environment.Exit(1); |
43 | | -} |
44 | | - |
45 | | -Console.Clear(); |
| 37 | + var mqttSessionClient = new MqttSessionClient(new MqttSessionClientOptions()); |
| 38 | + await mqttSessionClient.ConnectAsync(mqttClientOptions); |
| 39 | + var applicationContext = new ApplicationContext(); |
| 40 | + var ptzClient = new OnvifPtzClient(applicationContext, mqttSessionClient); |
| 41 | + var mediaClient = new OnvifMediaClient(applicationContext, mqttSessionClient); |
46 | 42 |
|
47 | | -var mqttClientTcpOptions = new MqttClientTcpOptions(host, port); |
| 43 | + var profiles = await mediaClient.GetProfilesAsync(new Dictionary<string, string> |
| 44 | + { |
| 45 | + { "ex:namespace", @namespace }, |
| 46 | + { "ex:asset", mediaAsset } |
| 47 | + }); |
| 48 | + var profile = profiles.GetProfilesCommandResponse.Profiles.First(); |
48 | 49 |
|
49 | | -var mqttClientOptions = new MqttClientOptions(mqttClientTcpOptions) { SessionExpiryInterval = 60 }; |
| 50 | + Console.WriteLine("Use arrow keys or WASD to move camera, Q to quit"); |
50 | 51 |
|
51 | | -var mqttSessionClient = new MqttSessionClient(new MqttSessionClientOptions()); |
| 52 | + if (mode == "relative") |
| 53 | + { |
| 54 | + await StartRelativeMoveAsync(ptzClient, profile, @namespace, ptzAsset); |
| 55 | + } |
| 56 | + else if (mode == "continuous") |
| 57 | + { |
| 58 | + await StartContinuousMoveAsync(ptzClient, profile, @namespace, ptzAsset); |
| 59 | + } |
52 | 60 |
|
53 | | -await mqttSessionClient.ConnectAsync(mqttClientOptions).ConfigureAwait(true); |
54 | | -var client = new PtzClient(mqttSessionClient); |
55 | | -client.CustomTopicTokenMap.Add("asset", assetName); |
56 | | -client.CustomTopicTokenMap.Add("namespace", aioNamespace); |
| 61 | + await StartRelativeMoveAsync(ptzClient, profile, @namespace, ptzAsset); |
| 62 | +}, hostOption, portOption, namespaceOption, ptzAssetOption, mediaAssetOption, modeOption); |
57 | 63 |
|
58 | | -Console.WriteLine("Use arrow keys or WASD to move camera, Q to quit"); |
| 64 | +await rootCommand.InvokeAsync(args); |
59 | 65 |
|
60 | | -while (true) |
| 66 | +async Task StartRelativeMoveAsync(OnvifPtzClient ptzClient, Profile profile, string @namespace, string ptzAssetName) |
61 | 67 | { |
62 | | - var key = Console.ReadKey(true).Key; |
63 | | - if (key == ConsoleKey.Q) |
| 68 | + while (true) |
64 | 69 | { |
65 | | - break; |
66 | | - } |
| 70 | + var key = Console.ReadKey(true).Key; |
| 71 | + if (key == ConsoleKey.Q) |
| 72 | + { |
| 73 | + return; |
| 74 | + } |
67 | 75 |
|
68 | | - (float x, float y)? delta = key switch |
69 | | - { |
70 | | - ConsoleKey.UpArrow => (0, 0.2f), |
71 | | - ConsoleKey.DownArrow => (0, -0.2f), |
72 | | - ConsoleKey.LeftArrow => (0.2f, 0), |
73 | | - ConsoleKey.RightArrow => (-0.2f, 0), |
74 | | - ConsoleKey.W => (0, 0.2f), |
75 | | - ConsoleKey.S => (0, -0.2f), |
76 | | - ConsoleKey.A => (0.2f, 0), |
77 | | - ConsoleKey.D => (-0.2f, 0), |
78 | | - _ => null |
79 | | - }; |
80 | | - |
81 | | - if (delta == null) |
82 | | - { |
83 | | - continue; |
84 | | - } |
| 76 | + (float x, float y)? delta = key switch |
| 77 | + { |
| 78 | + ConsoleKey.UpArrow => (0, 0.2f), |
| 79 | + ConsoleKey.DownArrow => (0, -0.2f), |
| 80 | + ConsoleKey.LeftArrow => (0.2f, 0), |
| 81 | + ConsoleKey.RightArrow => (-0.2f, 0), |
| 82 | + ConsoleKey.W => (0, 0.2f), |
| 83 | + ConsoleKey.S => (0, -0.2f), |
| 84 | + ConsoleKey.A => (0.2f, 0), |
| 85 | + ConsoleKey.D => (-0.2f, 0), |
| 86 | + _ => null |
| 87 | + }; |
85 | 88 |
|
86 | | - try |
87 | | - { |
88 | | - var request = new RelativeMoveRequestPayload |
| 89 | + if (delta == null) |
| 90 | + { |
| 91 | + continue; |
| 92 | + } |
| 93 | + |
| 94 | + try |
89 | 95 | { |
90 | | - RelativeMove = new Object_Onvif_Ptz_RelativeMove__1 |
| 96 | + var request = new RelativeMoveRequestPayload |
91 | 97 | { |
92 | | - ProfileToken = profileToken, |
93 | | - Translation = new Object_Onvif_Ptz_PTZVector__1 |
| 98 | + RelativeMove = new RelativeMove |
94 | 99 | { |
95 | | - PanTilt = new Object_Onvif_Ptz_Vector2D__1 |
| 100 | + ProfileToken = profile.Token, |
| 101 | + Translation = new Ptzvector |
96 | 102 | { |
97 | | - X = delta.Value.x, |
98 | | - Y = delta.Value.y, |
| 103 | + PanTilt = new PtzClient.Ptz.Vector2d |
| 104 | + { |
| 105 | + X = delta.Value.x, |
| 106 | + Y = delta.Value.y, |
| 107 | + } |
99 | 108 | } |
100 | 109 | } |
101 | | - } |
102 | | - }; |
| 110 | + }; |
103 | 111 |
|
104 | | - await client.RelativeMoveAsync(request); |
| 112 | + await ptzClient.RelativeMoveAsync(request, new Dictionary<string, string> |
| 113 | + { |
| 114 | + { "ex:namespace", @namespace }, |
| 115 | + { "ex:asset", ptzAssetName } |
| 116 | + }); |
| 117 | + } |
| 118 | + catch (Exception e) |
| 119 | + { |
| 120 | + Console.WriteLine($"Error: {e.Message}"); |
| 121 | + // Bad request is expected if the camera reaches the limit |
| 122 | + } |
| 123 | + |
| 124 | + await Task.Delay(1000).ConfigureAwait(true); |
105 | 125 | } |
106 | | - catch (System.Exception) |
| 126 | +} |
| 127 | + |
| 128 | +async Task StartContinuousMoveAsync(OnvifPtzClient ptzClient, Profile profile, string @namespace, string ptzAssetName) |
| 129 | +{ |
| 130 | + while (true) |
107 | 131 | { |
108 | | - // Bad request is expected if the camera reaches the limit |
109 | | - } |
| 132 | + var key = Console.ReadKey(true).Key; |
| 133 | + if (key == ConsoleKey.Q) |
| 134 | + { |
| 135 | + return; |
| 136 | + } |
| 137 | + |
| 138 | + (float x, float y)? delta = key switch |
| 139 | + { |
| 140 | + ConsoleKey.UpArrow => (0, 0.2f), |
| 141 | + ConsoleKey.DownArrow => (0, -0.2f), |
| 142 | + ConsoleKey.LeftArrow => (-0.2f, 0), |
| 143 | + ConsoleKey.RightArrow => (0.2f, 0), |
| 144 | + ConsoleKey.W => (0, 0.2f), |
| 145 | + ConsoleKey.S => (0, -0.2f), |
| 146 | + ConsoleKey.A => (-0.2f, 0), |
| 147 | + ConsoleKey.D => (0.2f, 0), |
| 148 | + _ => null |
| 149 | + }; |
110 | 150 |
|
111 | | - await Task.Delay(200).ConfigureAwait(true); |
| 151 | + if (delta == null) |
| 152 | + { |
| 153 | + continue; |
| 154 | + } |
| 155 | + |
| 156 | + try |
| 157 | + { |
| 158 | + var request = new ContinuousMoveRequestPayload |
| 159 | + { |
| 160 | + ContinuousMove = new ContinuousMove |
| 161 | + { |
| 162 | + ProfileToken = profile.Token, |
| 163 | + Velocity = new PtzClient.Ptz.Ptzspeed |
| 164 | + { |
| 165 | + PanTilt = new PtzClient.Ptz.Vector2d |
| 166 | + { |
| 167 | + X = delta.Value.x, |
| 168 | + Y = delta.Value.y, |
| 169 | + } |
| 170 | + } |
| 171 | + } |
| 172 | + }; |
| 173 | + |
| 174 | + await ptzClient.ContinuousMoveAsync(request, new Dictionary<string, string> |
| 175 | + { |
| 176 | + { "ex:namespace", @namespace }, |
| 177 | + { "ex:asset", ptzAssetName } |
| 178 | + }); |
| 179 | + await Task.Delay(1000).ConfigureAwait(true); |
| 180 | + await ptzClient.StopAsync(new StopRequestPayload { Stop = new Stop { ProfileToken = profile.Token, PanTilt = true } }, new Dictionary<string, string> |
| 181 | + { |
| 182 | + { "ex:namespace", @namespace }, |
| 183 | + { "ex:asset", ptzAssetName } |
| 184 | + }); |
| 185 | + } |
| 186 | + catch (Exception e) |
| 187 | + { |
| 188 | + Console.WriteLine($"Error: {e.Message}"); |
| 189 | + // Bad request is expected if the camera reaches the limit |
| 190 | + } |
| 191 | + } |
112 | 192 | } |
113 | 193 |
|
0 commit comments