Skip to content

01 系统服务模块

Xiaomi-Yule edited this page Jul 5, 2021 · 3 revisions

miot/Service

Service 模块提供的能力主要包括米家服务端及米家云平台提供的服务能力 能力主要包括: 账号管理(Account.js) 房间管理(room.js) 智能场景(scene.js) 云服务(smarthome.js) Spec协议(spec.js) 云存储(storage.js)

Export: public
Doc_name: 系统服务模块
Doc_index: 1
Doc_directory: service
Example

import {Service} from 'miot'

Service.getServerName().then(res=>{...})
Service.getUTCTimeFromServer().then(...)

Service.smarthome.reportGPSInfo(...).then(...)

Service.account.ID
Serivce.account.nickName
Service.account.avatar
Service.account.load().then(account=>{})

Service.scene.loadTimerScenes(...).then(scenes=>{})
Service.security.loadSecureKeys(...).then(keys=>{
...
})

Service.storage.getUserConfigs(key).then()

miot/Service.getServiceTokenWithSid(sid) ⇒ Promise

传入域名返回 serverToken 等信息,目前只支持小爱音箱的域名 Android从SDK-10039开始支持该接口

Kind: static method of miot/Service

Param Type Description
sid string 域名,类似"xxx.xiaomi.com"

miot/Service.revokePrivacyLicense() ⇒ [ 'Promise' ].<Object>

since 10042 撤销隐私授权,插件调用该接口后需要主动调用退出插件

Kind: static method of miot/Service
Returns: [ 'Promise' ].<Object> - 成功时返回:{code:0,data:true}; 失败时返回:{code:-1,message:'invalid device'} ,或 {code:-2,message:'xxxxx'}
Example

Service.revokePrivacyLicense()
       .then(res=>{
         console.log(JSON.stringify(res));
         if( res.code ===0){
           console.log('success');
         }
       }).catch(err=>{
          console.log(JSON.stringify(err));
       });

miot/Service.deleteDevice() ⇒ [ 'Promise' ].<Object>

since 10042 删除设备,插件调用该接口后需要主动调用退出插件

Kind: static method of miot/Service
Returns: [ 'Promise' ].<Object> - 成功时返回:{code:0,data:true}; 失败时返回:{code:-1,message:'invalid device'} ,或 {code:-2,message:'xxxxx'}
Example

Service.deleteDevice()
       .then(res=>{
         console.log(JSON.stringify(res));
         if( res.code ===0){
           console.log('success');
         }
       }).catch(err=>{
          console.log(JSON.stringify(err));
       });

miot/Service.applyForDeviceIDAndToken(model, mac) ⇒ Promise

某设备向服务器申请did和token Android暂不支持此方法

Kind: static method of miot/Service
Returns: Promise - resolve({res,did,token})

Param Type Description
model * 设备的model
mac * 设备的mac地址

miot/Service~smarthome

设备相关 API

Kind: inner property of miot/Service
See: module:miot/service/smarthome


miot/Service~miotcamera

摄像机相关 API

Kind: inner property of miot/Service
See: module:miot/service/miotcamera


miot/Service~ircontroller

红外 相关 API

Kind: inner property of miot/Service
See: module:miot/service/ircontroller


miot/Service~account : IAccount

当前的用户信息

Kind: inner property of miot/Service
See: module:miot/Account


miot/Service~scene

场景 API 的调用

Kind: inner property of miot/Service
See: module:miot/service/scene


miot/Service~security

安全相关设置操作

Kind: inner property of miot/Service
See: module:miot/service/security


miot/Service~storage

用户存储操作, userProfile

Kind: inner property of miot/Service
See: module:miot/service/storage


miot/Service~spec

spec 的请求

Kind: inner property of miot/Service
See: module:miot/service/spec


miot/Service~callSmartHomeAPI(api, params)

通用的请求米家后台接口的方法,与米家服务器交互。 不同设备开放的接口请参照与米家后台对接时提供的文档或说明,以后台给出的信息为准。 米家客户端只封装透传网络请求,无法对接口调用结果解释,有问题请直接联系项目对接后台人员或 PM。

