commit f05dbe30f4e66918657dcbc27477af1c0212b1aa from: Oliver Lowe date: Tue Jan 03 03:10:49 2023 UTC dishy, cmd/dishy: initial package commit Just implementing the simplest request/responses: stow, unstow, reboot commit - ea4bde5f1831b651791caeaf373d7f6b0ad7708c commit + f05dbe30f4e66918657dcbc27477af1c0212b1aa blob - /dev/null blob + 147b0817f915ff2871fb23ca627da9371eb95400 (mode 644) --- /dev/null +++ cmd/dishy/dishy.go @@ -0,0 +1,43 @@ +package main + +import ( + "flag" + "log" + + "olowe.co/dishy" +) + +const usage = "usage: dishy [-a address] command" + +var aFlag = flag.String("a", dishy.DefaultDishyAddr, "dishy device IP address") + +func main() { + log.SetFlags(0) + log.SetPrefix("dishy:") + + flag.Parse() + + if len(flag.Args()) != 1 { + log.Fatal(usage) + } + cmd := flag.Args()[0] + addr := *aFlag + client, err := dishy.Dial(addr) + if err != nil { + log.Fatalf("dial %s: %v", addr, err) + } + + switch cmd { + default: + log.Fatalf("unknown command %s", cmd) + case "reboot": + err = client.Reboot() + case "stow": + err = client.Stow() + case "unstow": + err = client.Unstow() + } + if err != nil { + log.Fatalf("%s: %v", cmd, err) + } +} blob - /dev/null blob + 6893fbe340039216aea4a53d12a7662d90cc73a3 (mode 644) --- /dev/null +++ dishy.go @@ -0,0 +1,78 @@ +package dishy + +import ( + "context" + "time" + + "google.golang.org/grpc" + "olowe.co/dishy/device" +) + +//go:generate ./protoc.sh 127.0.0.1:9200 + +const ( + DefaultDishyAddr = "192.168.100.1:9200" + DefaultWifiAddr = "192.168.1.1:9000" +) + +// A Client is a high-level client to communicate with dishy over the network. +// A new Client must be created with Dial. +type Client struct { + // Timeout specifies a time limit for requests made by the + // client. A timeout of zero means no timeout. + Timeout time.Duration + dc device.DeviceClient + conn *grpc.ClientConn +} + +// Dial returns a new Client connected to the dishy device at addr. +// Most callers should specify DefaultDishyAddr. +func Dial(addr string) (*Client, error) { + conn, err := grpc.Dial(addr, grpc.WithInsecure()) + return &Client{ + conn: conn, + dc: device.NewDeviceClient(conn), + }, err +} + +func (c *Client) Unstow() error { + req := &device.Request{ + Request: &device.Request_DishStow{ + DishStow: &device.DishStowRequest{ + Unstow: true, + }, + }, + } + _, err := c.do(req) + return err +} + +func (c *Client) Stow() error { + req := &device.Request{ + Request: &device.Request_DishStow{ + DishStow: &device.DishStowRequest{ + Unstow: false, + }, + }, + } + _, err := c.do(req) + return err +} + +func (c *Client) Reboot() error { + req := &device.Request{ + Request: &device.Request_Reboot{ + Reboot: &device.RebootRequest{}, + }, + } + _, err := c.do(req) + return err +} + +func (c *Client) do(req *device.Request) (*device.Response, error) { + ctx := context.Background() + if c.Timeout > 0 { + ctx, _ = context.WithTimeout(context.Background(), c.Timeout) + } + return c.dc.Handle(ctx, req) +}