Skip to content

Commit 5271704

Browse files
committed
feat: enhance device output formatting and API integration
- Added GboxDeviceID to the device output structure for improved identification in JSON and text formats. - Updated device connection status checks to handle cases where GboxDeviceID is not assigned, displaying a placeholder. - Refactored device API to dynamically retrieve the current profile, enhancing flexibility in API calls. - Removed deprecated device connection API handlers to streamline the codebase and improve maintainability.
1 parent 1c6db08 commit 5271704

File tree

7 files changed

+73
-698
lines changed

7 files changed

+73
-698
lines changed

packages/cli/cmd/device_connect_list.go

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ func ExecuteDeviceConnectList(cmd *cobra.Command, opts *DeviceConnectListOptions
8484
func outputDevicesJSONFromAPI(devices []map[string]interface{}) error {
8585
// Create a simplified JSON output for compatibility
8686
type SimpleDeviceInfo struct {
87+
GboxDeviceID string `json:"gbox_device_id"`
8788
DeviceID string `json:"device_id"`
8889
Name string `json:"name"`
8990
Type string `json:"type"`
@@ -95,9 +96,10 @@ func outputDevicesJSONFromAPI(devices []map[string]interface{}) error {
9596
deviceID, _ := device["id"].(string)
9697
name, _ := device["model"].(string)
9798
serialNo, _ := device["ro.serialno"].(string)
99+
gboxDeviceID, _ := device["gbox.device_id"].(string)
98100

99101
status := statusNotRegistered
100-
if _, ok := device["gbox.device_id"]; ok {
102+
if gboxDeviceID != "" {
101103
status = statusRegistered
102104
}
103105

@@ -108,6 +110,7 @@ func outputDevicesJSONFromAPI(devices []map[string]interface{}) error {
108110
}
109111

110112
simpleDevices = append(simpleDevices, SimpleDeviceInfo{
113+
GboxDeviceID: gboxDeviceID,
111114
DeviceID: deviceID,
112115
Name: name,
113116
Type: deviceType,
@@ -135,10 +138,14 @@ func outputDevicesTextFromAPI(devices []map[string]interface{}) error {
135138
deviceID, _ := device["id"].(string)
136139
name, _ := device["model"].(string)
137140
serialNo, _ := device["ro.serialno"].(string)
141+
gboxDeviceID, _ := device["gbox.device_id"].(string)
138142

139143
status := statusNotRegistered
140-
if gboxDeviceId, ok := device["gbox.device_id"]; ok && gboxDeviceId != "" {
144+
if gboxDeviceID != "" {
141145
status = statusRegistered
146+
} else {
147+
// Display "-" when GBOX Device ID is not assigned
148+
gboxDeviceID = "-"
142149
}
143150

144151
deviceType := deviceTypeDevice
@@ -147,18 +154,25 @@ func outputDevicesTextFromAPI(devices []map[string]interface{}) error {
147154
deviceType = deviceTypeEmulator
148155
}
149156

157+
// Display "-" for empty fields
158+
if name == "" {
159+
name = "-"
160+
}
161+
150162
tableData[i] = map[string]interface{}{
151-
"device_id": deviceID,
152-
"name": name,
153-
"type": deviceType,
154-
"status": status,
163+
"gbox_device_id": gboxDeviceID,
164+
"device_id": deviceID,
165+
"name": name,
166+
"type": deviceType,
167+
"status": status,
155168
}
156169
}
157170

158171
// Define table columns
159172
columns := []util.TableColumn{
160-
{Header: "DEVICE ID", Key: "device_id"},
161-
{Header: "NAME", Key: "name"},
173+
{Header: "GLOBAL DEVICE ID", Key: "gbox_device_id"},
174+
{Header: "DEVICE/TRANSPORT ID", Key: "device_id"},
175+
{Header: "MODEL", Key: "name"},
162176
{Header: "TYPE", Key: "type"},
163177
{Header: "STATUS", Key: "status"},
164178
}

packages/cli/internal/cloud/ap.go

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,17 +34,20 @@ type AccessPointList struct {
3434
}
3535

3636
type AccessPointAPI struct {
37-
client *http.Client
38-
profile *profile.Profile
37+
client *http.Client
3938
}
4039

4140
func NewAccessPointAPI() *AccessPointAPI {
4241
return &AccessPointAPI{
43-
client: &http.Client{},
44-
profile: profile.Default.GetCurrent(),
42+
client: &http.Client{},
4543
}
4644
}
4745

46+
// getCurrentProfile gets the current profile dynamically to support profile switching
47+
func (ap *AccessPointAPI) getCurrentProfile() *profile.Profile {
48+
return profile.Default.GetCurrent()
49+
}
50+
4851
func (ap *AccessPointAPI) List() (*AccessPointList, error) {
4952
url, err := ap.buildUrlFromEndpoint("/api/v1/access-points")
5053
if err != nil {
@@ -78,20 +81,30 @@ func (ap *AccessPointAPI) List() (*AccessPointList, error) {
7881
return apList, nil
7982
}
8083

81-
func (d *AccessPointAPI) buildUrlFromEndpoint(endpoint string) (*url.URL, error) {
82-
url, err := url.Parse(d.profile.BaseURL)
84+
func (ap *AccessPointAPI) buildUrlFromEndpoint(endpoint string) (*url.URL, error) {
85+
currentProfile := ap.getCurrentProfile()
86+
if currentProfile == nil {
87+
return nil, errors.New("no current profile set")
88+
}
89+
90+
url, err := url.Parse(currentProfile.BaseURL)
8391
if err != nil {
84-
return nil, errors.Wrapf(err, "failed to parse base url: %s", d.profile.BaseURL)
92+
return nil, errors.Wrapf(err, "failed to parse base url: %s", currentProfile.BaseURL)
8593
}
8694

8795
url.Path = endpoint
8896

8997
return url, nil
9098
}
9199

92-
func (d *AccessPointAPI) setCommonRequestHeaders(req *http.Request) {
100+
func (ap *AccessPointAPI) setCommonRequestHeaders(req *http.Request) {
101+
currentProfile := ap.getCurrentProfile()
102+
if currentProfile == nil {
103+
return
104+
}
105+
93106
req.Header.Set("content-type", "application/json")
94-
decodedBytes, _ := base64.StdEncoding.DecodeString(d.profile.APIKey)
107+
decodedBytes, _ := base64.StdEncoding.DecodeString(currentProfile.APIKey)
95108
apiKey := string(decodedBytes)
96109
if strings.HasPrefix(apiKey, "gbox-rack_") {
97110
req.Header.Set("x-rack-api-key", apiKey)

packages/cli/internal/cloud/device.go

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,17 +42,20 @@ type AccessPointToken struct {
4242
}
4343

4444
type DeviceAPI struct {
45-
client *http.Client
46-
profile *profile.Profile
45+
client *http.Client
4746
}
4847

4948
func NewDeviceAPI() *DeviceAPI {
5049
return &DeviceAPI{
51-
client: &http.Client{},
52-
profile: profile.Default.GetCurrent(),
50+
client: &http.Client{},
5351
}
5452
}
5553

54+
// getCurrentProfile gets the current profile dynamically to support profile switching
55+
func (d *DeviceAPI) getCurrentProfile() *profile.Profile {
56+
return profile.Default.GetCurrent()
57+
}
58+
5659
func (d *DeviceAPI) GetBySerialnoAndAndroidId(serialno string, androidId string) (*DeviceList, error) {
5760
queries := url.Values{}
5861
queries.Set("serialno", serialno)
@@ -199,9 +202,14 @@ func (d *DeviceAPI) GenerateAccessPointToken(deviceId, requestEndpoint string) (
199202
}
200203

201204
func (d *DeviceAPI) buildUrlFromEndpoint(endpoint string) (*url.URL, error) {
202-
url, err := url.Parse(d.profile.BaseURL)
205+
currentProfile := d.getCurrentProfile()
206+
if currentProfile == nil {
207+
return nil, errors.New("no current profile set")
208+
}
209+
210+
url, err := url.Parse(currentProfile.BaseURL)
203211
if err != nil {
204-
return nil, errors.Wrapf(err, "failed to parse base url: %s", d.profile.BaseURL)
212+
return nil, errors.Wrapf(err, "failed to parse base url: %s", currentProfile.BaseURL)
205213
}
206214

207215
url.Path = endpoint
@@ -210,9 +218,14 @@ func (d *DeviceAPI) buildUrlFromEndpoint(endpoint string) (*url.URL, error) {
210218
}
211219

212220
func (d *DeviceAPI) setCommonRequestHeaders(req *http.Request) {
221+
currentProfile := d.getCurrentProfile()
222+
if currentProfile == nil {
223+
return
224+
}
225+
213226
req.Header.Set("x-device-ap", "true")
214227
req.Header.Set("content-type", "application/json")
215-
decodedBytes, _ := base64.StdEncoding.DecodeString(d.profile.APIKey)
228+
decodedBytes, _ := base64.StdEncoding.DecodeString(currentProfile.APIKey)
216229
apiKey := string(decodedBytes)
217230
if strings.HasPrefix(apiKey, "gbox-rack_") {
218231
req.Header.Set("x-rack-api-key", apiKey)

packages/cli/internal/device/manager.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,13 @@ func (m *Manager) GetDevices() ([]DeviceInfo, error) {
7575
IsRegistrable: false, // Default to false, will be updated by caller if needed
7676
}
7777

78-
// Check if device is connected via IP (contains ":")
79-
if strings.Contains(deviceID, ":") {
78+
// Check if device is connected via network
79+
if strings.Contains(deviceID, "._adb._tcp") {
80+
// mDNS service name (e.g., "adb-A4RYVB3A20008848._adb._tcp")
81+
device.ConnectionType = "mdns"
82+
// Keep the full mDNS name as device ID
83+
} else if strings.Contains(deviceID, ":") {
84+
// IP address with port (e.g., "192.168.1.100:5555")
8085
device.ConnectionType = "ip"
8186
}
8287

@@ -132,7 +137,9 @@ func (m *Manager) GetDevicesAsMap() ([]map[string]interface{}, error) {
132137
"state": device.Status, // Add state field for compatibility
133138
"ro.serialno": device.SerialNo,
134139
"android_id": device.AndroidID,
140+
"model": device.Model, // Add model field for easy access
135141
"ro.product.model": device.Model,
142+
"device": device.Manufacturer, // Add device field for easy access
136143
"ro.product.manufacturer": device.Manufacturer,
137144
"connectionType": device.ConnectionType,
138145
"isRegistrable": device.IsRegistrable,

packages/cli/internal/device_connect/api/handlers.go

Lines changed: 0 additions & 172 deletions
This file was deleted.

0 commit comments

Comments
 (0)