Scene UI BizBundle

Last Updated on : 2023-05-22 06:38:27download

Scene UI BizBundle for iOS provides the service logic and UI to implement adding and editing smart scenes in the smart scene module.

Background information

Smart scenes include the following types: tap-to-run and automation.

  • Tap-to-run: Users can add actions to a smart scene. Later, they can tap the tap-to-run scene to run it when needed.
  • Automation: Users can set specific conditions to run a smart scene. When the conditions are met, the smart scene is automatically triggered.

Tuya supports users’ smart life requirements. For example, set weather conditions or device status as triggers for a specific smart scene. When any triggers occur, one or more linked devices will run predefined tasks.

Integrate with the UI BizBundle

Add the components of the Scene UI BizBundle to the Podfile and run the command pod update.

source 'https://github.com/tuya/tuya-private-specs.git'
source 'https://cdn.cocoapods.org/'
platform :ios, '11.0'

target 'your_target_name' do
	# Adds the Scene UI BizBundle.
	pod 'TuyaSmartSceneBizBundle'
	pod 'TuyaRNApi/Scene'
	pod 'TuyaSmartCameraSettingBizBundle'
end

Service permissions

  • The BizBundle supports the weather conditions feature with the location information. This requires specific declarations of privacy and permissions from Apple. Add the following permission declaration to info.plist of your project:

    NSLocationWhenInUseUsageDescription
    
  • The BizBundle supports the geofencing feature. This requires the Always permission declaration on the location information. Add the following permission declaration to info.plist of your project:

    NSLocationAlwaysUsageDescription
    

Implement service protocols

Provide services

The BizBundle implements TYSmartSceneProtocol, TYSmartSceneBizProtocol, and TYSmartGeofenceServiceProtocol to provide required services. You can check the TYModuleServices component, including the following protocol files:

  • TYSmartSceneProtocol.h:

    @protocol TYSmartSceneProtocol <NSObject>
    /**
    *	Navigates to the page for adding a smart scene, where a tap-to-run scene or automation scene can be added.
    *
    * @param callback The callback that is executed when a smart scene is created.
    */
    - (void)addAutoScene:(void(^)(TuyaSmartSceneModel *secneModel, BOOL addSuccess))callback;
    /**
    *	Navigates to the page for editing a smart scene, where a tap-to-run scene or automation scene can be edited.
    *  Before this method is called, you must call `getSceneListWithHomeId` of `TYSmartSceneBizProtocol` to get a list of scenes for a home. This way, scenes are cached to enable navigation to the edit page in follow-up operations.
    *
    * @param model The `model object for the scene to be edited.
    */
    - (void)editScene:(TuyaSmartSceneModel *)model;
    
    @end
    
  • TYSmartSceneBizProtocol.h:

    /**
    * Returns a list of scenes, including the automation and tap-to-run scenes.
    *
    * @param  The home ID.
    */
    - (void)getSceneListWithHomeId:(long long)homeId withSuccess:(void(^)(NSArray <TuyaSmartSceneModel *> *scenes))success failure:(void(^)(NSError *error))failure;
    
  • TYSmartGeofenceServiceProtocol.h:

    /// Returns a list of existing geofences and registers them to the system services.
    - (void)getAllSmartGeofenceListAndRegisterGeofenceIntoSystem;
    
    /// Removes all geofence services.
    - (void)removeAllMonitors;
    

Depend on services

The BizBundle depends on the method provided by the protocol TYSmartHomeDataProtocol. To call the BizBundle, you must implement the following protocols:

  • Protocol 1 (deprecated): TYSmartHomeDataProtocol

    This provides current home information required by the UI BizBundle.

    We recommend that you implement the method TYFamilyProtocol to provide the current home ID.

    /**
    Returns the current home. If the current user does not have a home, `nil` is returned.
    
    @return TuyaSmartHome
    */
    - (TuyaSmartHome *)getCurrentHome;
    
  • Protocol 2: TYFamilyProtocol

    This provides the current home ID required by the BizBundle.

    /// Returns the current home ID.
    - (long long)currentFamilyId;
    

    You can also integrate both the Home Management UI BizBundle and Scene UI BizBundle. For more information about the Home Management UI BizBundle, see Home Management UI BizBundle.

  • Protocol 3: TYSmartHouseIndexProtocol

    This provides the administrator information required by the BizBundle. If non-administrator users are allowed to edit scenes, YES is returned.

    /**
    * ndicates whether the user is the administrator of the current home.
    *
    * @return `YES` indicates an administrator.
    */
    - (BOOL)homeAdminValidation;
    

API protocols

