NestSDK 0.1.5

NestSDK 0.1.5

测试已测试
Lang语言 Obj-CObjective C
许可证 MIT
发布最后发布2016年9月

Petro Akzhygitov维护。



 
依赖关系
Firebase= 1.2.3
JSONModel= 1.2.0
SSKeychain= 1.3.1
 

NestSDK 0.1.5

  • 作者:
  • petroakzhygitov

适用于iOS Nest API的SDK[非官方]

这个开源库允许您将Nest API集成到您的iOS应用中。

更多关于Nest API的信息,请访问https://developer.nest.com/documentation/cloud/get-started

示例

要运行示例项目,首先从仓库克隆,然后在NestSDKDemoNestSDKDemoAdvancedNestSDKDemoSwift目录中运行pod install

安装

NestSDK通过CocoaPods提供。要安装它,只需将以下行添加到Podfile中

pod "NestSDK", "0.1.5"

通用Nest产品设置

要与NestSDK一起工作,您需要创建一个Nest产品。如果您已经有一个,您可以跳过此部分,如果没有,请按照以下步骤进行

  1. https://home.nest.com创建您的Nest开发者账户

  2. https://developer.nest.com/products/new创建您的Nest产品

  3. 指出重定向URI。不要让文本字段为空,因为不支持基于PIN的授权。

  4. 使用您的产品产品ID产品密钥重定向URI进行iOS项目设置。

通用iOS项目设置

为了使用NestSDK,任何iOS项目都必须进行适当的设置。按照以下步骤设置您的iOS项目

  1. 将NestSDK添加到您的项目(见安装)

  2. 配置您的项目的.plist

    在Xcode中,右键单击您的.plist文件,然后选择“将源代码打开”。将XML片段复制并粘贴到文件的正文(<dict>...</dict>)中。

      <key>NestProductID</key>
      <string>{your-product-id}</string>
      <key>NestProductSecret</key>
      <string>{your-product-secret}</string>
      <key>NestRedirectURL</key>
      <string>{your-product-redirect-url}</string>
    

    替换

    • {你的产品ID}与你的产品产品ID
    • {your-product-secret},使用您的产品Product Secret
    • {your-product-redirect-url},使用您的产品Redirect URI
  3. 连接应用代理

    将您的AppDelegate连接到NestSDKApplicationDelegate。在您的AppDelegate.m中添加以下内容

    Objective-C

    //  AppDelegate.m
    #import <NestSDK/NestSDK.h>
    
    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
        [[NestSDKApplicationDelegate sharedInstance] application:application didFinishLaunchingWithOptions:launchOptions];
    
        return YES;
    }

    Swift

    //  AppDelegate.swift
    import NestSDK
    
    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject:AnyObject]?) -> Bool {
        NestSDKApplicationDelegate.sharedInstance().application(application, didFinishLaunchingWithOptions: launchOptions)
    
        return true
    }

通用iOS应用程序工作流程

在读取、观察或更新Nest设备或结构中的数据之前,您应实现用户授权。因此,一般应用程序的工作流程应该是以下内容

  1. 使用预定义的Connect with Nest按钮(NestSDKConnectWithNestButton)或通过手动处理授权对话框来使用授权管理类(NestSDKAuthorizationManager)授权用户。

  2. 读取/观察/更新设备、结构或元数据。

连接Nest

iOS Nest SDK允许用户将他们的个人Nest账户连接到您的Nest产品应用。当用户将他们的个人Nest账户连接到您的Nest产品应用时,他们授予您的应用程序权限,以便您代表他们检索和修改信息。

注意:连接Nest仅支持基于Web的授权。不支持基于PIN的授权。

关于Nest授权的更多信息

添加“连接Nest”按钮代码

要将“连接Nest”按钮添加到您的应用,请将以下代码片段添加到视图控制器中

Objective-C

// Add this to the header of your file, e.g. in ViewController.m 
// after #import "ViewController.h"
#import <NestSDK/NestSDK.h>

// Add this to the body
@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    NestSDKConnectWithNestButton *connectWithNestButton = [[NestSDKConnectWithNestButton alloc] init];

    // Optional: Place the button in the center of your view.
    connectWithNestButton.center = self.view.center;

    [self.view addSubview:connectWithNestButton];
}

@end

Swift

// Add this to the top of your file, e.g. in ViewController.swift 
// after import UIKit
import NestSDK

// Add this to the class body
override func viewDidLoad() {
    super.viewDidLoad()

    let connectWithNestButton = NestSDKConnectWithNestButton(frame: CGRectMake(0, 0, 200, 44))

    // Optional: Place the button in the center of your view.
    connectWithNestButton.center = self.view.center

    view.addSubview(connectWithNestButton)
}

