use std::time::Duration;
use tokio::time::sleep;
use knust::protocol::address::{Address, GroupAddress, IndividualAddress};
use knust::protocol::telegram::{Direction, Priority, Telegram, TelegramType};
use knust::{ConnectionConfig, ConnectionType, Knx};
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
env_logger::init();
let config = ConnectionConfig {
connection_type: ConnectionType::Tunneling,
gateway_ip: Some("192.168.1.100".parse()?),
individual_address: IndividualAddress::new(1, 1, 240),
..Default::default()
};
let knx = Knx::new(config).await?;
knx.connect().await?;
println!("Sending custom telegrams...");
let switch_on_telegram = Telegram {
source: IndividualAddress::new(1, 1, 240),
destination: Address::Group(GroupAddress::from_parts(1, 2, 3)?),
payload: vec![0x01], priority: Priority::Normal,
direction: Direction::Outgoing,
telegram_type: TelegramType::GroupValueWrite,
gateway_id: None,
timestamp: std::time::SystemTime::now(),
};
println!(
"Sending switch ON telegram to {}...",
switch_on_telegram.destination
);
knx.send_telegram(&switch_on_telegram).await?;
sleep(Duration::from_secs(2)).await;
let switch_off_telegram = Telegram {
source: IndividualAddress::new(1, 1, 240),
destination: Address::Group(GroupAddress::from_parts(1, 2, 3)?),
payload: vec![0x00], priority: Priority::Normal,
direction: Direction::Outgoing,
telegram_type: TelegramType::GroupValueWrite,
gateway_id: None,
timestamp: std::time::SystemTime::now(),
};
println!(
"Sending switch OFF telegram to {}...",
switch_off_telegram.destination
);
knx.send_telegram(&switch_off_telegram).await?;
sleep(Duration::from_secs(1)).await;
let brightness_telegram = Telegram {
source: IndividualAddress::new(1, 1, 240),
destination: Address::Group(GroupAddress::from_parts(1, 2, 4)?),
payload: vec![128], priority: Priority::Normal,
direction: Direction::Outgoing,
telegram_type: TelegramType::GroupValueWrite,
gateway_id: None,
timestamp: std::time::SystemTime::now(),
};
println!(
"Sending brightness telegram to {}...",
brightness_telegram.destination
);
knx.send_telegram(&brightness_telegram).await?;
knx.disconnect().await?;
println!("Send telegrams example completed successfully!");
Ok(())
}