forked from SocketMobile/cocoapods-capture
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CaptureHelper.swift
1380 lines (1236 loc) · 57.2 KB
/
CaptureHelper.swift
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
CaptureHelper.swift
Copyright © 2017 Socket Mobile, Inc.
*/
import Foundation
// MARK: - Capture Helper Delegates
/**
Capture Helper base protocol does not have
any delegate, it could be used for View Controllers
that don't require any notification from Capture
*/
@objc
public protocol CaptureHelperDelegate {
}
/// Capture Helper protocol to comply in order to receive any error coming
/// from Capture
@objc
public protocol CaptureHelperErrorDelegate: CaptureHelperDelegate {
/// delegate called when an unexpected error arises
///
/// - Parameter error: contains the error code
func didReceiveError(_ error: SKTResult);
}
/// Capture Helper protocol to comply in order to receive the device arrival, removal notifications
@objc
public protocol CaptureHelperDevicePresenceDelegate: CaptureHelperDelegate {
/// delegate called when a device connects to the host
///
/// - Parameters:
/// - device: contains the device information, such as friendly name and device type
/// - result: contains the result of the device connecting to the host
func didNotifyArrivalForDevice(_ device: CaptureHelperDevice, withResult result: SKTResult)
/// delegate called when a device disconnects from the host
///
/// - Parameters:
/// - device: contains the device information, such as friendly name and device type
/// - result: contains the result of the device disconnecting from the host
func didNotifyRemovalForDevice(_ device: CaptureHelperDevice, withResult result: SKTResult)
}
/// Capture Helper protocol to comply in order to receive the device manager arrival, removal notifications
@objc
public protocol CaptureHelperDeviceManagerPresenceDelegate: CaptureHelperDelegate {
/// delegate called when a device manager connects to the host
///
/// - Parameters:
/// - device: contains the device manager information, such as device type
/// - result: contains the result of the device manager connecting to the host
func didNotifyArrivalForDeviceManager(_ device: CaptureHelperDeviceManager, withResult result: SKTResult)
/// delegate called when a device manager disconnects from the host
///
/// - Parameters:
/// - device: contains the device manager information, such as device type
/// - result: contains the result of the device manager disconnecting from the host
func didNotifyRemovalForDeviceManager(_ device: CaptureHelperDeviceManager, withResult result: SKTResult)
}
/// Capture Helper protocol to comply in order to receive the device manager discovery notifications
@objc
public protocol CaptureHelperDeviceManagerDiscoveryDelegate: CaptureHelperDelegate {
/// delegate called when a device manager discovered a device
///
/// - Parameters:
/// - device: contains the device information, such as device type
/// - deviceManager: contains the device manager information from which the discovery was launched
func didDiscoverDevice(_ device: String, fromDeviceManager deviceManager: CaptureHelperDeviceManager)
/// delegate called when a device manager ended the discovery
///
/// - Parameters:
/// - result: contains the result of the device manager disconnecting from the host
/// - deviceManager: contains the device manager information from which the discovery ended
func didEndDiscoveryWithResult(_ result: SKTResult, fromDeviceManager deviceManager: CaptureHelperDeviceManager)
}
/// Capture Helper protocol to comply in order to receive the decoded data
@objc
public protocol CaptureHelperDeviceDecodedDataDelegate: CaptureHelperDelegate {
/// delegate called when the decoded data is received from a device
///
/// - Parameters:
/// - decodedData: contains the decoded data with its related information
/// - device: contains the device information from which the data has been decoded
/// - result: contains the result of the decoded data, most of the time it's success
/// but in the case of SoftScan the result can be E_CANCELLED when the user cancel
/// the scan.
func didReceiveDecodedData( _ decodedData: SKTCaptureDecodedData?, fromDevice device: CaptureHelperDevice, withResult result: SKTResult)
}
/// Capture Helper protocol to comply in order to receive the power and battery information
@objc
public protocol CaptureHelperDevicePowerDelegate: CaptureHelperDelegate {
/// delegate called when the power state has changed, this is happening when plug in the device to a power adapter
///
/// - Parameters:
/// - powerState: contains the new power state of the device, @see SKTCapturePowerState
/// - device: contains the device information for which the power state has changed
func didChangePowerState(_ powerState: SKTCapturePowerState, forDevice device: CaptureHelperDevice)
/// delegate called when the battery level has changed
///
/// - Parameters:
/// - batteryLevel: contains the battery level in %
/// - device: contains the device information for which the battery level has changed
func didChangeBatteryLevel(_ batteryLevel: Int, forDevice device: CaptureHelperDevice)
}
/// Capture Helper protocol to comply in order to receive the buttons state
@objc
public protocol CaptureHelperDeviceButtonsDelegate : CaptureHelperDelegate {
/// delegate called when the state of the device's buttons has changed
///
/// - Parameters:
/// - buttonsState: contains the new buttons state
/// - device: contains the device information from which the buttons state has changed
func didChangeButtonsState(_ buttonsState: SKTCaptureButtonsState, forDevice device: CaptureHelperDevice)
}
/// Capture Helper protocol to comply in order to receive all the Capture delegates
/// use this protocol if the applications needs to handle all the notifications
/// coming from Capture
@objc
public protocol CaptureHelperAllDelegate : CaptureHelperErrorDelegate, CaptureHelperDevicePresenceDelegate,
CaptureHelperDevicePowerDelegate, CaptureHelperDeviceDecodedDataDelegate,
CaptureHelperDeviceButtonsDelegate, CaptureHelperDeviceManagerPresenceDelegate {
}
// MARK: - Capture Helper Device
/**
Capture Helper device, represents a device that is attached to Capture
*/
@objcMembers
public class CaptureHelperDevice : NSObject {
fileprivate var capture : SKTCapture
/// can store an object that can be used for extension
public var extensionProperties : Dictionary<String, Any>
/// contains information about the device
/// such as the device friendly name and device type
public var deviceInfo = SKTCaptureDeviceInfo()
/// specify the main Dispatch queue so the UI controls
/// can be updated directly inside the completion handlers
public var dispatchQueue : DispatchQueue?
fileprivate override init() {
self.capture = SKTCapture()
self.extensionProperties = Dictionary<String, Any>()
super.init()
}
convenience init(deviceInfo: SKTCaptureDeviceInfo, capture: SKTCapture){
self.init()
self.deviceInfo = deviceInfo
self.capture = capture
}
/// retrieve the device friendly name
///
/// - Parameter completion: closure receiving the response with the result and the friendly name of
/// the device if the result is successful
open func getFriendlyNameWithCompletionHandler(_ completion: @escaping (_ result: SKTResult, _ name: String?)->Void){
let property = SKTCaptureProperty()
property.id = .friendlyNameDevice
property.type = .none
getProperty(property) { (result, propertyResult) in
completion(result, propertyResult?.stringValue)
}
}
/// set the device friendly name. The device friendly name has a limit of
/// 32 UTF8 characters including the null terminated character, an error is
/// generated if the friendly name is too long.
///
/// - Parameters:
/// - name: friendly name to set the device with
/// - completion: closure receiving the result of setting the new friendly name
open func setFriendlyName(_ name: String, withCompletionHandler completion: @escaping(_ result: SKTResult)->Void){
let property = SKTCaptureProperty()
property.id = .friendlyNameDevice
property.type = .string
property.stringValue = name
setProperty(property) { (result, propertyResult) in
completion(result)
}
}
/// get the device Bluetooth address
///
/// - Parameter completion: receiving the result of getting the device Bluetooth Address if the result
open func getBluetoothAddressWithCompletionHandler(_ completion: @escaping(_ result: SKTResult, _ bluetoothAddress: Data?)->Void){
let property = SKTCaptureProperty()
property.id = .bluetoothAddressDevice
property.type = .none
getProperty(property) { (result, propertyResult) in
completion(result, propertyResult?.arrayValue)
}
}
/// get the device Type
///
/// - Parameter completion: receiving the result and the device Type if the result is successful
open func getTypeWithCompletionHandler(_ completion: @escaping(_ result: SKTResult, _ deviceType: UInt?)->Void){
let property = SKTCaptureProperty()
property.id = .deviceType
property.type = .none
capture.getProperty(property) { (result, propertyResult) in
completion(result, propertyResult?.uLongValue)
}
}
/// get the device Firmware version
///
/// - Parameter completion: receiving the result and the device Firmware version if the result is successful
open func getFirmwareVersionWithCompletionHandler(_ completion: @escaping(_ result: SKTResult,_ version: SKTCaptureVersion?)->Void){
let property = SKTCaptureProperty()
property.id = .versionDevice
property.type = .none
getProperty(property) { (result, propertyResult) in
completion(result, propertyResult?.version)
}
}
/// get the device battery level
///
/// note: to avoid pulling the battery level, some devices support a battery level change
/// notification.
///
/// - Parameter completion: receiving the result and the device battery level if the result is successful
open func getBatteryLevelWithCompletionHandler(_ completion: @escaping(_ result: SKTResult, _ batteryLevel: UInt)->Void){
let property = SKTCaptureProperty()
property.id = .batteryLevelDevice
property.type = .none
getProperty(property) { (result, propertyResult) in
completion(result, propertyResult?.uLongValue ?? 0)
}
}
/// get the device power state
///
/// note: to avoid pulling the power state, some devices support a power state change
/// notification.
///
/// - Parameter completion: receiving the result and the device power state if the result is successful
open func getPowerStateWithCompletionHandler(_ completion: @escaping(_ result: SKTResult, _ powerState: UInt?)->Void){
let property = SKTCaptureProperty()
property.id = .powerStateDevice
property.type = .none
getProperty(property) { (result, propertyResult) in
completion(result, propertyResult?.uLongValue)
}
}
/// get the device buttons state
///
/// note: some devices support buttons state change notifications
///
/// - Parameter completion: receiving the result and the device buttons state if the result is successful
open func getButtonsStateWithCompletionHandler(_ completion: @escaping(_ result: SKTResult, _ buttonsState: SKTCaptureButtonsState?)->Void){
let property = SKTCaptureProperty()
property.id = .buttonsStatusDevice
property.type = .none
getProperty(property) { (result, propertyResult) in
var buttonsState = nil as SKTCaptureButtonsState?
if result == SKTCaptureErrors.E_NOERROR {
if let byteValue = propertyResult?.byteValue {
buttonsState = SKTCaptureButtonsState(rawValue: Int(byteValue))
}
}
completion(result, buttonsState)
}
}
/// get the device stand configuration
///
/// - Parameter completion: receiving the result and the device stand configuration if the result is successful
open func getStandConfigWithCompletionHandler(_ completion: @escaping(_ result: SKTResult, _ config: SKTCaptureStandConfig?)->Void){
let property = SKTCaptureProperty()
property.id = .standConfigDevice
property.type = .none
getProperty(property) { (result, propertyResult) in
var standConfig = nil as SKTCaptureStandConfig?
if result == SKTCaptureErrors.E_NOERROR {
if let ulongValue = propertyResult?.uLongValue {
standConfig = SKTCaptureStandConfig(rawValue: Int(ulongValue))
}
}
completion(result, standConfig)
}
}
/// set the device stand configuration
///
/// - Parameters:
/// - config: config stand configuration to set the device with
/// - completion: block receiving the result of changing the device stand configuration
open func setStandConfig(_ config: SKTCaptureStandConfig, withCompletionHandler completion: @escaping(_ result: SKTResult)->Void){
let property = SKTCaptureProperty()
property.id = .standConfigDevice
property.type = .ulong
property.uLongValue = UInt(config.rawValue)
setProperty(property) { (result, propertyResult) in
completion(result)
}
}
/// get the device decode action
///
/// - Parameter completion: receiving the result and the device decode action if the result is successful
open func getDecodeActionWithCompletionHandler(_ completion: @escaping(_ result: SKTResult, _ decodeAction: SKTCaptureLocalDecodeAction?)->Void){
let property = SKTCaptureProperty()
property.id = .localDecodeActionDevice
property.type = .none
getProperty(property) { (result, propertyResult) in
var decodeAction = nil as SKTCaptureLocalDecodeAction?
if result == SKTCaptureErrors.E_NOERROR {
if let byteValue = propertyResult?.byteValue {
decodeAction = SKTCaptureLocalDecodeAction(rawValue: Int(byteValue))
}
}
completion(result, decodeAction)
}
}
/// set the device decode action
///
/// - Parameters:
/// - decodeAction: decode action to set the device with
/// - completion: receiving the result of changing the device decode action
open func setDecodeAction(_ decodeAction: SKTCaptureLocalDecodeAction, withCompletionHandler completion:@escaping(_ result: SKTResult)->Void){
let property = SKTCaptureProperty()
property.id = .localDecodeActionDevice
property.type = .byte
property.byteValue = Int8(decodeAction.rawValue)
setProperty(property) { (result, propertyResult) in
completion(result)
}
}
/// get the device local data acknowledgment
///
/// - Parameter completion: receiving the result and the device local acknowledgment if the result is successful
open func getDataAcknowledgmentWithCompletionHandler(_ completion: @escaping(_ result: SKTResult, _ dataAcknownledgement: SKTCaptureDeviceDataAcknowledgment?)->Void){
let property = SKTCaptureProperty()
property.id = .dataConfirmationDevice
property.type = .none
getProperty(property) { (result, propertyResult) in
var dataAck = nil as SKTCaptureDeviceDataAcknowledgment?
if result == SKTCaptureErrors.E_NOERROR {
if let uLongValue = propertyResult?.uLongValue {
dataAck = SKTCaptureDeviceDataAcknowledgment(rawValue: Int(uLongValue))
}
}
completion(result, dataAck)
}
}
/// set the device local data acknowledgment
///
/// - Parameters:
/// - dataAcknowledgment: set how the device acknwoledges data locally on the device
/// - completion: receiving the result of changing the device stand configuration
open func setDataAcknowledgment(_ dataAcknowledgment: SKTCaptureDeviceDataAcknowledgment, withCompletionHandler completion:@escaping(_ result: SKTResult)->Void){
let property = SKTCaptureProperty()
property.id = .dataConfirmationDevice
property.type = .ulong
property.uLongValue = UInt(dataAcknowledgment.rawValue)
setProperty(property) { (result, propertyResult) in
completion(result)
}
}
/// get the device postamble
///
/// - Parameter completion: receiving the result and the device postamble if the result is successful
open func getPostambleWithCompletionHandler(_ completion: @escaping(_ result: SKTResult, _ postamble: String?)->Void){
let property = SKTCaptureProperty()
property.id = .postambleDevice
property.type = .none
getProperty(property) { (result, propertyResult) in
completion(result, propertyResult?.stringValue)
}
}
/// set the device postamble
///
/// - Parameters:
/// - postamble: postamble to set the device with
/// - completion: receiving the result of changing the device postamble
open func setPostamble(_ postamble: String, withCompletionHandler completion:@escaping(_ result: SKTResult)->Void){
let property = SKTCaptureProperty()
property.id = .postambleDevice
property.type = .string
property.stringValue = postamble
setProperty(property) { (result, propertyResult) in
completion(result)
}
}
/// get the device data source information
///
/// - Parameters:
/// - data source Id: contains the data source ID for which the information would be retrieved
/// - completion: receiving the result and the device data source information if the result is successful
open func getDataSourceInfoFromId(_ dataSourceId: SKTCaptureDataSourceID, withCompletionHandler completion:@escaping(_ result: SKTResult, _ dataSourceInfo: SKTCaptureDataSource?)->Void){
let dataSource = SKTCaptureDataSource()
let property = SKTCaptureProperty()
property.id = .dataSourceDevice
property.type = .dataSource
dataSource.id = dataSourceId
dataSource.flags = .status
property.dataSource = dataSource
getProperty(property) { (result, propertyResult) in
completion(result, propertyResult?.dataSource)
}
}
/// set the device data source information
///
/// - Parameters:
/// - dataSource: data source to enable or disable
/// - completion: receiving the result of changing the device data source
open func setDataSourceInfo(_ dataSource: SKTCaptureDataSource, withCompletionHandler completion: @escaping(_ result: SKTResult)->Void){
let property = SKTCaptureProperty()
property.id = .dataSourceDevice
property.type = .dataSource
property.dataSource = dataSource
setProperty(property) { (result, propertyResult) in
completion(result)
}
}
/// set the device trigger
///
/// this operation can programmatically start a device read operation, or it can
/// disable the device trigger button until it gets re-enable again by using this
/// function too.
///
/// - Parameters:
/// - trigger: contains the trigger command to apply
/// - completion: receiving the result of setting the trigger
open func setTrigger(_ trigger: SKTCaptureTrigger, withCompletionHandler completion: @escaping(_ result: SKTResult)->Void){
let property = SKTCaptureProperty()
property.id = .triggerDevice
property.type = .byte
property.byteValue = Int8(trigger.rawValue)
setProperty(property) { (result, propertyResult) in
completion(result)
}
}
/// set the decoded data confirmation
///
/// This function is required to acknowledge the decoded data that has been received
/// when the data confirmation mode has been set to SKTCaptureDataConfirmationModeApp
///
/// This function could also be called at any point of time if something needs to
/// be reported to the user. By example making the scanner beep or vibrate to get
/// the user to look at a screen.
///
/// note: Good AND Bad settings can not be used together.
///
/// - Parameters:
/// - led: contains the led to light (None, Green, Red)
/// - beep: contains the beep to perform (None, Good, Bad)
/// - rumble: contains the rumble to perform (None, Good, Bad)
/// - completion: receiving the result of setting the decoded data confirmation
open func setDataConfirmationWithLed(_ led: SKTCaptureDataConfirmationLed, withBeep beep:SKTCaptureDataConfirmationBeep, withRumble rumble: SKTCaptureDataConfirmationRumble, withCompletionHandler completion:@escaping(_ result: SKTResult)->Void){
let property = SKTCaptureProperty()
property.id = .dataConfirmationDevice
property.type = .ulong
property.uLongValue = UInt(SKTHelper.getDataComfirmation(withReserve: 0, withRumble: rumble.rawValue, withBeep: beep.rawValue, withLed: led.rawValue))
setProperty(property) { (result, propertyResult) in
completion(result)
}
}
/// set the device notifications
///
/// - Parameters:
/// - notifications: select the notifications to receive
/// - completion: receiving the result of setting the notifications
open func setNotifications(_ notifications: SKTCaptureNotifications, withCompletionHandler completion: @escaping(_ result: SKTResult)->Void){
let property = SKTCaptureProperty()
property.id = .notificationsDevice
property.type = .ulong
property.uLongValue = UInt(notifications.rawValue)
setProperty(property) { (result, propertyResult) in
completion(result)
}
}
/// get the device notifications selection
///
/// - Parameter completion: receiving the result and the device notifications setting if the result is successful
open func getNotificationsWithCompletionHandler(_ completion: @escaping(_ result: SKTResult, _ notifications: SKTCaptureNotifications?)->Void){
let property = SKTCaptureProperty()
property.id = .notificationsDevice
property.type = .none
getProperty(property) { (result, propertyResult) in
var notifications = nil as SKTCaptureNotifications?
if let longValue = propertyResult?.uLongValue {
notifications = SKTCaptureNotifications(rawValue: Int(longValue))
}
completion(result, notifications)
}
}
/// set the SoftScan overlay view parameters mainly for the ViewFinder view
///
/// - Parameters:
/// - parameters: dictionary containing the parameters for the ViewFinder
/// - completion: closure receiving the result of setting the Overlay View parameters
open func setSoftScanOverlayViewParameter(_ parameters: Dictionary<String, Any>, withCompletionHandler completion: @escaping(_ result: SKTResult)->Void){
let property = SKTCaptureProperty()
property.id = .overlayViewDevice
property.type = .object
property.object = parameters as NSObject
setProperty(property) { (result, propertyResult) in
completion(result)
}
}
/// get the SoftScan overlay view parameters used mainly for the ViewFinder view
///
/// - Parameter completion: closure receiving the result and the overlay view parameters
open func getSoftScanOverlayViewParameterWithCompletionHandler(_ completion: @escaping(_ result: SKTResult, _ parameter: Dictionary<String, Any>?)->Void){
let property = SKTCaptureProperty()
property.id = .overlayViewDevice
property.type = .none
getProperty(property) { (result, propertyResult) in
var parameters = nil as Dictionary<String,Any>?
if let obj = propertyResult?.object {
parameters = obj as? Dictionary<String,Any>
}
completion(result, parameters)
}
}
/// send a specific command to the device
///
/// These commands are specific to a device, therefore the device should first be identified
/// before sending such commands otherwise an unpredicable result could happen if they are
/// sent to a different device.
///
/// - Parameters:
/// - command: an array of bytes that holds the specific command to send to the device
/// - completion: receiving the result and the device specific command response if the result is successful
open func getDeviceSpecificCommand(_ command: Data, withCompletionHandler completion: @escaping(_ result: SKTResult, _ commandResult: Data?)->Void){
let property = SKTCaptureProperty()
property.id = .deviceSpecific
property.type = .array
property.arrayValue = command
getProperty(property) { (result, propertyResult) in
completion(result, propertyResult?.arrayValue)
}
}
/// Set a data format to the device
///
/// Examples:
/// ID-Only, TagType-and-ID, Data-Only, TagType-and-Data
open func setDataFormat(dataFormat: SKTCaptureDataFormat, completion: @escaping(_ result: SKTResult)->Void) {
let property = SKTCaptureProperty()
property.id = .dataFormatDevice
property.type = .byte
property.byteValue = Int8(dataFormat.rawValue)
setProperty(property) { (result, propertyResult) in
completion(result)
}
}
/// Get the current data format from the device
///
/// Examples:
/// ID-Only, TagType-and-ID, Data-Only, TagType-and-Data
/// NOTE: Only tagType-and-ID , TagType-and-Data formats are accepted. The other two will purposely return an error
open func getDataFormatWithCompletionHandler(_ completion: @escaping(_ result: SKTResult, _ dataFormat: SKTCaptureDataFormat?)->Void) {
let property = SKTCaptureProperty()
property.id = .dataFormatDevice
property.type = .none
getProperty(property) {(result, propertyResult) in
var dataFormat = nil as SKTCaptureDataFormat?
if let byteValue = propertyResult?.byteValue {
dataFormat = SKTCaptureDataFormat(rawValue: Int(byteValue))
}
completion(result, dataFormat)
}
}
/// get a property using the Capture property object
///
/// Usually the get property sends a property without arguments but
/// the response in case of success contains a property response that
/// holds the eventual arguments of the property.
/// - Parameters:
/// - property: reference to the property to set
/// - completion: receiving the result and the device specific command response if the result is successful
open func getProperty(_ property: SKTCaptureProperty, withCompletionHandler completion: @escaping(_ result: SKTResult, _ complete: SKTCaptureProperty?)->Void){
capture.getProperty(property) {(result, propertyResult) in
if let dq = self.dispatchQueue {
dq.async{
completion(result, propertyResult)
}
} else {
completion(result, propertyResult)
}
}
}
/// set a property using the Capture property object
///
/// Usually the set property does not return any property arguments,
/// only the result is interesting to check to know if the set property
/// has been successful.
/// - Parameters:
/// - property: reference to the property to set
/// - completion: receiving the result and the device specific command response if the result is successful
open func setProperty(_ property: SKTCaptureProperty, withCompletionHandler completion: @escaping(_ result: SKTResult, _ complete: SKTCaptureProperty?)->Void){
capture.setProperty(property) { (result, propertyResult) in
if let dq = self.dispatchQueue {
dq.async {
completion(result, propertyResult)
}
} else {
completion(result, propertyResult)
}
}
}
}
// MARK: - Capture Helper Device Manager
@objcMembers
public class CaptureHelperDeviceManager : CaptureHelperDevice {
open func startDiscoveryWithTimeout(_ timeout: NSInteger, withCompletionHandler completion: @escaping(_ result: SKTResult)->Void){
let property = SKTCaptureProperty()
property.id = .startDiscovery
property.type = .ulong
property.uLongValue = UInt(timeout)
capture.setProperty(property) { (result, propertyResult) in
if let dq = self.dispatchQueue {
dq.async {
completion(result)
}
} else {
completion(result)
}
}
}
open func setFavoriteDevices(_ favorites: String, withCompletionHandler completion: @escaping(_ result: SKTResult)->Void){
let property = SKTCaptureProperty()
property.id = .favorite
property.type = .string
property.stringValue = favorites
capture.setProperty(property) { (result, propertyResult) in
if let dq = self.dispatchQueue {
dq.async {
completion(result)
}
} else {
completion(result)
}
}
}
open func getFavoriteDevicesWithCompletionHandler(_ completion: @escaping(_ result: SKTResult, _ favorites: String?)->Void){
let property = SKTCaptureProperty()
property.id = .favorite
property.type = .none
capture.getProperty(property) {(result, propertyResult) in
if let dq = self.dispatchQueue {
dq.async {
completion(result, propertyResult?.stringValue)
}
} else {
completion(result, propertyResult?.stringValue)
}
}
}
open func getDeviceUniqueIdentifierFromDeviceGuid(_ deviceGuid: String, withCompletionHandler completion: @escaping(_ result: SKTResult, _ deviceUniqueIdentifier: String?)->Void){
let property = SKTCaptureProperty()
property.id = .uniqueDeviceIdentifier
property.type = .string
property.stringValue = deviceGuid
capture.getProperty(property) {(result, propertyResult) in
if let dq = self.dispatchQueue {
dq.async {
completion(result, propertyResult?.stringValue)
}
} else {
completion(result, propertyResult?.stringValue)
}
}
}
}
// MARK: - Capture Helper Main Entry Point
/// Main entry point for using Capture
/// 1- get a Capture instance by using CaptureHelper.sharedInstance
/// 2- push a View Controller delegate reference that is compliant to a CaptureHelperDelegate protocol
/// 3- fill a SKTAppInfo with developer ID, bundle ID and AppKey coming from Socket Mobile developer portal
/// 4- open Capture with the SKTAppInfo instance
@objcMembers
public class CaptureHelper : NSObject, SKTCaptureDelegate {
private var capture : SKTCapture?
private var openCount = 0;
fileprivate var delegatesStack = Array<CaptureHelperDelegate>()
fileprivate var currentDelegate : CaptureHelperDelegate?
fileprivate var devices = Dictionary<String, CaptureHelperDevice>()
fileprivate var deviceManagers = Dictionary<String, CaptureHelperDeviceManager>()
/// can store any object that can be used in an extension
public var extensionProperties : Dictionary<String, Any>
/// dispatch queue that can be set to the
/// main queue so the completion handlers
/// can update directly the UI controls
/// set this property to DispatchQueue.main
public var dispatchQueue : DispatchQueue?
/// static shared instance of CaptureHelper
public static let sharedInstance = CaptureHelper()
fileprivate override init() {
extensionProperties = Dictionary<String, Any>()
}
/// reference to the Capture API that can be used in
/// extension class
public var captureApi : SKTCapture? {
get {
return capture;
}
}
/// push a delegate reference into the delegates stack
/// each new View can push its delegate reference and the
/// last one pushed is the one used for receiving the Capture
/// delegates.
/// NOTE: If a device is already connected to the host, doing a pushDelegate
/// will call the didNotifyArrivalForDevice if the delegate pushed supports
/// this in its protocol.
///
/// - Parameter delegate : reference to a delegate to push in the delegates stack
/// - Returns:
/// hasBeenPushed : true is the delegate has been pushed, false otherwise
@discardableResult open func pushDelegate(_ delegate: CaptureHelperDelegate) -> Bool {
var hasBeenPushed = false
// make sure the currentDelegate if not nil
// that is not equal to delegate passed in argument
if let current = currentDelegate {
if (current as AnyObject === delegate as AnyObject) {
return hasBeenPushed
}
}
hasBeenPushed = true
delegatesStack.append(delegate)
currentDelegate = delegate
if let delegate = self.currentDelegate as? CaptureHelperDevicePresenceDelegate {
for device in devices {
if let dq = self.dispatchQueue {
dq.async{
delegate.didNotifyArrivalForDevice(device.value, withResult: SKTCaptureErrors.E_NOERROR)
}
} else {
delegate.didNotifyArrivalForDevice(device.value, withResult: SKTCaptureErrors.E_NOERROR)
}
}
}
if let delegate = self.currentDelegate as? CaptureHelperDeviceManagerPresenceDelegate {
for device in deviceManagers {
if let dq = self.dispatchQueue {
dq.async{
delegate.didNotifyArrivalForDeviceManager(device.value, withResult: SKTCaptureErrors.E_NOERROR)
}
} else {
delegate.didNotifyArrivalForDeviceManager(device.value, withResult: SKTCaptureErrors.E_NOERROR)
}
}
}
return hasBeenPushed
}
/// remove the delegate from the delegates stack
///
/// - Parameter delegate: reference to the class that receive
/// the Capture Delegates
///
/// - Returns:
/// hasBeenPoped is true if the delegate has been poped from the
// delegates stack, false otherwise
@discardableResult open func popDelegate(_ delegate: CaptureHelperDelegate) -> Bool {
var hasBeenRemoved = false
if delegatesStack.count > 0 {
let last = delegatesStack.removeLast()
if (last as AnyObject === delegate as AnyObject) {
hasBeenRemoved = true
currentDelegate = delegatesStack.last
} else {
delegatesStack.append(last)
}
}
return hasBeenRemoved
}
/// get the list of devices this CaptureHelper has opened
///
/// - Returns: list of CaptureHelperDevice opened by Capture Helper
open func getDevices()->Array<CaptureHelperDevice> {
let newDevices = Array<CaptureHelperDevice>(devices.values)
return newDevices
}
/// get the list of device managers this CaptureHelper has opened
///
/// - Returns: list of CaptureHelperDeviceManager opened by Capture Helper
open func getDeviceManagers()->Array<CaptureHelperDeviceManager> {
let newDeviceManagers = Array<CaptureHelperDeviceManager>(deviceManagers.values)
return newDeviceManagers
}
/// open Capture
///
/// Main entry point of the Capture SDK. This method should be called first.
///
/// open Capture with the application information which contains
/// the developer ID, the application Bundle ID and the AppKey that
/// can be retrieved on the Socket Mobile developer portal
///
/// The delegates should be pushed before calling this method to make sure
/// none of the events are missed.
/// - Parameters:
/// - appInfo: class containing developer ID, application Bundle ID and the AppKey
/// - completionHandler: called upon completion with the result code
open func openWithAppInfo(_ appInfo: SKTAppInfo, withCompletionHandler completion: @escaping (_ result: SKTResult)->Void){
if (capture == nil) {
capture = SKTCapture(delegate: self)
capture?.open(with: appInfo, completionHandler: {(result) in
if result == SKTCaptureErrors.E_NOERROR {
self.openCount += 1;
} else {
self.capture = nil;
}
if let bundle = appInfo.mainBundle {
if SKTCapture.canConnectToBarcodeScanners(with: bundle) == false {
print("\nIf your application uses a Socket Mobile barcode scanner, you need to add a \"UISupportedExternalAccessoryProtocols\" Array to your Info.plist with \"com.socketmobile.chs\" as an Item in that Array. Otherwise the scanner won't connect to the application.\n")
}
}
self.callCompletion(withResult:result, withCompletion:completion)
})
} else {
self.openCount += 1;
self.callCompletion(withResult:SKTCaptureErrors.E_NOERROR, withCompletion:completion)
}
}
/// close Capture, once this method is called the application won't
/// receive anything from Capture
///
/// - Parameter completionHandler: called upon complete with the result code
open func closeWithCompletionHandler(_ completion: @escaping (_ result: SKTResult)->Void){
if let cap = capture {
self.openCount -= 1;
if self.openCount == 0 {
cap.close(completionHandler:{(result) in
self.capture = nil
self.callCompletion(withResult:result, withCompletion:completion)
})
} else {
callCompletion(withResult: SKTCaptureErrors.E_NOERROR, withCompletion: completion);
}
}
else {
callCompletion(withResult: SKTCaptureErrors.E_NOERROR, withCompletion: completion);
}
}
/// delegate from Capture that is called each time a Capture event is fired
///
/// - Parameters:
/// - event: Capture event sent by Capture to the application
/// - capture: reference of the capture the event refers to
/// - result: result code of the event
public func didReceive(_ event: SKTCaptureEvent, for capture: SKTCapture, withResult result: SKTResult) {
// Safely catch an error if it occurs
if event.id == SKTCaptureEventID.error {
if let delegate = currentDelegate as? CaptureHelperErrorDelegate {
if let dq = self.dispatchQueue {
dq.async{
delegate.didReceiveError(result)
}
} else {
delegate.didReceiveError(result)
}
}
return
}
switch event.id {
case SKTCaptureEventID.deviceArrival:
handleDeviceArrival(capture: capture, event: event, result: result)
break
case SKTCaptureEventID.deviceRemoval:
handleDeviceRemoval(capture: capture, event: event, result: result)
break
case SKTCaptureEventID.deviceManagerArrival:
handleDeviceManagerArrival(capture: capture, event: event, result: result)
break
case SKTCaptureEventID.deviceManagerRemoval:
handleDeviceManagerRemoval(capture: capture, event: event, result: result)
break
case SKTCaptureEventID.deviceDiscovered:
handleDeviceDiscovered(capture: capture, event: event)
break;
case SKTCaptureEventID.discoveryEnd:
handleDeviceDiscoveryDidEnd(capture: capture, event: event, result: result)
break;
case SKTCaptureEventID.decodedData:
handleDeviceDidReceiveDecodedData(capture: capture, event: event, result: result)
break
case SKTCaptureEventID.batteryLevel:
handleDeviceBatteryLevelDidChange(capture: capture, event: event)
break
case SKTCaptureEventID.power:
handleDevicePowerStateDidChange(capture: capture, event: event)
break
case SKTCaptureEventID.buttons:
handleDeviceButtonsStateDidChange(capture: capture, event: event)
break;
default: break
// not much to do
}
}
// MARK: - Switch case functions
private func handleDeviceArrival(capture: SKTCapture, event: SKTCaptureEvent, result: SKTResult) {