Browse Source

Change the UpdateListener API to me more ergonomic

master
Alex Mikhalev 4 years ago
parent
commit
cc1a5bb09d
  1. 2
      sprinklers_actors/src/section_runner.rs
  2. 25
      sprinklers_mqtt/src/update_listener.rs
  3. 17
      sprinklers_rs/src/main.rs

2
sprinklers_actors/src/section_runner.rs

@ -611,7 +611,7 @@ impl SectionRunner {
self.addr.send(Subscribe).map_err(From::from) self.addr.send(Subscribe).map_err(From::from)
} }
pub fn state_receiver(&self) -> SecRunnerStateRecv { pub fn get_state_recv(&self) -> SecRunnerStateRecv {
self.state_recv.clone() self.state_recv.clone()
} }
} }

25
sprinklers_mqtt/src/update_listener.rs

@ -160,18 +160,9 @@ pub struct UpdateListener {
} }
impl UpdateListener { impl UpdateListener {
pub fn start( pub fn start(mqtt_interface: MqttInterface) -> Self {
section_events: SectionEventRecv,
program_events: ProgramEventRecv,
sec_runner_state: SecRunnerStateRecv,
mqtt_interface: MqttInterface,
) -> Self {
let addr = UpdateListenerActor { mqtt_interface }.start(); let addr = UpdateListenerActor { mqtt_interface }.start();
let mut l = Self { addr }; Self { addr }
l.listen(section_events);
l.listen(program_events);
l.listen(sec_runner_state);
l
} }
fn listen<L: 'static>(&mut self, listener: L) fn listen<L: 'static>(&mut self, listener: L)
@ -181,6 +172,18 @@ impl UpdateListener {
self.addr.do_send(Listen(listener)); self.addr.do_send(Listen(listener));
} }
pub fn listen_section_events(&mut self, section_events: SectionEventRecv) {
self.listen(section_events);
}
pub fn listen_program_events(&mut self, program_events: ProgramEventRecv) {
self.listen(program_events);
}
pub fn listen_section_runner(&mut self, sec_runner_state_recv: SecRunnerStateRecv) {
self.listen(sec_runner_state_recv);
}
pub async fn quit(self) -> eyre::Result<()> { pub async fn quit(self) -> eyre::Result<()> {
Ok(self.addr.send(Quit).await?) Ok(self.addr.send(Quit).await?)
} }

17
sprinklers_rs/src/main.rs

@ -3,9 +3,9 @@
// mod option_future; // mod option_future;
use sprinklers_actors as actors;
use sprinklers_core::section_interface::MockSectionInterface; use sprinklers_core::section_interface::MockSectionInterface;
use sprinklers_database as database; use sprinklers_database as database;
use sprinklers_actors as actors;
use sprinklers_mqtt as mqtt; use sprinklers_mqtt as mqtt;
use eyre::Result; use eyre::Result;
@ -56,17 +56,10 @@ async fn main() -> Result<()> {
}; };
let mut mqtt_interface = mqtt::MqttInterfaceTask::start(mqtt_options, request_context); let mut mqtt_interface = mqtt::MqttInterfaceTask::start(mqtt_options, request_context);
let update_listener = { let mut update_listener = mqtt::UpdateListener::start(mqtt_interface.clone());
let section_events = section_runner.subscribe().await?; update_listener.listen_section_events(section_runner.subscribe().await?);
let program_events = program_runner.subscribe().await?; update_listener.listen_section_runner(section_runner.get_state_recv());
let sec_runner_state = section_runner.state_receiver(); update_listener.listen_program_events(program_runner.subscribe().await?);
mqtt::UpdateListener::start(
section_events,
program_events,
sec_runner_state,
mqtt_interface.clone(),
)
};
program_runner.update_sections(sections.clone()).await?; program_runner.update_sections(sections.clone()).await?;
// TODO: update listener should probably do this // TODO: update listener should probably do this

Loading…
Cancel
Save