diff --git a/.forgejo/workflows/build-and-release.yaml b/.forgejo/workflows/build-and-release.yaml index 83131e2..6488a39 100644 --- a/.forgejo/workflows/build-and-release.yaml +++ b/.forgejo/workflows/build-and-release.yaml @@ -7,7 +7,7 @@ jobs: steps: - run: apk add --no-cache nodejs git - name: login to container registry - run: echo "${{ secrets.PACKAGE_PUBLISH_TOKEN }}" | docker login --username finn --password-stdin git.janky.solutions + run: echo "${{ secrets.PACKAGE_PUBLISH_TOKEN }}" | docker login --username ${{ secrets.PACKAGE_PUBLISH_USER }} --password-stdin git.janky.solutions - name: gather metadata for container image tags uses: https://github.com/docker/metadata-action@v5 id: meta @@ -20,7 +20,9 @@ jobs: tags: ${{ steps.meta.outputs.tags }} labels: ${{ steps.meta.outputs.labels }} platforms: linux/amd64,linux/arm64,linux/arm/v7 - push: true + push: ${{ github.ref == 'refs/heads/main' || startsWith(github.ref, 'refs/tags/v') }} + build-args: | + VERSION_STRING=${{ env.GITHUB_REF_NAME }} - name: update hassio-addons if: startsWith(github.ref, 'refs/tags/v') run: | diff --git a/Containerfile b/Containerfile index 35afa7a..1ac589c 100644 --- a/Containerfile +++ b/Containerfile @@ -18,7 +18,8 @@ FROM alpine:latest AS build RUN apk add --no-cache go ADD . /go/lockserver WORKDIR /go/lockserver -RUN CGO_ENABLED=0 go build ./cmd/lockserver +ARG VERSION_STRING +RUN CGO_ENABLED=0 go build -ldflags "-X git.janky.solutions/finn/lockserver/config.Version=${VERSION_STRING}" ./cmd/lockserver FROM scratch COPY --from=build /go/lockserver/lockserver /lockserver diff --git a/README.md b/README.md new file mode 100644 index 0000000..9f62cb6 --- /dev/null +++ b/README.md @@ -0,0 +1,18 @@ +# lockserver + +_better Z-Wave Lock management for Home Assistant_ + +## Status + +This is a work in progress. I have some ideas of where I want it to go, but I'm mostly experimenting with my own needs. + +## Install + +To add to Home Assistant, add my hassio-addons repo by clicking the button below, then search for and install the "LockServer" addon. + +[](https://my.home-assistant.io/redirect/supervisor_add_addon_repository/?repository_url=https%3A%2F%2Fgit.janky.solutions%2Ffinn%2Fhassio-addons) + + +## Usage + +When you open the addon's web UI, it will show a list of Z-Wave locks. Clicking a lock shows all codes slots for that lock. Clicking edit on each slot allows changing the code, changing the name, enabling or disabling the slot, and seeing a log of recent uses of that code. diff --git a/config/version.go b/config/version.go new file mode 100644 index 0000000..c718d59 --- /dev/null +++ b/config/version.go @@ -0,0 +1,34 @@ +package config + +import ( + "runtime/debug" + + "github.com/sirupsen/logrus" +) + +var ( + BuildInfo *debug.BuildInfo + Version string +) + +func init() { + var ok bool + BuildInfo, ok = debug.ReadBuildInfo() + if !ok { + logrus.Error("failed to read build info") + return + } + + if Version == "" { + for _, setting := range BuildInfo.Settings { + if setting.Key == "vcs.revision" { + Version = setting.Value + break + } + } + + if Version == "" { + Version = "development" + } + } +} diff --git a/db/helpers.go b/db/helpers.go index a6c1062..85c135e 100644 --- a/db/helpers.go +++ b/db/helpers.go @@ -49,10 +49,11 @@ func Migrate() error { } func NullString(s string) sql.NullString { - return sql.NullString{ - Valid: s != "", - String: s, - } + return sql.NullString{Valid: s != "", String: s} +} + +func NullInt64(i int64) sql.NullInt64 { + return sql.NullInt64{Valid: true, Int64: i} } type loggingDBTX struct { diff --git a/db/lock_log.sql.go b/db/lock_log.sql.go index b777285..b6c4385 100644 --- a/db/lock_log.sql.go +++ b/db/lock_log.sql.go @@ -25,6 +25,44 @@ func (q *Queries) AddLogEntry(ctx context.Context, arg AddLogEntryParams) error return err } +const getLastLogForSlot = `-- name: GetLastLogForSlot :many +SELECT lock, timestamp, state, code, issued_code FROM lock_log WHERE lock = ? AND code = ? ORDER BY timestamp DESC LIMIT 1 +` + +type GetLastLogForSlotParams struct { + Lock int64 + Code sql.NullInt64 +} + +func (q *Queries) GetLastLogForSlot(ctx context.Context, arg GetLastLogForSlotParams) ([]LockLog, error) { + rows, err := q.db.QueryContext(ctx, getLastLogForSlot, arg.Lock, arg.Code) + if err != nil { + return nil, err + } + defer rows.Close() + var items []LockLog + for rows.Next() { + var i LockLog + if err := rows.Scan( + &i.Lock, + &i.Timestamp, + &i.State, + &i.Code, + &i.IssuedCode, + ); err != nil { + return nil, err + } + items = append(items, i) + } + if err := rows.Close(); err != nil { + return nil, err + } + if err := rows.Err(); err != nil { + return nil, err + } + return items, nil +} + const getLogForLock = `-- name: GetLogForLock :many SELECT lock, timestamp, state, code, issued_code FROM lock_log WHERE lock = ? ORDER BY timestamp DESC ` @@ -57,3 +95,41 @@ func (q *Queries) GetLogForLock(ctx context.Context, lock int64) ([]LockLog, err } return items, nil } + +const getLogForSlot = `-- name: GetLogForSlot :many +SELECT lock, timestamp, state, code, issued_code FROM lock_log WHERE lock = ? AND code = ? ORDER BY timestamp DESC LIMIT 100 +` + +type GetLogForSlotParams struct { + Lock int64 + Code sql.NullInt64 +} + +func (q *Queries) GetLogForSlot(ctx context.Context, arg GetLogForSlotParams) ([]LockLog, error) { + rows, err := q.db.QueryContext(ctx, getLogForSlot, arg.Lock, arg.Code) + if err != nil { + return nil, err + } + defer rows.Close() + var items []LockLog + for rows.Next() { + var i LockLog + if err := rows.Scan( + &i.Lock, + &i.Timestamp, + &i.State, + &i.Code, + &i.IssuedCode, + ); err != nil { + return nil, err + } + items = append(items, i) + } + if err := rows.Close(); err != nil { + return nil, err + } + if err := rows.Err(); err != nil { + return nil, err + } + return items, nil +} diff --git a/db/queries/lock_log.sql b/db/queries/lock_log.sql index adfe54f..0106b03 100644 --- a/db/queries/lock_log.sql +++ b/db/queries/lock_log.sql @@ -3,3 +3,9 @@ INSERT INTO lock_log (lock, state, code) VALUES (?, ?, ?); -- name: GetLogForLock :many SELECT * FROM lock_log WHERE lock = ? ORDER BY timestamp DESC; + +-- name: GetLogForSlot :many +SELECT * FROM lock_log WHERE lock = ? AND code = ? ORDER BY timestamp DESC LIMIT 100; + +-- name: GetLastLogForSlot :many +SELECT * FROM lock_log WHERE lock = ? AND code = ? ORDER BY timestamp DESC LIMIT 1; diff --git a/frontend/frontend.go b/frontend/frontend.go index eb6b1f2..8aa240d 100644 --- a/frontend/frontend.go +++ b/frontend/frontend.go @@ -4,6 +4,9 @@ import ( "embed" "html/template" "io/fs" + "time" + + "git.janky.solutions/finn/lockserver/config" ) var ( @@ -16,7 +19,8 @@ var ( Templates *template.Template funcs = template.FuncMap{ - "version": func() string { return "better-zwave-locks v0.x.x aaaaaaaa" }, + "version": func() string { return config.Version }, + "time_since": func(t time.Time) string { return time.Since(t).Round(time.Second).String() }, } ) diff --git a/frontend/header.html b/frontend/header.html index 84d62b3..d5fcf75 100644 --- a/frontend/header.html +++ b/frontend/header.html @@ -5,7 +5,7 @@
{{ if eq .Name "" }}Lock #{{ .ZwaveDeviceID }}{{ else }}{{ .Name }}{{ end }}
+ + {{ end }} + {{ template "footer.html" }} diff --git a/frontend/lock-code-edit.html b/frontend/lock-code-edit.html index 4faa47c..12750fa 100644 --- a/frontend/lock-code-edit.html +++ b/frontend/lock-code-edit.html @@ -2,10 +2,27 @@Slot | -Name | -Code | -Enabled? | -Actions | -
{{ $code.Slot }} | -{{ $code.Name }} | -{{ $code.Code }} | -{{ if $code.Enabled }}enabled{{ else }}disabled{{ end }} | -[ edit ] | -