Skip to content

Commit 6934c18

Browse files
authored
⭐ feat: enhance device registration with reg_id support (#255)
* ⭐ feat: enhance device registration with reg_id support - Added reg_id field to device structures and updated related APIs. - Refactored connection commands to handle reg_id effectively. - Prioritized local reg_id over cloud data for consistency. - Improved error handling and logging for better traceability. * feat: enhance FMP4Muxer audio configuration handling - Added default audio configuration for cases where audio parameters are missing. - Refactored initialization logic to ensure proper handling of audio settings. - Improved error handling when extracting SPS/PPS from video frames.
1 parent 6a8c70c commit 6934c18

File tree

9 files changed

+320
-91
lines changed

9 files changed

+320
-91
lines changed

.vscode/settings.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
"gbox",
55
"KUBECFG",
66
"kubeconfig",
7-
"modelcontextprotocol"
7+
"modelcontextprotocol",
8+
"serialno"
89
],
910
"python.analysis.extraPaths": [
1011
"./packages/sdk/python"

packages/cli/cmd/device_connect_list.go

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +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"`
87+
RegId string `json:"reg_id"`
8888
DeviceID string `json:"device_id"`
8989
Name string `json:"name"`
9090
Type string `json:"type"`
@@ -96,10 +96,11 @@ func outputDevicesJSONFromAPI(devices []map[string]interface{}) error {
9696
deviceID, _ := device["id"].(string)
9797
name, _ := device["model"].(string)
9898
serialNo, _ := device["ro.serialno"].(string)
99-
gboxDeviceID, _ := device["gbox.device_id"].(string)
99+
regId, _ := device["gbox.reg_id"].(string)
100+
isRegistered, _ := device["isRegistered"].(bool)
100101

101102
status := statusNotRegistered
102-
if gboxDeviceID != "" {
103+
if isRegistered {
103104
status = statusRegistered
104105
}
105106

@@ -110,7 +111,7 @@ func outputDevicesJSONFromAPI(devices []map[string]interface{}) error {
110111
}
111112

112113
simpleDevices = append(simpleDevices, SimpleDeviceInfo{
113-
GboxDeviceID: gboxDeviceID,
114+
RegId: regId,
114115
DeviceID: deviceID,
115116
Name: name,
116117
Type: deviceType,
@@ -138,14 +139,17 @@ func outputDevicesTextFromAPI(devices []map[string]interface{}) error {
138139
deviceID, _ := device["id"].(string)
139140
name, _ := device["model"].(string)
140141
serialNo, _ := device["ro.serialno"].(string)
141-
gboxDeviceID, _ := device["gbox.device_id"].(string)
142+
regId, _ := device["gbox.reg_id"].(string)
143+
isRegistered, _ := device["isRegistered"].(bool)
142144

143145
status := statusNotRegistered
144-
if gboxDeviceID != "" {
146+
if isRegistered {
145147
status = statusRegistered
146-
} else {
147-
// Display "-" when GBOX Device ID is not assigned
148-
gboxDeviceID = "-"
148+
}
149+
150+
// Display "-" for empty reg_id (always show reg_id if exists, regardless of registration status)
151+
if regId == "" {
152+
regId = "-"
149153
}
150154

151155
deviceType := deviceTypeDevice
@@ -160,17 +164,17 @@ func outputDevicesTextFromAPI(devices []map[string]interface{}) error {
160164
}
161165

162166
tableData[i] = map[string]interface{}{
163-
"gbox_device_id": gboxDeviceID,
164-
"device_id": deviceID,
165-
"name": name,
166-
"type": deviceType,
167-
"status": status,
167+
"reg_id": regId,
168+
"device_id": deviceID,
169+
"name": name,
170+
"type": deviceType,
171+
"status": status,
168172
}
169173
}
170174

171175
// Define table columns
172176
columns := []util.TableColumn{
173-
{Header: "GLOBAL DEVICE ID", Key: "gbox_device_id"},
177+
{Header: "REG ID", Key: "reg_id"},
174178
{Header: "DEVICE/TRANSPORT ID", Key: "device_id"},
175179
{Header: "MODEL", Key: "name"},
176180
{Header: "TYPE", Key: "type"},

packages/cli/cmd/device_connect_register.go

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,15 @@ func NewDeviceConnectRegisterCommand() *cobra.Command {
2626
2727
# Register specific device
2828
gbox device-connect register abc123xyz456-usb`,
29-
Args: cobra.MaximumNArgs(1),
29+
Args: cobra.MaximumNArgs(1),
30+
SilenceUsage: true, // Don't show usage on errors
31+
SilenceErrors: true, // Don't show errors twice (we handle them in RunE)
3032
RunE: func(cmd *cobra.Command, args []string) error {
31-
return ExecuteDeviceConnectRegister(cmd, opts, args)
33+
err := ExecuteDeviceConnectRegister(cmd, opts, args)
34+
if err != nil {
35+
fmt.Fprintln(cmd.ErrOrStderr(), err)
36+
}
37+
return nil // Return nil to prevent Cobra from printing again
3238
},
3339
}
3440

@@ -54,7 +60,7 @@ func ExecuteDeviceConnectRegister(cmd *cobra.Command, opts *DeviceConnectRegiste
5460
if deviceID == "" {
5561
return runInteractiveDeviceRegistration()
5662
}
57-
63+
5864
return registerDevice(deviceID)
5965
}
6066

@@ -64,15 +70,15 @@ func runInteractiveDeviceRegistration() error {
6470
Success bool `json:"success"`
6571
Devices []map[string]interface{} `json:"devices"`
6672
}
67-
73+
6874
if err := daemon.DefaultManager.CallAPI("GET", "/api/devices", nil, &response); err != nil {
6975
return fmt.Errorf("failed to get available devices: %v", err)
7076
}
71-
77+
7278
if !response.Success {
7379
return fmt.Errorf("failed to get devices from server")
7480
}
75-
81+
7682
devices := response.Devices
7783
if len(devices) == 0 {
7884
fmt.Println("No Android devices found.")
@@ -90,27 +96,27 @@ func runInteractiveDeviceRegistration() error {
9096
for i, device := range devices {
9197
status := "Not Registered"
9298
statusColor := color.New(color.Faint)
93-
99+
94100
// Extract device info from map
95101
serialNo := device["ro.serialno"].(string)
96102
connectionType := device["connectionType"].(string)
97103
isRegistered, _ := device["isRegistrable"].(bool)
98-
104+
99105
if isRegistered {
100106
status = "Registered"
101107
statusColor = color.New(color.FgGreen)
102108
}
103-
109+
104110
model := "Unknown"
105111
if m, ok := device["ro.product.model"].(string); ok {
106112
model = m
107113
}
108-
114+
109115
manufacturer := ""
110116
if mfr, ok := device["ro.product.manufacturer"].(string); ok {
111117
manufacturer = mfr
112118
}
113-
119+
114120
fmt.Printf("%d. %s (%s, %s) - %s [%s]\n",
115121
i+1,
116122
color.New(color.FgCyan).Sprint(serialNo+"-"+connectionType),
@@ -136,18 +142,18 @@ func registerDevice(deviceID string) error {
136142
// Register device via daemon API
137143
req := map[string]string{"deviceId": deviceID}
138144
var resp map[string]interface{}
139-
145+
140146
if err := daemon.DefaultManager.CallAPI("POST", "/api/devices/register", req, &resp); err != nil {
141147
return fmt.Errorf("failed to register device: %v", err)
142148
}
143-
149+
144150
if success, ok := resp["success"].(bool); !ok || !success {
145151
return fmt.Errorf("failed to register device: %v", resp["error"])
146152
}
147-
153+
148154
fmt.Printf("Establishing remote connection for device %s...\n", deviceID)
149155
fmt.Printf("Connection established successfully!\n")
150-
156+
151157
// Display local Web UI URL
152158
fmt.Printf("\n📱 View and control your device at: %s\n", color.CyanString("http://localhost:29888"))
153159
fmt.Printf(" This is the local live-view interface for device control\n")
@@ -161,6 +167,6 @@ func registerDevice(deviceID string) error {
161167
}
162168

163169
fmt.Printf("\n💡 Device registered successfully. Use 'gbox device-connect unregister %s' to disconnect when needed.\n", deviceID)
164-
170+
165171
return nil
166-
}
172+
}

packages/cli/cmd/device_connect_unregister.go

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,15 @@ func NewDeviceConnectUnregisterCommand() *cobra.Command {
2424
2525
# Unregister all active device connections:
2626
gbox device-connect unregister --all`,
27-
Args: cobra.MaximumNArgs(1),
27+
Args: cobra.MaximumNArgs(1),
28+
SilenceUsage: true, // Don't show usage on errors (e.g., device not found)
29+
SilenceErrors: true, // Don't show errors twice (we handle them in RunE)
2830
RunE: func(cmd *cobra.Command, args []string) error {
29-
return ExecuteDeviceConnectUnregister(cmd, opts, args)
31+
err := ExecuteDeviceConnectUnregister(cmd, opts, args)
32+
if err != nil {
33+
fmt.Fprintln(cmd.ErrOrStderr(), err)
34+
}
35+
return nil // Return nil to prevent Cobra from printing again
3036
},
3137
}
3238

@@ -60,11 +66,11 @@ func unregisterAllDevices() error {
6066
Success bool `json:"success"`
6167
Devices []map[string]interface{} `json:"devices"`
6268
}
63-
69+
6470
if err := daemon.DefaultManager.CallAPI("GET", "/api/devices", nil, &response); err != nil {
6571
return fmt.Errorf("failed to get available devices: %v", err)
6672
}
67-
73+
6874
if !response.Success {
6975
return fmt.Errorf("failed to get devices from server")
7076
}
@@ -75,7 +81,7 @@ func unregisterAllDevices() error {
7581
name, _ := device["ro.product.model"].(string)
7682
connectionType, _ := device["connectionType"].(string)
7783
isRegistrable, _ := device["isRegistrable"].(bool)
78-
84+
7985
if isRegistrable {
8086
fmt.Printf("Unregistering %s (%s, %s)...\n", deviceID, name, connectionType)
8187

@@ -105,11 +111,11 @@ func unregisterDevice(deviceID string) error {
105111
Success bool `json:"success"`
106112
Devices []map[string]interface{} `json:"devices"`
107113
}
108-
114+
109115
if err := daemon.DefaultManager.CallAPI("GET", "/api/devices", nil, &response); err != nil {
110116
return fmt.Errorf("failed to get available devices: %v", err)
111117
}
112-
118+
113119
if !response.Success {
114120
return fmt.Errorf("failed to get devices from server")
115121
}
@@ -122,7 +128,7 @@ func unregisterDevice(deviceID string) error {
122128
break
123129
}
124130
}
125-
131+
126132
if targetDevice == nil {
127133
return fmt.Errorf("device not found: %s", deviceID)
128134
}
@@ -131,7 +137,7 @@ func unregisterDevice(deviceID string) error {
131137
if m, ok := targetDevice["ro.product.model"].(string); ok {
132138
model = m
133139
}
134-
140+
135141
connectionType := "Unknown"
136142
if ct, ok := targetDevice["connectionType"].(string); ok {
137143
connectionType = ct
@@ -155,11 +161,11 @@ func runInteractiveUnregisterSelection() error {
155161
Success bool `json:"success"`
156162
Devices []map[string]interface{} `json:"devices"`
157163
}
158-
164+
159165
if err := daemon.DefaultManager.CallAPI("GET", "/api/devices", nil, &response); err != nil {
160166
return fmt.Errorf("failed to get available devices: %v", err)
161167
}
162-
168+
163169
if !response.Success {
164170
return fmt.Errorf("failed to get devices from server")
165171
}
@@ -194,7 +200,7 @@ func runInteractiveUnregisterSelection() error {
194200
if mfr, ok := device["ro.product.manufacturer"].(string); ok {
195201
manufacturer = mfr
196202
}
197-
203+
198204
fmt.Printf("%d. %s (%s, %s) - %s\n",
199205
i+1,
200206
deviceID,

packages/cli/internal/cloud/device.go

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717

1818
type Device struct {
1919
Id string `json:"id,omitempty"`
20+
RegId string `json:"regId,omitempty"`
2021
Ownership string `json:"ownership,omitempty"`
2122
OwnerId string `json:"ownerId,omitempty"`
2223
Metadata struct {
@@ -56,11 +57,8 @@ func (d *DeviceAPI) getCurrentProfile() *profile.Profile {
5657
return profile.Default.GetCurrent()
5758
}
5859

59-
func (d *DeviceAPI) GetBySerialnoAndAndroidId(serialno string, androidId string) (*DeviceList, error) {
60-
queries := url.Values{}
61-
queries.Set("serialno", serialno)
62-
queries.Set("androidId", androidId)
63-
60+
// getDevices is a generic method to query devices with query parameters
61+
func (d *DeviceAPI) getDevices(queries url.Values) (*DeviceList, error) {
6462
url, err := d.buildUrlFromEndpoint("/api/v1/devices")
6563
if err != nil {
6664
return nil, errors.Wrap(err, "failed to build url")
@@ -95,6 +93,19 @@ func (d *DeviceAPI) GetBySerialnoAndAndroidId(serialno string, androidId string)
9593
return deviceList, nil
9694
}
9795

96+
func (d *DeviceAPI) GetBySerialnoAndAndroidId(serialno string, androidId string) (*DeviceList, error) {
97+
queries := url.Values{}
98+
queries.Set("serialno", serialno)
99+
queries.Set("androidId", androidId)
100+
return d.getDevices(queries)
101+
}
102+
103+
func (d *DeviceAPI) GetByRegId(regId string) (*DeviceList, error) {
104+
queries := url.Values{}
105+
queries.Set("regId", regId)
106+
return d.getDevices(queries)
107+
}
108+
98109
func (d *DeviceAPI) Create(device *Device) (*Device, error) {
99110
url, err := d.buildUrlFromEndpoint("/api/v1/devices")
100111

0 commit comments

Comments
 (0)