IoT Platform では、デバイスのプロパティに期待される値を指定できます。これにより、期待される値を IoT Platform にキャッシュし、その値を使用してデバイスをリモートで制御できます。本トピックでは、IoT Platform コンソールでデバイスプロパティの期待される値を指定して、電球のステータスを制御する方法について説明します。
背景情報
電球が IoT Platform に接続された後、電球のステータスを制御するには、デバイスがオンライン状態を維持していることを確認する必要があります。IoT Platform コンソールの電球は、オン (1) またはオフ (0) の状態になります。実際のシナリオでは、電球がオンライン状態を維持できない場合があります。
電球の期待される値を指定し、その値を IoT Platform に保存できます。デバイスがオンラインになると、デバイスは IoT Platform に保存されている期待される属性値を読み取り、プロパティ値を更新できます。その後、更新されたプロパティ値は IoT Platform に送信され、IoT Platform コンソールの [実行ステータス] タブに表示されます。
プロダクトの作成とデバイスの追加
- IoT Platform コンソールにログインします。
[概要] ページで、管理するインスタンスを見つけ、インスタンス ID またはインスタンス名をクリックします。
- 左側のナビゲーションウィンドウで、 を選択します。[プロダクト] ページで、[プロダクトの作成] をクリックして電球プロダクトを作成します。

