-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
265 lines (219 loc) · 6.6 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
// Copyright (c) 2024 Joshua Rich <[email protected]>
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
// Package cameraapp demonstrates an app that can send images from a connected
// webcam to Home Assistant. It exposes the camera entity (through which images
// are published) as well as start and stop button entities for
// starting/stopping the camera respectively.
//
// It uses the excellent https://github.com/blackjack/webcam package for camera
// access.
//
// Unfortunately, this example app only runs on Linux.
package cameraapp
import (
"context"
"errors"
"fmt"
"log/slog"
"runtime"
"slices"
"github.com/blackjack/webcam"
"github.com/eclipse/paho.golang/paho"
mqtthass "github.com/joshuar/go-hass-anything/v12/pkg/hass"
mqttapi "github.com/joshuar/go-hass-anything/v12/pkg/mqtt"
)
const (
appName = "Go Hass Anything Camera Example App"
appID = "mqtt_camera_example"
)
// Some defaults for the device file, formats and image size.
var (
deviceFile = "/dev/video1"
preferredFmts = []string{"Motion-JPEG"}
defaultHeight = 640
defaultWidth = 480
)
var ErrUnsupportedOS = errors.New("example camera app only runs on Linux")
// CameraApp is our struct that represents an app to Go Hass Anything.
type CameraApp struct {
camera *webcam.Webcam
images *mqtthass.CameraEntity
startButton *mqtthass.ButtonEntity
stopButton *mqtthass.ButtonEntity
msgCh chan *mqttapi.Msg
}
// New sets up our example app. It creates entities for the camera images and
// start and stop buttons.
func New(ctx context.Context) (*CameraApp, error) {
if runtime.GOOS != "linux" {
return nil, ErrUnsupportedOS
}
app := &CameraApp{
msgCh: make(chan *mqttapi.Msg),
}
app.images = mqtthass.NewCameraEntity().
WithDetails(
mqtthass.App(appName),
mqtthass.Name("Webcam"),
mqtthass.ID("webcam"),
mqtthass.DeviceInfo(newDevice()),
)
app.startButton = mqtthass.NewButtonEntity().
WithDetails(
mqtthass.App(appName),
mqtthass.Name("Start Webcam"),
mqtthass.ID("start_webcam"),
mqtthass.DeviceInfo(newDevice()),
mqtthass.Icon("mdi:play"),
).
WithCommand(
mqtthass.CommandCallback(func(_ *paho.Publish) {
// Open the camera device.
camera, err := openCamera(deviceFile)
if err != nil {
slog.Error("Could not open camera device.",
slog.Any("error", err))
return
}
app.camera = camera
slog.Info("Start recording webcam.")
go publishImages(camera, app.images.Topic, app.msgCh)
}),
)
app.stopButton = mqtthass.NewButtonEntity().
WithDetails(
mqtthass.App(appName),
mqtthass.Name("Stop Webcam"),
mqtthass.ID("stop_webcam"),
mqtthass.DeviceInfo(newDevice()),
mqtthass.Icon("mdi:stop"),
).
WithCommand(
mqtthass.CommandCallback(func(_ *paho.Publish) {
if err := app.camera.StopStreaming(); err != nil {
slog.Error("Stop streaming failed.", slog.Any("error", err))
}
if err := app.camera.Close(); err != nil {
slog.Error("Close camera failed.", slog.Any("error", err))
}
slog.Info("Stop recording webcam.")
}),
)
go func() {
defer close(app.msgCh)
<-ctx.Done()
}()
return app, nil
}
//revive:disable:unused-receiver
func (a *CameraApp) Name() string {
return appName
}
//nolint:mnd
func (a *CameraApp) Configuration() []*mqttapi.Msg {
configs := make([]*mqttapi.Msg, 0, 3)
cameraCfg, err := a.images.MarshalConfig()
if err != nil {
slog.Error("Could not marshal camera entity config.",
slog.Any("error", err))
} else {
configs = append(configs, cameraCfg)
}
startButtonCfg, err := a.startButton.MarshalConfig()
if err != nil {
slog.Error("Could not marshal start button entity config.",
slog.Any("error", err))
} else {
configs = append(configs, startButtonCfg)
}
stopButtonCfg, err := a.stopButton.MarshalConfig()
if err != nil {
slog.Error("Could not marshal start button entity config.",
slog.Any("error", err))
} else {
configs = append(configs, stopButtonCfg)
}
return configs
}
// States is unused in this example app. This could be used to take an image
// snapshot via the webcam or expose other camera information as entities...
func (a *CameraApp) States() []*mqttapi.Msg { return nil }
//nolint:mnd
func (a *CameraApp) Subscriptions() []*mqttapi.Subscription {
subs := make([]*mqttapi.Subscription, 0, 2)
startButtonSub, err := a.startButton.MarshalSubscription()
if err != nil {
slog.Warn("Unable to marshal start button subscription.",
slog.Any("error", err))
} else {
subs = append(subs, startButtonSub)
}
stopButtonSub, err := a.stopButton.MarshalSubscription()
if err != nil {
slog.Warn("Unable to marshal stop button subscription.",
slog.Any("error", err))
} else {
subs = append(subs, stopButtonSub)
}
return subs
}
// Update is unused, there is no app data to update.
func (a *CameraApp) Update(_ context.Context) error { return nil }
// Our channel on which we send camera images as MQTT messages.
func (a *CameraApp) MsgCh() chan *mqttapi.Msg {
return a.msgCh
}
// openCamera opens the camera device and ensures that it has a preferred image
// format, framerate and dimensions.
func openCamera(cameraDevice string) (*webcam.Webcam, error) {
cam, err := webcam.Open(cameraDevice)
if err != nil {
return nil, fmt.Errorf("could not open camera %s: %w", cameraDevice, err)
}
// select pixel format
var preferredFormat webcam.PixelFormat
for format, desc := range cam.GetSupportedFormats() {
if slices.Contains(preferredFmts, desc) {
preferredFormat = format
break
}
}
if preferredFormat == 0 {
return nil, errors.New("could not determine an appropriate format")
}
_, _, _, err = cam.SetImageFormat(preferredFormat, uint32(defaultWidth), uint32(defaultHeight))
if err != nil {
return nil, fmt.Errorf("could not set camera parameters: %w", err)
}
return cam, nil
}
// publishImages loops over the received frames from the camera and wraps them
// as a MQTT message to be sent back on the bus.
func publishImages(cam *webcam.Webcam, topic string, msgCh chan *mqttapi.Msg) {
if err := cam.StartStreaming(); err != nil {
slog.Error("Could not start recording", slog.Any("error", err))
return
}
for {
err := cam.WaitForFrame(uint32(5))
if err != nil && errors.As(err, &webcam.Timeout{}) {
continue
}
frame, err := cam.ReadFrame()
if len(frame) == 0 || err != nil {
break
}
msgCh <- mqttapi.NewMsg(topic, frame)
}
}
func newDevice() *mqtthass.Device {
return &mqtthass.Device{
Name: appName,
Identifiers: []string{appID},
URL: "https://github.com/joshuar/go-hass-anything",
Manufacturer: "go-hass-anything",
Model: appID,
}
}