Skip to content

Commit

Permalink
add Update settings, modify add settings
Browse files Browse the repository at this point in the history
  • Loading branch information
Abra committed Jun 6, 2024
1 parent c544a02 commit e8dbbac
Showing 1 changed file with 56 additions and 4 deletions.
60 changes: 56 additions & 4 deletions api.go
Original file line number Diff line number Diff line change
Expand Up @@ -710,6 +710,7 @@ func (api *CacophonyAPI) Heartbeat(nextHeartBeat time.Time) ([]byte, error) {
return ioutil.ReadAll(resp.Body)
}

// Ensure names match the API
type Settings struct {
ReferenceImagePOV string
ReferenceImagePOVFileSize int
Expand Down Expand Up @@ -745,16 +746,63 @@ type Region struct {
RegionData []Point `json:"regionData"`
}

func (api *CacophonyAPI) GetDeviceSettings() (*Settings, error) {
func (api *CacophonyAPI) GetDeviceSettings() (map[string]interface{}, error) {
url := joinURL(api.serverURL, apiBasePath, "devices/"+strconv.Itoa(api.device.id)+"/settings")
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil, err
}
req.Header.Set("Authorization", api.token)
req.Header.Set("Content-Type", "application/json")

resp, err := api.httpClient.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()

if err := handleHTTPResponse(resp); err != nil {
return nil, err
}

body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}

var response struct {
Settings map[string]interface{} `json:"settings"`
Success bool `json:"success"`
Messages []string `json:"messages"`
}
err = json.Unmarshal(body, &response)
if err != nil {
return nil, err
}

return response.Settings, nil
}

// UpdateDeviceSettings updates the device settings on the API and returns the updated settings
func (api *CacophonyAPI) UpdateDeviceSettings(settings map[string]interface{}) (map[string]interface{}, error) {
if len(settings) == 0 {
fmt.Println("settings is empty")
return nil, nil
}
url := joinURL(api.serverURL, apiBasePath, "devices/"+strconv.Itoa(api.device.id)+"/settings")
payload, err := json.Marshal(map[string]interface{}{
"settings": settings,
})
if err != nil {
return nil, err
}
req, err := http.NewRequest("POST", url, bytes.NewReader(payload))
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", api.token)

resp, err := api.httpClient.Do(req)
if err != nil {
return nil, err
Expand All @@ -770,11 +818,15 @@ func (api *CacophonyAPI) GetDeviceSettings() (*Settings, error) {
return nil, err
}

var settings Settings
err = json.Unmarshal(body, &settings)
var response struct {
Settings map[string]interface{} `json:"settings"`
Success bool `json:"success"`
Messages []string `json:"messages"`
}
err = json.Unmarshal(body, &response)
if err != nil {
return nil, err
}

return &settings, nil
return response.Settings, nil
}

0 comments on commit e8dbbac

Please sign in to comment.