package httpserver import ( "fmt" "net/http" "strconv" "git.janky.solutions/finn/lockserver/db" "git.janky.solutions/finn/lockserver/zwavejs" echo "github.com/labstack/echo/v4" ) func lockHandler(c echo.Context) error { lockID, err := strconv.ParseInt(c.Param("lock"), 10, 32) if err != nil { return fmt.Errorf("invalid lock ID: %v", err) } ctx := c.Request().Context() queries, dbc, err := db.Get() if err != nil { return err } defer dbc.Close() lock, err := queries.GetLock(ctx, lockID) if err != nil { return err } codes, err := queries.GetAllLockCodesByLock(ctx, lockID) if err != nil { return err } return c.Render(http.StatusFound, "lock.html", map[string]interface{}{ "lock": lock, "codes": codes, }) } func lockEditHandler(c echo.Context) error { lockID, err := strconv.ParseInt(c.Param("lock"), 10, 32) if err != nil { return fmt.Errorf("invalid lock ID: %v", err) } ctx := c.Request().Context() queries, dbc, err := db.Get() if err != nil { return err } defer dbc.Close() if c.Request().Method == http.MethodGet { lock, err := queries.GetLock(ctx, lockID) if err != nil { return err } // return frontend.Templates.ExecuteTemplate(c.Response(), "lock-edit.html", lock) return c.Render(http.StatusFound, "lock-edit.html", lock) } err = queries.UpdateLockName(ctx, db.UpdateLockNameParams{ ID: lockID, Name: c.FormValue("name"), }) if err != nil { return err } return c.Redirect(http.StatusFound, fmt.Sprintf("/locks/%d", lockID)) } func lockCodeEditHandler(c echo.Context) error { lockID, err := strconv.ParseInt(c.Param("lock"), 10, 32) if err != nil { return fmt.Errorf("invalid lock ID: %v", err) } slot, err := strconv.ParseInt(c.Param("slot"), 10, 32) if err != nil { return fmt.Errorf("invalid lock ID: %v", err) } ctx := c.Request().Context() queries, dbc, err := db.Get() if err != nil { return err } defer dbc.Close() code, err := queries.GetLockCodeBySlot(ctx, db.GetLockCodeBySlotParams{ Lock: lockID, Slot: slot, }) if err != nil { return err } if c.Request().Method == http.MethodGet { return c.Render(http.StatusFound, "lock-code-edit.html", code) } lock, err := queries.GetLock(ctx, lockID) if err != nil { return err } newCode := c.FormValue("code") zwaveClient := c.Get(contextKeyZWaveClient).(*zwavejs.Client) err = zwaveClient.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: int(slot)}, }, zwavejs.AnyType{Type: zwavejs.AnyTypeString, String: newCode}) if err != nil { return fmt.Errorf("error pushing code to lock %s (ZWaveDeviceID=%d ID=%d): %v", lock.Name, lock.ZwaveDeviceID, lock.ID, err) } err = queries.UpsertCodeSlot(ctx, db.UpsertCodeSlotParams{ Lock: lockID, Slot: slot, Code: newCode, Enabled: true, }) if err != nil { return err } return c.Redirect(http.StatusFound, fmt.Sprintf("/locks/%d", lockID)) }