在开始授权过程之前,检查[NestSDKAccessToken currentToken]的结果以确定用户是否已经授权

Objective-C

if ([NestSDKAccessToken currentToken]) {
    NSLog(@"Authorized!");

} else {
    NSLog(@"Not authorized!);
}

Swift

if (NestSDKAccessToken.currentAccessToken() != nil) {
    print("Authorized!")

} else {
    print("Not authorized!")
}

自定义“连接Nest”按钮

您可以选择不使用预定义的“连接Nest”按钮(在添加“连接Nest”按钮的代码中解释了),而是设计自定义布局和行为。在以下代码示例中,我们使用授权管理类(NestSDKAuthorizationManager)和自定义按钮(UIButton)调用授权对话框。您可以使用任何其他自定义用户界面或事件处理来调用授权对话框。

Objective-C

// Add this to the header of your file
#import "ViewController.h"
#import <NestSDK/NestSDK.h>

// Add this to the body
@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    // Add a custom connect with button to your app
    UIButton *customConnectWithNestButton = [UIButton buttonWithType:UIButtonTypeCustom];
    customConnectWithNestButton.backgroundColor = [UIColor darkGrayColor];
    customConnectWithNestButton.frame = CGRectMake(0, 0, 240, 40);

    // Optional: Place the button in the center of your view.
    customConnectWithNestButton.center = self.view.center;
    [customConnectWithNestButton setTitle:@"Custom Connect Button" forState:UIControlStateNormal];

    // Handle clicks on the button
    [customConnectWithNestButton addTarget:self action:@selector(customConnectWithNestButtonClicked)
                          forControlEvents:UIControlEventTouchUpInside];

    // Add the button to the view
    [self.view addSubview:customConnectWithNestButton];
}

// Once the button is clicked, show the auth dialog
- (void)customConnectWithNestButtonClicked {
    NestSDKAuthorizationManager *authorizationManager = [[NestSDKAuthorizationManager alloc] init];
    [authorizationManager authorizeWithNestAccountFromViewController:self
                                                                 handler:^(NestSDKAuthorizationManagerAuthorizationResult *result, NSError *error) {
                                                                     if (error) {
                                                                         NSLog(@"Process error: %@", error);

                                                                     } else if (result.isCancelled) {
                                                                         NSLog(@"Cancelled");

                                                                     } else {
                                                                         NSLog(@"Authorized!");
                                                                     }
                                                                 }];
}

@end

Swift

// Add this to the top of your file, e.g. in ViewController.swift 
import UIKit
import NestSDK

// Add this to the class body
override func viewDidLoad() {
    super.viewDidLoad()

    // Add a custom connect with button to your app
    let customConnectWithNestButton = UIButton(type: .Custom)
    customConnectWithNestButton.backgroundColor = UIColor.darkGrayColor()
    customConnectWithNestButton.frame = CGRectMake(0, 0, 240, 40)

    // Optional: Place the button in the center of your view.
    customConnectWithNestButton.center = self.view.center
    customConnectWithNestButton.setTitle("Custom Connect Button", forState:.Normal)

    // Handle clicks on the button
    customConnectWithNestButton.addTarget(self, action: "customConnectWithNestButtonClicked:", forControlEvents: .TouchUpInside)

    // Add the button to the view
    self.view.addSubview(customConnectWithNestButton)
}

// Once the button is clicked, show the auth dialog
func customConnectWithNestButtonClicked(sender:UIButton!) {
    let authorizationManager = NestSDKAuthorizationManager()
    authorizationManager.authorizeWithNestAccountFromViewController(self, handler:{
        result, error in

        if (error == nil) {
            print("Process error: \(error)")

        } else if (result.isCancelled) {
            print("Cancelled")

        } else {
            print("Authorized!")
        }
    })
}

访问结构

所有Nest设备都属于一个结构。结构可以包含多个设备。

用户可能将多个结构附加到其Nest账户上,因此您的 产品应提供用户从可用结构中选择(结构选择器)的手段。

有关Nest结构的更多信息

观察结构

要观察结构,请使用NestSDKDataManager

Objective-C

NestSDKDataManager *dataManager = [[NestSDKDataManager alloc] init];
[dataManager observeStructuresWithBlock:^(NSArray <NestSDKStructure> *structuresArray, NSError *error) {
    if (error) {
        NSLog(@"Error occurred while observing structures: %@", error);
        return;
    }

    for (NestSDKStructure *structure in structuresArray) {
        NSLog(@"Updated structure with name: %@", structure.name);
    }
}];

Swift

