Finished basic google home implementation with some slight refactors along the way
This commit is contained in:
@@ -1,13 +1,13 @@
|
||||
use serde::Serialize;
|
||||
use serde_with::skip_serializing_none;
|
||||
|
||||
use crate::{response::State, errors::Errors};
|
||||
use crate::{response::State, errors::ErrorCode};
|
||||
|
||||
#[skip_serializing_none]
|
||||
#[derive(Debug, Serialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct Payload {
|
||||
pub error_code: Option<Errors>,
|
||||
pub error_code: Option<ErrorCode>,
|
||||
pub debug_string: Option<String>,
|
||||
commands: Vec<Command>,
|
||||
}
|
||||
@@ -18,7 +18,9 @@ impl Payload {
|
||||
}
|
||||
|
||||
pub fn add_command(&mut self, command: Command) {
|
||||
self.commands.push(command);
|
||||
if !command.is_empty() {
|
||||
self.commands.push(command);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -26,7 +28,7 @@ impl Payload {
|
||||
#[derive(Debug, Serialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct Command {
|
||||
pub error_code: Option<Errors>,
|
||||
pub error_code: Option<ErrorCode>,
|
||||
|
||||
ids: Vec<String>,
|
||||
status: Status,
|
||||
@@ -41,6 +43,10 @@ impl Command {
|
||||
pub fn add_id(&mut self, id: &str) {
|
||||
self.ids.push(id.into());
|
||||
}
|
||||
|
||||
pub fn is_empty(&self) -> bool {
|
||||
self.ids.is_empty()
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize)]
|
||||
@@ -67,7 +73,7 @@ mod tests {
|
||||
use std::str::FromStr;
|
||||
use uuid::Uuid;
|
||||
use super::*;
|
||||
use crate::response::{Response, ResponsePayload, State};
|
||||
use crate::{response::{Response, ResponsePayload, State}, errors::DeviceError};
|
||||
|
||||
#[test]
|
||||
fn serialize() {
|
||||
@@ -84,7 +90,7 @@ mod tests {
|
||||
execute_resp.add_command(command);
|
||||
|
||||
let mut command = Command::new(Status::Error);
|
||||
command.error_code = Some(Errors::DeviceNotFound);
|
||||
command.error_code = Some(DeviceError::DeviceNotFound.into());
|
||||
command.ids.push("456".into());
|
||||
execute_resp.add_command(command);
|
||||
|
||||
|
||||
@@ -3,15 +3,15 @@ use std::collections::HashMap;
|
||||
use serde::Serialize;
|
||||
use serde_with::skip_serializing_none;
|
||||
|
||||
use crate::{response::State, errors::Errors};
|
||||
use crate::{response::State, errors::ErrorCode};
|
||||
|
||||
#[skip_serializing_none]
|
||||
#[derive(Debug, Serialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct Payload {
|
||||
pub error_code: Option<Errors>,
|
||||
pub error_code: Option<ErrorCode>,
|
||||
pub debug_string: Option<String>,
|
||||
devices: HashMap<String, Device>,
|
||||
pub devices: HashMap<String, Device>,
|
||||
}
|
||||
|
||||
impl Payload {
|
||||
@@ -39,15 +39,28 @@ pub enum Status {
|
||||
pub struct Device {
|
||||
online: bool,
|
||||
status: Status,
|
||||
pub error_code: Option<Errors>,
|
||||
error_code: Option<ErrorCode>,
|
||||
|
||||
#[serde(flatten)]
|
||||
pub state: State,
|
||||
}
|
||||
|
||||
impl Device {
|
||||
pub fn new(online: bool, status: Status) -> Self {
|
||||
Self { online, status, error_code: None, state: State::default() }
|
||||
pub fn new() -> Self {
|
||||
Self { online: true, status: Status::Success, error_code: None, state: State::default() }
|
||||
}
|
||||
|
||||
pub fn set_offline(&mut self) {
|
||||
self.online = false;
|
||||
self.status = Status::Offline;
|
||||
}
|
||||
|
||||
pub fn set_error(&mut self, err: ErrorCode) {
|
||||
self.status = match err {
|
||||
ErrorCode::DeviceError(_) => Status::Error,
|
||||
ErrorCode::DeviceException(_) => Status::Exceptions,
|
||||
};
|
||||
self.error_code = Some(err);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -56,19 +69,17 @@ mod tests {
|
||||
use std::str::FromStr;
|
||||
use uuid::Uuid;
|
||||
use super::*;
|
||||
use crate::response::{Response, ResponsePayload, State};
|
||||
use crate::response::{Response, ResponsePayload};
|
||||
|
||||
#[test]
|
||||
fn serialize() {
|
||||
let mut query_resp = Payload::new();
|
||||
|
||||
let state = State::default();
|
||||
let mut device = Device::new(true, Status::Success);
|
||||
let mut device = Device::new();
|
||||
device.state.on = Some(true);
|
||||
query_resp.add_device("123", device);
|
||||
|
||||
let state = State::default();
|
||||
let mut device = Device::new(true, Status::Success);
|
||||
let mut device = Device::new();
|
||||
device.state.on = Some(false);
|
||||
query_resp.add_device("456", device);
|
||||
|
||||
|
||||
@@ -3,7 +3,7 @@ use serde_with::skip_serializing_none;
|
||||
|
||||
use crate::attributes::Attributes;
|
||||
use crate::device;
|
||||
use crate::errors::Errors;
|
||||
use crate::errors::ErrorCode;
|
||||
use crate::types::Type;
|
||||
use crate::traits::Trait;
|
||||
|
||||
@@ -12,7 +12,7 @@ use crate::traits::Trait;
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct Payload {
|
||||
agent_user_id: String,
|
||||
pub error_code: Option<Errors>,
|
||||
pub error_code: Option<ErrorCode>,
|
||||
pub debug_string: Option<String>,
|
||||
pub devices: Vec<Device>,
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user