Skip to content
This repository has been archived by the owner on Jun 27, 2021. It is now read-only.

Commit

Permalink
Close connection when executing command
Browse files Browse the repository at this point in the history
  • Loading branch information
vblinden committed Feb 14, 2021
1 parent d32646b commit 9722675
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 3 deletions.
7 changes: 4 additions & 3 deletions device.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,13 @@ func (d Device) Power() error {
}

func (d Device) PowerOn() error {
_, err := d.executeCommand("set_power", "on")
_, err := d.executeCommand("set_power", "on", "sudden", 0)

return err
}

func (d Device) PowerOff() error {
_, err := d.executeCommand("set_power", "off")
_, err := d.executeCommand("set_power", "off", "sudden", 0)

return err
}
Expand Down Expand Up @@ -123,7 +123,6 @@ func (d *Device) execute(cmd *Command) (*CommandResult, error) {
return nil, fmt.Errorf("cannot open connection to %s. %s", d.Address, err)
}

time.Sleep(time.Second)
conn.SetReadDeadline(time.Now().Add(timeout))

//write request/command
Expand All @@ -138,6 +137,8 @@ func (d *Device) execute(cmd *Command) (*CommandResult, error) {
return nil, fmt.Errorf("cannot read command result %s", err)
}

defer conn.Close()

var rs CommandResult
err = json.Unmarshal([]byte(res), &rs)

Expand Down
2 changes: 2 additions & 0 deletions yeelight.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ func Discover() (*Device, error) {
rsBuf := make([]byte, 1024)
size, _, err := socket.ReadFromUDP(rsBuf)

defer socket.Close()

if err != nil {
return nil, errors.New("no devices found")
}
Expand Down

0 comments on commit 9722675

Please # to comment.