@@ -17,6 +17,7 @@ package daemon
17
17
18
18
import (
19
19
"context"
20
+ "errors"
20
21
"fmt"
21
22
"io"
22
23
@@ -475,5 +476,66 @@ func (s *ArduinoCoreServerImpl) EnumerateMonitorPortSettings(ctx context.Context
475
476
476
477
// Monitor FIXMEDOC
477
478
func (s * ArduinoCoreServerImpl ) Monitor (stream rpc.ArduinoCoreService_MonitorServer ) error {
478
- return status .New (codes .Unimplemented , "Not implemented" ).Err ()
479
+ // The configuration must be sent on the first message
480
+ req , err := stream .Recv ()
481
+ if err != nil {
482
+ return err
483
+ }
484
+
485
+ portProxy , _ , err := monitor .Monitor (stream .Context (), req )
486
+ if err != nil {
487
+ return err
488
+ }
489
+ ctx , cancel := context .WithCancel (stream .Context ())
490
+ go func () {
491
+ defer cancel ()
492
+ for {
493
+ msg , err := stream .Recv ()
494
+ if errors .Is (err , io .EOF ) {
495
+ return
496
+ }
497
+ if err != nil {
498
+ stream .Send (& rpc.MonitorResponse {Error : err .Error ()})
499
+ return
500
+ }
501
+ if conf := msg .GetPortConfiguration (); conf != nil {
502
+ for _ , c := range conf .GetSettings () {
503
+ if err := portProxy .Config (c .SettingId , c .Value ); err != nil {
504
+ stream .Send (& rpc.MonitorResponse {Error : err .Error ()})
505
+ }
506
+ }
507
+ }
508
+ tx := msg .GetTxData ()
509
+ for len (tx ) > 0 {
510
+ n , err := portProxy .Write (tx )
511
+ if errors .Is (err , io .EOF ) {
512
+ return
513
+ }
514
+ if err != nil {
515
+ stream .Send (& rpc.MonitorResponse {Error : err .Error ()})
516
+ return
517
+ }
518
+ tx = tx [n :]
519
+ }
520
+ }
521
+ }()
522
+ go func () {
523
+ defer cancel ()
524
+ buff := make ([]byte , 4096 )
525
+ for {
526
+ n , err := portProxy .Read (buff )
527
+ if errors .Is (err , io .EOF ) {
528
+ return
529
+ }
530
+ if err != nil {
531
+ stream .Send (& rpc.MonitorResponse {Error : err .Error ()})
532
+ return
533
+ }
534
+ if err := stream .Send (& rpc.MonitorResponse {RxData : buff [:n ]}); err != nil {
535
+ return
536
+ }
537
+ }
538
+ }()
539
+ <- ctx .Done ()
540
+ return nil
479
541
}
0 commit comments