- プロダクトが作成されたら、[TSL モデルの定義] をクリックして、プロダクトに Thing Specification Language (TSL) モデルを追加し、公開します。詳細については、「単一プロダクトの TSL モデルの追加」をご参照ください。
この例では、稼働ステータス (LightStatus) という名前のプロパティを追加します。このプロパティのデータの型は
bool (ブール値)で、0はオフ、1はオンを表します。 - 左側のナビゲーションウィンドウで、 を選択し、[デバイスの追加] をクリックして Lamp デバイスをプロダクトに追加します。
デバイスが作成されたら、ProductKey、DeviceName、DeviceSecret を含むデバイス証明書を取得します。
デバイスリストで
Lampデバイスを見つけ、[表示] をクリックして [デバイス詳細] ページに移動します。[実行ステータス] タブに、デバイスのプロパティ値と期待される属性値が両方とも空であることが表示されます。初期状態では、期待されるプロパティバージョンは 0 です。
期待される属性値の設定とクエリ
API オペレーションを呼び出して、デバイスの最新の期待される属性値を設定およびクエリできます。
このトピックの例では、Java SDK (クラウド側開発用) を使用します。詳細については、「API オペレーション」をご参照ください。
- SetDeviceDesiredProperty オペレーションを呼び出して、期待される属性値を設定します。
DefaultProfile profile = DefaultProfile.getProfile( "<RegionId>", // リージョン ID。 "<accessKey>", // ご利用の Alibaba Cloud アカウントの AccessKey ID。 "<accessSecret>"); // ご利用の Alibaba Cloud アカウントの AccessKey Secret。 IAcsClient client = new DefaultAcsClient(profile); // API リクエストを作成し、パラメーターを設定します。 SetDeviceDesiredPropertyRequest request = new SetDeviceDesiredPropertyRequest(); request.setIotInstanceId("iot-060***"); request.setDeviceName("Lamp"); request.setProductKey("g4r***"); // 設定するプロパティ識別子と期待される値。 request.setItems("{\"LightStatus\": 1}"); request.setVersions("{\"LightStatus\": 0}"); // リクエストを送信し、応答または例外を処理します。 try { SetDeviceDesiredPropertyResponse response = client.getAcsResponse(request); System.out.println(new Gson().toJson(response)); } catch (ServerException e) { e.printStackTrace(); } catch (ClientException e) { System.out.println("ErrCode:" + e.getErrCode()); System.out.println("ErrMsg:" + e.getErrMsg()); System.out.println("RequestId:" + e.getRequestId()); } - QueryDeviceDesiredProperty オペレーションを呼び出して、デバイスの期待される属性値を表示します。
DefaultProfile profile = DefaultProfile.getProfile( "<RegionId>", // リージョン ID。 "<accessKey>", // ご利用の Alibaba Cloud アカウントの AccessKey ID。 "<accessSecret>"); // ご利用の Alibaba Cloud アカウントの AccessKey Secret。 IAcsClient client = new DefaultAcsClient(profile); // API リクエストを作成し、パラメーターを設定します。 QueryDeviceDesiredPropertyRequest request = new QueryDeviceDesiredPropertyRequest(); request.setIotInstanceId("iot-06****"); request.setProductKey("g4r****"); request.setDeviceName("Lamp"); // クエリするプロパティ識別子のリスト。このパラメーターを省略した場合、すべての書き込み可能なプロパティの期待される値がクエリされます。 List<String> identifierList = new ArrayList<String>(); identifierList.add("LightStatus"); request.setIdentifiers(identifierList); // リクエストを送信し、応答または例外を処理します。 try { QueryDeviceDesiredPropertyResponse response = client.getAcsResponse(request); System.out.println(new Gson().toJson(response)); } catch (ServerException e) { e.printStackTrace(); } catch (ClientException e) { System.out.println("ErrCode:" + e.getErrCode()); System.out.println("ErrMsg:" + e.getErrMsg()); System.out.println("RequestId:" + e.getRequestId()); }
コードでのパラメーターの設定方法の詳細については、「Java SDK の使用ガイド」をご参照ください。
IoT Platform で期待される属性値を設定すると、その値がデバイスの実行ステータスに表示されます。[デバイス詳細] ページで、[TSL データ] > [実行ステータス] をクリックします。プロパティの期待される値が [1 (オン)] と表示されていることがわかります。
デバイス側の開発
電球は、次のシナリオで期待される属性値を取得できます:
- 電球がオフラインからオンラインに移行すると、デバイスは IoT Platform にキャッシュされている期待される属性値をリクエストします。
- 電球がオンラインの場合、デバイスは IoT Platform からリアルタイムでプッシュされた期待される属性値を受信します。
デバイス側の開発の詳細については、「デバイス側 SDK を使用したデバイスの接続」をご参照ください。
このトピックでは、完全なデバイス側デモを提供します。完全なコードについては、「付録:デバイス側デモコード」をご参照ください。
- デバイス証明書、リージョン、および MQTT エンドポイントの情報を入力します。
/** * デバイス証明書情報。 */ private static String productKey = "******"; private static String deviceName = "********"; private static String deviceSecret = "**************"; /** * MQTT 接続情報。 */ private static String regionId = "******"; ...... /** * MQTT 初期化パラメーターを設定します。 */ config.channelHost = deviceInfo.productKey + ".iot-as-mqtt." + region + ".aliyuncs.com:1883";説明- デバイス証明書については、本トピックの「プロダクトの作成とデバイスの追加」セクションをご参照ください。
- regionId は、インスタンスがデプロイされているリージョンの ID です。リージョンは、IoT Platform コンソールの左上で確認できます。リージョン ID のリストについては、「サポートされているリージョン」をご参照ください。
- channelHost パラメーターは MQTT エンドポイントを指定します。エンドポイントの取得方法の詳細については、「インスタンスエンドポイントの表示と設定」をご参照ください。
- 次のメソッドを追加して、電球の物理的な状態を変更し、新しい状態をクラウドにレポートします。
/** * このメソッドは、物理デバイスがプロパティの変更を処理する必要があるときに呼び出されます。これには 2 つのシナリオがあります: * シナリオ 1:接続後、デバイスは最新の期待される属性値を能動的にフェッチします (プルモデル)。 * シナリオ 2:オンライン中、デバイスは `property.set` を介してクラウドからプッシュされた期待される属性値を受信します (プッシュモデル)。 * @param identifier プロパティ識別子。 * @param value 期待される属性値。 * @param needReport `property.post` を使用してステータスレポートを送信するかどうかを指定します。 * シナリオ 2 では、ハンドラにすでにレポートが含まれているため、このパラメーターは false に設定されます。 * @return */ private boolean handlePropertySet(String identifier, ValueWrapper value, boolean needReport) { ALog.d(TAG, "Physical device handling property change = [" + identifier + "], value = [" + value + "]"); // 実際のアプリケーションでは、設定操作が成功したかどうかを判断します。ここでは、成功したと仮定します。 boolean success = true; if (needReport) { reportProperty(identifier, value); } return success; } private void reportProperty(String identifier, ValueWrapper value){ if (StringUtils.isEmptyString(identifier) || value == null) { return; } ALog.d(TAG, "Reporting property identity=" + identifier); Map<String, ValueWrapper> reportData = new HashMap<>(); reportData.put(identifier, value); LinkKit.getInstance().getDeviceThing().thingPropertyPost(reportData, new IPublishResourceListener() { public void onSuccess(String s, Object o) { // プロパティは正常にレポートされました。 ALog.d(TAG, "Report successful. onSuccess() called with: s = [" + s + "], o = [" + o + "]"); } public void onError(String s, AError aError) { // プロパティのレポートに失敗しました。 ALog.d(TAG, "Report failed. onError() called with: s = [" + s + "], aError = [" + JSON.toJSONString(aError) + "]"); } }); } - 電球がオンラインのときに IoT Platform で期待される属性値が設定されると、その値はデバイスにプッシュされます。デバイスはメッセージを処理し、その状態を変更します。
次のコードでは、
connectNotifyListenerが呼び出されてメッセージを処理します。Alink プロトコルの詳細については、「デバイスによるプロパティのレポート」をご参照ください。非同期ダウンストリームデータが受信されると、
mCommonHandlerが呼び出され、それがhandlePropertySetを呼び出してデバイスの物理プロパティを更新します。/** * サービスコールとプロパティ設定のための応答関数を登録します。 * クラウドがデバイスサービスを呼び出すとき、デバイスはサービスコールに応答する必要があります。 */ public void connectNotifyListener() { List<Service> serviceList = LinkKit.getInstance().getDeviceThing().getServices(); for (int i = 0; serviceList != null && i < serviceList.size(); i++) { Service service = serviceList.get(i); LinkKit.getInstance().getDeviceThing().setServiceHandler(service.getIdentifier(), mCommonHandler); } } private ITResRequestHandler mCommonHandler = new ITResRequestHandler() { public void onProcess(String serviceIdentifier, Object result, ITResResponseCallback itResResponseCallback) { ALog.d(TAG, "onProcess() called with: s = [" + serviceIdentifier + "]," + " o = [" + result + "], itResResponseCallback = [" + itResResponseCallback + "]"); ALog.d(TAG, "Received asynchronous service call from cloud: " + serviceIdentifier); try { if (SERVICE_SET.equals(serviceIdentifier)) { Map<String, ValueWrapper> data = (Map<String, ValueWrapper>)((InputParams)result).getData(); ALog.d(TAG, "Received asynchronous downstream data: " + data); // 物理デバイスにプロパティを設定し、更新された値をレポートします。 boolean isSetPropertySuccess = handlePropertySet("LightStatus", data.get("LightStatus"), false); if (isSetPropertySuccess) { if (result instanceof InputParams) { // データが正常に受信されたことをクラウドに応答します。 itResResponseCallback.onComplete(serviceIdentifier, null, null); } else { itResResponseCallback.onComplete(serviceIdentifier, null, null); } } else { AError error = new AError(); error.setCode(100); error.setMsg("setPropertyFailed."); itResResponseCallback.onComplete(serviceIdentifier, new ErrorInfo(error), null); } } else if (SERVICE_GET.equals(serviceIdentifier)) { } else { // 特定の要件に基づいて他のサービスを処理します。 ALog.d(TAG, "Return a value based on the actual service. For an example, refer to the 'set' implementation."); OutputParams outputParams = new OutputParams(); // outputParams.put("op", new ValueWrapper.IntValueWrapper(20)); itResResponseCallback.onComplete(serviceIdentifier, null, outputParams); } } catch (Exception e) { e.printStackTrace(); ALog.d(TAG, "Invalid data format returned from the cloud."); } } public void onSuccess(Object o, OutputParams outputParams) { ALog.d(TAG, "onSuccess() called with: o = [" + o + "], outputParams = [" + outputParams + "]"); ALog.d(TAG, "Service registered successfully."); } public void onFail(Object o, ErrorInfo errorInfo) { ALog.d(TAG, "onFail() called with: o = [" + o + "], errorInfo = [" + errorInfo + "]"); ALog.d(TAG, "Failed to register service."); } }; - 電球がオフラインのときに IoT Platform で期待される属性値を設定すると、IoT Platform はその値をクラウドに保存します。
電球がオンラインになると、期待される値を能動的にフェッチし、
handlePropertySetを呼び出して物理的な状態を更新します。LinkKit.getInstance().init(params, new ILinkKitConnectListener() { public void onError(AError aError) { ALog.e(TAG, "Init Error error=" + aError); } public void onInitDone(InitResult initResult) { ALog.i(TAG, "onInitDone result=" + initResult); connectNotifyListener(); // クラウドから最新の期待される属性値を取得します。 getDesiredProperty(deviceInfo, Arrays.asList("LightStatus"), new IConnectSendListener() { public void onResponse(ARequest aRequest, AResponse aResponse) { if(aRequest instanceof MqttPublishRequest && aResponse.data != null) { JSONObject jsonObject = JSONObject.parseObject(aResponse.data.toString()); ALog.i(TAG, "onResponse result=" + jsonObject); JSONObject dataObj = jsonObject.getJSONObject("data"); if (dataObj != null) { if (dataObj.getJSONObject("LightStatus") == null) { // 期待される値は設定されていません。 } else { Integer value = dataObj.getJSONObject("LightStatus").getInteger("value"); handlePropertySet("LightStatus", new ValueWrapper.IntValueWrapper(value), true); } } } } public void onFailure(ARequest aRequest, AError aError) { ALog.d(TAG, "onFailure() called with: aRequest = [" + aRequest + "], aError = [" + aError + "]"); } }); } }); private void getDesiredProperty(BaseInfo info, List<String> properties, IConnectSendListener listener) { ALog.d(TAG, "getDesiredProperty() called with: info = [" + info + "], listener = [" + listener + "]"); if(info != null && !StringUtils.isEmptyString(info.productKey) && !StringUtils.isEmptyString(info.deviceName)) { MqttPublishRequest request = new MqttPublishRequest(); request.topic = DESIRED_PROPERTY_GET.replace("{productKey}", info.productKey).replace("{deviceName}", info.deviceName); request.replyTopic = DESIRED_PROPERTY_GET_REPLY.replace("{productKey}", info.productKey).replace("{deviceName}", info.deviceName); request.isRPC = true; RequestModel<List<String>> model = new RequestModel<>(); model.id = String.valueOf(IDGeneraterUtils.getId()); model.method = METHOD_GET_DESIRED_PROPERTY; model.params = properties; model.version = "1.0"; request.payloadObj = model.toString(); ALog.d(TAG, "getDesiredProperty: payloadObj=" + request.payloadObj); ConnectSDK.getInstance().send(request, listener); } else { ALog.w(TAG, "getDesiredProperty failed, baseInfo Empty."); if(listener != null) { AError error = new AError(); error.setMsg("BaseInfoEmpty."); listener.onFailure(null, error); } } }
移行結果の検証
次のシナリオに基づいてサンプルコードを実行し、電球のオンラインまたはオフライン状態を検証します。IoT Platform で期待される属性値を指定することで、デバイスのプロパティ値を変更できます。
- デバイスがオンラインの場合、クラウドで電球のステータスを変更すると、リアルタイムで応答がトリガーされます。[デバイス詳細] ページで、[TSL データ] > [実行ステータス] に移動します。[稼働ステータス] のプロパティ値は [1 (オン)] で、期待される値も [1 (オン)] です。
- デバイスがオフラインのときにクラウドで電球のステータスを変更すると、期待される値はデバイスが最後にレポートした値と異なります。[デバイス詳細] ページの [TSL データ] > [実行ステータス] タブで、[稼働ステータス] プロパティの現在の値は [1 (オン)] ですが、期待される値は [0 (オフ)] です。
- デバイスが再接続すると、期待される属性値をフェッチして状態を同期します。[TSL データ] > [実行ステータス] タブで、[稼働ステータス] プロパティの期待される値と現在の値が両方とも
0 (オフ)であることがわかります。これにより、デバイスのプロパティがクラウドからの期待される値と同期したことが確認されます。
付録:デバイスを設定するためのサンプルコード
package com.aliyun.alink.devicesdk.demo;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.aliyun.alink.apiclient.utils.StringUtils;
import com.aliyun.alink.dm.api.BaseInfo;
import com.aliyun.alink.dm.api.DeviceInfo;
import com.aliyun.alink.dm.api.InitResult;
import com.aliyun.alink.dm.model.RequestModel;
import com.aliyun.alink.dm.utils.IDGeneraterUtils;
import com.aliyun.alink.linkkit.api.ILinkKitConnectListener;
import com.aliyun.alink.linkkit.api.IoTMqttClientConfig;
import com.aliyun.alink.linkkit.api.LinkKit;
import com.aliyun.alink.linkkit.api.LinkKitInitParams;
import com.aliyun.alink.linksdk.cmp.api.ConnectSDK;
import com.aliyun.alink.linksdk.cmp.connect.channel.MqttPublishRequest;
import com.aliyun.alink.linksdk.cmp.core.base.ARequest;
import com.aliyun.alink.linksdk.cmp.core.base.AResponse;
import com.aliyun.alink.linksdk.cmp.core.listener.IConnectSendListener;
import com.aliyun.alink.linksdk.tmp.api.InputParams;
import com.aliyun.alink.linksdk.tmp.api.OutputParams;
import com.aliyun.alink.linksdk.tmp.device.payload.ValueWrapper;
import com.aliyun.alink.linksdk.tmp.devicemodel.Service;
import com.aliyun.alink.linksdk.tmp.listener.IPublishResourceListener;
import com.aliyun.alink.linksdk.tmp.listener.ITResRequestHandler;
import com.aliyun.alink.linksdk.tmp.listener.ITResResponseCallback;
import com.aliyun.alink.linksdk.tmp.utils.ErrorInfo;
import com.aliyun.alink.linksdk.tools.AError;
import com.aliyun.alink.linksdk.tools.ALog;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class LampDemo {
private static final String TAG = "LampDemo";
private final static String SERVICE_SET = "set";
private final static String SERVICE_GET = "get";
public static String DESIRED_PROPERTY_GET = "/sys/${productKey}/${deviceName}/thing/property/desired/get";
public static String DESIRED_PROPERTY_GET_REPLY = "/sys/${productKey}/${deviceName}/thing/property/desired/get_reply";
public static String METHOD_GET_DESIRED_PROPERTY = "thing.property.desired.get";
public static void main(String[] args) {
/**
* デバイス証明書情報。
*/
String productKey = "****";
String deviceName = "Lamp";
String deviceSecret = "****";
/**
* MQTT 接続情報。
*/
String regionId = "cn-shanghai";
LampDemo manager = new LampDemo();
DeviceInfo deviceInfo = new DeviceInfo();
deviceInfo.productKey = productKey;
deviceInfo.deviceName = deviceName;
deviceInfo.deviceSecret = deviceSecret;
manager.init(deviceInfo, regionId);
}
public void init(final DeviceInfo deviceInfo, String region) {
LinkKitInitParams params = new LinkKitInitParams();
/**
* MQTT 初期化パラメーターを設定します。
*/
IoTMqttClientConfig config = new IoTMqttClientConfig();
config.productKey = deviceInfo.productKey;
config.deviceName = deviceInfo.deviceName;
config.deviceSecret = deviceInfo.deviceSecret;
config.channelHost = deviceInfo.productKey + ".iot-as-mqtt." + region + ".aliyuncs.com:1883";
/**
* オフラインメッセージを受信するかどうかを指定します。
* これは MQTT の `cleanSession` フィールドに対応します。
*/
config.receiveOfflineMsg = false;
params.mqttClientConfig = config;
/**
* 初期化パラメーターを設定し、デバイス証明書情報を渡します。
*/
params.deviceInfo = deviceInfo;
LinkKit.getInstance().init(params, new ILinkKitConnectListener() {
public void onError(AError aError) {
ALog.e(TAG, "Init Error error=" + aError);
}
public void onInitDone(InitResult initResult) {
ALog.i(TAG, "onInitDone result=" + initResult);
connectNotifyListener();
// クラウドから最新の期待される属性値を取得します。
getDesiredProperty(deviceInfo, Arrays.asList("LightStatus"), new IConnectSendListener() {
public void onResponse(ARequest aRequest, AResponse aResponse) {
if(aRequest instanceof MqttPublishRequest && aResponse.data != null) {
JSONObject jsonObject = JSONObject.parseObject(aResponse.data.toString());
ALog.i(TAG, "onResponse result=" + jsonObject);
JSONObject dataObj = jsonObject.getJSONObject("data");
if (dataObj != null) {
if (dataObj.getJSONObject("LightStatus") == null) {
// 期待される値は設定されていません。
} else {
Integer value = dataObj.getJSONObject("LightStatus").getInteger("value");
handlePropertySet("LightStatus", new ValueWrapper.IntValueWrapper(value), true);
}
}
}
}
public void onFailure(ARequest aRequest, AError aError) {
ALog.d(TAG, "onFailure() called with: aRequest = [" + aRequest + "], aError = [" + aError + "]");
}
});
}
});
}
/**
* このメソッドは、物理デバイスがプロパティの変更を処理する必要があるときに呼び出されます。これには 2 つのシナリオがあります:
* シナリオ 1:接続後、デバイスは最新の期待される属性値を能動的にフェッチします (プルモデル)。
* シナリオ 2:オンライン中、デバイスは `property.set` を介してクラウドからプッシュされた期待される属性値を受信します (プッシュモデル)。
* @param identifier プロパティ識別子。
* @param value 期待される属性値。
* @param needReport `property.post` を使用してステータスレポートを送信するかどうかを指定します。
* シナリオ 2 では、ハンドラにすでにレポートが含まれているため、このパラメーターは false に設定されます。
* @return
*/
private boolean handlePropertySet(String identifier, ValueWrapper value, boolean needReport) {
ALog.d(TAG, "Physical device handling property change = [" + identifier + "], value = [" + value + "]");
// 実際のアプリケーションでは、設定操作が成功したかどうかを判断します。ここでは、成功したと仮定します。
boolean success = true;
if (needReport) {
reportProperty(identifier, value);
}
return success;
}
private void reportProperty(String identifier, ValueWrapper value){
if (StringUtils.isEmptyString(identifier) || value == null) {
return;
}
ALog.d(TAG, "Reporting property identity=" + identifier);
Map<String, ValueWrapper> reportData = new HashMap<>();
reportData.put(identifier, value);
LinkKit.getInstance().getDeviceThing().thingPropertyPost(reportData, new IPublishResourceListener() {
public void onSuccess(String s, Object o) {
// プロパティは正常にレポートされました。
ALog.d(TAG, "Report successful. onSuccess() called with: s = [" + s + "], o = [" + o + "]");
}
public void onError(String s, AError aError) {
// プロパティのレポートに失敗しました。
ALog.d(TAG, "Report failed. onError() called with: s = [" + s + "], aError = [" + JSON.toJSONString(aError) + "]");
}
});
}
/**
* サービスコールとプロパティ設定のための応答関数を登録します。
* クラウドがデバイスサービスを呼び出すとき、デバイスはサービスコールに応答する必要があります。
*/
public void connectNotifyListener() {
List<Service> serviceList = LinkKit.getInstance().getDeviceThing().getServices();
for (int i = 0; serviceList != null && i < serviceList.size(); i++) {
Service service = serviceList.get(i);
LinkKit.getInstance().getDeviceThing().setServiceHandler(service.getIdentifier(), mCommonHandler);
}
}
private ITResRequestHandler mCommonHandler = new ITResRequestHandler() {
public void onProcess(String serviceIdentifier, Object result, ITResResponseCallback itResResponseCallback) {
ALog.d(TAG, "onProcess() called with: s = [" + serviceIdentifier + "]," +
" o = [" + result + "], itResResponseCallback = [" + itResResponseCallback + "]");
ALog.d(TAG, "Received asynchronous service call from cloud: " + serviceIdentifier);
try {
if (SERVICE_SET.equals(serviceIdentifier)) {
Map<String, ValueWrapper> data = (Map<String, ValueWrapper>)((InputParams)result).getData();
ALog.d(TAG, "Received asynchronous downstream data: " + data);
// 物理デバイスにプロパティを設定し、更新された値をレポートします。
boolean isSetPropertySuccess =
handlePropertySet("LightStatus", data.get("LightStatus"), false);
if (isSetPropertySuccess) {
if (result instanceof InputParams) {
// データが正常に受信されたことをクラウドに応答します。
itResResponseCallback.onComplete(serviceIdentifier, null, null);
} else {
itResResponseCallback.onComplete(serviceIdentifier, null, null);
}
} else {
AError error = new AError();
error.setCode(100);
error.setMsg("setPropertyFailed.");
itResResponseCallback.onComplete(serviceIdentifier, new ErrorInfo(error), null);
}
} else if (SERVICE_GET.equals(serviceIdentifier)) {
} else {
// 特定の要件に基づいて他のサービスを処理します。
ALog.d(TAG, "Return a value based on the actual service. For an example, refer to the 'set' implementation.");
OutputParams outputParams = new OutputParams();
// outputParams.put("op", new ValueWrapper.IntValueWrapper(20));
itResResponseCallback.onComplete(serviceIdentifier, null, outputParams);
}
} catch (Exception e) {
e.printStackTrace();
ALog.d(TAG, "Invalid data format returned from the cloud.");
}
}
public void onSuccess(Object o, OutputParams outputParams) {
ALog.d(TAG, "onSuccess() called with: o = [" + o + "], outputParams = [" + outputParams + "]");
ALog.d(TAG, "Service registered successfully.");
}
public void onFail(Object o, ErrorInfo errorInfo) {
ALog.d(TAG, "onFail() called with: o = [" + o + "], errorInfo = [" + errorInfo + "]");
ALog.d(TAG, "Failed to register service.");
}
};
private void getDesiredProperty(BaseInfo info, List<String> properties, IConnectSendListener listener) {
ALog.d(TAG, "getDesiredProperty() called with: info = [" + info + "], listener = [" + listener + "]");
if(info != null && !StringUtils.isEmptyString(info.productKey) && !StringUtils.isEmptyString(info.deviceName)) {
MqttPublishRequest request = new MqttPublishRequest();
request.topic = DESIRED_PROPERTY_GET.replace("{productKey}", info.productKey).replace("{deviceName}", info.deviceName);
request.replyTopic = DESIRED_PROPERTY_GET_REPLY.replace("{productKey}", info.productKey).replace("{deviceName}", info.deviceName);
request.isRPC = true;
RequestModel<List<String>> model = new RequestModel<>();
model.id = String.valueOf(IDGeneraterUtils.getId());
model.method = METHOD_GET_DESIRED_PROPERTY;
model.params = properties;
model.version = "1.0";
request.payloadObj = model.toString();
ALog.d(TAG, "getDesiredProperty: payloadObj=" + request.payloadObj);
ConnectSDK.getInstance().send(request, listener);
} else {
ALog.w(TAG, "getDesiredProperty failed, baseInfo Empty.");
if(listener != null) {
AError error = new AError();
error.setMsg("BaseInfoEmpty.");
listener.onFailure(null, error);
}
}
}
}