設(shè)備影子
更新時(shí)間:
如果當(dāng)前產(chǎn)品不具備物模型的能力,可以通過設(shè)備影子將當(dāng)前設(shè)備的最新狀態(tài)緩存到云端。云端緩存的是一個(gè)最新的JSON格式數(shù)據(jù),需要用戶自己根據(jù)實(shí)際情況做解析。物模型具備更高級(jí)的設(shè)備影子能力,能根據(jù)各個(gè)屬性、事件、服務(wù)做獨(dú)立展示,并具有所有操作的歷史記錄。
數(shù)據(jù)上行
說(shuō)明 設(shè)備影子相關(guān)接口參見設(shè)備 IDeviceShadow。
- 獲取云端設(shè)備影子
- 更新云端設(shè)備影子
- 刪除云端設(shè)備影子
private int version = 1;
// 更新設(shè)備影子,需要根據(jù)獲得到的設(shè)備影子讀取返回的 version值,在更新的時(shí)候 {ver} 替換為version+1
private String shadowUpdate = "{" + "\"method\": \"update\"," + "\"state\": {" + "\"reported\": {" +
"\"color\": \"red\"" + ",\"mode\": \"1\"" + "}" + "}," + "\"version\": {ver}" + "}";
// 獲取設(shè)備影子
private String shadowGet = "{" + "\"method\": \"get\"" + "}";
//刪除設(shè)備影子 color 屬性 {ver}需要替換
private String shadowDelete = "{" + "\"method\": \"delete\"," + "\"state\": {" + "\"reported\": {" +
"\"color\": \"null\"" + "}" + "}," + "\"version\": {ver}" + "}";
/**
* 設(shè)備影子更新
*/
public void shadowUpdate() {
ALog.d(TAG, "shadowUpdate");
version++;
shadowUpstream(shadowUpdate.replace("{ver}", String.valueOf(version)));
}
/**
* 獲取設(shè)備影子
*/
public void shadowGet() {
ALog.d(TAG, "shadowGet");
shadowUpstream(shadowGet);
}
/**
* 刪除設(shè)備影子
*/
public void shadowDelete() {
ALog.d(TAG, "shadowDelete");
version++;
shadowUpstream(shadowDelete.replace("{ver}", String.valueOf(version)));
}
private void shadowUpstream(String requestData) {
if (requestData == null) {
ALog.e(TAG, "shadowUpstream error requestData=null.");
return;
}
LinkKit.getInstance().getDeviceShadow().shadowUpload(requestData, new IConnectSendListener() {
@Override
public void onResponse(ARequest aRequest, AResponse aResponse) {
ALog.d(TAG, "onResponse() called with: aRequest = [" + aRequest + "], aResponse = [" + (aResponse == null ? null : aResponse.data) + "]");
try {
if (aRequest instanceof MqttPublishRequest && aResponse != null) {
String dataStr = null;
if (aResponse.data instanceof byte[]) {
dataStr = new String((byte[]) aResponse.data, "UTF-8");
} else if (aResponse.data instanceof String) {
dataStr = (String) aResponse.data;
} else {
dataStr = String.valueOf(aResponse.data);
}
ALog.d(TAG, "dataStr = " + dataStr);
// {"method":"reply","payload":{"status":"success","state":{"reported":{}},"metadata":{"reported":{}}},"timestamp":1547641855,"version":7,"clientToken":"null"}
ShadowResponse<String> response = JSONObject.parseObject(dataStr, new TypeReference<ShadowResponse<String>>() {
}.getType());
if (response != null && response.version != null) {
version = Integer.valueOf(response.version);
}
}
} catch (NumberFormatException e) {
e.printStackTrace();
ALog.e(TAG, "update version failed.");
} catch (Exception e) {
ALog.e(TAG, "update response parse exception.");
}
}
@Override
public void onFailure(ARequest aRequest, AError aError) {
ALog.d(TAG, "onFailure() called with: aRequest = [" + aRequest + "], aError = [" + aError + "]");
}
});
}
數(shù)據(jù)下行
監(jiān)聽云端設(shè)備影子數(shù)據(jù)更新,通過云端API下發(fā)設(shè)備影子更新數(shù)據(jù)到設(shè)備端,設(shè)備端收到云端下行的設(shè)備影子數(shù)據(jù)后,根據(jù)desired
的值去執(zhí)行設(shè)備更新。
/**
* 先訂閱設(shè)備影子的更新 topic
* 云端下發(fā)設(shè)備影子數(shù)據(jù)之后
*/
public void listenDownStream() {
ThreadPool.execute(new Runnable() {
@Override
public void run() {
LinkKit.getInstance().getDeviceShadow().setShadowChangeListener(new IShadowRRPC() {
@Override
public void onSubscribeSuccess(ARequest aRequest) {
ALog.d(TAG, "設(shè)備影子下行訂閱成功");
ALog.d(TAG, "onSubscribeSuccess() called with: aRequest = [" + aRequest + "]");
}
@Override
public void onSubscribeFailed(ARequest aRequest, AError aError) {
ALog.d(TAG, "設(shè)備影子下行訂閱失敗");
ALog.d(TAG, "onSubscribeFailed() called with: aRequest = [" + aRequest + "], aError = [" + aError + "]");
}
@Override
public void onReceived(ARequest aRequest, AResponse aResponse, IConnectRrpcHandle iConnectRrpcHandle) {
ALog.d(TAG, "onReceived() called with: aRequest = [" + aRequest + "], iConnectRrpcHandle = [" + iConnectRrpcHandle + "]");
// TODO user logic
ALog.d(TAG, "收到設(shè)備影子下行指令");
try {
if (aRequest != null) {
String dataStr = null;
if (aResponse.data instanceof byte[]) {
dataStr = new String((byte[]) aResponse.data, "UTF-8");
} else if (aResponse.data instanceof String) {
dataStr = (String) aResponse.data;
} else {
dataStr = String.valueOf(aResponse.data);
}
ALog.d(TAG, "dataStr = " + dataStr);
// 返回?cái)?shù)據(jù)示例
//{"method":"control","payload":{"state":{"desired":{"mode":2,"color":"white"},"reported":{"mode":"1","color":"red"}},"metadata":{"desired":{"mode":{"timestamp":1547642408},"color":{"timestamp":1547642408}},"reported":{"mode":{"timestamp":1547642408},"color":{"timestamp":1547642408}}}},"timestamp":1547642408,"version":12}
// 僅供參考
ShadowResponse<String> shadowResponse = JSONObject.parseObject(dataStr, new TypeReference<ShadowResponse<String>>() {
}.getType());
if (shadowResponse != null && shadowResponse.version != null) {
version = Integer.valueOf(shadowResponse.version);
}
AResponse response = new AResponse();
// TODO 用戶實(shí)現(xiàn)控制設(shè)備
// 用戶控制設(shè)備之后,上報(bào)影子的值到云端
// 上報(bào)設(shè)置之后的值到云端
// 根據(jù)當(dāng)前實(shí)際值上報(bào)
response.data = shadowUpdate.replace("{ver}", String.valueOf(++version));
// 第一個(gè)值 replyTopic 有默認(rèn)值,您不需要設(shè)置
iConnectRrpcHandle.onRrpcResponse(null, response);
}
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void onResponseSuccess(ARequest aRequest) {
ALog.d(TAG, "onResponseSuccess() called with: aRequest = [" + aRequest + "]");
}
@Override
public void onResponseFailed(ARequest aRequest, AError aError) {
ALog.w(TAG, "onResponseFailed() called with: aRequest = [" + aRequest + "], aError = [" + aError + "]");
}
});
}
});
}
文檔內(nèi)容是否對(duì)您有幫助?