diff --git a/examples/bffh.dhall b/examples/bffh.dhall index 2fa6eba..308f19f 100644 --- a/examples/bffh.dhall +++ b/examples/bffh.dhall @@ -1,14 +1,14 @@ -- { actor_connections = [] : List { _1 : Text, _2 : Text } { actor_connections = -- Link up machines to actors - [ { _1 = "Testmachine", _2 = "Actor" } + [ { _1 = "Testmachine", _2 = "Shelly_1234" } , { _1 = "Another", _2 = "Bash" } -- One machine can have as many actors as it wants , { _1 = "Yetmore", _2 = "Bash2" } , { _1 = "Yetmore", _2 = "FailBash"} ] , actors = - { Actor = { module = "Dummy", params = {=} } + { Shelly_1234 = { module = "Shelly", params = {=} } , Bash = { module = "Process", params = { cmd = "./examples/actor.sh" , args = "your ad could be here" @@ -23,8 +23,8 @@ } , init_connections = [] : List { _1 : Text, _2 : Text } --, init_connections = [{ _1 = "Initiator", _2 = "Testmachine" }] -, initiators = --{=} - { Initiator = { module = "Dummy", params = {=} } } +, initiators = {=} + --{ Initiator = { module = "Dummy", params = {=} } } , listens = [ { address = "127.0.0.1", port = Some 59661 } , { address = "::1", port = Some 59661 } @@ -56,7 +56,7 @@ , write = "lab.test.write" } } -, mqtt_url = "" +, mqtt_url = "tcp://localhost:1883" , db_path = "/tmp/bffh" , roles = { testrole = diff --git a/src/actor.rs b/src/actor.rs index e4a3ae7..a8efaec 100644 --- a/src/actor.rs +++ b/src/actor.rs @@ -114,8 +114,8 @@ pub struct Dummy { } impl Dummy { - pub fn new(log: &Logger) -> Self { - Self { log: log.new(o!("module" => "Dummy Actor")) } + pub fn new(log: Logger) -> Self { + Self { log } } } @@ -129,8 +129,12 @@ impl Actuator for Dummy { pub fn load(log: &Logger, config: &Config) -> Result<(ActorMap, Vec)> { let mut map = HashMap::new(); + let mqtt = AsyncClient::new(config.mqtt_url.clone())?; + let tok = mqtt.connect(paho_mqtt::ConnectOptions::new()); + smol::block_on(tok)?; + let actuators = config.actors.iter() - .map(|(k,v)| (k, load_single(log, k, &v.module, &v.params))) + .map(|(k,v)| (k, load_single(log, k, &v.module, &v.params, mqtt.clone()))) .filter_map(|(k, n)| match n { None => None, Some(a) => Some((k, a)) @@ -151,20 +155,25 @@ fn load_single( log: &Logger, name: &String, module_name: &String, - params: &HashMap + params: &HashMap, + client: AsyncClient, ) -> Option> { use crate::modules::*; info!(log, "Loading actor \"{}\" with module {} and params {:?}", name, module_name, params); + let log = log.new(o!("name" => name.clone())); match module_name.as_ref() { "Dummy" => { Some(Box::new(Dummy::new(log))) } "Process" => { - Process::new(log.new(o!("name" => name.clone())), name.clone(), params) + Process::new(log, name.clone(), params) .map(|a| a.into_boxed_actuator()) } + "Shelly" => { + Some(Box::new(Shelly::new(log, name.clone(), client))) + } _ => { error!(log, "No actor found with name \"{}\", configured as \"{}\".", module_name, name); None diff --git a/src/main.rs b/src/main.rs index 11faa80..3e7df9b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -167,11 +167,6 @@ fn maybe(matches: clap::ArgMatches, log: Arc) -> Result<(), Error> { let ex = Executor::new(); let db = db::Databases::new(&log, &config)?; - //let mqtt = AsyncClient::new(config.mqtt_url.clone())?; - //let tok = mqtt.connect(paho_mqtt::ConnectOptions::new()); - - //smol::block_on(tok)?; - let machines = machine::load(&config)?; let (actor_map, actors) = actor::load(&log, &config)?; let (init_map, initiators) = initiator::load(&log, &config)?; diff --git a/src/modules/shelly.rs b/src/modules/shelly.rs index 1e12df7..3ae1975 100644 --- a/src/modules/shelly.rs +++ b/src/modules/shelly.rs @@ -22,8 +22,7 @@ pub struct Shelly { } impl Shelly { - pub fn new(log_view: &Logger, name: String, client: mqtt::AsyncClient) -> Self { - let log = log_view.new(o!("shelly_name" => name.clone())); + pub fn new(log: Logger, name: String, client: mqtt::AsyncClient) -> Self { debug!(log, "Starting shelly module for {}", &name); Shelly { log, name, client, } }