Skip to content

Commit c544a02

Browse files
authored
Merge pull request #37 from TheCacophonyProject/device-settings
Device settings
2 parents 8c1b044 + 28bb680 commit c544a02

File tree

1 file changed

+129
-7
lines changed

1 file changed

+129
-7
lines changed

api.go

Lines changed: 129 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@ type CacophonyAPI struct {
6262

6363
// joinURL creates an absolute url with supplied baseURL, and all paths
6464
func joinURL(baseURL string, paths ...string) string {
65-
6665
u, err := url.Parse(baseURL)
6766
if err != nil {
6867
return ""
@@ -79,6 +78,7 @@ func (api *CacophonyAPI) getAPIURL() string {
7978
func (api *CacophonyAPI) getAuthURL() string {
8079
return joinURL(api.serverURL, authURL)
8180
}
81+
8282
func (api *CacophonyAPI) getRegURL() string {
8383
return joinURL(api.serverURL, apiBasePath, regURL)
8484
}
@@ -233,7 +233,6 @@ func (api *CacophonyAPI) authenticate() error {
233233
data["groupname"] = api.device.group
234234
}
235235
payload, err := json.Marshal(data)
236-
237236
if err != nil {
238237
return err
239238
}
@@ -294,7 +293,7 @@ func shaHash(r io.Reader) (string, error) {
294293
func (api *CacophonyAPI) UploadVideo(r io.Reader, data map[string]interface{}) (int, error) {
295294
buf := new(bytes.Buffer)
296295
w := multipart.NewWriter(buf)
297-
//This will write to fileBytes as it reads r to get the sha hash
296+
// This will write to fileBytes as it reads r to get the sha hash
298297
var fileBytes bytes.Buffer
299298
tee := io.TeeReader(r, &fileBytes)
300299
hash, err := shaHash(tee)
@@ -311,7 +310,6 @@ func (api *CacophonyAPI) UploadVideo(r io.Reader, data map[string]interface{}) (
311310

312311
// JSON encoded "data" parameter.
313312
dataBuf, err := json.Marshal(data)
314-
315313
if err != nil {
316314
return 0, err
317315
}
@@ -519,7 +517,7 @@ func handleHTTPResponse(resp *http.Response) error {
519517
return nil
520518
}
521519

522-
//formatTimestamp to time.RFC3339Nano format
520+
// formatTimestamp to time.RFC3339Nano format
523521
func formatTimestamp(t time.Time) string {
524522
return t.UTC().Format(time.RFC3339Nano)
525523
}
@@ -539,7 +537,7 @@ func (api *CacophonyAPI) GetSchedule() ([]byte, error) {
539537
return nil, err
540538
}
541539
req.Header.Set("Authorization", api.token)
542-
//client := new(http.Client)
540+
// client := new(http.Client)
543541

544542
resp, err := api.httpClient.Do(req)
545543
if err != nil {
@@ -550,9 +548,64 @@ func (api *CacophonyAPI) GetSchedule() ([]byte, error) {
550548
return ioutil.ReadAll(resp.Body)
551549
}
552550

551+
// This allows the device to be registered even
552+
func (api *CacophonyAPI) ReRegisterByAuthorized(newName, newGroup, newPassword, authToken string) error {
553+
data := map[string]string{
554+
"newName": newName,
555+
"newGroup": newGroup,
556+
"newPassword": newPassword,
557+
"authorizedToken": authToken,
558+
}
559+
jsonAll, err := json.Marshal(data)
560+
if err != nil {
561+
return err
562+
}
563+
url := joinURL(api.serverURL, apiBasePath, "devices/reregister-authorized")
564+
req, err := http.NewRequest("POST", url, bytes.NewReader(jsonAll))
565+
if err != nil {
566+
return err
567+
}
568+
req.Header.Set("Authorization", api.token)
569+
req.Header.Set("Content-Type", "application/json")
570+
resp, err := api.httpClient.Do(req)
571+
if err != nil {
572+
return err
573+
}
574+
defer resp.Body.Close()
575+
if err := handleHTTPResponse(resp); err != nil {
576+
return err
577+
}
578+
var respData tokenResponse
579+
d := json.NewDecoder(resp.Body)
580+
if err := d.Decode(&respData); err != nil {
581+
return fmt.Errorf("decode: %v", err)
582+
}
583+
api.device = &CacophonyDevice{
584+
id: respData.ID,
585+
group: newGroup,
586+
name: newName,
587+
password: newPassword,
588+
}
589+
590+
api.token = respData.Token
591+
api.device.password = newPassword
592+
conf, err := NewConfig(goconfig.DefaultConfigDir)
593+
if err != nil {
594+
return err
595+
}
596+
conf.DeviceName = newName
597+
conf.Group = newGroup
598+
conf.ServerURL = api.serverURL
599+
conf.DevicePassword = newPassword
600+
conf.DeviceID = respData.ID
601+
if err := conf.write(); err != nil {
602+
return err
603+
}
604+
return updateHostnameFiles(api.getHostname())
605+
}
606+
553607
// Reregister will register getting a new name and/or group
554608
func (api *CacophonyAPI) Reregister(newName, newGroup, newPassword string) error {
555-
556609
data := map[string]string{
557610
"newName": newName,
558611
"newGroup": newGroup,
@@ -656,3 +709,72 @@ func (api *CacophonyAPI) Heartbeat(nextHeartBeat time.Time) ([]byte, error) {
656709

657710
return ioutil.ReadAll(resp.Body)
658711
}
712+
713+
type Settings struct {
714+
ReferenceImagePOV string
715+
ReferenceImagePOVFileSize int
716+
ReferenceImageInSitu string
717+
ReferenceImageInSituFileSize int
718+
Warp Warp
719+
MaskRegions []Region
720+
RatThresh interface{}
721+
Success bool
722+
Messages []string
723+
}
724+
725+
type Warp struct {
726+
Dimensions Dimensions
727+
Origin Point
728+
TopLeft Point
729+
TopRight Point
730+
BottomLeft Point
731+
BottomRight Point
732+
}
733+
734+
type Dimensions struct {
735+
Width int
736+
Height int
737+
}
738+
739+
type Point struct {
740+
X int
741+
Y int
742+
}
743+
744+
type Region struct {
745+
RegionData []Point `json:"regionData"`
746+
}
747+
748+
func (api *CacophonyAPI) GetDeviceSettings() (*Settings, error) {
749+
url := joinURL(api.serverURL, apiBasePath, "devices/"+strconv.Itoa(api.device.id)+"/settings")
750+
req, err := http.NewRequest("GET", url, nil)
751+
req.Header.Set("Authorization", api.token)
752+
req.Header.Set("Content-Type", "application/json")
753+
754+
if err != nil {
755+
return nil, err
756+
}
757+
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 settings Settings
774+
err = json.Unmarshal(body, &settings)
775+
if err != nil {
776+
return nil, err
777+
}
778+
779+
return &settings, nil
780+
}

0 commit comments

Comments
 (0)