Remove no-longer-needed REST endpoints
Some checks failed
/ build-container (push) Has been cancelled
Some checks failed
/ build-container (push) Has been cancelled
This commit is contained in:
parent
a5433beca0
commit
22cafb347b
13 changed files with 22 additions and 828 deletions
|
@ -10,25 +10,18 @@ import (
|
|||
"git.janky.solutions/finn/lockserver/frontend"
|
||||
)
|
||||
|
||||
type browserEndpoints struct{}
|
||||
|
||||
type baseTemplateData struct {
|
||||
Username string
|
||||
UserDisplayName string
|
||||
}
|
||||
|
||||
func (b browserEndpoints) Register(e *echo.Echo) {
|
||||
e.GET("/", b.Index)
|
||||
e.StaticFS("/static", frontend.Static)
|
||||
}
|
||||
|
||||
type indexTemplateData struct {
|
||||
baseTemplateData
|
||||
|
||||
Locks []db.Lock
|
||||
}
|
||||
|
||||
func (browserEndpoints) Index(c echo.Context) error {
|
||||
func indexHandler(c echo.Context) error {
|
||||
queries, dbc, err := db.Get()
|
||||
if err != nil {
|
||||
return err
|
11
httpserver/lock.go
Normal file
11
httpserver/lock.go
Normal file
|
@ -0,0 +1,11 @@
|
|||
package httpserver
|
||||
|
||||
import (
|
||||
"errors"
|
||||
|
||||
echo "github.com/labstack/echo/v4"
|
||||
)
|
||||
|
||||
func lockHandler(c echo.Context) error {
|
||||
return errors.New("not yet implemented")
|
||||
}
|
|
@ -11,7 +11,7 @@ import (
|
|||
"github.com/sirupsen/logrus"
|
||||
|
||||
"git.janky.solutions/finn/lockserver/config"
|
||||
"git.janky.solutions/finn/lockserver/openapi"
|
||||
"git.janky.solutions/finn/lockserver/frontend"
|
||||
"git.janky.solutions/finn/lockserver/zwavejs"
|
||||
)
|
||||
|
||||
|
@ -28,9 +28,9 @@ func ListenAndServe(client *zwavejs.Client) {
|
|||
server.HTTPErrorHandler = handleError
|
||||
server.Use(accessLogMiddleware)
|
||||
|
||||
browserEndpoints{}.Register(server)
|
||||
|
||||
openapi.RegisterHandlersWithBaseURL(server, lockserver{ZWaveJS: client}, "/api")
|
||||
server.StaticFS("/static", frontend.Static)
|
||||
server.GET("/", indexHandler)
|
||||
server.GET("/locks/{id}", lockHandler)
|
||||
|
||||
logrus.WithField("address", config.C.HTTPBind).Info("starting http server")
|
||||
err := server.Start(config.C.HTTPBind)
|
||||
|
|
|
@ -1,112 +0,0 @@
|
|||
package httpserver
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
echo "github.com/labstack/echo/v4"
|
||||
|
||||
"git.janky.solutions/finn/lockserver/db"
|
||||
"git.janky.solutions/finn/lockserver/openapi"
|
||||
"git.janky.solutions/finn/lockserver/zwavejs"
|
||||
)
|
||||
|
||||
func (lockserver) GetLockCodeSlot(c echo.Context, lock int, slot int) error {
|
||||
queries, dbc, err := db.Get()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer dbc.Close()
|
||||
|
||||
slotData, err := queries.GetLockCodeBySlot(c.Request().Context(), db.GetLockCodeBySlotParams{
|
||||
Lock: int64(lock),
|
||||
Slot: int64(slot),
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return c.JSON(http.StatusFound, slotData.OpenAPI())
|
||||
}
|
||||
|
||||
func (l lockserver) PutLockCodeSlot(c echo.Context, lockID int, slot int) error {
|
||||
queries, dbc, err := db.Get()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer dbc.Close()
|
||||
|
||||
var body openapi.LockCodeSlot
|
||||
if err := c.Bind(&body); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
ctx := c.Request().Context()
|
||||
|
||||
lock, err := queries.GetLock(ctx, int64(lockID))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// sample from https://github.com/FutureTense/keymaster/blob/f4f1046bddb7901cbd3ce7820886be1ff7895fe7/tests/test_services.py#L88
|
||||
//
|
||||
// {
|
||||
// "ccVersion": 1,
|
||||
// "commandClassName": "User Code",
|
||||
// "commandClass": 99,
|
||||
// "endpoint": 0,
|
||||
// "property": "userCode",
|
||||
// "propertyName": "userCode",
|
||||
// "propertyKey": 1,
|
||||
// "propertyKeyName": "1",
|
||||
// "metadata": {
|
||||
// "type": "string",
|
||||
// "readable": True,
|
||||
// "writeable": True,
|
||||
// "minLength": 4,
|
||||
// "maxLength": 10,
|
||||
// "label": "User Code (1)",
|
||||
// },
|
||||
// "value": "123456",
|
||||
// }
|
||||
err = l.ZWaveJS.SetNodeValue(ctx, int(lock.ZwaveDeviceID), zwavejs.NodeValue{
|
||||
CCVersion: 1,
|
||||
CommandClassName: zwavejs.CommandClassNameUserCode,
|
||||
CommandClass: zwavejs.CommandClassUserCode,
|
||||
Endpoint: 0,
|
||||
Property: zwavejs.AnyType{Type: zwavejs.AnyTypeString, String: string(zwavejs.PropertyUserCode)},
|
||||
PropertyName: zwavejs.AnyType{Type: zwavejs.AnyTypeString, String: string(zwavejs.PropertyUserCode)},
|
||||
PropertyKey: zwavejs.AnyType{Type: zwavejs.AnyTypeInt, Int: slot},
|
||||
}, zwavejs.AnyType{Type: zwavejs.AnyTypeString, String: body.Code})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
enabled := 0
|
||||
if body.Enabled {
|
||||
enabled = 1
|
||||
}
|
||||
err = l.ZWaveJS.SetNodeValue(ctx, int(lock.ZwaveDeviceID), zwavejs.NodeValue{
|
||||
CCVersion: 1,
|
||||
CommandClassName: zwavejs.CommandClassNameUserCode,
|
||||
CommandClass: zwavejs.CommandClassUserCode,
|
||||
Endpoint: 0,
|
||||
Property: zwavejs.AnyType{Type: zwavejs.AnyTypeString, String: string(zwavejs.PropertyUserIDStatus)},
|
||||
PropertyName: zwavejs.AnyType{Type: zwavejs.AnyTypeString, String: string(zwavejs.PropertyUserIDStatus)},
|
||||
PropertyKey: zwavejs.AnyType{Type: zwavejs.AnyTypeInt, Int: slot},
|
||||
}, zwavejs.AnyType{Type: zwavejs.AnyTypeInt, Int: enabled})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = queries.UpsertCodeSlot(ctx, db.UpsertCodeSlotParams{
|
||||
Lock: lock.ID,
|
||||
Slot: int64(slot),
|
||||
Code: body.Code,
|
||||
Enabled: body.Enabled,
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return c.JSON(http.StatusOK, body)
|
||||
}
|
|
@ -1,51 +0,0 @@
|
|||
package httpserver
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"errors"
|
||||
"net/http"
|
||||
|
||||
"git.janky.solutions/finn/lockserver/db"
|
||||
"git.janky.solutions/finn/lockserver/openapi"
|
||||
echo "github.com/labstack/echo/v4"
|
||||
)
|
||||
|
||||
func (l lockserver) AddUserCode(c echo.Context) error {
|
||||
queries, dbc, err := db.Get()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer dbc.Close()
|
||||
ctx := c.Request().Context()
|
||||
|
||||
if _, err = queries.CreateUserCode(ctx, db.CreateUserCodeParams{}); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return c.NoContent(http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
func (l lockserver) GetAllUserCodes(c echo.Context) error {
|
||||
queries, dbc, err := db.Get()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer dbc.Close()
|
||||
ctx := c.Request().Context()
|
||||
|
||||
resp := []openapi.UserCode{}
|
||||
|
||||
codes, err := queries.GetAllUserCodes(ctx)
|
||||
if err != nil {
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return c.JSON(http.StatusOK, resp)
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
for _, code := range codes {
|
||||
resp = append(resp, code.OpenAPI())
|
||||
}
|
||||
|
||||
return c.JSON(http.StatusOK, resp)
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue