文档

物模型SDK

更新时间:
一键部署

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

依赖的SDK

依赖 SDK

概述

API通道SDK

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

长连接通道SDK

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

初始化

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

使用方式

设备的创建和销毁

// 创建设备
IotThing * iotThing = [IotThingManager.sharedManager buildThing:iotId];

// 结束使用后务必要销毁设备
[IotThingManager.sharedManager destroyThing:iotThing];

设备控制

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

  • 获取设备状态

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

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

    [iotThing setProperties:@"{\"IpcVersion\":\"1.2.3\"}" handler:^(bool success, NSString * _Nullable data, int errCode, NSString * _Nullable errMessage, NSString * _Nullable errLocalizedMessage) {
            // success表示是否获取成功,true为成功,false为失败
            // 成功时data为空字符串
            // 失败时data为空字符串,错误码及错误内容描述见errorCode和errMessage
        }];
  • 获取设备的信息

     [iotThing getThingInfo:^(bool success, NSString * _Nullable data, int errCode, NSString * _Nullable errMessage, NSString * _Nullable errLocalizedMessage) {
            // success表示是否获取成功,true为成功,false为失败
            // 成功时data为json格式,结构如下:
            /*{
      "status" : 3,
      "gmtCreate" : 1694486162000,
      "deviceSecret" : "728923**********8d27290eb",
      "iotId" : "HsYbli*********kOmk05vt0",
      "firmwareVersion" : "1.0",
      "activeTime" : 1694492215000,
      "productKey" : "k0******nP",
      "netAddress" : "140.205.11.242",
      "name" : "haitao_001"
    }
            */
            // 失败时data为空字符串,错误码及错误内容描述见errorCode和errMessage
        }];
  • 调用服务

    [iotThing invokeService:@"PTZActionControl" args:@"{\"ActionType\":2,\"Step\":2}" handler:^(bool success, NSString * _Nullable data, int errCode, NSString * _Nullable errMessage, NSString * _Nullable errLocalizedMessage) {
        // success表示是否获取成功,true为成功,false为失败
        // 成功时data为json格式,结构如下:
        /* {"messageId":"1870032450","data":"{}"}
            */
        // 失败时data为空字符串,错误码及错误内容描述见errorCode和errMessage
    }];

获取物的最新上报的事件

[iotThing getLastEvent:^(bool success, NSString * _Nullable data, int errCode, NSString * _Nullable errMessage, NSString * _Nullable errLocalizedMessage) {
        // success表示是否获取成功,true为成功,false为失败
        // 成功时data为json格式,结构如下:
        /* [
  {
    "type" : "alert",
    "value" : "{\"AlarmPicID\":\"3\/2A3-_iKTDMxlXy********rkySuS1g\/osssts_ae_5834a0_1_636707584\",\"AlarmType\":1}",
    "identifier" : "AlarmEvent",
    "time" : 1696905094948,
    "eventCode" : "AlarmEvent"
  },
  {
    "type" : "alert",
    "value" : "{\"EventList\":[{\"AlarmPicID\":\"3\/2A3-_iKTD**********PFRPrkySuS1g\/osssts_ia_6c7c57_1_5_265671\",\"EventTime\":\"1696908553830\",\"AlarmType\":1,\"AlarmPicSize\":48666,\"Data\":\"test\"}]}",
    "identifier" : "IntelligentAlarm",
    "time" : 1696908554283,
    "eventCode" : "IntelligentAlarm"
  }
]
        */
        // 失败时data为空字符串,错误码及错误内容描述见errorCode和errMessage
    }];

订阅或取消订阅所有事件

- (void)onEventHappen:(NSString * _Nonnull)iotId params:(NSString * _Nonnull)params {
    NSLog("物事件产生 iotId:%@ params:%@", iotId, params);
}

- (void)onPropertyChange:(NSString * _Nonnull)iotId params:(NSString * _Nonnull)params {
    NSLog("物属性变更 iotId:%@ params:%@", iotId, params);
}

- (void)onStatusChange:(NSString * _Nonnull)iotId params:(NSString * _Nonnull)params {
    NSLog("物状态变化 iotId:%@ params:%@", iotId, params);
}

// 订阅与该iotid设备相关的事件
[iotThing registerThingObserver:self];

// 取消订阅
[iotThing unregisterThingObserver:self];

获取物模型

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

[iotThing getTsl:^(bool success, NSString * _Nullable data, int errCode, NSString * _Nullable errMessage, NSString * _Nullable 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)
文档反馈