|
@ -30,6 +30,57 @@ func Devices(r gin.IRoutes) { |
|
|
return fetchDevices(ctxOf(c), c.Param("fetch")) |
|
|
return fetchDevices(ctxOf(c), c.Param("fetch")) |
|
|
})) |
|
|
})) |
|
|
|
|
|
|
|
|
|
|
|
r.PUT("/batch", handler(func(c *gin.Context) (interface{}, error) { |
|
|
|
|
|
var body []struct { |
|
|
|
|
|
Fetch string `json:"fetch"` |
|
|
|
|
|
SetState models.NewDeviceState `json:"setState"` |
|
|
|
|
|
} |
|
|
|
|
|
err := parseBody(c, &body) |
|
|
|
|
|
if err != nil { |
|
|
|
|
|
return nil, err |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
set := make(map[int]bool) |
|
|
|
|
|
changed := make([]models.Device, 0, 64) |
|
|
|
|
|
for _, job := range body { |
|
|
|
|
|
devices, err := fetchDevices(ctxOf(c), job.Fetch) |
|
|
|
|
|
if err != nil { |
|
|
|
|
|
return nil, err |
|
|
|
|
|
} |
|
|
|
|
|
if len(devices) == 0 { |
|
|
|
|
|
return []models.Device{}, nil |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
for i := range devices { |
|
|
|
|
|
if set[devices[i].ID] { |
|
|
|
|
|
continue |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
err := devices[i].SetState(job.SetState) |
|
|
|
|
|
if err != nil { |
|
|
|
|
|
return nil, err |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
set[devices[i].ID] = true |
|
|
|
|
|
changed = append(changed, devices[i]) |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
config.PublishChannel <- changed |
|
|
|
|
|
|
|
|
|
|
|
go func() { |
|
|
|
|
|
for _, device := range changed { |
|
|
|
|
|
err := config.DeviceRepository().Save(context.Background(), &device) |
|
|
|
|
|
if err != nil { |
|
|
|
|
|
log.Println("Failed to save device for state:", err) |
|
|
|
|
|
continue |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
}() |
|
|
|
|
|
|
|
|
|
|
|
return changed, nil |
|
|
|
|
|
})) |
|
|
|
|
|
|
|
|
r.PUT("/:fetch/state", handler(func(c *gin.Context) (interface{}, error) { |
|
|
r.PUT("/:fetch/state", handler(func(c *gin.Context) (interface{}, error) { |
|
|
state := models.NewDeviceState{} |
|
|
state := models.NewDeviceState{} |
|
|
err := parseBody(c, &state) |
|
|
err := parseBody(c, &state) |
|
|