@@ -3,11 +3,12 @@ use crate::{
33 state:: { GlobalState , Scene } ,
44 structs:: { ApiError , FromPacket , TimerPacket , TimerPacketInner } ,
55} ;
6- use alloc:: string:: String ;
6+ use alloc:: { rc :: Rc , string:: String } ;
77use core:: str:: FromStr ;
88use embassy_net:: { tcp:: TcpSocket , IpAddress , Stack } ;
99use embassy_sync:: {
1010 blocking_mutex:: raw:: CriticalSectionRawMutex , channel:: Channel , pubsub:: PubSubChannel ,
11+ signal:: Signal ,
1112} ;
1213use embassy_time:: { Instant , Timer } ;
1314use embedded_io_async:: Write ;
@@ -22,7 +23,12 @@ static TAGGED_RETURN: PubSubChannel<CriticalSectionRawMutex, (u64, TimerPacket),
2223 PubSubChannel :: new ( ) ;
2324
2425#[ embassy_executor:: task]
25- pub async fn ws_task ( stack : Stack < ' static > , ws_url : String , global_state : GlobalState ) {
26+ pub async fn ws_task (
27+ stack : Stack < ' static > ,
28+ ws_url : String ,
29+ global_state : GlobalState ,
30+ ws_sleep_sig : Rc < Signal < CriticalSectionRawMutex , bool > > ,
31+ ) {
2632 let ws_url = WsUrl :: from_str ( & ws_url) . expect ( "Ws url parse error" ) ;
2733
2834 let mut rx_buf = [ 0 ; 8192 ] ;
@@ -41,7 +47,7 @@ pub async fn ws_task(stack: Stack<'static>, ws_url: String, global_state: Global
4147 }
4248
4349 loop {
44- let res = ws_loop (
50+ let ws_fut = ws_loop (
4551 & global_state,
4652 & ws_url,
4753 stack,
@@ -51,11 +57,26 @@ pub async fn ws_task(stack: Stack<'static>, ws_url: String, global_state: Global
5157 & mut ws_tx_buf,
5258 & mut ssl_rx_buf,
5359 & mut ssl_tx_buf,
54- )
55- . await ;
60+ ) ;
61+
62+ let res = embassy_futures:: select:: select ( ws_fut, ws_sleep_sig. wait ( ) ) . await ;
5663
57- if let Err ( e) = res {
58- log:: error!( "Ws_loop errored! {e:?}" ) ;
64+ match res {
65+ embassy_futures:: select:: Either :: First ( res) => {
66+ if let Err ( e) = res {
67+ log:: error!( "Ws_loop errored! {e:?}" ) ;
68+ }
69+ }
70+ embassy_futures:: select:: Either :: Second ( sleep) => {
71+ if sleep {
72+ loop {
73+ let sleep = ws_sleep_sig. wait ( ) . await ;
74+ if !sleep {
75+ break ;
76+ }
77+ }
78+ }
79+ }
5980 }
6081
6182 Timer :: after_millis ( 500 ) . await ;
@@ -105,8 +126,6 @@ async fn ws_loop(
105126 * addr
106127 } ;
107128
108- Timer :: after_millis ( 10000000 ) . await ;
109- continue ;
110129 let mut socket = TcpSocket :: new ( stack, rx_buf, tx_buf) ;
111130 socket. set_timeout ( Some ( embassy_time:: Duration :: from_secs ( 15 ) ) ) ;
112131
0 commit comments