diff --git a/fan-controller/src/main.rs b/fan-controller/src/main.rs index 3c75b90..d9cfbda 100644 --- a/fan-controller/src/main.rs +++ b/fan-controller/src/main.rs @@ -141,147 +141,6 @@ async fn gain_control( (net_device, control) } -enum MqttError { - WriteConnectError(connect::EncodeError), - WriteError(tcp::Error), - FlushError(tcp::Error), - ReadError(tcp::Error), - ReadPacketError(packet::ReadError), - ConnectError(mqtt::ConnectErrorReasonCode), - WriteSubscribeError(subscribe::EncodeError), - UnexpectedPacketType(u8), - WritePublishError(publish::EncodeError), -} -#[embassy_executor::task] -async fn mqtt_task_client( - spawner: Spawner, - pwr_pin: PIN_23, - cs_pin: PIN_25, - pio: PIO0, - dma: DMA_CH0, - dio: impl PioPin, - clk: impl PioPin, -) -> () { - let (net_device, mut control) = - gain_control(spawner, pwr_pin, cs_pin, pio, dma, dio, clk).await; - - static STACK: StaticCell>> = StaticCell::new(); - static RESOURCES: StaticCell> = StaticCell::new(); - let configuration = Config::dhcpv4(Default::default()); - let mut random = RoscRng; - let seed = random.next_u64(); - // Initialize network stack - let stack = &*STACK.init(Stack::new( - net_device, - configuration, - RESOURCES.init(StackResources::<5>::new()), - seed, - )); - - unwrap!(spawner.spawn(network_task(stack))); - - // Join Wi-Fi network - loop { - match control - .join_wpa2(configuration::WIFI_NETWORK, configuration::WIFI_PASSWORD) - .await - { - Ok(_) => break, - Err(error) => info!("Error joining Wi-Fi network with status: {}", error.status), - } - } - - // Wait for DHCP - info!("Waiting for DHCP"); - while !stack.is_config_up() { - Timer::after_millis(100).await; - } - - info!("DHCP is up"); - - info!("Waiting for link up"); - while !stack.is_link_up() { - Timer::after_millis(500).await; - } - - info!("Link is up"); - - info!("Waiting for stack to be up"); - stack.wait_config_up().await; - info!("Stack is up"); - - // Now we can use it - let mut receive_buffer = [0; 1024]; - let mut send_buffer = [0; 1024]; - let mut socket = TcpSocket::new(stack, &mut receive_buffer, &mut send_buffer); - - let dns_client = DnsSocket::new(stack); - - info!("Resolving MQTT broker IP address"); - // Get home assistant MQTT broker IP address - let address = loop { - //TODO support IPv6 - let result = dns_client - .query(configuration::MQTT_BROKER_ADDRESS, DnsQueryType::A) - .await; - - let mut addresses = match result { - Ok(addresses) => addresses, - Err(error) => { - info!( - "Error resolving Home Assistant MQTT broker IP address with {}: {:?}", - configuration::MQTT_BROKER_ADDRESS, - error, - ); - // Exponential backoff doesn't seem necessary here - // Maybe the current installation of Home Assistant is in the process of - // being set up and the entry is not yet available - Timer::after_secs(25).await; - continue; - } - }; - - if addresses.is_empty() { - info!("No addresses found for Home Assistant MQTT broker"); - Timer::after_millis(500).await; - continue; - } - - break addresses.swap_remove(0); - }; - info!("MQTT broker IP address resolved"); - - info!("Connecting to MQTT broker through TCP"); - let endpoint = IpEndpoint::new(address, configuration::MQTT_BROKER_PORT); - // Connect - while let Err(error) = socket.connect(endpoint).await { - info!( - "Error connecting to Home Assistant MQTT broker: {:?}", - error - ); - Timer::after_millis(500).await; - } - - use crate::mqtt::client::Client as MqttClient; - - let (reader, writer) = socket.split(); - if let Err(error) = - MqttClient::::connect( - reader, writer, - ) - .await - { - info!( - "Error connecting to Home Assistant MQTT broker: {:?}", - error - ); - return; - } - info!("MQTT connection established"); - - info!("Subscribing to MQTT topics"); -} - /// This task handles inputs from physical buttons to change the fan speed #[embassy_executor::task] async fn input(pin_18: PIN_18) { diff --git a/fan-controller/src/task.rs b/fan-controller/src/task.rs index 03016b6..aa8bbae 100644 --- a/fan-controller/src/task.rs +++ b/fan-controller/src/task.rs @@ -619,77 +619,6 @@ pub(super) trait Publish { fn payload(&self) -> &[u8]; } -/// Queen Client the fourth -pub(super) struct Client< - 'tcp, - 'sender, - 'outgoing, - Receive, - const RECEIVE: usize, - Send, - const SEND: usize, -> where - Receive: for<'a> From>, - Send: Publish, -{ - // Future 1 arguments - reader: TcpReader<'tcp>, - receiver: channel::Sender<'sender, CriticalSectionRawMutex, Receive, RECEIVE>, - client_state: Signal, - acknowledgements: Mutex, - ping_response: Signal, - - // Future 2 arguments - writer: Mutex>, - outgoing: Channel, 8>, - last_packet: Signal, - // Future 3 arguments - waker: AtomicWaker, -} - -impl<'tcp, 'sender, 'outgoing, Receive, const RECEIVE: usize, Send, const SEND: usize> - Client<'tcp, 'sender, 'outgoing, Receive, RECEIVE, Send, SEND> -where - Receive: for<'a> From>, - Send: Publish, -{ - async fn run(&mut self) { - // Future 1 - let listen = listen( - &mut self.reader, - &self.receiver, - &self.client_state, - &self.acknowledgements, - &self.ping_response, - ); - - // Future 2 - let talk = talk(&self.writer, &self.outgoing, &self.last_packet); - - // Future 3 - let set_up = join( - set_up_subscriptions( - non_zero_u16!(1), - &self.acknowledgements, - &self.outgoing, - &SUBSCRIPTIONS, - &self.waker, - ), - set_up_discovery(&self.outgoing), - ); - - // Future 4 - let keep_alive = keep_alive( - &self.writer, - &self.last_packet, - &self.client_state, - &self.ping_response, - ); - - join4(listen, talk, set_up, keep_alive).await; - } -} - pub(super) async fn set_up_network_stack( spawner: Spawner, pwr_pin: PIN_23,