Finished basic google home implementation with some slight refactors along the way
This commit is contained in:
@@ -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);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user