Things to note

  • Before the call of any API method, make sure that the user has logged in to the app.

  • Before the Scene UI BizBundle is used, you must implement:

    • The method currentFamilyId of TYFamilyProtocol.
    • The method homeAdminValidation of TYSmartHouseIndexProtocol.
  • For a Swift project, you must reference the header files of the BizBundle in the bridging header file.

    #import <TuyaSmartBizCore/TuyaSmartBizCore.h>
    #import <TYModuleServices/TYModuleServices.h>
    #import <TuyaSmartSceneKit/TuyaSmartSceneModel.h>
    

    ObjC:

    #import <TuyaSmartBizCore/TuyaSmartBizCore.h>
    #import <TYModuleServices/TYModuleServices.h>
    #import <TuyaSmartDeviceKit/TuyaSmartDeviceKit.h>
    - (void)registerProtocol {
    	// Registers the protocols to be implemented.
    	[[TuyaSmartBizCore sharedInstance] registerService:@protocol(TYSmartHomeDataProtocol) withInstance:self];
    	[[TuyaSmartBizCore sharedInstance] registerService:@protocol(TYSmartHouseIndexProtocol) withInstance:self];
    	[[TuyaSmartBizCore sharedInstance] registerService:@protocol(TYFamilyProtocol) withInstance:self];
    }
    
    // Implements the protocol method.
    - (TuyaSmartHome *)getCurrentHome {
    	TuyaSmartHome *home = [TuyaSmartHome homeWithHomeId:@"Current home ID"];
    	return home;
    }
    
    - (BOOL)homeAdminValidation {
    	// Returns a specific user information, or returns `YES` to allow all users to edit scenes.
    	return YES;
    }
    
    /// Returns the current home ID.
    - (long long)currentFamilyId {
    	return [TYDemoSmartHomeManager sharedInstance].currentHomeModel.homeId;
    }
    

    Swift:

    class TYSceneTest: NSObject,TYSmartHomeDataProtocol,TYSmartHouseIndexProtocol,TYFamilyProtocol {
    	func test() {
    TuyaSmartBizCore.sharedInstance().registerService(TYSmartHomeDataProtocol.self, withInstance: self)
    TuyaSmartBizCore.sharedInstance().registerService(TYSmartHouseIndexProtocol.self, withInstance: self)
    TuyaSmartBizCore.sharedInstance().registerService(TYFamilyProtocol.self, withInstance: self)
    	}
    
    	func getCurrentHome() -> TuyaSmartHome! {
    		let home = TuyaSmartHome.init(homeId: 111)
    		return home
    	}
    
    	func homeAdminValidation() -> Bool {
    		return true
    	}
    
    	func currentFamilyId() -> Int64 {
    		return 111
    	}
    }
    

Add a scene

ObjC:

#import <TuyaSmartBizCore/TuyaSmartBizCore.h>
#import <TYModuleServices/TYSmartSceneProtocol.h>

- (void)gotoAddScene {
	id<TYSmartSceneProtocol> impl = [[TuyaSmartBizCore sharedInstance] serviceOfProtocol:@protocol(TYSmartSceneProtocol)];
	[impl addAutoScene:^(TuyaSmartSceneModel *secneModel, BOOL addSuccess) {

	}];
}

Swift:

func addScene() {
	let impl = TuyaSmartBizCore.sharedInstance().service(of: TYSmartSceneProtocol.self) as? TYSmartSceneProtocol
	impl?.addAutoScene({ (sceneModel, result) in

	})
}

Edit a scene

ObjC:

#import <TuyaSmartBizCore/TuyaSmartBizCore.h>
#import <TYModuleServices/TYSmartSceneProtocol.h>

- (void)gotoEditScene {
	id<TYSmartSceneProtocol> impl = [[TuyaSmartBizCore sharedInstance] serviceOfProtocol:@protocol(TYSmartSceneProtocol)];
	[impl editScene:(your sceneModel)];
}

Swift:

func editScene() {
	let impl = TuyaSmartBizCore.sharedInstance().service(of: TYSmartSceneProtocol.self) as? TYSmartSceneProtocol
	impl?.editScene(your scenemodel)

	}
}

Query a list of smart scenes

ObjC:

#import <TuyaSmartBizCore/TuyaSmartBizCore.h>
#import <TYModuleServices/TYSmartSceneBizProtocol.h>

- (void)getSceneList {
	id<TYSmartSceneBizProtocol> impl = [[TuyaSmartBizCore sharedInstance] serviceOfProtocol:@protocol(TYSmartSceneBizProtocol)];
	[impl getSceneListWithHomeId:'your homeId' withSuccess:^(NSArray<TuyaSmartSceneModel *> * _Nonnull scenes) {

	} failure:^(NSError * _Nonnull error) {

	}];
}

Swift:

func getSceneList() {
	let impl = TuyaSmartBizCore.sharedInstance().service(of: TYSmartSceneBizProtocol.self) as? TYSmartSceneBizProtocol
	   impl?.getSceneList(withHomeId: 111, withSuccess: { (sceneArr) in

	   }, failure: { (error) in

	  })
}

Query geofencing information

The BizBundle supports the geofencing feature. Before the BizBundle is called, you must get the list of geofences and register them to the system services.

ObjC:

- (void)getGeofenceListAndRegister {
	id<TYSmartGeofenceServiceProtocol> geofenceService = [[TuyaSmartBizCore sharedInstance] serviceOfProtocol:@protocol(TYSmartGeofenceServiceProtocol)];
	[geofenceService getAllSmartGeofenceListAndRegisterGeofenceIntoSystem];
}

Swift:

func getGeofenceListAndRegister() {
   let geofenceImpl = TuyaSmartBizCore.sharedInstance().service(of: TYSmartGeofenceServiceProtocol.self) as! TYSmartGeofenceServiceProtocol
   geofenceImpl.getAllSmartGeofenceListAndRegisterGeofenceIntoSystem()
}

Send event notifications

  • kNotificationSmartSceneListUpdate supports notifications of the following events:

    • A scene is added.
    • A scene is edited.
    • A scene is deleted. In this case, the BizBundle supports the following default scene types: TuyaSmartSceneRecommendTypeNone and TuyaSmartSceneCollectionTypeNone.
  • kNotificationSmartSceneSaved supports notifications of the following events:

    • A scene is added.
    • A scene is edited.
  • kNotificationSmartSceneRecomDeleted supports the notification of the following event: A recommended scene or favorite scene is deleted.