forked from mycontroller-org/esphome_api
/
main.go
62 lines (51 loc) · 1.04 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package main
import (
"bytes"
"fmt"
"io/fs"
"io/ioutil"
"log"
"time"
"github.com/rockavoldy/esphome_api/api"
examples "github.com/rockavoldy/esphome_api/examples"
"google.golang.org/protobuf/proto"
)
func main() {
client, err := examples.GetClient(handleFuncImpl)
if err != nil {
log.Fatalln(err)
}
defer client.Close()
// subscribe state changes
client.Send(&api.SubscribeStatesRequest{})
// wait for a second
<-time.After(1 * time.Second)
// take a picture
client.Send(&api.CameraImageRequest{Single: true})
// wait 10 seconds
<-time.After(3 * time.Second)
// if image received, convert it to jpeg
if received {
err = ioutil.WriteFile("camera_image.jpeg", buffer.Bytes(), fs.ModePerm)
if err != nil {
fmt.Println(err)
}
}
}
var (
buffer = new(bytes.Buffer)
received = false
)
func handleFuncImpl(msg proto.Message) {
switch msg := msg.(type) {
case *api.CameraImageResponse:
if !received {
buffer.Write(msg.Data)
if msg.Done {
received = true
fmt.Println("Image received")
}
}
default:
}
}