@@ -2,6 +2,7 @@ use super::discovery_impl::util;
2
2
use super :: discovery_utils:: {
3
3
OnvifQuery , OnvifQueryImpl , ONVIF_DEVICE_IP_ADDRESS_LABEL_ID ,
4
4
ONVIF_DEVICE_MAC_ADDRESS_LABEL_ID , ONVIF_DEVICE_SERVICE_URL_LABEL_ID ,
5
+ ONVIF_DEVICE_UUID_LABEL_ID ,
5
6
} ;
6
7
use akri_discovery_utils:: {
7
8
discovery:: {
@@ -73,7 +74,7 @@ impl DiscoveryHandler for DiscoveryHandlerImpl {
73
74
deserialize_discovery_details ( & discover_request. discovery_details )
74
75
. map_err ( |e| tonic:: Status :: new ( tonic:: Code :: InvalidArgument , format ! ( "{}" , e) ) ) ?;
75
76
tokio:: spawn ( async move {
76
- let mut previous_cameras = Vec :: new ( ) ;
77
+ let mut previous_cameras = HashMap :: new ( ) ;
77
78
let mut filtered_camera_devices = HashMap :: new ( ) ;
78
79
loop {
79
80
// Before each iteration, check if receiver has dropped
@@ -98,17 +99,19 @@ impl DiscoveryHandler for DiscoveryHandlerImpl {
98
99
. unwrap ( ) ;
99
100
trace ! ( "discover - discovered:{:?}" , & latest_cameras) ;
100
101
// Remove cameras that have gone offline
101
- previous_cameras. iter ( ) . for_each ( |c| {
102
- if !latest_cameras. contains ( c) {
102
+ previous_cameras. keys ( ) . for_each ( |c| {
103
+ if !latest_cameras. contains_key ( c) {
103
104
changed_camera_list = true ;
104
105
filtered_camera_devices. remove ( c) ;
105
106
}
106
107
} ) ;
107
108
108
109
let futures: Vec < _ > = latest_cameras
109
110
. iter ( )
110
- . filter ( |c| !previous_cameras. contains ( c) )
111
- . map ( |c| apply_filters ( & discovery_handler_config, c, & onvif_query) )
111
+ . filter ( |( k, _) | !previous_cameras. contains_key ( * k) )
112
+ . map ( |( uri, uuid) | {
113
+ apply_filters ( & discovery_handler_config, uri, uuid, & onvif_query)
114
+ } )
112
115
. collect ( ) ;
113
116
let options = futures_util:: future:: join_all ( futures) . await ;
114
117
// Insert newly discovered camera that are not filtered out
@@ -150,9 +153,13 @@ impl DiscoveryHandler for DiscoveryHandlerImpl {
150
153
async fn apply_filters (
151
154
discovery_handler_config : & OnvifDiscoveryDetails ,
152
155
device_service_uri : & str ,
156
+ device_uuid : & str ,
153
157
onvif_query : & impl OnvifQuery ,
154
158
) -> Option < ( String , Device ) > {
155
- info ! ( "apply_filters - device service url {}" , device_service_uri) ;
159
+ info ! (
160
+ "apply_filters - device service url {}, uuid {}" ,
161
+ device_service_uri, device_uuid
162
+ ) ;
156
163
let ( ip_address, mac_address) = match onvif_query
157
164
. get_device_ip_and_mac_address ( device_service_uri)
158
165
. await
@@ -184,19 +191,20 @@ async fn apply_filters(
184
191
return None ;
185
192
}
186
193
187
- let ip_and_mac_joined = format ! ( "{}-{}" , & ip_address , & mac_address ) ;
194
+ let service_uri_and_uuid_joined = format ! ( "{}-{}" , device_service_uri , device_uuid ) ;
188
195
let mut properties = HashMap :: new ( ) ;
189
196
properties. insert (
190
197
ONVIF_DEVICE_SERVICE_URL_LABEL_ID . to_string ( ) ,
191
198
device_service_uri. to_string ( ) ,
192
199
) ;
200
+ properties. insert ( ONVIF_DEVICE_UUID_LABEL_ID . into ( ) , device_uuid. to_string ( ) ) ;
193
201
properties. insert ( ONVIF_DEVICE_IP_ADDRESS_LABEL_ID . into ( ) , ip_address) ;
194
202
properties. insert ( ONVIF_DEVICE_MAC_ADDRESS_LABEL_ID . into ( ) , mac_address) ;
195
203
196
204
Some ( (
197
205
device_service_uri. to_string ( ) ,
198
206
Device {
199
- id : ip_and_mac_joined ,
207
+ id : service_uri_and_uuid_joined ,
200
208
properties,
201
209
mounts : Vec :: default ( ) ,
202
210
device_specs : Vec :: default ( ) ,
@@ -239,20 +247,20 @@ mod tests {
239
247
. returning ( move |_| Ok ( ( ip. to_string ( ) , mac. to_string ( ) ) ) ) ;
240
248
}
241
249
242
- fn expected_device ( uri : & str , ip : & str , mac : & str ) -> ( String , Device ) {
250
+ fn expected_device ( uri : & str , uuid : & str , ip : & str , mac : & str ) -> ( String , Device ) {
243
251
let mut properties = HashMap :: new ( ) ;
244
252
properties. insert (
245
253
ONVIF_DEVICE_SERVICE_URL_LABEL_ID . to_string ( ) ,
246
254
uri. to_string ( ) ,
247
255
) ;
248
-
256
+ properties . insert ( ONVIF_DEVICE_UUID_LABEL_ID . into ( ) , uuid . to_string ( ) ) ;
249
257
properties. insert ( ONVIF_DEVICE_IP_ADDRESS_LABEL_ID . into ( ) , ip. to_string ( ) ) ;
250
258
properties. insert ( ONVIF_DEVICE_MAC_ADDRESS_LABEL_ID . into ( ) , mac. to_string ( ) ) ;
251
259
252
260
(
253
261
uri. to_string ( ) ,
254
262
Device {
255
- id : format ! ( "{}-{}" , ip , mac ) ,
263
+ id : format ! ( "{}-{}" , uri , uuid ) ,
256
264
properties,
257
265
mounts : Vec :: default ( ) ,
258
266
device_specs : Vec :: default ( ) ,
@@ -271,6 +279,7 @@ mod tests {
271
279
#[ tokio:: test]
272
280
async fn test_apply_filters_no_filters ( ) {
273
281
let mock_uri = "device_uri" ;
282
+ let mock_uuid = "device_uuid" ;
274
283
let mock_ip = "mock.ip" ;
275
284
let mock_mac = "mock:mac" ;
276
285
@@ -290,14 +299,20 @@ mod tests {
290
299
scopes : None ,
291
300
discovery_timeout_seconds : 1 ,
292
301
} ;
293
- let instance = apply_filters ( & onvif_config, mock_uri, & mock) . await . unwrap ( ) ;
302
+ let instance = apply_filters ( & onvif_config, mock_uri, mock_uuid, & mock)
303
+ . await
304
+ . unwrap ( ) ;
294
305
295
- assert_eq ! ( expected_device( mock_uri, mock_ip, mock_mac) , instance) ;
306
+ assert_eq ! (
307
+ expected_device( mock_uri, mock_uuid, mock_ip, mock_mac) ,
308
+ instance
309
+ ) ;
296
310
}
297
311
298
312
#[ tokio:: test]
299
313
async fn test_apply_filters_include_ip_exist ( ) {
300
314
let mock_uri = "device_uri" ;
315
+ let mock_uuid = "device_uuid" ;
301
316
let mock_ip = "mock.ip" ;
302
317
let mock_mac = "mock:mac" ;
303
318
@@ -320,14 +335,20 @@ mod tests {
320
335
scopes : None ,
321
336
discovery_timeout_seconds : 1 ,
322
337
} ;
323
- let instance = apply_filters ( & onvif_config, mock_uri, & mock) . await . unwrap ( ) ;
338
+ let instance = apply_filters ( & onvif_config, mock_uri, mock_uuid, & mock)
339
+ . await
340
+ . unwrap ( ) ;
324
341
325
- assert_eq ! ( expected_device( mock_uri, mock_ip, mock_mac) , instance) ;
342
+ assert_eq ! (
343
+ expected_device( mock_uri, mock_uuid, mock_ip, mock_mac) ,
344
+ instance
345
+ ) ;
326
346
}
327
347
328
348
#[ tokio:: test]
329
349
async fn test_apply_filters_include_ip_nonexist ( ) {
330
350
let mock_uri = "device_uri" ;
351
+ let mock_uuid = "device_uuid" ;
331
352
332
353
let mut mock = MockOnvifQuery :: new ( ) ;
333
354
configure_scenario (
@@ -348,14 +369,15 @@ mod tests {
348
369
scopes : None ,
349
370
discovery_timeout_seconds : 1 ,
350
371
} ;
351
- assert ! ( apply_filters( & onvif_config, mock_uri, & mock)
372
+ assert ! ( apply_filters( & onvif_config, mock_uri, mock_uuid , & mock)
352
373
. await
353
374
. is_none( ) ) ;
354
375
}
355
376
356
377
#[ tokio:: test]
357
378
async fn test_apply_filters_include_ip_similar ( ) {
358
379
let mock_uri = "device_uri" ;
380
+ let mock_uuid = "device_uuid" ;
359
381
360
382
let mut mock = MockOnvifQuery :: new ( ) ;
361
383
configure_scenario (
@@ -376,14 +398,15 @@ mod tests {
376
398
scopes : None ,
377
399
discovery_timeout_seconds : 1 ,
378
400
} ;
379
- assert ! ( apply_filters( & onvif_config, mock_uri, & mock)
401
+ assert ! ( apply_filters( & onvif_config, mock_uri, mock_uuid , & mock)
380
402
. await
381
403
. is_none( ) ) ;
382
404
}
383
405
384
406
#[ tokio:: test]
385
407
async fn test_apply_filters_exclude_ip_nonexist ( ) {
386
408
let mock_uri = "device_uri" ;
409
+ let mock_uuid = "device_uuid" ;
387
410
let mock_ip = "mock.ip" ;
388
411
let mock_mac = "mock:mac" ;
389
412
@@ -406,14 +429,20 @@ mod tests {
406
429
scopes : None ,
407
430
discovery_timeout_seconds : 1 ,
408
431
} ;
409
- let instance = apply_filters ( & onvif_config, mock_uri, & mock) . await . unwrap ( ) ;
432
+ let instance = apply_filters ( & onvif_config, mock_uri, mock_uuid, & mock)
433
+ . await
434
+ . unwrap ( ) ;
410
435
411
- assert_eq ! ( expected_device( mock_uri, mock_ip, mock_mac) , instance) ;
436
+ assert_eq ! (
437
+ expected_device( mock_uri, mock_uuid, mock_ip, mock_mac) ,
438
+ instance
439
+ ) ;
412
440
}
413
441
414
442
#[ tokio:: test]
415
443
async fn test_apply_filters_exclude_ip_exist ( ) {
416
444
let mock_uri = "device_uri" ;
445
+ let mock_uuid = "device_uuid" ;
417
446
let mock_ip = "mock.ip" ;
418
447
419
448
let mut mock = MockOnvifQuery :: new ( ) ;
@@ -435,14 +464,15 @@ mod tests {
435
464
scopes : None ,
436
465
discovery_timeout_seconds : 1 ,
437
466
} ;
438
- assert ! ( apply_filters( & onvif_config, mock_uri, & mock)
467
+ assert ! ( apply_filters( & onvif_config, mock_uri, mock_uuid , & mock)
439
468
. await
440
469
. is_none( ) ) ;
441
470
}
442
471
443
472
#[ tokio:: test]
444
473
async fn test_apply_filters_exclude_ip_similar ( ) {
445
474
let mock_uri = "device_uri" ;
475
+ let mock_uuid = "device_uuid" ;
446
476
let mock_ip = "mock.ip" ;
447
477
let mock_mac = "mock:mac" ;
448
478
@@ -465,14 +495,20 @@ mod tests {
465
495
scopes : None ,
466
496
discovery_timeout_seconds : 1 ,
467
497
} ;
468
- let instance = apply_filters ( & onvif_config, mock_uri, & mock) . await . unwrap ( ) ;
498
+ let instance = apply_filters ( & onvif_config, mock_uri, mock_uuid, & mock)
499
+ . await
500
+ . unwrap ( ) ;
469
501
470
- assert_eq ! ( expected_device( mock_uri, mock_ip, mock_mac) , instance) ;
502
+ assert_eq ! (
503
+ expected_device( mock_uri, mock_uuid, mock_ip, mock_mac) ,
504
+ instance
505
+ ) ;
471
506
}
472
507
473
508
#[ tokio:: test]
474
509
async fn test_apply_filters_include_mac_exist ( ) {
475
510
let mock_uri = "device_uri" ;
511
+ let mock_uuid = "device_uuid" ;
476
512
let mock_ip = "mock.ip" ;
477
513
let mock_mac = "mock:mac" ;
478
514
@@ -495,14 +531,20 @@ mod tests {
495
531
scopes : None ,
496
532
discovery_timeout_seconds : 1 ,
497
533
} ;
498
- let instance = apply_filters ( & onvif_config, mock_uri, & mock) . await . unwrap ( ) ;
534
+ let instance = apply_filters ( & onvif_config, mock_uri, mock_uuid, & mock)
535
+ . await
536
+ . unwrap ( ) ;
499
537
500
- assert_eq ! ( expected_device( mock_uri, mock_ip, mock_mac) , instance) ;
538
+ assert_eq ! (
539
+ expected_device( mock_uri, mock_uuid, mock_ip, mock_mac) ,
540
+ instance
541
+ ) ;
501
542
}
502
543
503
544
#[ tokio:: test]
504
545
async fn test_apply_filters_include_mac_nonexist ( ) {
505
546
let mock_uri = "device_uri" ;
547
+ let mock_uuid = "device_uuid" ;
506
548
507
549
let mut mock = MockOnvifQuery :: new ( ) ;
508
550
configure_scenario (
@@ -523,14 +565,15 @@ mod tests {
523
565
scopes : None ,
524
566
discovery_timeout_seconds : 1 ,
525
567
} ;
526
- assert ! ( apply_filters( & onvif_config, mock_uri, & mock)
568
+ assert ! ( apply_filters( & onvif_config, mock_uri, mock_uuid , & mock)
527
569
. await
528
570
. is_none( ) ) ;
529
571
}
530
572
531
573
#[ tokio:: test]
532
574
async fn test_apply_filters_exclude_mac_nonexist ( ) {
533
575
let mock_uri = "device_uri" ;
576
+ let mock_uuid = "device_uuid" ;
534
577
let mock_ip = "mock.ip" ;
535
578
let mock_mac = "mock:mac" ;
536
579
@@ -553,14 +596,20 @@ mod tests {
553
596
scopes : None ,
554
597
discovery_timeout_seconds : 1 ,
555
598
} ;
556
- let instance = apply_filters ( & onvif_config, mock_uri, & mock) . await . unwrap ( ) ;
599
+ let instance = apply_filters ( & onvif_config, mock_uri, mock_uuid, & mock)
600
+ . await
601
+ . unwrap ( ) ;
557
602
558
- assert_eq ! ( expected_device( mock_uri, mock_ip, mock_mac) , instance) ;
603
+ assert_eq ! (
604
+ expected_device( mock_uri, mock_uuid, mock_ip, mock_mac) ,
605
+ instance
606
+ ) ;
559
607
}
560
608
561
609
#[ tokio:: test]
562
610
async fn test_apply_filters_exclude_mac_exist ( ) {
563
611
let mock_uri = "device_uri" ;
612
+ let mock_uuid = "device_uuid" ;
564
613
let mock_mac = "mock:mac" ;
565
614
566
615
let mut mock = MockOnvifQuery :: new ( ) ;
@@ -582,14 +631,15 @@ mod tests {
582
631
scopes : None ,
583
632
discovery_timeout_seconds : 1 ,
584
633
} ;
585
- assert ! ( apply_filters( & onvif_config, mock_uri, & mock)
634
+ assert ! ( apply_filters( & onvif_config, mock_uri, mock_uuid , & mock)
586
635
. await
587
636
. is_none( ) ) ;
588
637
}
589
638
590
639
#[ tokio:: test]
591
640
async fn test_apply_filters_include_mac_exist_different_letter_cases ( ) {
592
641
let mock_uri = "device_uri" ;
642
+ let mock_uuid = "device_uuid" ;
593
643
let mock_ip = "mock.ip" ;
594
644
let mock_mac = "MocK:Mac" ;
595
645
@@ -612,14 +662,20 @@ mod tests {
612
662
scopes : None ,
613
663
discovery_timeout_seconds : 1 ,
614
664
} ;
615
- let instance = apply_filters ( & onvif_config, mock_uri, & mock) . await . unwrap ( ) ;
665
+ let instance = apply_filters ( & onvif_config, mock_uri, mock_uuid, & mock)
666
+ . await
667
+ . unwrap ( ) ;
616
668
617
- assert_eq ! ( expected_device( mock_uri, mock_ip, mock_mac) , instance) ;
669
+ assert_eq ! (
670
+ expected_device( mock_uri, mock_uuid, mock_ip, mock_mac) ,
671
+ instance
672
+ ) ;
618
673
}
619
674
620
675
#[ tokio:: test]
621
676
async fn test_apply_filters_exclude_mac_exist_different_letter_cases ( ) {
622
677
let mock_uri = "device_uri" ;
678
+ let mock_uuid = "device_uuid" ;
623
679
let mock_ip = "mock.ip" ;
624
680
let mock_mac = "MocK:Mac" ;
625
681
@@ -642,7 +698,7 @@ mod tests {
642
698
scopes : None ,
643
699
discovery_timeout_seconds : 1 ,
644
700
} ;
645
- assert ! ( apply_filters( & onvif_config, mock_uri, & mock)
701
+ assert ! ( apply_filters( & onvif_config, mock_uri, mock_uuid , & mock)
646
702
. await
647
703
. is_none( ) ) ;
648
704
}
0 commit comments