Architectural changes and added first basic implementation for handling queries (does not handle errors)
This commit is contained in:
@@ -1,13 +1,13 @@
|
||||
use serde::Serialize;
|
||||
use serde_with::skip_serializing_none;
|
||||
|
||||
use crate::response::State;
|
||||
use crate::{response::State, errors::Errors};
|
||||
|
||||
#[skip_serializing_none]
|
||||
#[derive(Debug, Serialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct Payload {
|
||||
pub error_code: Option<String>,
|
||||
pub error_code: Option<Errors>,
|
||||
pub debug_string: Option<String>,
|
||||
commands: Vec<Command>,
|
||||
}
|
||||
@@ -26,7 +26,7 @@ impl Payload {
|
||||
#[derive(Debug, Serialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct Command {
|
||||
pub error_code: Option<String>,
|
||||
pub error_code: Option<Errors>,
|
||||
|
||||
ids: Vec<String>,
|
||||
status: Status,
|
||||
@@ -49,7 +49,7 @@ pub struct States {
|
||||
pub online: bool,
|
||||
|
||||
#[serde(flatten)]
|
||||
pub state: Option<State>,
|
||||
pub state: State,
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize)]
|
||||
@@ -73,17 +73,18 @@ mod tests {
|
||||
fn serialize() {
|
||||
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);
|
||||
command.states = Some(States {
|
||||
online: true,
|
||||
state: Some(state)
|
||||
state,
|
||||
});
|
||||
command.ids.push("123".into());
|
||||
execute_resp.add_command(command);
|
||||
|
||||
let mut command = Command::new(Status::Error);
|
||||
command.error_code = Some("deviceTurnedOff".into());
|
||||
command.error_code = Some(Errors::DeviceNotFound);
|
||||
command.ids.push("456".into());
|
||||
execute_resp.add_command(command);
|
||||
|
||||
|
||||
@@ -3,13 +3,13 @@ use std::collections::HashMap;
|
||||
use serde::Serialize;
|
||||
use serde_with::skip_serializing_none;
|
||||
|
||||
use crate::response::State;
|
||||
use crate::{response::State, errors::Errors};
|
||||
|
||||
#[skip_serializing_none]
|
||||
#[derive(Debug, Serialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct Payload {
|
||||
pub error_code: Option<String>,
|
||||
pub error_code: Option<Errors>,
|
||||
pub debug_string: Option<String>,
|
||||
devices: HashMap<String, Device>,
|
||||
}
|
||||
@@ -39,15 +39,15 @@ pub enum Status {
|
||||
pub struct Device {
|
||||
online: bool,
|
||||
status: Status,
|
||||
pub error_code: Option<String>,
|
||||
pub error_code: Option<Errors>,
|
||||
|
||||
#[serde(flatten)]
|
||||
pub state: Option<State>,
|
||||
pub state: State,
|
||||
}
|
||||
|
||||
impl Device {
|
||||
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() {
|
||||
let mut query_resp = Payload::new();
|
||||
|
||||
let state = State::default().on(true);
|
||||
let state = State::default();
|
||||
let mut device = Device::new(true, Status::Success);
|
||||
device.state = Some(state);
|
||||
device.state.on = Some(true);
|
||||
query_resp.add_device("123", device);
|
||||
|
||||
let state = State::default().on(false);
|
||||
let state = State::default();
|
||||
let mut device = Device::new(true, Status::Success);
|
||||
device.state = Some(state);
|
||||
device.state.on = Some(false);
|
||||
query_resp.add_device("456", device);
|
||||
|
||||
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::device;
|
||||
use crate::errors::Errors;
|
||||
use crate::types::Type;
|
||||
use crate::traits::Trait;
|
||||
|
||||
@@ -11,7 +12,7 @@ use crate::traits::Trait;
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct Payload {
|
||||
agent_user_id: String,
|
||||
pub error_code: Option<String>,
|
||||
pub error_code: Option<Errors>,
|
||||
pub debug_string: Option<String>,
|
||||
pub devices: Vec<Device>,
|
||||
}
|
||||
@@ -90,6 +91,6 @@ mod tests {
|
||||
|
||||
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"}}]}}"#)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user