miservice

package module
v0.0.0-...-0edbd01 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 5, 2023 License: MIT Imports: 24 Imported by: 0

README

MiService

XiaoMi Cloud Service for mi.com This is a fork from https://github.com/yihong618/MiService and use chatgpt convert python to Go, then can to run some box.

Library

miservice:XiaoMi Cloud Service
  |
  |-- account.go:Account Srvice
  |     |
  |     |-- io.go:MiIO Service (sid=xiaomiio)
  |     |     |
  |     |     |-- iot.go:MIoT Service, Based on MiIO
  |     |
  |     |-- ai.go:MiAI Service (sid=micoapi)
  |     |
  |     |-- MiAPIService:(TODO)
  |-- command.go:MiIO Command Style Interface

Command Line

Usage: The following variables must be set:
           export MI_USER=<Username>
           export MI_PASS=<Password>
           export MI_DID=<Device ID|Name>

Get Props: micli <siid[-piid]>[,...]
           micli 1,1-2,1-3,1-4,2-1,2-2,3
Set Props: micli <siid[-piid]=[#]value>[,...]
           micli 2=#60,2-2=#false,3=test
Do Action: micli <siid[-piid]> <arg1|#NA> [...] 
           micli 2 #NA
           micli 5 Hello
           micli 5-4 Hello #1

Call MIoT: micli <cmd=prop/get|/prop/set|action> <params>
           micli action '{"did":"267090026","siid":5,"aiid":1,"in":["Hello"]}'

Call MiIO: micli /<uri> <data>
           micli /home/device_list '{"getVirtualModel":false,"getHuamiDevices":1}'

Devs List: micli list [name=full|name_keyword] [getVirtualModel=false|true] [getHuamiDevices=0|1]
           micli list Light true 0

MIoT Spec: micli spec [model_keyword|type_urn] [format=text|python|json]
           micli spec
           micli spec speaker
           micli spec xiaomi.wifispeaker.lx04
           micli spec urn:miot-spec-v2:device:speaker:0000A015:xiaomi-lx04:1

MIoT Decode: micli decode <ssecurity> <nonce> <data> [gzip]
1. 先设置账号
export MI_USER=<Username>
export MI_PASS=<Password>
2. 查询自己的设备
micli list

可以显示自己账号下的设备列表,包含名称、类型、DID、Token 等信息。

3. 设置 DID

为了后续操作,请设置 Device ID(来自上面这条命令的结果)。

export MI_DID=<Device ID|Name>
4. 查询设备的接口文档

查询设备的 MIoT 接口能力描述:

micli spec xiaomi.wifispeaker.lx04

其中分为属性获取、属性设置、动作调用三种描述。

5. 查询音量属性
micli 2-1

其中 2siid1piid(如果是 1 则可以省略),可从 spec 接口描述中查得。

6. 设置音量属性
micli 2=#60

siidpiid 规则同属性查询命令。注意 # 号的意思是整数类型,如果不带则默认是文本字符串类型,要根据接口描述文档来确定类型。

7. 动作调用:TTS 播报和执行文本

以下命令执行后小爱音箱会播报“您好”:

micli 5 您好

其中,5 为 siid,此处省略了 1aiid

以下命令执行后相当于直接对对音箱说“小爱同学,查询天气”是一个效果:

micli 5-4 查询天气 #1

其中 #1 表示设备语音回应,如果要执行默默关灯(不要音箱回应),可以如下:

micli 5-4 关灯 #0

Documentation

Index

Constants

View Source
const MIIO_SID = "xiaomiio"
View Source
const UA = "APP/com.xiaomi.mihome APPV/6.0.103 iosPassportSDK/3.9.0 iOS/14.4 miHSTS"

Variables

This section is empty.

Functions

func IOCommand

func IOCommand(service *IOService, did string, text string, prefix string) (interface{}, error)

func IOCommandHelp

func IOCommandHelp(did string, prefix string) string

Types

type AIService

type AIService struct {
	// contains filtered or unexported fields
}

func NewAIService

func NewAIService(account *Account) *AIService

func (*AIService) DeviceList

func (mnas *AIService) DeviceList(master int) (devices []DeviceData, err error)

func (*AIService) PlayByUrl

func (mnas *AIService) PlayByUrl(deviceId, url string) (map[string]interface{}, error)

func (*AIService) PlayerGetStatus

func (mnas *AIService) PlayerGetStatus(deviceId string) (
	*PlayerStatus, error)

func (*AIService) PlayerPause

func (mnas *AIService) PlayerPause(deviceId string) (map[string]interface{}, error)

func (*AIService) PlayerPlay

func (mnas *AIService) PlayerPlay(deviceId string) (map[string]interface{}, error)

func (*AIService) PlayerSetVolume

func (mnas *AIService) PlayerSetVolume(deviceId string, volume int) (map[string]interface{}, error)

func (*AIService) Request

func (mnas *AIService) Request(uri string, data url.Values, out any) error

func (*AIService) SendMessage

func (mnas *AIService) SendMessage(devices []map[string]interface{}, devno int, message string, volume *int) (bool, error)

func (*AIService) TextToSpeech

func (mnas *AIService) TextToSpeech(deviceId, text string) (map[string]interface{}, error)

func (*AIService) UbusRequest

func (mnas *AIService) UbusRequest(deviceId, method, path string, message map[string]interface{}, res any) error

type Account

type Account struct {
	// contains filtered or unexported fields
}

func NewAccount

func NewAccount(username string, password string, tokenStore TokenStore) *Account

func (*Account) Login

func (ma *Account) Login(sid string) error

sid: service id, like "xiaomiio", "micoapi", "mina"

func (*Account) NewRequest

func (ma *Account) NewRequest(sid, u string, data url.Values, cb DataCb, headers http.Header) *http.Request

func (*Account) Request

func (ma *Account) Request(sid, u string, data url.Values, cb DataCb, headers http.Header, reLogin bool, output any) error

type DataCb

type DataCb func(tokens *Tokens, cookie map[string]string) url.Values

type DeviceData

type DeviceData struct {
	DeviceID     string `json:"deviceID"`
	SerialNumber string `json:"serialNumber"`
	Name         string `json:"name"`
	Alias        string `json:"alias"`
	Current      bool   `json:"current"`
	Presence     string `json:"presence"`
	Address      string `json:"address"`
	MiotDID      string `json:"miotDID"`
	Hardware     string `json:"hardware"`
	RomVersion   string `json:"romVersion"`
	Capabilities struct {
		ChinaMobileIms      int `json:"china_mobile_ims"`
		SchoolTimetable     int `json:"school_timetable"`
		NightMode           int `json:"night_mode"`
		UserNickName        int `json:"user_nick_name"`
		PlayerPauseTimer    int `json:"player_pause_timer"`
		DialogH5            int `json:"dialog_h5"`
		ChildMode2          int `json:"child_mode_2"`
		ReportTimes         int `json:"report_times"`
		AlarmVolume         int `json:"alarm_volume"`
		AiInstruction       int `json:"ai_instruction"`
		ClassifiedAlarm     int `json:"classified_alarm"`
		AiProtocol30        int `json:"ai_protocol_3_0"`
		NightModeDetail     int `json:"night_mode_detail"`
		ChildMode           int `json:"child_mode"`
		BabySchedule        int `json:"baby_schedule"`
		ToneSetting         int `json:"tone_setting"`
		Earthquake          int `json:"earthquake"`
		AlarmRepeatOptionV2 int `json:"alarm_repeat_option_v2"`
		XiaomiVoip          int `json:"xiaomi_voip"`
		NearbyWakeupCloud   int `json:"nearby_wakeup_cloud"`
		FamilyVoice         int `json:"family_voice"`
		BluetoothOptionV2   int `json:"bluetooth_option_v2"`
		Yunduantts          int `json:"yunduantts"`
		MicoCurrent         int `json:"mico_current"`
		VoipUsedTime        int `json:"voip_used_time"`
	} `json:"capabilities"`
	RemoteCtrlType  string `json:"remoteCtrlType"`
	DeviceSNProfile string `json:"deviceSNProfile"`
	DeviceProfile   string `json:"deviceProfile"`
	BrokerEndpoint  string `json:"brokerEndpoint"`
	BrokerIndex     int    `json:"brokerIndex"`
	Mac             string `json:"mac"`
	Ssid            string `json:"ssid"`
}

type DeviceInfo

type DeviceInfo struct {
	Name  string `json:"name"`
	Model string `json:"model"`
	Did   string `json:"did"`
	Token string `json:"token"`
}

type Devices

type Devices struct {
	Code    int          `json:"code"`
	Message string       `json:"message"`
	Data    []DeviceData `json:"data"`
}

type DummyTokenStore

type DummyTokenStore struct {
	// contains filtered or unexported fields
}

func (*DummyTokenStore) LoadToken

func (d *DummyTokenStore) LoadToken() (*Tokens, error)

func (*DummyTokenStore) SaveToken

func (d *DummyTokenStore) SaveToken(tokens *Tokens) error

type FileTokenStore

type FileTokenStore struct {
	// contains filtered or unexported fields
}

func NewTokenStore

func NewTokenStore(tokenPath string) *FileTokenStore

func (*FileTokenStore) LoadToken

func (mts *FileTokenStore) LoadToken() (*Tokens, error)

func (*FileTokenStore) SaveToken

func (mts *FileTokenStore) SaveToken(tokens *Tokens) error

type IOService

type IOService struct {
	// contains filtered or unexported fields
}

func NewIOService

func NewIOService(account *Account, region *string) *IOService

func (*IOService) DeviceList

func (s *IOService) DeviceList(getVirtualModel bool, getHuamiDevices int) (devices []DeviceInfo, err error)

func (*IOService) HomeGetProp

func (s *IOService) HomeGetProp(did, prop string) (interface{}, error)

func (*IOService) HomeGetProps

func (s *IOService) HomeGetProps(did string, props []string) (map[string]interface{}, error)

func (*IOService) HomeRequest

func (s *IOService) HomeRequest(did, method string, params interface{}) (map[string]interface{}, error)

func (*IOService) HomeSetProp

func (s *IOService) HomeSetProp(did, prop string, value interface{}) (int, error)

func (*IOService) HomeSetProps

func (s *IOService) HomeSetProps(did string, props map[string]interface{}) (map[string]int, error)

func (*IOService) IotDecode

func (s *IOService) IotDecode(ssecurity string, nonce string, data string, gzip bool) (interface{}, error)

func (*IOService) IotSpec

func (s *IOService) IotSpec(kind string) ([]string, error)

func (*IOService) MiotAction

func (s *IOService) MiotAction(did string, iid []int, args []interface{}) (int, error)

func (*IOService) MiotGetProp

func (s *IOService) MiotGetProp(did string, iid Iid) (interface{}, error)

func (*IOService) MiotGetProps

func (s *IOService) MiotGetProps(did string, iids []Iid) ([]interface{}, error)

func (*IOService) MiotRequest

func (s *IOService) MiotRequest(cmd string, params interface{}) (map[string]interface{}, error)

func (*IOService) MiotSetProp

func (s *IOService) MiotSetProp(did string, iid Iid, value interface{}) (int, error)

func (*IOService) MiotSetProps

func (s *IOService) MiotSetProps(did string, props map[Iid]interface{}) ([]int, error)

func (*IOService) Request

func (s *IOService) Request(uri string, data map[string]interface{}) (map[string]interface{}, error)

type Iid

type Iid struct {
	Siid int `json:"siid"`
	Piid int `json:"piid"`
}

type MiotSpecInstances

type MiotSpecInstances struct {
	Instances []struct {
		Status  string `json:"status"`
		Model   string `json:"model"`
		Version int    `json:"version"`
		Type    string `json:"type"`
		Ts      int    `json:"ts"`
	} `json:"instances"`
}

type PlayerStatus

type PlayerStatus struct {
	Code    int    `json:"code"`
	Message string `json:"message"`
	Data    struct {
		Code int    `json:"code"`
		Info string `json:"info"`
	} `json:"data"`
}

type SidToken

type SidToken struct {
	Ssecurity    string `json:"ssecurity"`
	ServiceToken string `json:"service_token"`
}

type TokenStore

type TokenStore interface {
	LoadToken() (*Tokens, error)
	SaveToken(tokens *Tokens) error
}

type Tokens

type Tokens struct {
	UserName  string              `json:"user_name"`
	DeviceId  string              `json:"device_id"`
	UserId    string              `json:"user_id"`
	PassToken string              `json:"pass_token"`
	Sids      map[string]SidToken `json:"sids"`
}

func NewTokens

func NewTokens() *Tokens

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL