machinelock-manager/type.go

77 lines
1.9 KiB
Go
Raw Permalink Normal View History

2024-12-14 21:30:39 +00:00
package main
import (
"context"
"log"
"example.com/henne/client"
)
type Users = map[string]User
type User struct {
ID string `json:"id" form:"id"`
User string `json:"user" form:"user"`
Bandsaege string `json:"mlock-bandsaege" form:"bandsaege"`
Bohrmaschine string `json:"mlock-bohrmaschine" form:"bohrmaschine"`
Drehbank string `json:"mlock-drehbank" form:"drehbank"`
Dreiddrucker string `json:"mlock-dreiddrucker" form:"dreiddrucker"`
Folienplotter string `json:"mlock-folienplotter" form:"folienplotter"`
Lasercutter string `json:"mlock-lasercutter" form:"lasercutter"`
Shapeoko string `json:"mlock-shapeoko" form:"shapeoko"`
}
func (u *User) Get(id string) {
ctx := context.Background()
h, err := client.Client.Read(ctx, "/maschinenlock/"+id)
if err != nil {
log.Fatal(err)
}
for i, d := range h.Data {
data := d.(string)
switch i {
case "user":
u.User = data
case "mlock-bandsaege":
u.Bandsaege = parseBool(data)
case "mlock-bohrmaschine":
u.Bohrmaschine = parseBool(data)
case "mlock-drehbank":
u.Drehbank = parseBool(data)
case "mlock-dreiddrucker":
u.Dreiddrucker = parseBool(data)
case "mlock-folienplotter":
u.Folienplotter = parseBool(data)
case "mlock-lasercutter":
u.Lasercutter = parseBool(data)
case "mlock-shapeoko":
u.Shapeoko = parseBool(data)
}
}
}
func parseBool(data string) string {
if data == "1" {
return data
}
return ""
}
func (u *User) Save(id string) error {
ctx := context.Background()
_, err := client.Client.Write(ctx, "/maschinenlock/"+id, map[string]interface{}{
"user": u.User,
"mlock-bandsaege": u.Bandsaege,
"mlock-bohrmaschine": u.Bohrmaschine,
"mlock-drehbank": u.Drehbank,
"mlock-dreiddrucker": u.Dreiddrucker,
"mlock-folienplotter": u.Folienplotter,
"mlock-lasercutter": u.Lasercutter,
"mlock-shapeoko": u.Shapeoko,
})
if err != nil {
return err
}
return nil
}