✨ mqtt connections
almost working, need encryption
This commit is contained in:
parent
20ab0153d1
commit
125d50530d
15 changed files with 1259 additions and 42 deletions
|
@ -1,8 +1,22 @@
|
|||
use sheepd_core::args::{SheepdArgs, SheepdCommand};
|
||||
use api::Identity;
|
||||
use sheepd_core::{
|
||||
args::{SheepdArgs, SheepdCommand},
|
||||
config::AgentConfig,
|
||||
};
|
||||
mod api;
|
||||
mod sheepd_core;
|
||||
use rumqttc::{AsyncClient, Event, MqttOptions, QoS, Transport};
|
||||
use std::{error::Error, path::PathBuf, time::Duration};
|
||||
use tokio::{
|
||||
sync::OnceCell,
|
||||
task,
|
||||
time::{self, sleep},
|
||||
};
|
||||
|
||||
fn main() {
|
||||
pub static MQTT: OnceCell<AsyncClient> = OnceCell::const_new();
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
tracing_subscriber::fmt::init();
|
||||
let args: SheepdArgs = argh::from_env();
|
||||
|
||||
|
@ -13,6 +27,40 @@ fn main() {
|
|||
} else {
|
||||
log::info!("Starting sheepd");
|
||||
|
||||
// TODO : daemon loop
|
||||
let conf = AgentConfig::try_load();
|
||||
if conf.is_none() {
|
||||
log::error!("No config file at /etc/sheepd/config.toml");
|
||||
std::process::exit(1);
|
||||
}
|
||||
|
||||
let i = if let Some(i) = Identity::try_load(&PathBuf::from("/etc/sheepd")) {
|
||||
i
|
||||
} else {
|
||||
let i = Identity::new();
|
||||
i.save(&PathBuf::from("/etc/sheepd"));
|
||||
i
|
||||
};
|
||||
|
||||
let conf = conf.unwrap();
|
||||
let machine_id = std::fs::read_to_string("/etc/machine-id").unwrap();
|
||||
let machine_id = machine_id.trim();
|
||||
|
||||
log::info!("Connecting to MQTT as {machine_id}");
|
||||
|
||||
let (client, mut eventloop) = api::mqtt_connect(machine_id, &conf.mqtt);
|
||||
|
||||
crate::MQTT.set(client).unwrap();
|
||||
|
||||
log::info!("Connection done");
|
||||
|
||||
log::info!("Start sub for {}", format!("{machine_id}/cmd"));
|
||||
crate::MQTT
|
||||
.get()
|
||||
.unwrap()
|
||||
.subscribe(format!("{machine_id}/cmd"), QoS::AtMostOnce)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
api::run_event_loop(eventloop).await;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue