Thing 情報の操作

Kii Cloud SDK は、Thing に関する属性を管理するための既定フィールドを定義しています。開発者は、これらに加えてカスタムフィールドを追加できます(これらフィールドの概要については Thing 管理 をご覧ください)。

既定フィールドの操作

Kii Cloud SDK は、以下の情報を管理するための既定フィールドを定義しています(設定可能なフィールドについて詳しくは、Thing の登録 をご覧ください)。

  • Thing のベンダーが割り当てた ID
  • パスワード
  • タイプ
  • ベンダー名
  • ファームウェアバージョン
  • プロダクト名
  • ロット番号
  • 予約済みの文字列フィールド
  • 予約済みの数字フィールド
  • オンラインステータス(読み取り専用)
  • オンラインステータスの最終更新日時(読み取り専用)

これら既定フィールド(Thing ベンダーが割り当てた ID およびパスワードを除く)の操作は KiiThing が提供するメソッドで行います。

既定フィールドの設定・変更

既定フィールドを設定・変更する例を以下に挙げます。なお、1 回設定した既定フィールドは削除できません。

Android

  • try {
      // Instantiate a thing by vendor thing ID.
      KiiThing thing = KiiThing.loadWithVendorThingID("rBnvSPOXBDF9r29GJeGS");
    
      // Set predefined fields.
      thing.setThingType("GPS");
      thing.setVendor("Kii Corporation");
      thing.setFirmwareVersion("1.01");
      thing.setProductName("Kii Cloud");
      thing.setLot("KII20140711-AB-002134D");
      thing.setStringField1("S001");
      thing.setStringField2("S002");
      thing.setStringField3("S003");
      thing.setStringField4("S004");
      thing.setStringField5("S005");
      thing.setNumberField1(101L);
      thing.setNumberField2(102L);
      thing.setNumberField3(103L);
      thing.setNumberField4(104L);
      thing.setNumberField5(105L);
    
      // Update the thing.
      thing.update();
    } catch (AppException e) {
      // Handle the error.
    } catch (IOException e) {
      // Handle the error.
    }
  • // Instantiate a thing by vendor thing ID.
    KiiThing.loadWithVendorThingID("rBnvSPOXBDF9r29GJeGS", new KiiCallback<KiiThing>() {
      @Override
      public void onComplete(KiiThing result, Exception e) {
        if (e != null) {
          // Handle the error.
          return;
        }
    
        // Set predefined fields.
        result.setThingType("GPS");
        result.setVendor("Kii Corporation");
        result.setFirmwareVersion("1.01");
        result.setProductName("Kii Cloud");
        result.setLot("KII20140711-AB-002134D");
        result.setStringField1("S001");
        result.setStringField2("S002");
        result.setStringField3("S003");
        result.setStringField4("S004");
        result.setStringField5("S005");
        result.setNumberField1(101L);
        result.setNumberField2(102L);
        result.setNumberField3(103L);
        result.setNumberField4(104L);
        result.setNumberField5(105L);
    
        // Update the thing.
        result.update(new KiiCallback<KiiThing>() {
          @Override
          public void onComplete(KiiThing target, Exception e) {
            if (e != null) {
              // Handle the error.
              return;
            }
          }
        });
      }
    });

iOS

Swift 3:

  • let thing : KiiThing
    
    do{
      // Instantiate a thing by vendor thing ID.
      thing = try KiiThing.loadSynchronous(withVendorThingID: "rBnvSPOXBDF9r29GJeGS")
    }catch(let error as NSError){
      // Handle the error.
      return
    }
    
    // Set predefined fields.
    thing.thingType = "GPS"
    thing.vendor = "Kii Corporation"
    thing.firmwareVersion = "1.01"
    thing.productName = "Kii Cloud"
    thing.lot = "KII20140711-AB-002134D"
    thing.stringField1 = "S001"
    thing.stringField2 = "S002"
    thing.stringField3 = "S003"
    thing.stringField4 = "S004"
    thing.stringField5 = "S005"
    thing.numberField1 = 101
    thing.numberField2 = 102
    thing.numberField3 = 103
    thing.numberField4 = 104
    thing.numberField5 = 105
    
    do{
      // Update the thing.
      try thing.updateSynchronous()
    } catch let error as NSError {
      // Handle the error.
      return
    }
  • // Instantiate a thing by vendor thing ID.
    KiiThing.load(withVendorThingID: "rBnvSPOXBDF9r29GJeGS") { (thing , error) -> Void in
      if error != nil {
        // Handle the error.
        return
      }
    
      // Set predefined fields.
      thing!.thingType = "GPS"
      thing!.vendor = "Kii Corporation"
      thing!.firmwareVersion = "1.01"
      thing!.productName = "Kii Cloud"
      thing!.lot = "KII20140711-AB-002134D"
      thing!.stringField1 = "S001"
      thing!.stringField2 = "S002"
      thing!.stringField3 = "S003"
      thing!.stringField4 = "S004"
      thing!.stringField5 = "S005"
      thing!.numberField1 = 101
      thing!.numberField2 = 102
      thing!.numberField3 = 103
      thing!.numberField4 = 104
      thing!.numberField5 = 105
    
      // Update the thing.
      thing!.update({ (thing , error) -> Void in
        if error != nil {
          // Handle the error.
          return
        }
      })
    
    }

Objective-C:

  • NSError* error = nil;
    
    // Instantiate a thing by vendor thing ID.
    KiiThing* thing = [KiiThing loadSynchronousWithVendorThingID:@"rBnvSPOXBDF9r29GJeGS"
                                                           error:&error];
    if (error != nil) {
      // Handle the error.
      return;
    }
    
    // Set predefined fields.
    thing.thingType = @"GPS";
    thing.vendor = @"Kii Corporation";
    thing.firmwareVersion = @"1.01";
    thing.productName = @"Kii Cloud";
    thing.lot = @"KII20140711-AB-002134D";
    thing.stringField1= @"S001";
    thing.stringField2= @"S002";
    thing.stringField3= @"S003";
    thing.stringField4= @"S004";
    thing.stringField5= @"S005";
    thing.numberField1= @101;
    thing.numberField2= @102;
    thing.numberField3= @103;
    thing.numberField4= @104;
    thing.numberField5= @105;
    
    // Update the thing.
    [thing updateSynchronous:&error];
    if (error != nil) {
      // Handle the error.
      return;
    }
  • // Instantiate a thing by vendor thing ID.
    [KiiThing loadWithVendorThingID:@"rBnvSPOXBDF9r29GJeGS"
                              block:^(KiiThing *thing, NSError *error) {
      if (error != nil) {
        // Handle the error.
        return;
      }
    
      // Set predefined fields.
      thing.thingType = @"GPS";
      thing.vendor = @"Kii Corporation";
      thing.firmwareVersion = @"1.01";
      thing.productName = @"Kii Cloud";
      thing.lot = @"KII20140711-AB-002134D";
      thing.stringField1= @"S001";
      thing.stringField2= @"S002";
      thing.stringField3= @"S003";
      thing.stringField4= @"S004";
      thing.stringField5= @"S005";
      thing.numberField1= @101;
      thing.numberField2= @102;
      thing.numberField3= @103;
      thing.numberField4= @104;
      thing.numberField5= @105;
    
      // Update the thing.
      [thing update:^(KiiThing *thing, NSError *error) {
        if (error != nil) {
          // Handle the error.
          return;
        }
      }];
    }];

JavaScript

// Instantiate a thing by vendor thing ID.
KiiThing.loadWithVendorThingID("rBnvSPOXBDF9r29GJeGS", {
  success: function(thing) {
    // Prepare predefined fields.
    var thingFields = {
      _thingType: "GPS",
      _vendor: "Kii Corporation",
      _firmwareVersion: "1.01",
      _productName: "Kii Cloud",
      _lot: "KII20140711-AB-002134D",
      _stringField1: "S001",
      _stringField2: "S002",
      _stringField3: "S003",
      _stringField4: "S004",
      _stringField5: "S005",
      _numberField1: 101,
      _numberField2: 102,
      _numberField3: 103,
      _numberField4: 104,
      _numberField5: 105
    };

    // Set the predefined fields.
    thing.fields = thingFields;

    // Update the thing.
    thing.update({
      success: function(theThing) {
        // Do something.
      },
      failure: function(theThing, error) {
        // Handle the error.
      }
    });
  },
  failure: function(error) {
    // Handle the error.
  }
});

既定フィールドは、指定したフィールドのみが更新されます。リクエスト内で指定しなかったフィールドの値はそのままキープされます。なお、すでに存在するフィールドは削除できません。

既定フィールドの参照

既定フィールドを参照する例を以下に挙げます。

Android

  • try {
      // Instantiate a thing by vendor thing ID.
      KiiThing things = KiiThing.loadWithVendorThingID("rBnvSPOXBDF9r29GJeGS");
    
      // Get predefined fields.
      String thingType = things.getThingType();
      String vendor = things.getVendor();
      String firmwareVersion = things.getFirmwareVersion();
      String productName = things.getProductName();
      String lot = things.getLot();
      String stringField1 = things.getStringField1();
      String stringField2 = things.getStringField2();
      String stringField3 = things.getStringField3();
      String stringField4 = things.getStringField4();
      String stringField5 = things.getStringField5();
      Long numberField1 = things.getNumberField1();
      Long numberField2 = things.getNumberField2();
      Long numberField3 = things.getNumberField3();
      Long numberField4 = things.getNumberField4();
      Long numberField5 = things.getNumberField5();
      Boolean online = things.isOnline();
      long statusModifiedAt = things.getOnlineStatusModifiedAt();
    } catch (AppException e) {
      // Handle the error.
    } catch (IOException e) {
      // Handle the error.
    }
  • // Instantiate a thing by vendor thing ID.
    KiiThing.loadWithVendorThingID("rBnvSPOXBDF9r29GJeGS", new KiiCallback<KiiThing>() {
      @Override
      public void onComplete(KiiThing result, Exception e) {
        if (e != null) {
          // Handle the error.
          return;
        }
    
        // Get predefined fields.
        String thingType = result.getThingType();
        String vendor = result.getVendor();
        String firmwareVersion = result.getFirmwareVersion();
        String productName = result.getProductName();
        String lot = result.getLot();
        String stringField1 = result.getStringField1();
        String stringField2 = result.getStringField2();
        String stringField3 = result.getStringField3();
        String stringField4 = result.getStringField4();
        String stringField5 = result.getStringField5();
        Long numberField1 = result.getNumberField1();
        Long numberField2 = result.getNumberField2();
        Long numberField3 = result.getNumberField3();
        Long numberField4 = result.getNumberField4();
        Long numberField5 = result.getNumberField5();
        Boolean online = result.isOnline();
        long statusModifiedAt = result.getOnlineStatusModifiedAt();
      }
    });

iOS

Swift 3:

  • let thing : KiiThing
    
    do{
      // Instantiate a thing by vendor thing ID.
      thing = try KiiThing.loadSynchronous(withVendorThingID: "rBnvSPOXBDF9r29GJeGS")
    }catch(let error as NSError){
      // Handle the error.
      return
    }
    
    // Get predefined fields.
    let thingType = thing.thingType
    let vendor = thing.vendor
    let firmwareVersion = thing.firmwareVersion
    let productName = thing.productName
    let lot = thing.lot
    let stringField1 = thing.stringField1
    let stringField2 = thing.stringField2
    let stringField3 = thing.stringField3
    let stringField4 = thing.stringField4
    let stringField5 = thing.stringField5
    let numberField1 = thing.numberField1
    let numberField2 = thing.numberField2
    let numberField3 = thing.numberField3
    let numberField4 = thing.numberField4
    let numberField5 = thing.numberField5
  • // Instantiate a thing by vendor thing ID.
    KiiThing.load(withVendorThingID: "rBnvSPOXBDF9r29GJeGS") { (thing , error) -> Void in
      if error != nil {
        // Handle the error.
        return
      }
    
      // Get predefined fields.
      let thingType = thing!.thingType
      let vendor = thing!.vendor
      let firmwareVersion = thing!.firmwareVersion
      let productName = thing!.productName
      let lot = thing!.lot
      let stringField1 = thing!.stringField1
      let stringField2 = thing!.stringField2
      let stringField3 = thing!.stringField3
      let stringField4 = thing!.stringField4
      let stringField5 = thing!.stringField5
      let numberField1 = thing!.numberField1
      let numberField2 = thing!.numberField2
      let numberField3 = thing!.numberField3
      let numberField4 = thing!.numberField4
      let numberField5 = thing!.numberField5
    }

