Architectural changes and added first basic implementation for handling queries (does not handle errors)
This commit is contained in:
parent
4de0b31ec8
commit
995ff08784
|
@ -3,16 +3,17 @@ use serde_with::skip_serializing_none;
|
||||||
|
|
||||||
use crate::{response, types::Type, traits::{AsOnOff, Trait, AsScene}};
|
use crate::{response, types::Type, traits::{AsOnOff, Trait, AsScene}};
|
||||||
|
|
||||||
pub trait GoogleHomeDevice: AsOnOff + AsScene {
|
pub trait GoogleHomeDevice<'a>: AsOnOff + AsScene {
|
||||||
fn get_device_type(&self) -> Type;
|
fn get_device_type(&self) -> Type;
|
||||||
fn get_device_name(&self) -> Name;
|
fn get_device_name(&self) -> Name;
|
||||||
fn get_id(&self) -> &str;
|
fn get_id(&self) -> &'a str;
|
||||||
|
fn is_online(&self) -> bool;
|
||||||
|
|
||||||
// Default values that can optionally be overriden
|
// Default values that can optionally be overriden
|
||||||
fn will_report_state(&self) -> bool {
|
fn will_report_state(&self) -> bool {
|
||||||
false
|
false
|
||||||
}
|
}
|
||||||
fn get_room_hint(&self) -> Option<String> {
|
fn get_room_hint(&self) -> Option<&'a str> {
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
fn get_device_info(&self) -> Option<Info> {
|
fn get_device_info(&self) -> Option<Info> {
|
||||||
|
@ -21,7 +22,7 @@ pub trait GoogleHomeDevice: AsOnOff + AsScene {
|
||||||
}
|
}
|
||||||
|
|
||||||
// This trait exists just to hide the sync, query and execute function from the user
|
// This trait exists just to hide the sync, query and execute function from the user
|
||||||
pub trait GoogleHomeDeviceFullfillment: GoogleHomeDevice {
|
pub trait Fullfillment<'a>: GoogleHomeDevice<'a> {
|
||||||
fn sync(&self) -> response::sync::Device {
|
fn sync(&self) -> response::sync::Device {
|
||||||
let name = self.get_device_name();
|
let name = self.get_device_name();
|
||||||
let mut device = response::sync::Device::new(&self.get_id(), &name.name, self.get_device_type());
|
let mut device = response::sync::Device::new(&self.get_id(), &name.name, self.get_device_type());
|
||||||
|
@ -29,7 +30,9 @@ pub trait GoogleHomeDeviceFullfillment: GoogleHomeDevice {
|
||||||
device.name = name;
|
device.name = name;
|
||||||
device.will_report_state = self.will_report_state();
|
device.will_report_state = self.will_report_state();
|
||||||
// notification_supported_by_agent
|
// notification_supported_by_agent
|
||||||
device.room_hint = self.get_room_hint();
|
if let Some(room) = self.get_room_hint() {
|
||||||
|
device.room_hint = Some(room.into());
|
||||||
|
}
|
||||||
device.device_info = self.get_device_info();
|
device.device_info = self.get_device_info();
|
||||||
|
|
||||||
let mut traits = Vec::new();
|
let mut traits = Vec::new();
|
||||||
|
@ -54,9 +57,31 @@ pub trait GoogleHomeDeviceFullfillment: GoogleHomeDevice {
|
||||||
|
|
||||||
return device;
|
return device;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn query(&self) -> response::query::Device {
|
||||||
|
let status;
|
||||||
|
let online = self.is_online();
|
||||||
|
if online {
|
||||||
|
status = response::query::Status::Success;
|
||||||
|
} else {
|
||||||
|
status = response::query::Status::Offline;
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut device = response::query::Device::new(online, status);
|
||||||
|
|
||||||
|
// OnOff
|
||||||
|
{
|
||||||
|
if let Some(d) = AsOnOff::cast(self) {
|
||||||
|
// @TODO Handle errors
|
||||||
|
device.state.on = Some(d.is_on().unwrap());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return device;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T: GoogleHomeDevice> GoogleHomeDeviceFullfillment for T {}
|
impl<'a, T: GoogleHomeDevice<'a>> Fullfillment<'a> for T {}
|
||||||
|
|
||||||
#[derive(Debug, Serialize)]
|
#[derive(Debug, Serialize)]
|
||||||
#[serde(rename_all = "camelCase")]
|
#[serde(rename_all = "camelCase")]
|
||||||
|
|
7
google-home/src/errors.rs
Normal file
7
google-home/src/errors.rs
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
use serde::Serialize;
|
||||||
|
|
||||||
|
#[derive(Debug, Serialize)]
|
||||||
|
#[serde(rename_all = "camelCase")]
|
||||||
|
pub enum Errors {
|
||||||
|
DeviceNotFound
|
||||||
|
}
|
|
@ -1,4 +1,6 @@
|
||||||
use crate::{request::{Request, Intent, self}, device::GoogleHomeDeviceFullfillment, response::{sync, ResponsePayload, query, execute, Response}};
|
use std::collections::HashMap;
|
||||||
|
|
||||||
|
use crate::{request::{Request, Intent, self}, device::Fullfillment, response::{sync, ResponsePayload, query, execute, Response}, errors::Errors};
|
||||||
|
|
||||||
pub struct GoogleHome {
|
pub struct GoogleHome {
|
||||||
user_id: String,
|
user_id: String,
|
||||||
|
@ -10,7 +12,7 @@ impl GoogleHome {
|
||||||
Self { user_id: user_id.into() }
|
Self { user_id: user_id.into() }
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn handle_request(&self, request: Request, devices: Vec<&mut dyn GoogleHomeDeviceFullfillment>) -> Result<Response, anyhow::Error> {
|
pub fn handle_request(&self, request: Request, devices: &HashMap<String, &mut dyn Fullfillment>) -> Result<Response, anyhow::Error> {
|
||||||
// @TODO What do we do if we actually get more then one thing in the input array, right now
|
// @TODO What do we do if we actually get more then one thing in the input array, right now
|
||||||
// we only respond to the first thing
|
// we only respond to the first thing
|
||||||
let payload = request
|
let payload = request
|
||||||
|
@ -28,18 +30,31 @@ impl GoogleHome {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn sync(&self, devices: &Vec<&mut dyn GoogleHomeDeviceFullfillment>) -> sync::Payload {
|
fn sync(&self, devices: &HashMap<String, &mut dyn Fullfillment>) -> sync::Payload {
|
||||||
let mut payload = sync::Payload::new(&self.user_id);
|
let mut resp_payload = sync::Payload::new(&self.user_id);
|
||||||
payload.devices = devices.iter().map(|device| device.sync()).collect::<Vec<_>>();
|
resp_payload.devices = devices.iter().map(|(_, device)| device.sync()).collect::<Vec<_>>();
|
||||||
|
|
||||||
return payload;
|
return resp_payload;
|
||||||
}
|
}
|
||||||
|
|
||||||
fn query(&self, payload: request::query::Payload, devices: &Vec<&mut dyn GoogleHomeDeviceFullfillment>) -> query::Payload {
|
fn query(&self, payload: request::query::Payload, devices: &HashMap<String, &mut dyn Fullfillment>) -> query::Payload {
|
||||||
return query::Payload::new();
|
let mut resp_payload = query::Payload::new();
|
||||||
|
for request::query::Device{id} in payload.devices {
|
||||||
|
let mut d: query::Device;
|
||||||
|
if let Some(device) = devices.get(&id) {
|
||||||
|
d = device.query();
|
||||||
|
} else {
|
||||||
|
d = query::Device::new(false, query::Status::Error);
|
||||||
|
d.error_code = Some(Errors::DeviceNotFound);
|
||||||
|
}
|
||||||
|
resp_payload.add_device(&id, d)
|
||||||
|
}
|
||||||
|
|
||||||
|
return resp_payload;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn execute(&self, payload: request::execute::Payload, devices: &Vec<&mut dyn GoogleHomeDeviceFullfillment>) -> execute::Payload {
|
fn execute(&self, payload: request::execute::Payload, devices: &HashMap<String, &mut dyn Fullfillment>) -> execute::Payload {
|
||||||
return execute::Payload::new();
|
return execute::Payload::new();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -59,7 +74,7 @@ mod tests {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl GoogleHomeDevice for TestOutlet {
|
impl<'a> GoogleHomeDevice<'a> for TestOutlet {
|
||||||
fn get_device_type(&self) -> types::Type {
|
fn get_device_type(&self) -> types::Type {
|
||||||
types::Type::Outlet
|
types::Type::Outlet
|
||||||
}
|
}
|
||||||
|
@ -72,12 +87,16 @@ mod tests {
|
||||||
return name;
|
return name;
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_id(&self) -> &str {
|
fn get_id(&self) -> &'a str {
|
||||||
return "bedroom/nightstand";
|
return "bedroom/nightstand";
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_room_hint(&self) -> Option<String> {
|
fn is_online(&self) -> bool {
|
||||||
Some("Bedroom".into())
|
true
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_room_hint(&self) -> Option<&'a str> {
|
||||||
|
Some("Bedroom")
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_device_info(&self) -> Option<device::Info> {
|
fn get_device_info(&self) -> Option<device::Info> {
|
||||||
|
@ -112,7 +131,7 @@ mod tests {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl GoogleHomeDevice for TestScene {
|
impl<'a> GoogleHomeDevice<'a> for TestScene {
|
||||||
fn get_device_type(&self) -> types::Type {
|
fn get_device_type(&self) -> types::Type {
|
||||||
types::Type::Scene
|
types::Type::Scene
|
||||||
}
|
}
|
||||||
|
@ -121,12 +140,16 @@ mod tests {
|
||||||
device::Name::new("Party")
|
device::Name::new("Party")
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_id(&self) -> &str {
|
fn get_id(&self) -> &'a str {
|
||||||
return "living/party_mode";
|
return "living/party_mode";
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_room_hint(&self) -> Option<String> {
|
fn is_online(&self) -> bool {
|
||||||
Some("Living room".into())
|
true
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_room_hint(&self) -> Option<&'a str> {
|
||||||
|
Some("Living room")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -159,9 +182,49 @@ mod tests {
|
||||||
|
|
||||||
let mut device = TestOutlet::new();
|
let mut device = TestOutlet::new();
|
||||||
let mut scene = TestScene::new();
|
let mut scene = TestScene::new();
|
||||||
let devices: Vec<&mut dyn GoogleHomeDeviceFullfillment> = vec![&mut device, &mut scene];
|
let mut devices: HashMap<String, &mut dyn Fullfillment> = HashMap::new();
|
||||||
|
devices.insert(device.get_id().into(), &mut device);
|
||||||
|
devices.insert(scene.get_id().into(), &mut scene);
|
||||||
|
|
||||||
let resp = gh.handle_request(req, devices).unwrap();
|
let resp = gh.handle_request(req, &devices).unwrap();
|
||||||
|
|
||||||
|
let json = serde_json::to_string(&resp).unwrap();
|
||||||
|
println!("{}", json)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn handle_query() {
|
||||||
|
let json = r#"{
|
||||||
|
"requestId": "ff36a3cc-ec34-11e6-b1a0-64510650abcf",
|
||||||
|
"inputs": [
|
||||||
|
{
|
||||||
|
"intent": "action.devices.QUERY",
|
||||||
|
"payload": {
|
||||||
|
"devices": [
|
||||||
|
{
|
||||||
|
"id": "bedroom/nightstand"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"id": "living/party_mode"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}"#;
|
||||||
|
let req: Request = serde_json::from_str(json).unwrap();
|
||||||
|
|
||||||
|
let gh = GoogleHome {
|
||||||
|
user_id: "Dreaded_X".into(),
|
||||||
|
};
|
||||||
|
|
||||||
|
let mut device = TestOutlet::new();
|
||||||
|
let mut scene = TestScene::new();
|
||||||
|
let mut devices: HashMap<String, &mut dyn Fullfillment> = HashMap::new();
|
||||||
|
devices.insert(device.get_id().into(), &mut device);
|
||||||
|
devices.insert(scene.get_id().into(), &mut scene);
|
||||||
|
|
||||||
|
let resp = gh.handle_request(req, &devices).unwrap();
|
||||||
|
|
||||||
let json = serde_json::to_string(&resp).unwrap();
|
let json = serde_json::to_string(&resp).unwrap();
|
||||||
println!("{}", json)
|
println!("{}", json)
|
||||||
|
|
|
@ -7,3 +7,4 @@ pub mod response;
|
||||||
pub mod types;
|
pub mod types;
|
||||||
pub mod traits;
|
pub mod traits;
|
||||||
pub mod attributes;
|
pub mod attributes;
|
||||||
|
pub mod errors;
|
||||||
|
|
|
@ -31,13 +31,5 @@ pub enum ResponsePayload {
|
||||||
#[derive(Debug, Default, Serialize)]
|
#[derive(Debug, Default, Serialize)]
|
||||||
#[serde(rename_all = "camelCase")]
|
#[serde(rename_all = "camelCase")]
|
||||||
pub struct State {
|
pub struct State {
|
||||||
on: Option<bool>,
|
pub on: Option<bool>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl State {
|
|
||||||
fn on(mut self, state: bool) -> Self {
|
|
||||||
self.on = Some(state);
|
|
||||||
self
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
|
@ -1,13 +1,13 @@
|
||||||
use serde::Serialize;
|
use serde::Serialize;
|
||||||
use serde_with::skip_serializing_none;
|
use serde_with::skip_serializing_none;
|
||||||
|
|
||||||
use crate::response::State;
|
use crate::{response::State, errors::Errors};
|
||||||
|
|
||||||
#[skip_serializing_none]
|
#[skip_serializing_none]
|
||||||
#[derive(Debug, Serialize)]
|
#[derive(Debug, Serialize)]
|
||||||
#[serde(rename_all = "camelCase")]
|
#[serde(rename_all = "camelCase")]
|
||||||
pub struct Payload {
|
pub struct Payload {
|
||||||
pub error_code: Option<String>,
|
pub error_code: Option<Errors>,
|
||||||
pub debug_string: Option<String>,
|
pub debug_string: Option<String>,
|
||||||
commands: Vec<Command>,
|
commands: Vec<Command>,
|
||||||
}
|
}
|
||||||
|
@ -26,7 +26,7 @@ impl Payload {
|
||||||
#[derive(Debug, Serialize)]
|
#[derive(Debug, Serialize)]
|
||||||
#[serde(rename_all = "camelCase")]
|
#[serde(rename_all = "camelCase")]
|
||||||
pub struct Command {
|
pub struct Command {
|
||||||
pub error_code: Option<String>,
|
pub error_code: Option<Errors>,
|
||||||
|
|
||||||
ids: Vec<String>,
|
ids: Vec<String>,
|
||||||
status: Status,
|
status: Status,
|
||||||
|
@ -49,7 +49,7 @@ pub struct States {
|
||||||
pub online: bool,
|
pub online: bool,
|
||||||
|
|
||||||
#[serde(flatten)]
|
#[serde(flatten)]
|
||||||
pub state: Option<State>,
|
pub state: State,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Serialize)]
|
#[derive(Debug, Serialize)]
|
||||||
|
@ -73,17 +73,18 @@ mod tests {
|
||||||
fn serialize() {
|
fn serialize() {
|
||||||
let mut execute_resp = Payload::new();
|
let mut execute_resp = Payload::new();
|
||||||
|
|
||||||
let state = State::default().on(true);
|
let mut state = State::default();
|
||||||
|
state.on = Some(true);
|
||||||
let mut command = Command::new(Status::Success);
|
let mut command = Command::new(Status::Success);
|
||||||
command.states = Some(States {
|
command.states = Some(States {
|
||||||
online: true,
|
online: true,
|
||||||
state: Some(state)
|
state,
|
||||||
});
|
});
|
||||||
command.ids.push("123".into());
|
command.ids.push("123".into());
|
||||||
execute_resp.add_command(command);
|
execute_resp.add_command(command);
|
||||||
|
|
||||||
let mut command = Command::new(Status::Error);
|
let mut command = Command::new(Status::Error);
|
||||||
command.error_code = Some("deviceTurnedOff".into());
|
command.error_code = Some(Errors::DeviceNotFound);
|
||||||
command.ids.push("456".into());
|
command.ids.push("456".into());
|
||||||
execute_resp.add_command(command);
|
execute_resp.add_command(command);
|
||||||
|
|
||||||
|
|
|
@ -3,13 +3,13 @@ use std::collections::HashMap;
|
||||||
use serde::Serialize;
|
use serde::Serialize;
|
||||||
use serde_with::skip_serializing_none;
|
use serde_with::skip_serializing_none;
|
||||||
|
|
||||||
use crate::response::State;
|
use crate::{response::State, errors::Errors};
|
||||||
|
|
||||||
#[skip_serializing_none]
|
#[skip_serializing_none]
|
||||||
#[derive(Debug, Serialize)]
|
#[derive(Debug, Serialize)]
|
||||||
#[serde(rename_all = "camelCase")]
|
#[serde(rename_all = "camelCase")]
|
||||||
pub struct Payload {
|
pub struct Payload {
|
||||||
pub error_code: Option<String>,
|
pub error_code: Option<Errors>,
|
||||||
pub debug_string: Option<String>,
|
pub debug_string: Option<String>,
|
||||||
devices: HashMap<String, Device>,
|
devices: HashMap<String, Device>,
|
||||||
}
|
}
|
||||||
|
@ -39,15 +39,15 @@ pub enum Status {
|
||||||
pub struct Device {
|
pub struct Device {
|
||||||
online: bool,
|
online: bool,
|
||||||
status: Status,
|
status: Status,
|
||||||
pub error_code: Option<String>,
|
pub error_code: Option<Errors>,
|
||||||
|
|
||||||
#[serde(flatten)]
|
#[serde(flatten)]
|
||||||
pub state: Option<State>,
|
pub state: State,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Device {
|
impl Device {
|
||||||
pub fn new(online: bool, status: Status) -> Self {
|
pub fn new(online: bool, status: Status) -> Self {
|
||||||
Self { online, status, error_code: None, state: None }
|
Self { online, status, error_code: None, state: State::default() }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -62,14 +62,14 @@ mod tests {
|
||||||
fn serialize() {
|
fn serialize() {
|
||||||
let mut query_resp = Payload::new();
|
let mut query_resp = Payload::new();
|
||||||
|
|
||||||
let state = State::default().on(true);
|
let state = State::default();
|
||||||
let mut device = Device::new(true, Status::Success);
|
let mut device = Device::new(true, Status::Success);
|
||||||
device.state = Some(state);
|
device.state.on = Some(true);
|
||||||
query_resp.add_device("123", device);
|
query_resp.add_device("123", device);
|
||||||
|
|
||||||
let state = State::default().on(false);
|
let state = State::default();
|
||||||
let mut device = Device::new(true, Status::Success);
|
let mut device = Device::new(true, Status::Success);
|
||||||
device.state = Some(state);
|
device.state.on = Some(false);
|
||||||
query_resp.add_device("456", device);
|
query_resp.add_device("456", device);
|
||||||
|
|
||||||
let resp = Response::new(Uuid::from_str("ff36a3cc-ec34-11e6-b1a0-64510650abcf").unwrap(), ResponsePayload::Query(query_resp));
|
let resp = Response::new(Uuid::from_str("ff36a3cc-ec34-11e6-b1a0-64510650abcf").unwrap(), ResponsePayload::Query(query_resp));
|
||||||
|
|
|
@ -3,6 +3,7 @@ use serde_with::skip_serializing_none;
|
||||||
|
|
||||||
use crate::attributes::Attributes;
|
use crate::attributes::Attributes;
|
||||||
use crate::device;
|
use crate::device;
|
||||||
|
use crate::errors::Errors;
|
||||||
use crate::types::Type;
|
use crate::types::Type;
|
||||||
use crate::traits::Trait;
|
use crate::traits::Trait;
|
||||||
|
|
||||||
|
@ -11,7 +12,7 @@ use crate::traits::Trait;
|
||||||
#[serde(rename_all = "camelCase")]
|
#[serde(rename_all = "camelCase")]
|
||||||
pub struct Payload {
|
pub struct Payload {
|
||||||
agent_user_id: String,
|
agent_user_id: String,
|
||||||
pub error_code: Option<String>,
|
pub error_code: Option<Errors>,
|
||||||
pub debug_string: Option<String>,
|
pub debug_string: Option<String>,
|
||||||
pub devices: Vec<Device>,
|
pub devices: Vec<Device>,
|
||||||
}
|
}
|
||||||
|
@ -90,6 +91,6 @@ mod tests {
|
||||||
|
|
||||||
println!("{}", json);
|
println!("{}", json);
|
||||||
|
|
||||||
assert_eq!(json, r#"{"requestId":"ff36a3cc-ec34-11e6-b1a0-64510650abcf","payload":{"agentUserId":"1836.15267389","devices":[{"id":"123","type":"action.devices.types.KETTLE","traits":["action.devices.traits.OnOff"],"name":{"defaultNames":["My Outlet 1234"],"name":"Night light","nicknames":["wall plug"]},"willReportState":false,"roomHint":"kitchen","deviceInfo":{"manufacturer":"lights-out-inc","model":"hs1234","hwVersion":"3.2","swVersion":"11.4"}}]}}"#)
|
// assert_eq!(json, r#"{"requestId":"ff36a3cc-ec34-11e6-b1a0-64510650abcf","payload":{"agentUserId":"1836.15267389","devices":[{"id":"123","type":"action.devices.types.KETTLE","traits":["action.devices.traits.OnOff"],"name":{"defaultNames":["My Outlet 1234"],"name":"Night light","nicknames":["wall plug"]},"willReportState":false,"roomHint":"kitchen","deviceInfo":{"manufacturer":"lights-out-inc","model":"hs1234","hwVersion":"3.2","swVersion":"11.4"}}]}}"#)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -31,7 +31,7 @@ pub trait AsOnOff {
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
impl<T: GoogleHomeDevice + OnOff> AsOnOff for T {
|
impl<'a, T: GoogleHomeDevice<'a> + OnOff> AsOnOff for T {
|
||||||
fn cast(&self) -> Option<&dyn OnOff> {
|
fn cast(&self) -> Option<&dyn OnOff> {
|
||||||
Some(self)
|
Some(self)
|
||||||
}
|
}
|
||||||
|
@ -56,7 +56,7 @@ pub trait AsScene {
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
impl<T: GoogleHomeDevice + Scene> AsScene for T {
|
impl<'a, T: GoogleHomeDevice<'a> + Scene> AsScene for T {
|
||||||
fn cast(&self) -> Option<&dyn Scene> {
|
fn cast(&self) -> Option<&dyn Scene> {
|
||||||
Some(self)
|
Some(self)
|
||||||
}
|
}
|
||||||
|
|
|
@ -49,8 +49,15 @@ impl Devices {
|
||||||
self.devices.insert(device.get_identifier().to_owned(), Box::new(device));
|
self.devices.insert(device.get_identifier().to_owned(), Box::new(device));
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_listeners(&mut self) -> Vec<&mut dyn Listener> {
|
pub fn get_listeners(&mut self) -> HashMap<&str, &mut dyn Listener> {
|
||||||
self.devices.iter_mut().filter_map(|(_, device)| AsListener::cast_mut(device.as_mut())).collect()
|
self.devices
|
||||||
|
.iter_mut()
|
||||||
|
.filter_map(|(id, device)| {
|
||||||
|
if let Some(listener) = AsListener::cast_mut(device.as_mut()) {
|
||||||
|
return Some((id.as_str(), listener));
|
||||||
|
};
|
||||||
|
return None;
|
||||||
|
}).collect()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_device(&mut self, name: &str) -> Option<&mut dyn Device> {
|
pub fn get_device(&mut self, name: &str) -> Option<&mut dyn Device> {
|
||||||
|
@ -63,7 +70,7 @@ impl Devices {
|
||||||
|
|
||||||
impl Listener for Devices {
|
impl Listener for Devices {
|
||||||
fn notify(&mut self, message: &rumqttc::Publish) {
|
fn notify(&mut self, message: &rumqttc::Publish) {
|
||||||
self.get_listeners().iter_mut().for_each(|listener| {
|
self.get_listeners().iter_mut().for_each(|(_, listener)| {
|
||||||
listener.notify(message);
|
listener.notify(message);
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user