Skip to content

Commit

Permalink
feat: generate bus and event topics and home ass.
Browse files Browse the repository at this point in the history
  • Loading branch information
ItsNotGoodName committed Dec 17, 2023
1 parent cf579e6 commit 73072ad
Show file tree
Hide file tree
Showing 22 changed files with 619 additions and 255 deletions.
8 changes: 7 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,17 @@ dev-assets:

# Gen

gen: gen-sqlc
gen: gen-sqlc gen-pubsub gen-bus

gen-sqlc:
sqlc generate

gen-pubsub:
sh ./scripts/generate-pubsub-events.sh ./internal/models/event.go

gen-bus:
go run ./scripts/generate-bus.go -input ./internal/models/event.go -output ./internal/core/bus.gen.go

# Database

db-inspect:
Expand Down
24 changes: 15 additions & 9 deletions cmd/ipcmanview/cmd_serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/ItsNotGoodName/ipcmanview/internal/core"
"github.com/ItsNotGoodName/ipcmanview/internal/dahua"
"github.com/ItsNotGoodName/ipcmanview/internal/dahuacore"
"github.com/ItsNotGoodName/ipcmanview/internal/hass"
"github.com/ItsNotGoodName/ipcmanview/internal/http"
"github.com/ItsNotGoodName/ipcmanview/internal/mqtt"
"github.com/ItsNotGoodName/ipcmanview/internal/webserver"
Expand All @@ -17,12 +18,14 @@ import (

type CmdServe struct {
Shared
HTTPHost string `env:"HTTP_HOST" help:"HTTP host to listen on."`
HTTPPort string `env:"HTTP_PORT" default:"8080" help:"HTTP port to listen on."`
MQTTAddress string `env:"MQTT_ADDRESS" help:"MQTT broker to publish events."`
MQTTPrefix string `env:"MQTT_PREFIX" default:"ipcmanview/" help:"MQTT broker topic prefix"`
MQTTUsername string `env:"MQTT_USERNAME" help:"MQTT broker username."`
MQTTPassword string `env:"MQTT_PASSWORD" help:"MQTT broker password."`
HTTPHost string `env:"HTTP_HOST" help:"HTTP host to listen on."`
HTTPPort string `env:"HTTP_PORT" default:"8080" help:"HTTP port to listen on."`
MQTTAddress string `env:"MQTT_ADDRESS" help:"MQTT broker to publish events."`
MQTTTopic mqtt.Topic `env:"MQTT_PREFIX" default:"ipcmanview" help:"MQTT broker topic."`
MQTTUsername string `env:"MQTT_USERNAME" help:"MQTT broker username."`
MQTTPassword string `env:"MQTT_PASSWORD" help:"MQTT broker password."`
MQTTHass bool `env:"MQTT_HASS" help:"Enable HomeAssistant MQTT discovery"`
MQTTHassTopic mqtt.Topic `env:"MQTT_HASS_TOPIC" default:"homeassistant" help:"HomeAssistant MQTT discovery topic"`
}

func (c *CmdServe) Run(ctx *Context) error {
Expand Down Expand Up @@ -71,9 +74,12 @@ func (c *CmdServe) Run(ctx *Context) error {

// MQTT
if c.MQTTAddress != "" {
mqttPublisher := mqtt.NewPublisher(c.MQTTPrefix, c.MQTTAddress, c.MQTTUsername, c.MQTTPassword)
mqttPublisher.Register(bus)
super.Add(mqttPublisher)
mqttConn := mqtt.NewConn(c.MQTTTopic, c.MQTTAddress, c.MQTTUsername, c.MQTTPassword)
mqtt.Register(mqttConn, bus)
super.Add(mqttConn)

hassConn := hass.NewConn(mqttConn, db, c.MQTTHassTopic)
super.Add(hassConn)
}

// HTTP Router
Expand Down
131 changes: 131 additions & 0 deletions internal/core/bus.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

125 changes: 0 additions & 125 deletions internal/core/bus.go

This file was deleted.

5 changes: 4 additions & 1 deletion internal/dahua/camera.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@ import (
"context"

"github.com/ItsNotGoodName/ipcmanview/internal/core"
"github.com/ItsNotGoodName/ipcmanview/internal/models"
"github.com/ItsNotGoodName/ipcmanview/internal/repo"
)

func DeleteCamera(ctx context.Context, db repo.DB, bus *core.Bus, id int64) error {
if err := db.DeleteDahuaCamera(ctx, id); err != nil {
return err
}
bus.DahuaCameraDeleted(id)
bus.EventDahuaCameraDeleted(models.EventDahuaCameraDeleted{
CameraID: id,
})
return nil
}
18 changes: 14 additions & 4 deletions internal/dahua/event_hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ func (e EventHooks) Connecting(ctx context.Context, cameraID int64) {
State: models.DahuaEventWorkerStateConnecting,
CreatedAt: types.NewTime(time.Now()),
}))
e.bus.DahuaEventWorkerConnecting(cameraID)
e.bus.EventDahuaEventWorkerConnecting(models.EventDahuaEventWorkerConnecting{
CameraID: cameraID,
})
}

func (e EventHooks) Connect(ctx context.Context, cameraID int64) {
Expand All @@ -47,7 +49,9 @@ func (e EventHooks) Connect(ctx context.Context, cameraID int64) {
State: models.DahuaEventWorkerStateConnected,
CreatedAt: types.NewTime(time.Now()),
}))
e.bus.DahuaEventWorkerConnect(cameraID)
e.bus.EventDahuaEventWorkerConnect(models.EventDahuaEventWorkerConnect{
CameraID: cameraID,
})
}

func (e EventHooks) Disconnect(cameraID int64, err error) {
Expand All @@ -57,7 +61,10 @@ func (e EventHooks) Disconnect(cameraID int64, err error) {
Error: repo.ErrorToNullString(err),
CreatedAt: types.NewTime(time.Now()),
}))
e.bus.DahuaEventWorkerDisconnect(cameraID, err)
e.bus.EventDahuaEventWorkerDisconnect(models.EventDahuaEventWorkerDisconnect{
CameraID: cameraID,
Error: err,
})
}

func (e EventHooks) Event(ctx context.Context, event models.DahuaEvent) {
Expand All @@ -83,5 +90,8 @@ func (e EventHooks) Event(ctx context.Context, event models.DahuaEvent) {
event.ID = id
}

e.bus.DahuaCameraEvent(ctx, event, eventRule)
e.bus.EventDahuaCameraEvent(models.EventDahuaCameraEvent{
Event: event,
EventRule: eventRule,
})
}
Loading

0 comments on commit 73072ad

Please sign in to comment.