Objective-C:

  • NSError* error = nil;
    
    // Instantiate a thing by vendor thing ID.
    KiiThing* thing = [KiiThing loadSynchronousWithVendorThingID:@"rBnvSPOXBDF9r29GJeGS"
                                                           error:&error];
    if (error != nil) {
      // Handle the error.
      return;
    }
    
    // Get predefined fields.
    NSString* thingType = thing.thingType;
    NSString* vendor = thing.vendor;
    NSString* firmwareVersion = thing.firmwareVersion;
    NSString* productName = thing.productName;
    NSString* lot = thing.lot;
    NSString* stringField1 = thing.stringField1;
    NSString* stringField2 = thing.stringField2;
    NSString* stringField3 = thing.stringField3;
    NSString* stringField4 = thing.stringField4;
    NSString* stringField5 = thing.stringField5;
    NSNumber* numberField1 = thing.numberField1;
    NSNumber* numberField2 = thing.numberField2;
    NSNumber* numberField3 = thing.numberField3;
    NSNumber* numberField4 = thing.numberField4;
    NSNumber* numberField5 = thing.numberField5;
    NSNUmber* online = thing.online;
    NSDate* onlineStatusModifiedAt = thing.onlineStatusModifiedAt;
  • // Instantiate a thing by vendor thing ID.
    [KiiThing loadWithVendorThingID:@"rBnvSPOXBDF9r29GJeGS"
                              block:^(KiiThing *thing, NSError *error) {
      if (error != nil) {
        // Handle the error.
        return;
      }
    
      // Get predefined fields.
      NSString* thingType = thing.thingType;
      NSString* vendor = thing.vendor;
      NSString* firmwareVersion = thing.firmwareVersion;
      NSString* productName = thing.productName;
      NSString* lot = thing.lot;
      NSString* stringField1 = thing.stringField1;
      NSString* stringField2 = thing.stringField2;
      NSString* stringField3 = thing.stringField3;
      NSString* stringField4 = thing.stringField4;
      NSString* stringField5 = thing.stringField5;
      NSNumber* numberField1 = thing.numberField1;
      NSNumber* numberField2 = thing.numberField2;
      NSNumber* numberField3 = thing.numberField3;
      NSNumber* numberField4 = thing.numberField4;
      NSNumber* numberField5 = thing.numberField5;
      NSNUmber* online = thing.online;
      NSDate* onlineStatusModifiedAt = thing.onlineStatusModifiedAt;
    }];

JavaScript

// Instantiate a thing by vendor thing ID.
KiiThing.loadWithVendorThingID("rBnvSPOXBDF9r29GJeGS", {
  success: function(thing) {
    // Get predefined fields.
    var thingType = thing.fields._thingType;
    var vendor = thing.fields._vendor;
    var firmwareVersion = thing.fields._firmwareVersion;
    var productName = thing.fields._productName;
    var lot = thing.fields._lot;
    var stringField1 = thing.fields._stringField1;
    var stringField2 = thing.fields._stringField2;
    var stringField3 = thing.fields._stringField3;
    var stringField4 = thing.fields._stringField4;
    var stringField5 = thing.fields._stringField5;
    var numberField1 = thing.fields._numberField1;
    var numberField2 = thing.fields._numberField2;
    var numberField3 = thing.fields._numberField3;
    var numberField4 = thing.fields._numberField4;
    var numberField5 = thing.fields._numberField5;
    var online = thing.isOnline();
    var statusModifiedAt = thing.getOnlineStatusModifiedAt();
  },
  failure: function(error) {
    // Handle the error.
  }
});

カスタムフィールドの操作

