文档

物模型SDK

更新时间:

物模型SDK提供了App端的物模型(属性、事件、服务),用来开发设备界面,实现手机对设备的查看和控制。

依赖的SDK

依赖 SDK

概述

API通道SDK

提供IoT业务协议封装的HTTPS请求能力,并通过整合安全组件来提升通道的安全性。

长连接通道SDK

提供IoT业务协议封装的云端数据下行能力;为App提供订阅、发布消息的能力,和支持请求响应模型。

初始化

初始化前需确保已正确集成安全图片。具体操作,请参见SDK初始化集成安全图片

使用方式

设备的创建和销毁

// 创建设备
IotThing iotThing = IotThingManager.getInstance().buildThing(iotId);

// 结束使用后务必要销毁设备
IotThingManager.getInstance().destroyThing(iotThing);

设备控制

设备控制是基于物模型对设备定义的属性、事件、服务进行操作。关于属性、事件、服务的描述,请参见客户端API参考>物模型管理文档。

  • 获取设备状态

    iotThing.getStatus(new IIotThingActionResponseListener() {
        @Override
        public void onComplete(boolean success, String data, int errorCode, String errMessage, String errLocalizedMessage) {
            // success表示是否获取成功,true为成功,false为失败
            // 成功时data为json格式,结构如下:
            /*{"time":1696902734327,"status":3}
             说明:status表示设备生命周期,目前有以下几个状态,
            0:未激活;1:上线;3:离线;8:禁用;time表示当前状态的开始时间
            */
            // 失败时data为空字符串,错误码及错误内容描述见errorCode和errMessage
        }
    });
  • 获取设备属性

    iotThing.getProperties(new IIotThingActionResponseListener() {
        @Override
        public void onComplete(boolean success, String data, int errorCode, String errMessage, String errLocalizedMessage) {
            // success表示是否获取成功,true为成功,false为失败
            // 成功时data为json格式,结构如下:
            /* {"StatusLightSwitch":{"time":1696728804421,"value":"0"},"MicSwitch":{"time":1696728804421,"value":"0"},"EncryptSwitch":{"time":1696728804421,"value":"0"},"PreRecordSupport":{"time":1696816383804,"value":"0"},"StorageRecordMode":{"time":1696728804421,"value":"0"},"EncryptTypeList":{"time":1696816383804,"value":"[0]"},"SpeakerSwitch":{"time":1696728804421,"value":"0"},"StorageStatus":{"time":1696728804421,"value":"1"},"StorageTotalCapacity":{"time":1696728804421,"value":"128.0"},"AlarmSwitch":{"time":1696728804421,"value":"0"},"VoiceDetectionSensitivity":{"time":1696728804421,"value":"0"},"StreamVideoQuality":{"time":1696733488242,"value":"1"},"MotionDetectSensitivity":{"time":1696728804421,"value":"0"},"ImageFlipState":{"time":1696728804421,"value":"0"},"SubStreamVideoQuality":{"time":1696728804421,"value":"0"},"UploadPictureType":{"time":1696816383804,"value":"1"},"DayNightMode":{"time":1696728804421,"value":"0"},"AlarmFrequencyLevel":{"time":1696728804421,"value":"0"},"StorageRemainCapacity":{"time":1696728804421,"value":"64.12"}}
            */
            // 失败时data为空字符串,错误码及错误内容描述见errorCode和errMessage
        }
    });
  • 设置设备属性

    iotThing.setProperties("{\"power\":\"on\", \"temperature\":30}",new IIotThingActionResponseListener() {
        @Override
        public void onComplete(boolean success, String data, int errorCode, String errMessage, String errLocalizedMessage) {
            // success表示是否获取成功,true为成功,false为失败
            // 成功时data为空字符串
            // 失败时data为空字符串,错误码及错误内容描述见errorCode和errMessage
        }
    });
  • 获取设备的信息

    iotThing.getThingInfo(new IIotThingActionResponseListener() {
        @Override
        public void onComplete(boolean success, String data, int errorCode, String errMessage, String errLocalizedMessage) {
            // success表示是否获取成功,true为成功,false为失败
            // 成功时data为json格式,结构如下:
            /* {"netAddress":"1**.***.***.***","deviceSecret":"728923**********d27290eb","iotId":"HsYbli53I**********vt0","activeTime":1694492215000,"name":"haitao_001","productKey":"k05*******","firmwareVersion":"app-1.0.0-2*****.1000","gmtCreate":1694486162000,"status":3}
            */
            // 失败时data为空字符串,错误码及错误内容描述见errorCode和errMessage
        }
    });
  • 调用服务

    iotThing.invokeService("PTZActionControl", "{\"ActionType\":2,\"Step\":2}",new IIotThingActionResponseListener() {
        @Override
        public void onComplete(boolean success, String data, int errorCode, String errMessage, String errLocalizedMessage) {
            // success表示是否获取成功,true为成功,false为失败
            // 成功时data为json格式,结构如下:
            /* {"data":"{}","messageId":"1157605793"}
            */
            // 失败时data为空字符串,错误码及错误内容描述见errorCode和errMessage
        }
    });

获取物的最新上报的事件

