miio

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jun 3, 2022 License: Apache-2.0 Imports: 5 Imported by: 0

README

miio.go

Go implementation for MiIO protocol. inpsired by miio.dart

Install

go get github.com/icepie/miio.go

Usage

for get device token and device id, you can try micloud

device := miio.New("192.168.1.12").
	SetToken("ffffffffffffffffffffffffffffffff").
	SetDid("did") // some device must use the true device id

// get device info
info, err := device.Info()
if err != nil {
	panic(err)
}

fmt.Printf("%s\n", info)

// for get siid and piid, see https://home.miot-spec.com/
// get properties
getProps, err := device.GetProps(miio.PropParam{
	Siid: 5,
	Piid: 1,
}, miio.PropParam{
	Siid: 5,
	Piid: 2,
})

if err != nil {
	panic(err)
}

fmt.Printf("%s\n", getProps)

// set properties
setPropsParams := []miio.PropParam{
	{
		Siid:  5,
		Piid:  1,
		Value: true,
	},
	{
		Siid:  5,
		Piid:  2,
		Value: 0,
	},
}

setProps, err := device.SetProps(setPropsParams...)
if err != nil {
	panic(err)
}

// Do action
action, err := device.DoAction(miio.ActionParam{
	Siid: 2,
	Aiid: 1,
	// In:   []any{0, "test"}, if need to send extra data
})

if err != nil {
	panic(err)
}

fmt.Printf("%s\n", action)

// some old devices can not use the standard way to control...
// https://github.com/icepie/miio.go/issues/1
// you can try to send raw data, like:
getProps, err := device.Send("get_prop", []interface{}{"power", "mode", "fan_level", "ver_swing"})
setProps, err := device.Send("set_mode", []interface{}{"cool"})


// or you can try: https://github.com/icepie/micloud

Protocol

0                   1                   2                   3
 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Magic Number = 0x2131         | Packet Length (incl. header)  |
|-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-|
| Unknown                                                       |
|-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-|
| Device ID ("did")                                             |
|-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-|
| Stamp                                                         |
|-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-|
| MD5 Checksum                                                  |
| ... or Device Token in response to the "Hello" packet         |
|-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-|
| Optional variable-sized payload (encrypted)                   |
|...............................................................|


Packet Length: 16 bits unsigned int
    Length in bytes of the whole packet, including header(0x20 bytes).

Unknown: 32 bits
    This value is always 0.
    0xFFFFFFFF in "Hello" packet.

Device ID: 32 bits
    Unique number. Possibly derived from the MAC address.
    0xFFFFFFFF in "Hello" packet.

Stamp: 32 bit unsigned int
    Continously increasing counter.
    Number of seconds since device startup.

MD5 Checksum:
    Calculated for the whole packet including the MD5 field itself,
    which must be initialized with token.

    In "Hello" packet,
    this field contains the 128-bit 0xFF.

    In the response to the first "Hello" packet,
    this field contains the 128-bit device token.

Optional variable-sized payload:
    Payload encrypted with AES-128-CBC (PKCS#7 padding).

        Key = MD5(Token)
        IV  = MD5(Key + Token)

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ActionParam

type ActionParam struct {
	Did  string `json:"did"`
	Siid int    `json:"siid"`
	Aiid int    `json:"aiid"`
	In   []any  `json:"in,omitempty"`
	Out  []any  `json:"out,omitempty"`
}

func (*ActionParam) SetDid

func (a *ActionParam) SetDid(did string) *ActionParam

type Client

type Client struct {
	sync.Mutex
	proto.Conn

	Did string
	// contains filtered or unexported fields
}

Client is device client that extends protocol connection.

func New

func New(addr string) *Client

New creates new device client.

Example:

New("192.168.0.3")

func (*Client) Close

func (c *Client) Close() error

close connection.

func (*Client) ConfigRouter

func (c *Client) ConfigRouter(ssid string, passwd string, uid string) ([]byte, error)

ConfigRouter configures wifi network on device.

func (*Client) DoAction

func (c *Client) DoAction(param ActionParam) ([]byte, error)

Action execute device action.

func (*Client) GetProps

func (c *Client) GetProps(params ...PropParam) ([]byte, error)

GetProperties gets device propetriest.

func (*Client) Info

func (c *Client) Info() ([]byte, error)

func (*Client) OTA

func (c *Client) OTA(url string, fileMD5 string) ([]byte, error)

OTA updates the device.

func (*Client) OTAProgress

func (c *Client) OTAProgress() ([]byte, error)

OTAProgress requests OTA update progress.

func (*Client) OTAState

func (c *Client) OTAState() ([]byte, error)

OTAState requests available update for device.

func (*Client) Send

func (c *Client) Send(method string, params interface{}) ([]byte, error)

Send sends request to device.

func (*Client) SetDid

func (c *Client) SetDid(did string) *Client

Set Device ID for device.

func (*Client) SetProps

func (c *Client) SetProps(params ...PropParam) ([]byte, error)

SetProperties sets device propetriest.

func (*Client) SetToken

func (c *Client) SetToken(token string) *Client

Set Token for device.

type Info

type Info struct {
	Life      int    `json:"life"`
	Model     string `json:"model"`
	Token     string `json:"token"`
	Ipflag    int    `json:"ipflag"`
	MiioVer   string `json:"miio_ver"`
	HwVer     string `json:"hw_ver"`
	Mmfree    int    `json:"mmfree"`
	FwVer     string `json:"fw_ver"`
	Mac       string `json:"mac"`
	WifiFwVer string `json:"wifi_fw_ver"`
	Ap        struct {
		Ssid    string `json:"ssid"`
		Bssid   string `json:"bssid"`
		Rssi    int    `json:"rssi"`
		Primary int    `json:"primary"`
	} `json:"ap"`
	Netif struct {
		LocalIP string `json:"localIp"`
		Mask    string `json:"mask"`
		Gw      string `json:"gw"`
	} `json:"netif"`
	PartnerID string `json:"partner_id"`
}

type PropParam

type PropParam struct {
	Did   string `json:"did"`
	Siid  int    `json:"siid"`
	Piid  int    `json:"piid"`
	Value any    `json:"value,omitempty"`
}

func (*PropParam) SetDid

func (p *PropParam) SetDid(did string) *PropParam

type PropParams

type PropParams []PropParam

type PropParamsReq

type PropParamsReq struct {
	Params PropParams `json:"params"`
	Method string     `json:"method,omitempty"`
}

type PropRet

type PropRet struct {
	PropParam
	Code    int `json:"code"`
	ExeTime int `json:"exe_time"`
}

type PropRets

type PropRets []PropRet

type Ret

type Ret struct {
	ID    int `json:"id"`
	Error struct {
		Code    int    `json:"code"`
		Message string `json:"message"`
	} `json:"error"`
	Result  any `json:"result"`
	ExeTime int `json:"exe_time"`
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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