Skip to content

Commit e8dbbac

Browse files
author
Abra
committed
add Update settings, modify add settings
1 parent c544a02 commit e8dbbac

File tree

1 file changed

+56
-4
lines changed

1 file changed

+56
-4
lines changed

api.go

Lines changed: 56 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -710,6 +710,7 @@ func (api *CacophonyAPI) Heartbeat(nextHeartBeat time.Time) ([]byte, error) {
710710
return ioutil.ReadAll(resp.Body)
711711
}
712712

713+
// Ensure names match the API
713714
type Settings struct {
714715
ReferenceImagePOV string
715716
ReferenceImagePOVFileSize int
@@ -745,16 +746,63 @@ type Region struct {
745746
RegionData []Point `json:"regionData"`
746747
}
747748

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

758+
resp, err := api.httpClient.Do(req)
759+
if err != nil {
760+
return nil, err
761+
}
762+
defer resp.Body.Close()
763+
764+
if err := handleHTTPResponse(resp); err != nil {
765+
return nil, err
766+
}
767+
768+
body, err := io.ReadAll(resp.Body)
769+
if err != nil {
770+
return nil, err
771+
}
772+
773+
var response struct {
774+
Settings map[string]interface{} `json:"settings"`
775+
Success bool `json:"success"`
776+
Messages []string `json:"messages"`
777+
}
778+
err = json.Unmarshal(body, &response)
754779
if err != nil {
755780
return nil, err
756781
}
757782

783+
return response.Settings, nil
784+
}
785+
786+
// UpdateDeviceSettings updates the device settings on the API and returns the updated settings
787+
func (api *CacophonyAPI) UpdateDeviceSettings(settings map[string]interface{}) (map[string]interface{}, error) {
788+
if len(settings) == 0 {
789+
fmt.Println("settings is empty")
790+
return nil, nil
791+
}
792+
url := joinURL(api.serverURL, apiBasePath, "devices/"+strconv.Itoa(api.device.id)+"/settings")
793+
payload, err := json.Marshal(map[string]interface{}{
794+
"settings": settings,
795+
})
796+
if err != nil {
797+
return nil, err
798+
}
799+
req, err := http.NewRequest("POST", url, bytes.NewReader(payload))
800+
if err != nil {
801+
return nil, err
802+
}
803+
req.Header.Set("Content-Type", "application/json")
804+
req.Header.Set("Authorization", api.token)
805+
758806
resp, err := api.httpClient.Do(req)
759807
if err != nil {
760808
return nil, err
@@ -770,11 +818,15 @@ func (api *CacophonyAPI) GetDeviceSettings() (*Settings, error) {
770818
return nil, err
771819
}
772820

773-
var settings Settings
774-
err = json.Unmarshal(body, &settings)
821+
var response struct {
822+
Settings map[string]interface{} `json:"settings"`
823+
Success bool `json:"success"`
824+
Messages []string `json:"messages"`
825+
}
826+
err = json.Unmarshal(body, &response)
775827
if err != nil {
776828
return nil, err
777829
}
778830

779-
return &settings, nil
831+
return response.Settings, nil
780832
}

0 commit comments

Comments
 (0)