Camera Control

Last Updated on : 2024-06-27 09:49:54download

The App SDK controls devices with data points (DPs) and facilitates interaction between the device and the app through standard DPs. The Smart Camera SDK, built on top of the Smart Life App SDK, includes smart camera-specific DPs.

  • The Smart Life App SDK uses a DP code to uniquely identify a DP, while the device uses a DP ID for this purpose. When creating custom DPs on the Tuya Developer Platform, make sure that the DP ID and DP code are unique. See Product Functions for creating a DP.
  • See Device Control for camera control.
  • See Standard DPs for common DPs.

Get object

  • ThingSmartCameraDPManager allows communication with the device, such as sending control commands and querying the current device status.
  • ThingSmartCameraDPObserver listens for changes in device status.

Initialize device

API description

- (instancetype)initWithDeviceId:(NSString *)devId;

Parameters

Parameter Description
devId The device ID.

Add or remove device status listener

API description

/// Adds a listener of device status.
- (void)addObserver:(id<ThingSmartCameraDPObserver>)observer;

/// Removes a listener of device status.
- (void)removeObserver:(id<ThingSmartCameraDPObserver>)observer;

Parameters

Parameter Description
observer The listener method. You need to implement ThingSmartCameraDPObserver.

Query DP code by DP ID

API description

This method retrieves the DP code for a DP ID.

- (NSString *)dpCodeWithDpId:(NSString *)dpId;

Example

#import <ThingSmartCameraKit/ThingSmartCameraKit.h>

NSString *dpCode = [deviceModel dpCodeWithDpId:dpId];

Query DP ID by DP code

API description

This method retrieves the DP ID for a DP code.

- (NSString *)dpIdWithDpCode:(NSString *)dpCode;

Example

#import <ThingSmartCameraKit/ThingSmartCameraKit.h>


NSString *dpId = [deviceModel dpIdWithDpCode:dpCode];

Check device support for DP code

API description

- (BOOL)isSupportDPCode:(ThingSmartCameraDPKey)DPCode;

Parameters

Parameter Description
DPCode The DP code.

Return value

Type Description
BOOL Whether the specified DP code is supported.

Query DP value

From cache

API description

Retrieve the DP value directly from the cache. If the DP is not supported, return nil.

- (id)valueForDPCode:(ThingSmartCameraDPKey)DPCode;

Parameters

Parameter Description
DPCode The DP code.

Return value

Parameter Description
id Convert the value based on the DP data type. See Standard DPs for details.

By command

This method is not recommended except for memory card features.

API description

- (void)valueForDPCode:(ThingSmartCameraDPKey)DPCode success:(ThingSuccessID)success failure:(ThingFailureError)failure;

Parameters

Parameter Description
DPCode The DP code.
success The success callback, returning the current value of the specified DP.
failure The failure callback, returning an error message.

Send data to device

API description

Set the value of a DP.

- (void)setValue:(id)value forDPCode:(ThingSmartCameraDPKey)DPCode success:(ThingSuccessID)success failure:(ThingFailureError)failure;

Parameters

Parameter Description
value The value of the DP. A numeric or Boolean value should be wrapped as an NSNumber.
DPCode The DP code.
success The success callback, returning the current value of the specified DP.
failure The failure callback, returning an error message.
  • The smart camera reports the current value to the cloud when the DP status changes, except for certain memory card features. The Smart Camera SDK updates the DP status cache in real time, allowing you to usually retrieve values directly from the cache.
  • When querying a DP asynchronously, the app sends NULL to the device. The device will then typically proactively report the value of the specified DP on receiving NULL. The camera manufacturer implements this logic. If they do not implement it, sending NULL will crash the camera firmware. When using this method, make sure to verify with the manufacturer that they have properly managed the NULL logic for relevant DPs.

Data callback

ThingSmartCameraDPObserver enables you to listen for DP status changes proactively reported by the device. After setting a DP value, the device will proactively report the updated value.

API description

The delegate callback to be invoked when the DP status changes.

- (void)cameraDPDidUpdate:(ThingSmartCameraDPManager *)manager dps:(NSDictionary *)dpsData;

Parameters

Parameter Description
manager The ThingSmartCameraDPManager object that triggers the callback.
dpsData The ID and current value of the changed DP, formatted as { dpName: value }.

Example

Objective-C:

- (void)viewDidLoad {
    [super viewDidLoad];
    self.dpManager = [[ThingSmartCameraDPManager alloc] initWithDeviceId:self.devId];
    [self.dpManager addObserver:self];
    if ([self.dpManager isSupportDPCode:@"basic_osd"]){
      self.osdSwitch = [[self.dpManager valueForDPCode:@"basic_osd"] boolValue];
    }
}

- (void)openOSD {
    if ([self.dpManager isSupportDPCode:@"basic_osd"]) {
        __weak typeof(self) weakSelf = self;
        [self.dpManager setValue:@(YES) forDPCode:@"basic_osd" success:^(id result) {
            weakSelf.osdSwitch = [result boolValue];
        } failure:^(NSError *error) {
            // A network error.
        }];
    }
}

#pragma mark - ThingSmartCameraDPObserver
- (void)cameraDPDidUpdate:(ThingSmartCameraDPManager *)manager dps:(NSDictionary *)dpsData {
    // The updated DPs contain the DP that enables or disables time watermarks.
    if ([dpsData objectForKey:ThingSmartCameraBasicOSDDPName]) {
        self.osdSwitch = [[dpsData objectForKey:ThingSmartCameraBasicOSDDPName] boolValue];
    }
}

Swift:

override func viewDidLoad() {
    super.viewDidLoad()
    self.dpManager = ThingSmartCameraDPManager(deviceId: self.devId)
    self.dpManager.addObserver(self)
    if self.dpManager.isSupportDPCode("basic_osd") {
        self.osdSwitch = self.dpManager.value(forDPCode: "basic_osd") as! Bool
    }
}

func openOSD() {
    // Determine if the device supports this feature.
    guard self.dpManager.isSupportDPCode("basic_osd") else {
        return
    }
    self.dpManager.setValue(true, forDPCode: "basic_osd", success: { result in
        self.osdSwitch = result as! Bool
    }) { _ in
        // Network error
    }
}

func cameraDPDidUpdate(_ manager: ThingSmartCameraDPManager!, dps dpsData: [AnyHashable : Any]!) {
    // The updated DPs contain the DP that enables or disables time watermarks.
    if let osdValue = dpsData[ThingSmartCameraDPKey.basicOSDDPName] {
        self.osdSwitch = osdValue as! Bool
    }
}

Standard DPs

You can review the standard DPs for smart cameras on the Tuya Developer Platform.
See Standard DPs for details.