Skip to content

Commit a39d40d

Browse files
committed
feat: return jsonrpc notifications
1 parent 3b2884c commit a39d40d

File tree

4 files changed

+21
-17
lines changed

4 files changed

+21
-17
lines changed

cli/screenshot.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77

88
"github.com/mobile-next/mobilecli/commands"
99
"github.com/mobile-next/mobilecli/devices"
10+
"github.com/mobile-next/mobilecli/utils"
1011
"github.com/spf13/cobra"
1112
)
1213

@@ -72,7 +73,7 @@ var screencaptureCmd = &cobra.Command{
7273
// Start agent
7374
err = targetDevice.StartAgent(devices.StartAgentConfig{
7475
OnProgress: func(message string) {
75-
fmt.Fprintf(os.Stderr, "%s\n", message)
76+
utils.Verbose(message)
7677
},
7778
})
7879
if err != nil {
@@ -87,7 +88,7 @@ var screencaptureCmd = &cobra.Command{
8788
Quality: devices.DefaultMJPEGQuality,
8889
Scale: devices.DefaultMJPEGScale,
8990
OnProgress: func(message string) {
90-
fmt.Fprintf(os.Stderr, "%s\n", message)
91+
utils.Verbose(message)
9192
},
9293
OnData: func(data []byte) bool {
9394
_, writeErr := os.Stdout.Write(data)

devices/ios.go

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -277,9 +277,6 @@ func (d *IOSDevice) StartAgent(config StartAgentConfig) error {
277277
}
278278

279279
if webdriverBundleId == "" {
280-
if config.OnProgress != nil {
281-
config.OnProgress("Installing WebDriverAgent")
282-
}
283280
return fmt.Errorf("WebDriverAgent is not installed")
284281
}
285282

@@ -319,7 +316,6 @@ func (d *IOSDevice) StartAgent(config StartAgentConfig) error {
319316
}
320317

321318
// launch WebDriverAgent using testmanagerd
322-
utils.Verbose("Launching WebDriverAgent")
323319
err = d.LaunchWda(webdriverBundleId, webdriverBundleId, "WebDriverAgentRunner.xctest")
324320
if err != nil {
325321
return fmt.Errorf("failed to launch WebDriverAgent: %w", err)
@@ -330,7 +326,6 @@ func (d *IOSDevice) StartAgent(config StartAgentConfig) error {
330326
}
331327

332328
// wait for WebDriverAgent to start
333-
utils.Verbose("Waiting for WebDriverAgent to start")
334329
err = d.wdaClient.WaitForAgent()
335330
if err != nil {
336331
return fmt.Errorf("failed to wait for WebDriverAgent: %w", err)
@@ -339,8 +334,6 @@ func (d *IOSDevice) StartAgent(config StartAgentConfig) error {
339334
// wait 1 second after pressing home, so we make sure wda is in the background
340335
_ = d.wdaClient.PressButton("HOME")
341336
time.Sleep(1 * time.Second)
342-
343-
utils.Verbose("WebDriverAgent started")
344337
}
345338
}
346339

devices/simulator.go

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -313,12 +313,6 @@ func (s SimulatorDevice) InstallWebDriverAgent(onProgress func(string)) error {
313313

314314
defer func() { _ = os.Remove(file) }()
315315

316-
utils.Verbose("Downloaded WebDriverAgent to %s", file)
317-
318-
if onProgress != nil {
319-
onProgress("Installing WebDriverAgent")
320-
}
321-
322316
dir, err := utils.Unzip(file)
323317
if err != nil {
324318
return fmt.Errorf("failed to unzip WebDriverAgent: %v", err)
@@ -470,11 +464,16 @@ func (s *SimulatorDevice) StartAgent(config StartAgentConfig) error {
470464
return fmt.Errorf("simulator is offline, use 'mobilecli device boot --device %s' to start the simulator", s.UDID)
471465
case "Booting":
472466
// simulator is already booting, just wait for it to finish
467+
if config.OnProgress != nil {
468+
config.OnProgress("Waiting for Simulator to boot")
469+
}
470+
473471
utils.Verbose("Simulator is booting, waiting for boot to complete...")
474472
output, err := runSimctl("bootstatus", s.UDID)
475473
if err != nil {
476474
return fmt.Errorf("failed to wait for boot status: %w\n%s", err, output)
477475
}
476+
478477
utils.Verbose("Simulator booted successfully")
479478
s.Simulator.State = "Booted"
480479
case "ShuttingDown":
@@ -505,6 +504,10 @@ func (s *SimulatorDevice) StartAgent(config StartAgentConfig) error {
505504

506505
if !installed {
507506
utils.Verbose("WebdriverAgent is not installed. Will try to install now")
507+
if config.OnProgress != nil {
508+
config.OnProgress("Installing WebDriverAgent on Simulator")
509+
}
510+
508511
err = s.InstallWebDriverAgent(config.OnProgress)
509512
if err != nil {
510513
return fmt.Errorf("SimulatorDevice: failed to install WebDriverAgent: %v", err)
@@ -514,7 +517,7 @@ func (s *SimulatorDevice) StartAgent(config StartAgentConfig) error {
514517
}
515518

516519
if config.OnProgress != nil {
517-
config.OnProgress("Launching WebDriverAgent")
520+
config.OnProgress("Starting WebDriverAgent")
518521
}
519522

520523
// find available ports

server/server.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -716,7 +716,14 @@ func handleScreenCapture(w http.ResponseWriter, params json.RawMessage) error {
716716

717717
// progress callback sends JSON-RPC notifications through the MJPEG stream
718718
progressCallback := func(message string) {
719-
statusJSON, _ := json.Marshal(map[string]string{"message": message})
719+
notification := map[string]interface{}{
720+
"jsonrpc": "2.0",
721+
"method": "notification/message",
722+
"params": map[string]string{
723+
"message": message,
724+
},
725+
}
726+
statusJSON, _ := json.Marshal(notification)
720727
mimeMessage := fmt.Sprintf("--BoundaryString\r\nContent-Type: application/json\r\nContent-Length: %d\r\n\r\n%s\r\n", len(statusJSON), statusJSON)
721728
_, _ = w.Write([]byte(mimeMessage))
722729
if flusher, ok := w.(http.Flusher); ok {

0 commit comments

Comments
 (0)