Implemented data structures for SYNC, QUERY, and EXECUTE
This commit is contained in:
83
google-home/src/response/query.rs
Normal file
83
google-home/src/response/query.rs
Normal file
@@ -0,0 +1,83 @@
|
||||
use std::collections::HashMap;
|
||||
|
||||
use serde::Serialize;
|
||||
|
||||
use crate::response::State;
|
||||
|
||||
#[derive(Debug, Serialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct Payload {
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub error_code: Option<String>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub debug_string: Option<String>,
|
||||
devices: HashMap<String, Device>,
|
||||
}
|
||||
|
||||
impl Payload {
|
||||
pub fn new() -> Self {
|
||||
Self { error_code: None, debug_string: None, devices: HashMap::new() }
|
||||
}
|
||||
|
||||
pub fn add_device(&mut self, id: &str, device: Device) {
|
||||
self.devices.insert(id.into(), device);
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize)]
|
||||
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
|
||||
pub enum Status {
|
||||
Success,
|
||||
Offline,
|
||||
Exceptions,
|
||||
Error,
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct Device {
|
||||
online: bool,
|
||||
status: Status,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub error_code: Option<String>,
|
||||
|
||||
#[serde(flatten)]
|
||||
pub state: Option<State>,
|
||||
}
|
||||
|
||||
impl Device {
|
||||
pub fn new(online: bool, status: Status) -> Self {
|
||||
Self { online, status, error_code: None, state: None }
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use std::str::FromStr;
|
||||
use uuid::Uuid;
|
||||
use super::*;
|
||||
use crate::response::{Response, ResponsePayload, State};
|
||||
|
||||
#[test]
|
||||
fn serialize() {
|
||||
let mut query_resp = Payload::new();
|
||||
|
||||
let state = State::default().on(true);
|
||||
let mut device = Device::new(true, Status::Success);
|
||||
device.state = Some(state);
|
||||
query_resp.add_device("123", device);
|
||||
|
||||
let state = State::default().on(false);
|
||||
let mut device = Device::new(true, Status::Success);
|
||||
device.state = Some(state);
|
||||
query_resp.add_device("456", device);
|
||||
|
||||
let resp = Response::new(Uuid::from_str("ff36a3cc-ec34-11e6-b1a0-64510650abcf").unwrap(), ResponsePayload::Query(query_resp));
|
||||
|
||||
let json = serde_json::to_string(&resp).unwrap();
|
||||
|
||||
println!("{}", json);
|
||||
|
||||
// @TODO Add a known correct output to test against
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user