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) }