Device - blockdate/ios-rn-sdk GitHub Wiki
设备相关 API IDevice 当前设备实例对象,用于获取当前设备属性等 IDeviceWifi 当前设备网络操作实例对象,用于发送设备网络操作请求等 DeviceEvent 当前设备的可订阅事件,用于订阅设备名称状态变化等
Export:
Example
import {Device} from 'miot'
...
//IDevice
//属性获取
let did = Device.deviceID
let deviceModel = Device.model
//设备方法,e.g 场景创建
let scene = Device.createTimerScene(params)
//IDeviceWifi
//wifi方法 e.g RPC请求
Device.getDeviceWifi().callMethod('method_name', params)
.then(res => {//here is the success result})
.catch(err => {//error happened})
...
其余具体使用请参考具体API文档-
miot/Device
-
module.exports ⏏
-
static
-
.IDeviceWifi
-
.deviceID :
string -
.loadProperties(...propNames) ⇒
Promise.<Map> -
.loadPropertiesFromCloud(...propNames) ⇒
Promise.<Map> -
.callMethod(method, args, extraPayload) ⇒
Promise.<json> -
.callMethodFromCloud(method, args) ⇒
Promise.<json> -
.localPing() ⇒
Promise.<boolean> -
.subscribeMessages(...propertyOrEventNames) ⇒
Promise.<EventSubscription> -
.getVersion() ⇒
Promise.<any> -
.startUpgradingFirmware() ⇒
Promise.<DeviceVersion> - .setFirmwareUpdateErrDic(message) ⇒
-
.setFirmwareNotCheckUpdate(notCheck) ⇒
Promise
-
.deviceID :
-
.IDeviceWifi
-
inner
-
~IDevice
-
.deviceID :
string -
.model :
string -
.isReady :
boolean -
.parentDevice :
IDevice -
.isRootDevice :
boolean -
.isVirtualDevice :
boolean -
.owner :
IAccount -
.extra :
string -
.name :
string -
.session :
string -
.iconURL :
string -
.permitLevel :
int -
.isSetPinCode :
boolean -
.parentModel :
string -
.showMode :
int -
.timeZone :
string -
.mac :
string -
.lastVersion :
string -
.propInfo :
json -
.IP :
string -
.RSSI :
string -
.SSID :
string -
.BSSID :
string -
.type :
int -
.lastModified :
long -
.location :
int -
.latitude :
double -
.longitude :
double -
.isVoiceDevice ⇒
boolean -
.isOnline :
boolean -
.resetFlag :
int -
.isOwner :
boolean -
.isFamily :
boolean -
.isShared :
boolean -
.isBinded2 :
boolean -
.isReadonlyShared :
boolean -
.getDeviceWifi() ⇒
IDeviceWifi -
.getSubDevices() ⇒
Promise.<Array.<IDevice>> -
.getVirtualDevices() ⇒
Promise.<Array.<IDevice>> -
.getBluetoothLE(peripheralUUID) ⇒
IBluetoothLE - .reportLog(log)
-
.loadScenes(sceneType, opt) ⇒
Promise.<Array.<IScene>> -
.loadTimerScenes(opt) ⇒
Promise.<Array.<IScene>> -
.createScene(sceneType, opt) ⇒
IScene -
.createTimerScene(opt) ⇒
IScene
-
.deviceID :
-
~DeviceEvent :
object
-
~IDevice
-
static
-
module.exports ⏏
Kind: static interface of module.exports
-
.IDeviceWifi
-
.deviceID :
string -
.loadProperties(...propNames) ⇒
Promise.<Map> -
.loadPropertiesFromCloud(...propNames) ⇒
Promise.<Map> -
.callMethod(method, args, extraPayload) ⇒
Promise.<json> -
.callMethodFromCloud(method, args) ⇒
Promise.<json> -
.localPing() ⇒
Promise.<boolean> -
.subscribeMessages(...propertyOrEventNames) ⇒
Promise.<EventSubscription> -
.getVersion() ⇒
Promise.<any> -
.startUpgradingFirmware() ⇒
Promise.<DeviceVersion> - .setFirmwareUpdateErrDic(message) ⇒
-
.setFirmwareNotCheckUpdate(notCheck) ⇒
Promise
-
.deviceID :
获取设备ID,same as Device.deviceID
Kind: instance property of IDeviceWifi
Example
import {Device} from 'miot'
...
let did = Device.getDeviceWifi().deviceID加载属性数据 内部调用get_prop 方法,会依据当前环境选择从本地局域网或者云端获取, 并将返回数据写成{key:value}格式
Kind: instance method of IDeviceWifi
Returns: Promise.<Map> - Map<name, value>
| Param | Type |
|---|---|
| ...propNames | * |
Example
Device.getDeviceWifi().loadProperties("a", "b").then(map=>{
const a = map.get("a")
const b = map.get("b")
})从云端加载属性数据 内部调用get_prop 方法, 并将返回数据写成{key:value}格式
Kind: instance method of IDeviceWifi
Returns: Promise.<Map> - Map<name, value>
| Param | Type |
|---|---|
| ...propNames | * |
调用设备方法 若与设备通信处于同一个 wifi 下会使用局域网传输数据,如果不在同一个 wifi 下由米家服务器转发请求
Kind: instance method of IDeviceWifi
Returns: Promise.<json> - {code:0,result:{},id:""}
| Param | Type | Description |
|---|---|---|
| method | string |
方法名 |
| args | json |
参数 |
| extraPayload | json |
额外参数,根据设备需求设定。在payload数据中设置额外参数 |
Example
Device.getDeviceWifi().callMethod('getProps', [prop1,prop2])
.then(res => console.log('success:', res))
.catch(err => console.error('failed:', err))
//对应payload参考:
//{'method': 'getProps', 'params':[prop1,prop2]}
Device.getDeviceWifi().callMethod('getProps', [prop1,prop2], {sid:Device.deviceID, 'key1':'xxxx'})
.then(res => console.log('success:', res))
.catch(err => console.error('failed:', err))
//对应payload参考:
//{'method': 'getProps', 'params':[prop1,prop2], 'sid':'xxxxx', 'key1': 'xxxx'}云端调用设备方法 同 callMethod 函数 不在同一个 wifi 下的情况
Kind: instance method of IDeviceWifi
Returns: Promise.<json> - 请求成功返回 {code:0,result:{} }
| Param | Type | Description |
|---|---|---|
| method | string |
方法名 |
| args | json |
参数 |
ping 操作 检查设备本地局域网通信是否可用
Kind: instance method of IDeviceWifi
Example
Device.getDeviceWifi().localPing()
.then(res => console.log('success:', res))
.catch(err => console.log('failed:', err))订阅设备消息
Kind: instance method of IDeviceWifi
| Param | Type | Description |
|---|---|---|
| ...propertyOrEventNames | string |
在开发平台上声明的 prop 与 event 名 |
Example
import {Device, DeviceEvent} from 'miot'
...
let msgSubscription = null;
Device.getDeviceWifi().subscribeMessages('prop1','prop2')
.then(subcription => {
//call this when you need to unsubscribe the message
msgSubscription = subcription;
})
.catch(() => console.log('subscribe failed'))
...
//等效于使用DeviceEvent
const subscription = DeviceEvent.deviceReceivedMessages.addListener(
(device, messages)=>{
if(messages.has('prop1')){
const prop1 = messages.get('prop1');
...
}
...
})
...
//unsubscribe the props
msgSubscription&&msgSubscription.remove();获取版本号
Kind: instance method of IDeviceWifi
获取固件的状态,可以确认是否需要升级,也可以获得当前的升级状态。 /home/devupgrade
Kind: instance method of IDeviceWifi
Example
Device.getDeviceWifi().startUpgradingFirmware()
.then(res => console.log('success:', res))
.catch(err => console.log('failed:', err))为设备固件升级失败添加自定义的errorCode与错误提示信息的索引 注意 分享过来的设备是无法进行固件升级的,所以此时此方法无效。 Android暂未适配
Kind: instance method of IDeviceWifi
Returns: boolean 设置是否成功
| Param | Type | Description |
|---|---|---|
| message | json |
以errorCode为key,以错误提示信息为value的字典。key和value的数据类型都须是string。 |
Example
let ret = Device.getDeviceWifi().setFirmwareUpdateErrDic('message')设置设备控制页不检查固件升级 Android暂未适配
Kind: instance method of IDeviceWifi
| Param | Type | Description |
|---|---|---|
| notCheck | boolean |
是否 不检查更新 true-不自动检查 false-自动检查 |
Example
Device.getDeviceWifi().setFirmwareNotCheckUpdate(true|false)
.then(res => console.log('success:', res))
.catch(err => console.log('failed:', err))设备信息的实例 仅适用于当前插件适配的设备
Kind: inner interface of module.exports
Example
import 'Device' from 'miot'
...
let did = Device.deviceID
let model = Device.model
...-
~IDevice
-
.deviceID :
string -
.model :
string -
.isReady :
boolean -
.parentDevice :
IDevice -
.isRootDevice :
boolean -
.isVirtualDevice :
boolean -
.owner :
IAccount -
.extra :
string -
.name :
string -
.session :
string -
.iconURL :
string -
.permitLevel :
int -
.isSetPinCode :
boolean -
.parentModel :
string -
.showMode :
int -
.timeZone :
string -
.mac :
string -
.lastVersion :
string -
.propInfo :
json -
.IP :
string -
.RSSI :
string -
.SSID :
string -
.BSSID :
string -
.type :
int -
.lastModified :
long -
.location :
int -
.latitude :
double -
.longitude :
double -
.isVoiceDevice ⇒
boolean -
.isOnline :
boolean -
.resetFlag :
int -
.isOwner :
boolean -
.isFamily :
boolean -
.isShared :
boolean -
.isBinded2 :
boolean -
.isReadonlyShared :
boolean -
.getDeviceWifi() ⇒
IDeviceWifi -
.getSubDevices() ⇒
Promise.<Array.<IDevice>> -
.getVirtualDevices() ⇒
Promise.<Array.<IDevice>> -
.getBluetoothLE(peripheralUUID) ⇒
IBluetoothLE - .reportLog(log)
-
.loadScenes(sceneType, opt) ⇒
Promise.<Array.<IScene>> -
.loadTimerScenes(opt) ⇒
Promise.<Array.<IScene>> -
.createScene(sceneType, opt) ⇒
IScene -
.createTimerScene(opt) ⇒
IScene
-
.deviceID :
获取设备 id,每一个真实设备都有一个唯一的 id
Kind: instance property of IDevice
Read only: true
获取设备的 model,设备类型的唯一标识
Kind: instance property of IDevice
Read only: true
设备是否已经可用
Kind: instance property of IDevice
Read only: true
如果有父设备,直接获取 父设备 Device
Kind: instance property of IDevice
Read only: true
是否是根设备
Kind: instance property of IDevice
Read only: true
是否虚拟设备
Kind: instance property of IDevice
Read only: true
设备所有者的小米账号, 可以使用 load 获取 account 下的所有数据。 不调用 load 只有 ID,nickName 字段有值。 注:Service.account 不load时只有ID可用,与此处不一样。
Kind: instance property of IDevice
Read only: true
See: module:miot/Account
除了基本信息的其他数据都在这个字段返回,如:{"fw_version":"1.4.0","mcu_version":"0001"} 可以解析为 json
Kind: instance property of IDevice
Read only: true
开发者平台配置的设备的中文名
Kind: instance property of IDevice
Read only: true
设备的 token 加密后生成的固定值
Kind: instance property of IDevice
Read only: true
开发者平台配置的设备图标 一个图片的下载地址
Kind: instance property of IDevice
Read only: true
当前账户对设备的控制权限
Kind: instance property of IDevice
Read only: true
是否设置了进入插件使用密码
Kind: instance property of IDevice
Read only: true
父设备的 model
Kind: instance property of IDevice
Read only: true
是否在设备列表显示 0 -- 不显示
Kind: instance property of IDevice
Read only: true
获取设备时区,非实时加载,可能为空.如果需要自行获取最新设备时区值,请使用smarthome.getDeviceTimeZone(did)
Kind: instance property of IDevice
获取设备的 mac 地址
Kind: instance property of IDevice
Read only: true
获取当前wifi设备固件的版本
Kind: instance property of IDevice
Read only: true
获取 prop中保存的信息
Kind: instance property of IDevice
Read only: true
获取设备的 ip
Kind: instance property of IDevice
Read only: true
获取 wifi 信号强度
Kind: instance property of IDevice
Read only: true
获取连接 wifi 的名称
Kind: instance property of IDevice
Read only: true
获取连接 wifi 的mac 地址
Kind: instance property of IDevice
Read only: true
获取设备类型
Kind: instance property of IDevice
Read only: true
获取上次修改时间戳, 例如1532587811237
Kind: instance property of IDevice
Read only: true
本地设备还是远程设备, 0未知 1本地 2远程
Kind: instance property of IDevice
Read only: true
经纬度
Kind: instance property of IDevice
Read only: true
经纬度
Kind: instance property of IDevice
Read only: true
是否支持语音控制
Kind: instance property of IDevice
Read only: true
设备是否在线 true 在线
Kind: instance property of IDevice
Read only: true
重置标志
Kind: instance property of IDevice
Read only: true
是否是自己的设备
Kind: instance property of IDevice
Read only: true
是否是自己家庭的设备
Kind: instance property of IDevice
Read only: true
是否是别人分享的设备
Kind: instance property of IDevice
Read only: true
是否是Binded2的设备
Kind: instance property of IDevice
Read only: true
是否是别人分享的只读设备
Kind: instance property of IDevice
Read only: true
获得设备 wifi 操作对象
Kind: instance method of IDevice
获取子设备列表
Kind: instance method of IDevice
Example
import 'Device' from 'miot'
Device.getSubDevices()
.then(devices => {//get device list})获取虚拟设备 /home/virtualdevicectr
Kind: instance method of IDevice
获取小米BLE蓝牙控制类, 注意: 在 iOS 平台上, 如果没有指定peripheralUUID, 则须先执行Bluetooth.scan(), 扫描到与device.mac匹配的蓝牙设备之后才能 connect 成功, 否则将不能成功执行后来的所有操作.
Kind: instance method of IDevice
See: module:miot/Bluetooth
| Param | Type | Default | Description |
|---|---|---|---|
| peripheralUUID | string |
null |
- iOS平台上可以直接指定与设备 mac 匹配的peripheralUUID, android 平台不需要此参数 |
Example
const peripheralUUID4IOS = ...;
Device.getBluetoothLE(peripheralUUID4IOS).connect()
.then(ble=>{
ble....
})
.catch(error=>{
})上报日志,写入文件,在用户反馈时可以查看
Kind: instance method of IDevice
| Param | Type |
|---|---|
| log | string |
加载本设备相关的场景
Kind: instance method of IDevice
See: module:miot/service/scene
| Param | Type | Default |
|---|---|---|
| sceneType | * |
|
| opt | * |
|
加载定时场景
Kind: instance method of IDevice
See: module:miot/service/scene
| Param | Type | Default |
|---|---|---|
| opt | json |
|
创建场景
Kind: instance method of IDevice
See: module:miot/service/scene
| Param | Type | Default |
|---|---|---|
| sceneType | SceneType |
|
| opt | json |
|
创建定时场景
Kind: instance method of IDevice
See: module:miot/service/scene
| Param | Type | Default |
|---|---|---|
| opt | json |
|
Device事件集合
Kind: inner namespace of module.exports
Example
import {DeviceEvent} from 'miot'
...
class MyPage extends React.Component{
componentWillMount(){
this.subscription = DeviceEvent.deviceReceivedMessages.addListener((device, messages)=>{
if(!this.props.navigation.isFocused()){
return;
}
...
})
}
...
componentWillUnmount(){
...
this.subscription.remove()
}
...
}
...-
~DeviceEvent :
object
设备名称变更事件
Kind: event emitted by DeviceEvent
Since: 1.0.0
| Param | Type | Description |
|---|---|---|
| device | IDevice |
发生变更的设备 |
设备状态变更事件
Kind: event emitted by DeviceEvent
| Param | Type | Description |
|---|---|---|
| device | IDevice |
发生变更的设备 |
设备消息
Kind: event emitted by DeviceEvent
| Param | Type | Description |
|---|---|---|
| device | IDevice |
|
| messages | Map.<string, object> |
接收到的数据 |
| originData | array |
接收到的数据, [{key,time,value}] |
Example
import {Device, DeviceEvent} from 'miot'
let msgSubscription = null;
Device.getDeviceWifi().subscribeMessages("prop.power", "event.something").then(subcription=>{
msgSubscription = subcription;
});
...
const subscription = DeviceEvent.deviceReceivedMessages.addListener(
(device, messages)=>{
if(messages.has('prop.power')){
const power = messages.get('prop.power');
...
}
...
})
...
msgSubscription&&msgSubscription.remove();