let dataManager = NestSDKDataManager()
dataManager.observeStructuresWithBlock({
    structuresArray, error in

    if (error == nil) {
        print("Error occurred while observing structures: \(error)")
        return
    }

    for structure in structuresArray as! [NestSDKStructure] {
        print("Updated structure with name: \(structure.name)")
    }
})

读取结构

通常,观察结构比读取结构更好,因为您将收到任何发生的更改的更新。

如果只想简单地读取结构,则请使用NestSDKDataManager

Objective-C

NestSDKDataManager *dataManager = [[NestSDKDataManager alloc] init];
[dataManager structuresWithBlock:^(NSArray <NestSDKStructure> *structuresArray, NSError *error) {
    if (error) {
        NSLog(@"Error occurred while reading structures: %@", error);
        return;
    }

    for (NestSDKStructure *structure in structuresArray) {
        NSLog(@"Read structure with name: %@", structure.name);
    }
}];

Swift

let dataManager = NestSDKDataManager()
dataManager.structuresWithBlock({
    structuresArray, error in

    if (error == nil) {
        print("Error occurred while reading structures: \(error)")
        return
    }

    for structure in structuresArray as! [NestSDKStructure] {
        print("Read structure with name: \(structure.name)")
    }
})

更新结构

可以更新结构的away状态并设置eta。使用NestSDKDataManager来更新结构。

Objective-C

NestSDKDataManager *dataManager = [[NestSDKDataManager alloc] init];
[dataManager setStructure:structure block:^(id <NestSDKStructure> structure, NSError *) {
    if (error) {
        NSLog(@"Error occurred while updating structure: %@", error);
        return;
    }

    NSLog(@"Updated structure with name: %@", structure.name);
}];

Swift

let dataManager = NestSDKDataManager()
dataManager.setStructure(structure, block:{
    structure, error in

    if (error == nil) {
        print("Error occurred while updating structures: \(error)")
        return
    }

    print("Updated structure with name: \(structure.name)")
})

访问设备

有三种类型的Nest设备可供读取/监视/更新

注意:请确保您已为您的Nest产品设置适当的权限以读取特定设备的数据。

监视设备

要监视设备,请使用NestSDKDataManager

Objective-C

NestSDKDataManager *dataManager = [[NestSDKDataManager alloc] init];

NSString *thermostatId = structure.thermostats[someIndex];
[self.dataManager observeThermostatWithId:thermostatId block:^(id <NestSDKThermostat> thermostat, NSError *error) {
    if (error) {
        NSLog(@"Error occurred while observing thermostat: %@", error);
        return;
    }

    NSLog(@"Updated thermostat with name: %@", thermostat.name);
}];

NSString *smokeCOAlarmId = structure.smokeCoAlarms[someIndex];
[self.dataManager observeSmokeCOAlarmWithId:smokeCOAlarmId block:^(id <NestSDKSmokeCOAlarm> smokeCOAlarm, NSError *error) {
        if (error) {
        NSLog(@"Error occurred while observing smoke CO alarm: %@", error);
        return;
    }

    NSLog(@"Updated smoke+CO Alarm with name: %@", smokeCOAlarm.name);
}];

NSString *cameraId = structure.cameras[someIndex];
[self.dataManager observeCameraWithId:cameraId block:^(id <NestSDKCamera> camera, NSError *error) {
    if (error) {
        NSLog(@"Error occurred while observing camera: %@", error);
        return;
    }

    NSLog(@"Updated camera with name: %@", camera.name);
}];

Swift

let dataManager = NestSDKDataManager()

let thermostatId = structure.thermostats[someIndex] as! String
dataManager.observeThermostatWithId(thermostatId, block:{
    thermostat, error in

    if (error == nil) {
        print("Error occurred while observing thermostat \(error)")
        return
    }

    print("Updated thermostat with name \(thermostat.name)")
})

let smokeCOAlarmId = structure.smokeCoAlarms[someIndex] as! String
dataManager.observeSmokeCOAlarmWithId(smokeCOAlarmId, block:{
        smokeCOAlarm, error in

        if (error == nil) {
        print("Error occurred while observing smoke CO alarm \(error)")
        return
    }

    print("Updated smoke+CO Alarm with name \(smokeCOAlarm.name)")
})

let cameraId = structure.cameras[someIndex] as! String
dataManager.observeCameraWithId(cameraId, block:{
    camera, error in

    if (error == nil) {
        print("Error occurred while observing camera \(error)")
        return
    }

    print("Updated camera with name \(camera.name)")
})

读取设备

一般来说,监视设备比读取设备更好,因为任何更改都会更新您。

如果您只想简单地读取设备,请使用NestSDKDataManager

Objective-C

NestSDKDataManager *dataManager = [[NestSDKDataManager alloc] init];

