|
6 | 6 | using MediaClient.Media; |
7 | 7 | using PtzClient.Ptz; |
8 | 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 | +var hostOption = new Option<string>("--mqtt-host", "-h") { Description = "The Hostname or IP of the MQTT Listener the demo connects to", DefaultValueFactory = _ => "localhost" }; |
| 10 | +var portOption = new Option<int>("--mqtt-port", "-p") { Description = "The port of the MQTT Listener the demo connects to", DefaultValueFactory = _ => 1883 }; |
| 11 | +var namespaceOption = new Option<string>("--namespace", "-n") { Description = "The Kubernetes namespace AIO is deployed to", DefaultValueFactory = _ => "azure-iot-operations" }; |
| 12 | +var assetOption = new Option<string>("--asset", "-a") { Description = "The name of the asset", Required = true }; |
| 13 | +var modeOption = new Option<string>("--mode", "-m") { Description = "The method that should be used to move the camera", DefaultValueFactory = _ => "relative", }.AcceptOnlyFromAmong("relative", "continuous"); |
| 14 | + |
| 15 | +var rootCommand = new RootCommand("AIO ONVIF Connector Demo") |
| 16 | +{ |
| 17 | + hostOption, |
| 18 | + portOption, |
| 19 | + namespaceOption, |
| 20 | + assetOption, |
| 21 | + modeOption |
| 22 | +}; |
| 23 | + |
| 24 | +rootCommand.SetAction(async parseResult => |
25 | 25 | { |
| 26 | + var host = parseResult.GetValue(hostOption) ?? throw new ArgumentNullException(nameof(hostOption), "MQTT host cannot be null"); |
| 27 | + var port = parseResult.GetValue(portOption); |
| 28 | + var @namespace = parseResult.GetValue(namespaceOption) ?? throw new ArgumentNullException(nameof(namespaceOption), "Namespace cannot be null"); |
| 29 | + var asset = parseResult.GetValue(assetOption) ?? throw new ArgumentNullException(nameof(assetOption), "Asset cannot be null"); |
| 30 | + var mode = parseResult.GetValue(modeOption) ?? throw new ArgumentNullException(nameof(modeOption), "Mode cannot be null"); |
| 31 | + |
26 | 32 | Console.WriteLine($"MQTT Host: {host}"); |
27 | 33 | Console.WriteLine($"MQTT Port: {port}"); |
28 | 34 | Console.WriteLine($"Namespace: {@namespace}"); |
29 | | - Console.WriteLine($"PTZ Asset: {ptzAsset}"); |
30 | | - Console.WriteLine($"Media Asset: {mediaAsset}"); |
| 35 | + Console.WriteLine($"Asset: {asset}"); |
31 | 36 | Console.WriteLine($"Mode: {mode}"); |
32 | 37 |
|
33 | 38 | var mqttClientTcpOptions = new MqttClientTcpOptions(host, port); |
|
37 | 42 | var mqttSessionClient = new MqttSessionClient(new MqttSessionClientOptions()); |
38 | 43 | await mqttSessionClient.ConnectAsync(mqttClientOptions); |
39 | 44 | var applicationContext = new ApplicationContext(); |
40 | | - var ptzClient = new OnvifPtzClient(applicationContext, mqttSessionClient); |
41 | | - var mediaClient = new OnvifMediaClient(applicationContext, mqttSessionClient); |
42 | | - |
43 | | - var profiles = await mediaClient.GetProfilesAsync(new Dictionary<string, string> |
| 45 | + var ptzClient = new OnvifPtzClient(applicationContext, mqttSessionClient, new Dictionary<string, string> |
| 46 | + { |
| 47 | + { "namespace", @namespace }, |
| 48 | + { "asset", asset } |
| 49 | + }); |
| 50 | + var mediaClient = new OnvifMediaClient(applicationContext, mqttSessionClient, new Dictionary<string, string> |
44 | 51 | { |
45 | | - { "ex:namespace", @namespace }, |
46 | | - { "ex:asset", mediaAsset } |
| 52 | + { "namespace", @namespace }, |
| 53 | + { "asset", asset } |
47 | 54 | }); |
48 | | - var profile = profiles.GetProfilesCommandResponse.Profiles.First(); |
| 55 | + |
| 56 | + var profiles = await mediaClient.GetProfilesAsync(); |
| 57 | + var profile = profiles.Result.Profiles.First(); |
49 | 58 |
|
50 | 59 | Console.WriteLine("Use arrow keys or WASD to move camera, Q to quit"); |
51 | 60 |
|
52 | 61 | if (mode == "relative") |
53 | 62 | { |
54 | | - await StartRelativeMoveAsync(ptzClient, profile, @namespace, ptzAsset); |
| 63 | + await StartRelativeMoveAsync(ptzClient, profile, @namespace, asset); |
55 | 64 | } |
56 | 65 | else if (mode == "continuous") |
57 | 66 | { |
58 | | - await StartContinuousMoveAsync(ptzClient, profile, @namespace, ptzAsset); |
| 67 | + await StartContinuousMoveAsync(ptzClient, profile, @namespace, asset); |
59 | 68 | } |
60 | 69 |
|
61 | | - await StartRelativeMoveAsync(ptzClient, profile, @namespace, ptzAsset); |
62 | | -}, hostOption, portOption, namespaceOption, ptzAssetOption, mediaAssetOption, modeOption); |
| 70 | + await StartRelativeMoveAsync(ptzClient, profile, @namespace, asset); |
| 71 | +}); |
63 | 72 |
|
64 | | -await rootCommand.InvokeAsync(args); |
| 73 | +await new CommandLineConfiguration(rootCommand).InvokeAsync(args); |
65 | 74 |
|
66 | 75 | async Task StartRelativeMoveAsync(OnvifPtzClient ptzClient, Profile profile, string @namespace, string ptzAssetName) |
67 | 76 | { |
@@ -109,11 +118,7 @@ async Task StartRelativeMoveAsync(OnvifPtzClient ptzClient, Profile profile, str |
109 | 118 | } |
110 | 119 | }; |
111 | 120 |
|
112 | | - await ptzClient.RelativeMoveAsync(request, new Dictionary<string, string> |
113 | | - { |
114 | | - { "ex:namespace", @namespace }, |
115 | | - { "ex:asset", ptzAssetName } |
116 | | - }); |
| 121 | + await ptzClient.RelativeMoveAsync(request); |
117 | 122 | } |
118 | 123 | catch (Exception e) |
119 | 124 | { |
@@ -171,17 +176,9 @@ async Task StartContinuousMoveAsync(OnvifPtzClient ptzClient, Profile profile, s |
171 | 176 | } |
172 | 177 | }; |
173 | 178 |
|
174 | | - await ptzClient.ContinuousMoveAsync(request, new Dictionary<string, string> |
175 | | - { |
176 | | - { "ex:namespace", @namespace }, |
177 | | - { "ex:asset", ptzAssetName } |
178 | | - }); |
| 179 | + await ptzClient.ContinuousMoveAsync(request); |
179 | 180 | 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 | | - }); |
| 181 | + await ptzClient.StopAsync(new StopRequestPayload { Stop = new Stop { ProfileToken = profile.Token, PanTilt = true } }); |
185 | 182 | } |
186 | 183 | catch (Exception e) |
187 | 184 | { |
|
0 commit comments