Browse Source

Keep track of last message and camera frame

master
parent
commit
fd981d9475
2 changed files with 24 additions and 8 deletions
  1. +8
    -0
      camera.go
  2. +16
    -8
      client.go

+ 8
- 0
camera.go View File

@ -13,6 +13,7 @@ import (
// Camera is an ESP32 camera.
type Camera struct {
Entity
lastFrame time.Time
}
func newCamera(client *Client, entity *api.ListEntitiesCameraResponse) *Camera {
@ -49,6 +50,7 @@ func (entity *Camera) Image() (image.Image, error) {
out <- message.Data
if message.Done {
close(out)
entity.lastFrame = time.Now()
return
}
}
@ -95,6 +97,7 @@ func (entity *Camera) Stream() (<-chan *bytes.Buffer, error) {
if frame.Done {
out <- buffer
buffer = new(bytes.Buffer)
entity.lastFrame = time.Now()
}
case <-ticker.C:
@ -130,3 +133,8 @@ func (entity *Camera) ImageStream() (<-chan image.Image, error) {
return out, nil
}
// LastFrame returns the time of the last camera frame received.
func (entity *Camera) LastFrame() time.Time {
return entity.lastFrame
}

+ 16
- 8
client.go View File

@ -30,14 +30,15 @@ type Client struct {
// Clock returns the current time.
Clock func() time.Time
conn net.Conn
br *bufio.Reader
entities clientEntities
err error
in chan proto.Message
stop chan struct{}
waitMutex sync.RWMutex
wait map[uint64]chan proto.Message
conn net.Conn
br *bufio.Reader
entities clientEntities
err error
in chan proto.Message
stop chan struct{}
waitMutex sync.RWMutex
wait map[uint64]chan proto.Message
lastMessage time.Time
}
type clientEntities struct {
@ -171,6 +172,7 @@ func (c *Client) waitMessageTimeout(messageType uint64, timeout time.Duration) (
func (c *Client) readMessage() (err error) {
var message proto.Message
if message, err = api.ReadMessage(c.br); err == nil {
c.lastMessage = time.Now()
if !c.handleInternal(message) {
c.waitMutex.Lock()
in, waiting := c.wait[api.TypeOf(message)]
@ -339,6 +341,11 @@ func (c *Client) Close() error {
return err
}
// LastMessage returns the time of the last message received.
func (c *Client) LastMessage() time.Time {
return c.lastMessage
}
// DeviceInfo contains information about the ESPHome node.
type DeviceInfo struct {
UsesPassword bool
@ -360,6 +367,7 @@ type DeviceInfo struct {
// The model of the board. For example NodeMCU
Model string
// HasDeepSleep indicates the device has deep sleep mode enabled when idle.
HasDeepSleep bool
}


Loading…
Cancel
Save