開発者は、上記の既定フィールドに加え、任意のフィールド(シリアルナンバー、国コード等)を追加できます。ただし、SDK により予約されているフィールド名は使用できません。

カスタムフィールドの設定・変更

カスタムフィールドを設定・変更する例を以下に挙げます。なお、1 回設定したカスタムフィールドは削除できません。

Android

  • try {
      // Instantiate a thing by vendor thing ID.
      KiiThing thing = KiiThing.loadWithVendorThingID("rBnvSPOXBDF9r29GJeGS");
    
      // Set a custom field.
      thing.set("serial_number", "K00001233214001");
    
      // Update the thing.
      thing.update();
    } catch (AppException e) {
      // Handle the error.
    } catch (IOException e) {
      // Handle the error.
    }
  • // Instantiate a thing by vendor thing ID.
    KiiThing.loadWithVendorThingID("rBnvSPOXBDF9r29GJeGS", new KiiCallback<KiiThing>() {
      @Override
      public void onComplete(KiiThing result, Exception e) {
        if (e != null) {
          // Handle the error.
          return;
        }
    
        // Set a custom field.
        result.set("serial_number", "K00001233214001");
    
        // Update the thing.
        result.update(new KiiCallback<KiiThing>() {
          @Override
          public void onComplete(KiiThing target, Exception e) {
            if (e != null) {
              // Handle the error.
              return;
            }
          }
        });
      }
    });

iOS

Swift 3:

  • let thing : KiiThing
    
    do{
      // Instantiate a thing by vendor thing ID.
      thing = try KiiThing.loadSynchronous(withVendorThingID: "rBnvSPOXBDF9r29GJeGS")
    }catch(let error as NSError){
      // Handle the error.
      return
    }
    
    // Set a custom field.
    thing.setObject("K00001233214001", forKey: "serial_number")
    
    // Update the thing.
    thing.update({ (thing , error) -> Void in
      if error != nil {
        // Handle the error.
        return
      }
    })
  • // Instantiate a thing by vendor thing ID.
    KiiThing.load(withVendorThingID: "rBnvSPOXBDF9r29GJeGS") { (thing , error) -> Void in
      if error != nil {
        // Handle the error.
        return
      }
    
      // Set a custom field.
      thing!.setObject("K00001233214001", forKey: "serial_number")
    
      // Update the thing.
      thing!.update({ (thing , error) -> Void in
        if error != nil {
          // Handle the error.
          return
        }
      })
    }

Objective-C:

  • NSError* error = nil;
    
    // Instantiate a thing by vendor thing ID.
    KiiThing* thing = [KiiThing loadSynchronousWithVendorThingID:@"rBnvSPOXBDF9r29GJeGS"
                                                  error:&error];
    if (error != nil) {
      // Handle the error.
      return;
    }
    
    // Set a custom field.
    [thing setObject:@"K00001233214001" forKey:@"serial_number"];
    
    // Update the thing.
    [thing updateSynchronous:&error];
    if (error != nil) {
      // Handle the error.
      return;
    }
  • // Instantiate a thing by vendor thing ID.
    [KiiThing loadWithVendorThingID:@"rBnvSPOXBDF9r29GJeGS"
                              block:^(KiiThing *thing, NSError *error) {
      if (error != nil) {
        // Handle the error.
        return;
      }
    
      // Set a custom field.
      [thing setObject:@"K00001233214001" forKey:@"serial_number"];
    
      // Update the thing.
      [thing update:^(KiiThing *thing, NSError *error) {
        if (error != nil) {
          // Handle the error.
          return;
        }
      }];
    }];

JavaScript

// Instantiate a thing by vendor thing ID.
KiiThing.loadWithVendorThingID("rBnvSPOXBDF9r29GJeGS", {
  success: function(thing) {
    // Set a custom field.
    thing.fields.serial_number = "K00001233214001";

    // Update the thing.
    thing.update({
      success: function(thing) {
        // Do something.
      },
      failure: function(theThing, error) {
        // Handle the error.
      }
    });
  },
  failure: function(error) {
    // Handle the error.
  }
});

カスタムフィールドの参照

カスタムフィールドを参照する例を以下に挙げます。

