@@ -17,6 +17,7 @@ package daemon
1717
1818import (
1919 "context"
20+ "errors"
2021 "fmt"
2122 "io"
2223
@@ -475,5 +476,66 @@ func (s *ArduinoCoreServerImpl) EnumerateMonitorPortSettings(ctx context.Context
475476
476477// Monitor FIXMEDOC
477478func (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
479541}
0 commit comments