想使用某个接口之前,先检查 SDK 是否已经收录,可在 miot-sdk/service/apiRepo.js 文件中查阅。 如果 SDK 暂时没有收录,可通过 issue 提出申请,提供接口的相关信息。

Kind: inner method of miot/Service
Since: 10024

Param Type Description
api string 接口地址,比如'/location/set'
params object 传入参数,根据和米家后台商议的数据格式来传入,比如{ did: 'xxxx', pid: 'xxxx' }

miot/Service~callSmartHomeCameraAPI(api, subDomain, post, params)

专用摄像头相关接口请求 api in miot-sdk/service/apiRepo.js subDomain in miot-sdk/service/cameraSubDomain.js

Kind: inner method of miot/Service
Since: 10035

Param Type Description
api string 接口地址
subDomain string subDomain
post bool 是否POST方法
params object 传入参数

miot/Service~callSmartHomeCameraAPI(api, subDomain, post, params)

专用摄像头相关接口请求 api in miot-sdk/service/apiRepo.js subDomain in miot-sdk/service/cameraSubDomain.js

Kind: inner method of miot/Service
Since: 10044

Param Type Description
api string 接口地址
subDomain string subDomain
post bool 是否POST方法
params string BigJSON.strinify(object);

miot/Service~callSmartHomeCameraAPI(host, path, method, params, needDevice) ⇒ [ 'Promise' ].<object>

小爱音箱相关接口请求,注意此请求传的是一个对象,里面部分对象有默认值,可不传

Kind: inner method of miot/Service
Returns: [ 'Promise' ].<object> - 透传接口,直接返回服务端返回的值 path, method = 0, params, needDevice = 1
Since: 10041

Param Type Description
host string 请求的host,取值normal,hd,profile,lbs,skillstore,aifile,ai,aitrain,grayupgrade,homealbum。表示的host分别如下... { "normal": "https://api2.mina.mi.com", "hd": "https://hd.mina.mi.com", "profile": "https://userprofile.mina.mi.com", "lbs": "https://lbs.mina.mi.com", "skillstore": "https://skillstore.mina.mi.com", "aifile": "https://file.ai.xiaomi.com", "ai": "https://api.ai.xiaomi.com", "aitrain": "https://i.ai.mi.com/mico", "grayupgrade": "https://api.miwifi.com/rs/grayupgrade/v2/micoiOS", "homealbum": "https://display.api.mina.mi.com" }
path string 请求的路径,比如"/device_profile/conversation"
method number 默认为0(表示get方法),1表示post方法,2表示put方法
params object 请求的参数,比如{limit:20}
needDevice bool cookie中是否需要带上deviceId,默认为true

miot/Service~getServerName() ⇒ [ 'Promise' ].<{countryName:"", countryCode:"", serverCode:""}>

获取 米家 App 设置的地区和服务器信息 Android上返回的countryCode为大写,iOS上为小写,建议使用时在拿到countryCode后调用一下toLowerCase方法,都统一成小写

Kind: inner method of miot/Service


miot/Service~getTimeZoneOfServer()

获取服务器所在时区

Kind: inner method of miot/Service


miot/Service~getUTCFromServer() ⇒ [ 'Promise' ].<long>

从米家服务器获取当前UTC时间戳(会发送网络请求)

Kind: inner method of miot/Service


miot/Service~callSpecificAPI(url, method, params) ⇒ Promise

调用当前手机设备的网关http服务 只封装透传网络请求,无法对接口调用结果解释,有问题请直接联系项目对接后台人员或 PM。

Kind: inner method of miot/Service
Returns: Promise - 成功时:返回网络请求的结果对应字符串, 相当于:response.body().string() 失败时:{"code":xxx, "message":"xxx" }
Since: 10031

Param Type Description
url string url
method string 如 'get', 'post'等 不区分大小写 暂时只支持 get 和 post
params object 传入参数,比如{ did: 'xxxx', pid: 'xxxx','allow_private_certificates':true/false };allow_private_certificates是10056新增加的参数(10055及以前的版本该参数不生效),传true表明该请求使用小米路由器私有证书,默认为false;

Clone this wiki locally