Scene UI BizBundle

Last Updated on : 2024-07-16 06:28:45download

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-pod-specs.git'
source 'https://cdn.cocoapods.org/'
platform :ios, '11.0'

target 'your_target_name' do
    # Adds the Scene UI BizBundle.
    pod 'ThingSmartSceneBizBundle'
    pod 'ThingSmartSceneExtendBizBundle'
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
    

Service protocols

Provide services

The UI BizBundle implements ThingSmartSceneProtocol, ThingSmartSceneBizProtocol, and ThingSmartGeofenceServiceProtocol to provide required services. You can check the ThingModuleServices component, including the following protocol files:

  • ThingSmartSceneProtocol.h:

    @protocol ThingSmartSceneProtocol <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 to invoke when a smart scene is created.
     */
    - (void)addAutoScene:(void(^)(ThingSmartSceneModel *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 ThingSmartSceneBizProtocol 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:(ThingSmartSceneModel *)model;
    
    @end
    
  • ThingSmartSceneBizProtocol.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 <ThingSmartSceneModel *> *scenes))success failure:(void(^)(NSError *error))failure;
    
  • ThingSmartGeofenceServiceProtocol.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 UI BizBundle depends on the method provided by the protocol ThingSmartHomeDataProtocol. To call the UI BizBundle, you must implement the following protocols:

  • Protocol 1 (deprecated): ThingSmartHomeDataProtocol

    Provides current home information required by the UI BizBundle.

    We recommend that you implement the methods in the ThingFamilyProtocol to provide the current home ID.

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

    1. Provides the current home ID required by the UI 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.

    2. Provides the required permissions to complete scene operations.

      /// Returns the required permissions to complete scene operations.
      - (BOOL)checkAdminAndRightLimit:(BOOL)alert;
      
  • Protocol: ThingSmartHouseIndexProtocol

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

    /**
    * Indicates 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 in ThingFamilyProtocol.
    • The method checkAdminAndRightLimit: or checkAdminAndRightLimit:withHomeId: in ThingFamilyProtocol.
    • The method homeAdminValidation in ThingSmartHouseIndexProtocol.
  • For a Swift project, you must reference the header files of the UI BizBundle in the bridging header file.

    #import <ThingSmartBizCore/ThingSmartBizCore.h>
    #import <ThingModuleServices/ThingModuleServices.h>
    #import <ThingSmartSceneKit/ThingSmartSceneModel.h>
    

    Objective-C:

    #import <ThingSmartBizCore/ThingSmartBizCore.h>
    #import <ThingModuleServices/ThingModuleServices.h>
    #import <ThingSmartDeviceKit/ThingSmartDeviceKit.h>
    
    - (void)registerProtocol {
        // Registers the protocol to be implemented.
        [[ThingSmartBizCore sharedInstance] registerService:@protocol(ThingSmartHomeDataProtocol) withInstance:self];
        [[ThingSmartBizCore sharedInstance] registerService:@protocol(ThingSmartHouseIndexProtocol) withInstance:self];
        [[ThingSmartBizCore sharedInstance] registerService:@protocol(ThingFamilyProtocol) withInstance:self];
    }
    
    // Implements the protocol method.
    - (ThingSmartHome *)getCurrentHome {
        ThingSmartHome *home = [ThingSmartHome homeWithHomeId:@"Current home ID"];
        return home;
    }
    
    // Returns the result based on the user role, or returns YES to allow all users to edit scenes.
    - (BOOL)homeAdminValidation {
        return YES;
    }
    
    // Returns the current home ID.
    - (long long)currentFamilyId {
        return [ThingDemoSmartHomeManager sharedInstance].currentHomeModel.homeId;
    }
    
    - (BOOL)checkAdminAndRightLimit:(BOOL)alert {
       return YES;
    }
    

    Swift:

    class ThingSceneTest: NSObject,ThingSmartHomeDataProtocol,ThingSmartHouseIndexProtocol,ThingFamilyProtocol {
        func test() {
            ThingSmartBizCore.sharedInstance().registerService(ThingSmartHomeDataProtocol.self, withInstance: self)
            ThingSmartBizCore.sharedInstance().registerService(ThingSmartHouseIndexProtocol.self, withInstance: self)
            ThingSmartBizCore.sharedInstance().registerService(ThingFamilyProtocol.self, withInstance: self)
        }
    
        func getCurrentHome() -> ThingSmartHome! {
            let home = ThingSmartHome.init(homeId: 111)
            return home
        }
    
        func homeAdminValidation() -> Bool {
            return true
        }
    
        func checkAdminAndRightLimit(alert: Bool) -> Bool {
            return true
        }
    
        func currentFamilyId() -> Int64 {
            return 111
        }
    }
    

Add a scene

ObjC:

#import <ThingSmartBizCore/ThingSmartBizCore.h>
#import <ThingModuleServices/ThingSmartSceneProtocol.h>

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

    }];
}

Swift:

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

    })
}

Edit a scene

ObjC:

#import <ThingSmartBizCore/ThingSmartBizCore.h>
#import <ThingModuleServices/ThingSmartSceneProtocol.h>

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

Swift:

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

    }
}

Query a list of smart scenes

ObjC:

#import <ThingSmartBizCore/ThingSmartBizCore.h>
#import <ThingModuleServices/ThingSmartSceneBizProtocol.h>

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

    } failure:^(NSError * _Nonnull error) {

    }];
}

Swift:

func getSceneList() {
    let impl = ThingSmartBizCore.sharedInstance().service(of: ThingSmartSceneBizProtocol.self) as? ThingSmartSceneBizProtocol
    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<ThingSmartGeofenceServiceProtocol> geofenceService = [[ThingSmartBizCore sharedInstance] serviceOfProtocol:@protocol(ThingSmartGeofenceServiceProtocol)];
    [geofenceService getAllSmartGeofenceListAndRegisterGeofenceIntoSystem];
}

Swift:

func getGeofenceListAndRegister() {
    let geofenceImpl = ThingSmartBizCore.sharedInstance().service(of: ThingSmartGeofenceServiceProtocol.self) as! ThingSmartGeofenceServiceProtocol
    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: ThingSmartSceneRecommendTypeNone and ThingSmartSceneCollectionTypeNone.
  • 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.