NSString *thermostatId = structure.thermostats[someIndex];
[self.dataManager thermostatWithId:thermostatId block:^(id <NestSDKThermostat> thermostat, NSError *error) {
    if (error) {
        NSLog(@"Error occurred while reading thermostat: %@", error);
        return;
    }

    NSLog(@"Read thermostat with name: %@", thermostat.name);
}];

NSString *smokeCOAlarmId = structure.smokeCoAlarms[someIndex];
[self.dataManager smokeCOAlarmWithId:smokeCOAlarmId block:^(id <NestSDKSmokeCOAlarm> smokeCOAlarm, NSError *error) {
        if (error) {
        NSLog(@"Error occurred while reading smoke CO alarm: %@", error);
        return;
    }

    NSLog(@"Read smoke+CO Alarm with name: %@", smokeCOAlarm.name);
}];

NSString *cameraId = structure.cameras[someIndex];
[self.dataManager cameraWithId:cameraId block:^(id <NestSDKCamera> camera, NSError *error) {
    if (error) {
        NSLog(@"Error occurred while reading camera: %@", error);
        return;
    }

    NSLog(@"Read camera with name: %@", camera.name);
}];

Swift

let dataManager = NestSDKDataManager()

let thermostatId = structure.thermostats[someIndex] as! String
dataManager.thermostatWithId(thermostatId, block:{
    thermostat, error in

    if (error == nil) {
        print("Error occurred while reading thermostat \(error)")
        return
    }

    print("Read thermostat with name \(thermostat.name)")
})

let smokeCOAlarmId = structure.smokeCoAlarms[someIndex] as! String
dataManager.smokeCOAlarmWithId(smokeCOAlarmId, block:{
    smokeCOAlarm, error in

    if (error == nil) {
        print("Error occurred while reading smoke CO alarm \(error)")
        return
    }

    print("Read smoke+CO Alarm with name \(smokeCOAlarm.name)")
})

let cameraId = structure.cameras[someIndex] as! String
dataManager.cameraWithId(cameraId, block:{
    camera, error in

    if (error == nil) {
        print("Error occurred while reading camera \(error)")
        return
    }

    print("Read camera with name \(camera.name)")
})

更新设备

  • 您可以将恒温器的以下值更新为fanTimerActivefanTimerTimeouttargetTemperatureFtargetTemperatureCtargetTemperatureHighFtargetTemperatureHighCtargetTemperatureLowFtargetTemperatureLowChvacMode
  • 您可以将摄像机的isStreaming状态更新。
  • 无法更新烟雾+一氧化碳报警器的状态/值。

要更新设备,请使用NestSDKDataManager

Objective-C

NestSDKDataManager *dataManager = [[NestSDKDataManager alloc] init];

[self.dataManager setThermostat:thermostat block:^(id <NestSDKThermostat> thermostat, NSError *error) {
    if (error) {
        NSLog(@"Error occurred while updating thermostat: %@", error);
        return;
    }

    NSLog(@"Updated thermostat with name: %@", thermostat.name);
}];

[self.dataManager setCamera:camera block:^(id <NestSDKCamera> camera, NSError *error) {
    if (error) {
        NSLog(@"Error occurred while updating camera: %@", error);
        return;
    }

    NSLog(@"Updated camera with name: %@", camera.name);
}];

Swift

let dataManager = NestSDKDataManager()

dataManager.setThermostat(thermostat, block:{
    thermostat, error in

    if (error == nil) {
        print("Error occurred while updating thermostat \(error)")
        return
    }

    print("Updated thermostat with name \(thermostat.name)")
})

dataManager.setCamera(camera, block:{
    camera, error in

    if (error == nil) {
        print("Error occurred while updating camera \(error)")
        return
    }

    print("Updated camera with name \(camera.name)")
})

访问元数据

数据的关于数据的数据。元数据值不能直接访问且没有相关权限。

读取元数据

要读取元数据,请使用NestSDKDataManager

Objective-C

NestSDKDataManager *dataManager = [[NestSDKDataManager alloc] init];

[self.dataManager metadataWithBlock:^(id <NestSDKMetadata> metadata, NSError *error) {
    if (error) {
        NSLog(@"Error occurred while reading metadata: %@", error);
        return;
    }

    NSLog(@"Read metadata: %@", metadata);
}];

Swift

let dataManager = NestSDKDataManager()

dataManager.metadataWithBlock({
    metadata, error in

    if (error == nil) {
        print("Error occurred while reading metadata \(error)")
        return
    }

    print("Read metadata \(metadata)")
})

作者

Petro Akzhygitov ([email protected])

许可证

NestSDK可在MIT许可证下使用。有关更多信息,请参阅LICENSE文件。