| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162 |
- use std::env;
- use std::io;
- use std::thread::sleep;
- use std::time::Duration;
-
- use log::error;
- use ::mqtt::control::variable_header::ConnectReturnCode;
-
- use crate::mqtt::MQTT;
-
- mod mqtt;
- mod radio;
- mod spi;
-
- fn main() {
- env::set_var(
- "RUST_LOG",
- env::var_os("RUST_LOG").unwrap_or_else(|| "info".into()),
- );
- env_logger::init();
-
- radio::start();
-
- // Whenever the MQTT connection returns an error, we wait for a couple of seconds and then try
- // to reconnect.
- loop {
- // TODO: Pass events from ratio to mqtt connection.
- if let Err(e) = run_mqtt_connection() {
- error!("MQTT error: {:?}", e);
- }
- sleep(Duration::from_secs(3));
- }
- }
-
- fn run_mqtt_connection() -> Result<(), Error> {
- let mut m = MQTT::connect("127.0.0.1:1883")?;
-
- let mut counter = 0;
- loop {
- // TODO: Real values
- m.publish("gottschlag/livingroom/temperature", &format!("{}", counter))?;
-
- sleep(Duration::from_secs(3));
- counter += 1;
- }
- }
-
- #[derive(thiserror::Error, Debug)]
- pub enum Error {
- #[error("I/O error")]
- Io(#[from] io::Error),
- #[error("connection refused, return code {0:?}")]
- ConnectionRefused(ConnectReturnCode),
- #[error("radio error: {0:?}")]
- Radio(embedded_nrf24l01::Error<std::io::Error>)
- }
-
- impl From<embedded_nrf24l01::Error<std::io::Error>> for Error {
- fn from(e: embedded_nrf24l01::Error<std::io::Error>) -> Self {
- Error::Radio(e)
- }
- }
|