Skip to content

Commit

Permalink
Add database functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
Travis Ralston committed Sep 18, 2023
1 parent 3e89dd2 commit ebc7f93
Show file tree
Hide file tree
Showing 6 changed files with 116 additions and 2 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
/bin
/pkg
/logs
/controller.db

# Generated files
assets.bin.go
Expand Down
8 changes: 8 additions & 0 deletions cmd/controller/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/ents-source/door-control/api"
"github.com/ents-source/door-control/api/auth"
"github.com/ents-source/door-control/assets"
"github.com/ents-source/door-control/db"
"github.com/ents-source/door-control/doors"
"github.com/kelseyhightower/envconfig"
)
Expand All @@ -36,6 +37,8 @@ type config struct {
AmpCategoryId int `envconfig:"amp_category_id"`
AmpBufferDays int `envconfig:"amp_buffer_days" default:"3"`
AmpResyncHours int `envconfig:"amp_resync_hours" default:"4"`

DbPath string `envconfig:"db_path" default:"./controller.db"`
}

func main() {
Expand All @@ -47,6 +50,8 @@ func main() {

webPath := assets.SetupWeb()

db.ConnectionString = c.DbPath

auth.ApiAuthKey = getPassword(c.ApiSharedKey, c.ApiSharedKeyFile)

doors.OfflineAfter = time.Duration(c.EspInterval) * time.Second
Expand Down Expand Up @@ -96,6 +101,9 @@ func main() {
log.Println("Stopping api...")
api.Stop()

log.Println("Stopping database...")
db.Stop()

log.Println("Cleaning up...")
_ = os.RemoveAll(webPath)

Expand Down
53 changes: 53 additions & 0 deletions db/db.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package db

import (
"database/sql"
"time"

_ "github.com/mattn/go-sqlite3"
)

var ConnectionString string

var db *sql.DB
var insertStatement *sql.Stmt

func prepare() error {
if db != nil {
return nil
}

var err error
db, err = sql.Open("sqlite3", ConnectionString)
if err != nil {
return err
}

_, err = db.Exec("CREATE TABLE IF NOT EXISTS access (fob TEXT NOT NULL, door TEXT NOT NULL, ts BIGINT NOT NULL, allowed BOOL NOT NULL)")
if err != nil {
return err
}

insertStatement, err = db.Prepare("INSERT INTO access (fob, door, ts, allowed) VALUES (?, ?, ?, ?)")
if err != nil {
return err
}

return nil
}

func Stop() {
_ = db.Close()
}

func InsertAccess(door string, fob string, time time.Time, granted bool) error {
if err := prepare(); err != nil {
return err
}

if _, err := insertStatement.Exec(fob, door, time.UnixMilli(), granted); err != nil {
return err
}

return nil
}
46 changes: 46 additions & 0 deletions doors/mqtt.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"time"

"github.com/eclipse/paho.mqtt.golang"
"github.com/ents-source/door-control/db"
)

type MqttOptions struct {
Expand Down Expand Up @@ -86,6 +87,51 @@ func onDoorSync(client mqtt.Client, message mqtt.Message) {
func onDoorSend(client mqtt.Client, message mqtt.Message) {
log.Printf("[MQTT:Send<<] %s %s", message.Topic(), message.Payload())

msg := parseMessage(message.Payload())

if c, err := readMessageVal[string](msg, "cmd"); err != nil {
log.Println("[MQTT:Send<<] [Command Parse Error]", err)
return
} else if c == "log" {
if t, err := readMessageVal[string](msg, "type"); err != nil {
log.Println("[MQTT:Send<<] [Type Parse Error]", err)
return
} else if t == "access" {
ts, err := readMessageVal[float64](msg, "time")
if err != nil {
log.Println("[MQTT:Send<<] [TS Parse Error]", err)
return
}

fob, err := readMessageVal[string](msg, "uid")
if err != nil {
log.Println("[MQTT:Send<<] [Fob Parse Error]", err)
return
}

door, err := readMessageVal[string](msg, "door")
if err != nil {
log.Println("[MQTT:Send<<] [Door Parse Error]", err)
return
}

accessStr, err := readMessageVal[string](msg, "access")
if err != nil {
log.Println("[MQTT:Send<<] [Access Parse Error]", err)
return
}

access := accessStr == "Always"

err = db.InsertAccess(door, fob, time.UnixMilli(int64(ts)*1000), access)
if err != nil {
log.Println("[MQTT:Send<<] [DB Insert Error]", err)
return
}

log.Println("Access record stored in database")
}
}
}

func onDoorRoot(client mqtt.Client, message mqtt.Message) {
Expand Down
8 changes: 6 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@ module github.com/ents-source/door-control
go 1.20

require (
github.com/eclipse/paho.mqtt.golang v1.4.3 // indirect
github.com/eclipse/paho.mqtt.golang v1.4.3
github.com/kelseyhightower/envconfig v1.4.0
github.com/mattn/go-sqlite3 v1.14.17
)

require (
github.com/gorilla/websocket v1.5.0 // indirect
github.com/kelseyhightower/envconfig v1.4.0 // indirect
golang.org/x/net v0.8.0 // indirect
golang.org/x/sync v0.1.0 // indirect
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWm
github.com/gorilla/websocket v1.5.0/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/kelseyhightower/envconfig v1.4.0 h1:Im6hONhd3pLkfDFsbRgu68RDNkGF1r3dvMUtDTo2cv8=
github.com/kelseyhightower/envconfig v1.4.0/go.mod h1:cccZRl6mQpaq41TPp5QxidR+Sa3axMbJDNb//FQX6Gg=
github.com/mattn/go-sqlite3 v1.14.17 h1:mCRHCLDUBXgpKAqIKsaAaAsrAlbkeomtRFKXh2L6YIM=
github.com/mattn/go-sqlite3 v1.14.17/go.mod h1:2eHXhiwb8IkHr+BDWZGa96P6+rkvnG63S2DGjv9HUNg=
golang.org/x/net v0.8.0 h1:Zrh2ngAOFYneWTAIAPethzeaQLuHwhuBkuV6ZiRnUaQ=
golang.org/x/net v0.8.0/go.mod h1:QVkue5JL9kW//ek3r6jTKnTFis1tRmNAW2P1shuFdJc=
golang.org/x/sync v0.1.0 h1:wsuoTGHzEhffawBOhz5CYhcrV4IdKZbEyZjBMuTp12o=
Expand Down

0 comments on commit ebc7f93

Please sign in to comment.