iotThing.getLastEvent(new IIotThingActionResponseListener() {
    @Override
    public void onComplete(boolean success, String data, int errorCode, String errMessage, String errLocalizedMessage) {
        // success表示是否获取成功,true为成功,false为失败
        // 成功时data为json格式,结构如下:
        /* [{"eventCode":"AlarmEvent","identifier":"AlarmEvent","time":1696905094948,"type":"alert","value":"{\"AlarmPicID\":\"3/2A3-_iKTDMxlXyQ4aVqQvZUOStkxDkyPFRPrkySuS1g/osssts_ae_5834a0_1_636707584\",\"AlarmType\":1}"},{"eventCode":"IntelligentAlarm","identifier":"IntelligentAlarm","time":1696906367317,"type":"alert","value":"{\"EventList\":[{\"AlarmPicID\":\"3/2A3-_iKTDMxlXyQ4aVqQvZUOStkxDkyPFRPrkySuS1g/osssts_ia_a33938_1_4_263484\",\"EventTime\":\"1696906366644\",\"AlarmType\":1,\"AlarmPicSize\":48666,\"Data\":\"test\"}]}"}]
        */
        // 失败时data为空字符串,错误码及错误内容描述见errorCode和errMessage
    }
});

订阅或取消订阅所有事件

private IIotThingObserver thingObserver = new IIotThingObserver() {
        @Override
        public void onStatusChange(String iotId, String params) {
            makeToast("物状态变化 iotId:"+iotId + " params:"+ params);
        }

        @Override
        public void onEventHappen(String iotId, String params) {
            makeToast("物事件产生 iotId:"+iotId + " params:"+ params);
        }

        @Override
        public void onPropertyChange(String iotId, String params) {
            makeToast("物属性变更 iotId:"+iotId + " params:"+ params);
        }
    };

// 订阅与该iotid设备相关的事件
iotThing.registerThingObserver(thingObserver);

// 取消订阅
iotThing.unregisterThingObserver(thingObserver);

获取物模型

开发者也可以使用云端接口来获取原始的物的模型。具体内容,请参见客户端API参考>物模型管理>查询物模型TSL文档。

iotThing.getTsl(new IIotThingActionResponseListener() {
    @Override
    public void onComplete(boolean success, String data, int errorCode, String errMessage, String errLocalizedMessage) {
        // success表示是否获取成功,true为成功,false为失败
        // 成功时data为json格式,结构如下:
        /* {"schema":"https://iotx-******.aliyuncs.com/schema.json","profile":{"productKey":"k05vt******","version":"1.0"},"services":[{"outputData":[{"identifier":"Result","dataType":{"specs":{"0":"成功","1":"设备不支持","2":"设备资源不足","3":"推流连接失败"},"type":"enum"},"name":"推流结果"}],"identifier":"StartPushStreaming","inputData":[{"identifier":"PushUrl","dataType":{"specs":{"length":"255"},"type":"text"},"name":"推流地址"},{"identifier":"StreamType","dataType":{"specs":{"0":"主码流","1":"辅码流"},"type":"enum"},"name":"码流类型"},{"identifier":"Scheme","dataType":{"specs":{"0":"RTMP","1":"RTSP"},"type":"enum"},"name":"流协议"},{"identifier":"EncryptKey","dataType":{"specs":{"length":"255"},"type":"text"},"name":"密钥"},{"identifier":"EncryptType","dataType":{"specs":{"min":"0","unitName":"无","max":"9999","step":"1"},"type":"int"},"name":"加密类型"},{"identifier":"PreTime","dataType":{"specs":{"unit":"s","min":"0","unitName":"秒","max":"60","step":"1"},"type":"int"},"name":"提前推流时间"}],"method":"thing.service.StartPushStreaming","name":"开始直播","required":true,"callType":"async"}],"properties":[{"identifier":"AlarmSwitch","dataType":{"specs":{"0":"关闭","1":"开启"},"type":"bool"},"name":"报警开关","accessMode":"rw","required":true},{"identifier":"MotionDetectSensitivity","dataType":{"specs":{"0":"关闭","1":"最低档","2":"低档","3":"中档","4":"高档","5":"最高档"},"type":"enum"},"name":"移动侦测灵敏度","accessMode":"rw","required":true}],"events":[{"outputData":[{"identifier":"AlarmType","dataType":{"specs":{"1":"移动侦测","2":"声音侦测","3":"人形侦测","4":"宠物侦测","5":"越界侦测","6":"区域入侵侦测","7":"跌倒侦测","8":"人脸检测","9":"笑脸检测","10":"异响侦测","11":"哭声侦测","12":"笑声侦测","13":"火警侦测","14":"人群聚集","15":"人流统计"},"type":"enum"},"name":"告警类型"},{"identifier":"AlarmPicID","dataType":{"specs":{"length":"128"},"type":"text"},"name":"报警抓图ID"},{"identifier":"Data","dataType":{"specs":{"length":"1024"},"type":"text"},"name":"告警内容"}],"identifier":"AlarmEvent","method":"thing.event.AlarmEvent.post","name":"侦测报警","type":"alert","required":true},{"outputData":[{"identifier":"ErrorCode","dataType":{"specs":{"0":"恢复正常","1":"网络异常","2":"存储介质异常"},"type":"enum"},"name":"故障代码"}],"identifier":"Error","method":"thing.event.Error.post","name":"故障上报","type":"info","required":true}]}
        */
        // 失败时data为空字符串,错误码及错误内容描述见errorCode和errMessage
    }
});

  • 本页导读 (1)
文档反馈