Skip to content

Commit

Permalink
feat: Add methods blink, update and device to supervisor v1 struct
Browse files Browse the repository at this point in the history
Added the following methods:
- Blink: start a blink pattern on a LED for 15 s
- Update: trigger a check for the target state of configurations
- Device: return the current device state
  • Loading branch information
cakoolen committed Feb 25, 2023
1 parent 55acbac commit 14563d1
Show file tree
Hide file tree
Showing 2 changed files with 387 additions and 2 deletions.
86 changes: 84 additions & 2 deletions supervisorv1.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,22 +29,104 @@ func (s *SupervisorV1Service) Reboot(ctx context.Context, force bool) error {
&request{Force: force},
)
if err != nil {
return fmt.Errorf("unable to create restart service request: %v", err)
return fmt.Errorf("unable to create restart service request: %w", err)
}
var resp struct {
Data string `json:"Data"`
Error string `json:"Error"`
}
err = s.client.Do(req, &resp)
if err != nil {
return fmt.Errorf("unable to reboot device: %v", err)
return fmt.Errorf("unable to reboot device: %w", err)
}
if resp.Data != "OK" {
return fmt.Errorf("unable to reboot device: %v", resp.Error)
}
return nil
}

// Blink starts a blink pattern on a LED for 15 seconds, if your device has one.
// https://docs.balena.io/reference/supervisor/supervisor-api/#post-v1blink
func (s *SupervisorV1Service) Blink(ctx context.Context) error {
req, err := s.newRequest(
ctx,
http.MethodPost,
supervisorv1BasePath+"/blink",
nil,
)
if err != nil {
return fmt.Errorf("unable to create blink request: %w", err)
}

err = s.client.Do(req, nil)
if err != nil {
return fmt.Errorf("unable to start blink pattern: %w", err)
}

return nil
}

// Update triggers a check for the target state of configurations and app services.
// https://docs.balena.io/reference/supervisor/supervisor-api/#post-v1update
func (s *SupervisorV1Service) Update(ctx context.Context, force bool) error {
type request struct {
Force bool `json:"force"`
}
req, err := s.newRequest(
ctx,
http.MethodPost,
supervisorv1BasePath+"/update",
&request{Force: force},
)
if err != nil {
return fmt.Errorf("unable to create update request: %w", err)
}

err = s.client.Do(req, nil)
if err != nil {
return fmt.Errorf("unable to trigger update: %w", err)
}

return nil
}

type SupervisorV1DeviceResponse struct {
APIPort uint `json:"api_port,omitempty"`
Commit string `json:"commit,omitempty"`
IPAddress string `json:"ip_address,omitempty"`
MACAddress string `json:"mac_address,omitempty"`
Status string `json:"status,omitempty"`
DownloadProgress uint `json:"download_progress,omitempty"`
OSVersion string `json:"os_version,omitempty"`
SupervisorVersion string `json:"supervisor_version,omitempty"`
UpdatePending bool `json:"update_pending,omitempty"`
UpdateDownloaded bool `json:"update_downloaded,omitempty"`
UpdateFailed bool `json:"update_failed,omitempty"`
}

// Device returns the current device state.
// https://docs.balena.io/reference/supervisor/supervisor-api/#get-v1device
func (s *SupervisorV1Service) Device(ctx context.Context) (*SupervisorV1DeviceResponse, error) {
req, err := s.newRequest(
ctx,
http.MethodGet,
supervisorv1BasePath+"/device",
nil,
)
if err != nil {
return nil, fmt.Errorf("unable to create device request: %w", err)
}

var response SupervisorV1DeviceResponse

err = s.client.Do(req, &response)
if err != nil {
return nil, fmt.Errorf("unable to get device state: %w", err)
}

return &response, nil
}

func (s *SupervisorV1Service) newRequest(
ctx context.Context,
method string,
Expand Down
Loading

0 comments on commit 14563d1

Please sign in to comment.