Android

  • try {
      // Instantiate a thing by vendor thing ID.
      KiiThing things = KiiThing.loadWithVendorThingID("rBnvSPOXBDF9r29GJeGS");
    
      // Get custom fields.
      int version = things.getInt("version");
      String serialNumber = things.getString("serialNumber");
      double accuracy = things.getDouble("accuracy");
      boolean isInitialized = things.getBoolean("isInitialized");
    } catch (AppException e) {
      // Handle the error.
    } catch (IOException e) {
      // Handle the error.
    }
  • // Instantiate a thing by vendor thing ID.
    KiiThing.loadWithVendorThingID("rBnvSPOXBDF9r29GJeGS", new KiiCallback<KiiThing>() {
      @Override
      public void onComplete(KiiThing result, Exception e) {
        if (e != null) {
          // Handle the error.
          return;
        }
    
        // Get custom fields.
        int version = result.getInt("version");
        String serialNumber = result.getString("serialNumber");
        double accuracy = result.getDouble("accuracy");
        boolean isInitialized = result.getBoolean("isInitialized");
      }
    });

iOS

Swift 3:

  • let thing : KiiThing
    
    do{
      // Instantiate a thing by vendor thing ID.
      thing = try KiiThing.loadSynchronous(withVendorThingID: "rBnvSPOXBDF9r29GJeGS")
    }catch(let error as NSError){
      // Handle the error.
      return
    }
    
    // Get custom fields.
    let serialNumber = thing.getForKey("serialNumber") as! String
    let accuracy = thing.getForKey("accuracy") as! Double
    let version = thing.getForKey("version") as! Int
    let isInitialized = thing.getForKey("isInitialized") as! Bool
  • // Instantiate a thing by vendor thing ID.
    KiiThing.load(withVendorThingID: "rBnvSPOXBDF9r29GJeGS") { (thing , error) -> Void in
      if error != nil {
        // Handle the error.
        return
      }
    
      // Get custom fields.
      let serialNumber = thing!.getForKey("serialNumber") as! String
      let accuracy = thing!.getForKey("accuracy") as! Double
      let version = thing!.getForKey("version") as! Int
      let isInitialized = thing!.getForKey("isInitialized") as! Bool
    }

Objective-C:

  • NSError* error = nil;
    
    // Instantiate a thing by vendor thing ID.
    KiiThing* thing = [KiiThing loadSynchronousWithVendorThingID:@"rBnvSPOXBDF9r29GJeGS"
                                                           error:&error];
    if (error != nil) {
      // Handle the error.
      return;
    }
    
    // Get custom fields.
    NSString* serialNumber = [thing getObjectForKey:@"serialNumber"];
    double accuracy = [(NSNumber*)[thing getObjectForKey:@"accuracy"] doubleValue];
    int version = [(NSNumber*)[thing getObjectForKey:@"version"] intValue];
    BOOL isInitialized = [(NSNumber*)[thing getObjectForKey:@"isInitialized"] boolValue];
  • // Instantiate a thing by vendor thing ID.
    [KiiThing loadWithVendorThingID:@"rBnvSPOXBDF9r29GJeGS"
                              block:^(KiiThing *thing, NSError *error) {
      if (error != nil) {
        // Handle the error.
        return;
      }
    
      // Get custom fields.
      NSString* serialNumber = [thing getObjectForKey:@"serialNumber"];
      double accuracy = [(NSNumber*)[thing getObjectForKey:@"accuracy"] doubleValue];
      int version = [(NSNumber*)[thing getObjectForKey:@"version"] intValue];
      BOOL isInitialized = [(NSNumber*)[thing getObjectForKey:@"isInitialized"] boolValue];
    }];

JavaScript

// Instantiate a thing by vendor thing ID.
KiiThing.loadWithVendorThingID("rBnvSPOXBDF9r29GJeGS", {
  success: function(thing) {
    // Get custom fields.
    var version = thing.fields.version;
    var serialNumber = thing.fields.serialNumber;
    var accuracy = thing.fields.accuracy;
    var isInitialized = thing.fields.isInitialized;
  },
  failure: function(error) {
    // Handle the error.
  }
});