Skip to content

Commit a30b058

Browse files
authored
Merge pull request #39 from TheCacophonyProject/sync-settings
add Update settings, modify add settings
2 parents f4bcf97 + e8dbbac commit a30b058

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
@@ -702,6 +702,7 @@ func (api *CacophonyAPI) Heartbeat(nextHeartBeat time.Time) ([]byte, error) {
702702
return ioutil.ReadAll(resp.Body)
703703
}
704704

705+
// Ensure names match the API
705706
type Settings struct {
706707
ReferenceImagePOV string
707708
ReferenceImagePOVFileSize int
@@ -737,16 +738,63 @@ type Region struct {
737738
RegionData []Point `json:"regionData"`
738739
}
739740

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

750+
resp, err := api.httpClient.Do(req)
751+
if err != nil {
752+
return nil, err
753+
}
754+
defer resp.Body.Close()
755+
756+
if err := handleHTTPResponse(resp); err != nil {
757+
return nil, err
758+
}
759+
760+
body, err := io.ReadAll(resp.Body)
761+
if err != nil {
762+
return nil, err
763+
}
764+
765+
var response struct {
766+
Settings map[string]interface{} `json:"settings"`
767+
Success bool `json:"success"`
768+
Messages []string `json:"messages"`
769+
}
770+
err = json.Unmarshal(body, &response)
746771
if err != nil {
747772
return nil, err
748773
}
749774

775+
return response.Settings, nil
776+
}
777+
778+
// UpdateDeviceSettings updates the device settings on the API and returns the updated settings
779+
func (api *CacophonyAPI) UpdateDeviceSettings(settings map[string]interface{}) (map[string]interface{}, error) {
780+
if len(settings) == 0 {
781+
fmt.Println("settings is empty")
782+
return nil, nil
783+
}
784+
url := joinURL(api.serverURL, apiBasePath, "devices/"+strconv.Itoa(api.device.id)+"/settings")
785+
payload, err := json.Marshal(map[string]interface{}{
786+
"settings": settings,
787+
})
788+
if err != nil {
789+
return nil, err
790+
}
791+
req, err := http.NewRequest("POST", url, bytes.NewReader(payload))
792+
if err != nil {
793+
return nil, err
794+
}
795+
req.Header.Set("Content-Type", "application/json")
796+
req.Header.Set("Authorization", api.token)
797+
750798
resp, err := api.httpClient.Do(req)
751799
if err != nil {
752800
return nil, err
@@ -762,11 +810,15 @@ func (api *CacophonyAPI) GetDeviceSettings() (*Settings, error) {
762810
return nil, err
763811
}
764812

765-
var settings Settings
766-
err = json.Unmarshal(body, &settings)
813+
var response struct {
814+
Settings map[string]interface{} `json:"settings"`
815+
Success bool `json:"success"`
816+
Messages []string `json:"messages"`
817+
}
818+
err = json.Unmarshal(body, &response)
767819
if err != nil {
768820
return nil, err
769821
}
770822

771-
return &settings, nil
823+
return response.Settings, nil
772824
}

0 commit comments

Comments
 (0)