Updated to newest rust nightly
This commit is contained in:
parent
2cf4e40ad5
commit
6c797820dc
|
@ -1,4 +1,4 @@
|
||||||
[toolchain]
|
[toolchain]
|
||||||
channel = "nightly-2023-11-15"
|
channel = "nightly-2024-07-25"
|
||||||
components = ["rustfmt", "clippy"]
|
components = ["rustfmt", "clippy", "rust-analyzer"]
|
||||||
profile = "minimal"
|
profile = "minimal"
|
||||||
|
|
|
@ -45,6 +45,13 @@ pub trait LuaDeviceCreate {
|
||||||
}
|
}
|
||||||
|
|
||||||
macro_rules! register_device {
|
macro_rules! register_device {
|
||||||
|
($lua:expr, $device:ty) => {
|
||||||
|
$lua.globals()
|
||||||
|
.set(stringify!($device), $lua.create_proxy::<$device>()?)?;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
macro_rules! impl_device {
|
||||||
($lua:expr, $device:ty) => {
|
($lua:expr, $device:ty) => {
|
||||||
impl mlua::UserData for $device {
|
impl mlua::UserData for $device {
|
||||||
fn add_methods<'lua, M: mlua::UserDataMethods<'lua, Self>>(methods: &mut M) {
|
fn add_methods<'lua, M: mlua::UserDataMethods<'lua, Self>>(methods: &mut M) {
|
||||||
|
@ -60,12 +67,23 @@ macro_rules! register_device {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$lua.globals()
|
|
||||||
.set(stringify!($device), $lua.create_proxy::<$device>()?)?;
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl_device!(lua, AirFilter);
|
||||||
|
impl_device!(lua, AudioSetup);
|
||||||
|
impl_device!(lua, ContactSensor);
|
||||||
|
impl_device!(lua, DebugBridge);
|
||||||
|
impl_device!(lua, HueBridge);
|
||||||
|
impl_device!(lua, HueGroup);
|
||||||
|
impl_device!(lua, IkeaOutlet);
|
||||||
|
impl_device!(lua, KasaOutlet);
|
||||||
|
impl_device!(lua, LightSensor);
|
||||||
|
impl_device!(lua, Ntfy);
|
||||||
|
impl_device!(lua, Presence);
|
||||||
|
impl_device!(lua, WakeOnLAN);
|
||||||
|
impl_device!(lua, Washer);
|
||||||
|
|
||||||
pub fn register_with_lua(lua: &mlua::Lua) -> mlua::Result<()> {
|
pub fn register_with_lua(lua: &mlua::Lua) -> mlua::Result<()> {
|
||||||
register_device!(lua, AirFilter);
|
register_device!(lua, AirFilter);
|
||||||
register_device!(lua, AudioSetup);
|
register_device!(lua, AudioSetup);
|
||||||
|
|
46
src/main.rs
46
src/main.rs
|
@ -1,4 +1,3 @@
|
||||||
#![feature(async_closure)]
|
|
||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
use std::process;
|
use std::process;
|
||||||
|
|
||||||
|
@ -9,13 +8,12 @@ use automation::device_manager::DeviceManager;
|
||||||
use automation::error::ApiError;
|
use automation::error::ApiError;
|
||||||
use automation::mqtt::{self, WrappedAsyncClient};
|
use automation::mqtt::{self, WrappedAsyncClient};
|
||||||
use automation::{devices, LUA};
|
use automation::{devices, LUA};
|
||||||
use axum::extract::FromRef;
|
use axum::extract::{FromRef, State};
|
||||||
use axum::http::StatusCode;
|
use axum::http::StatusCode;
|
||||||
use axum::response::IntoResponse;
|
|
||||||
use axum::routing::post;
|
use axum::routing::post;
|
||||||
use axum::{Json, Router};
|
use axum::{Json, Router};
|
||||||
use dotenvy::dotenv;
|
use dotenvy::dotenv;
|
||||||
use google_home::{GoogleHome, Request};
|
use google_home::{GoogleHome, Request, Response};
|
||||||
use mlua::LuaSerdeExt;
|
use mlua::LuaSerdeExt;
|
||||||
use rumqttc::AsyncClient;
|
use rumqttc::AsyncClient;
|
||||||
use tracing::{debug, error, info, warn};
|
use tracing::{debug, error, info, warn};
|
||||||
|
@ -23,6 +21,7 @@ use tracing::{debug, error, info, warn};
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
struct AppState {
|
struct AppState {
|
||||||
pub openid_url: String,
|
pub openid_url: String,
|
||||||
|
pub device_manager: DeviceManager,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl FromRef<AppState> for String {
|
impl FromRef<AppState> for String {
|
||||||
|
@ -44,6 +43,24 @@ async fn main() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async fn fulfillment(
|
||||||
|
State(state): State<AppState>,
|
||||||
|
user: User,
|
||||||
|
Json(payload): Json<Request>,
|
||||||
|
) -> Result<Json<Response>, ApiError> {
|
||||||
|
debug!(username = user.preferred_username, "{payload:#?}");
|
||||||
|
let gc = GoogleHome::new(&user.preferred_username);
|
||||||
|
let devices = state.device_manager.devices().await;
|
||||||
|
let result = gc
|
||||||
|
.handle_request(payload, &devices)
|
||||||
|
.await
|
||||||
|
.map_err(|err| ApiError::new(StatusCode::INTERNAL_SERVER_ERROR, err.into()))?;
|
||||||
|
|
||||||
|
debug!(username = user.preferred_username, "{result:#?}");
|
||||||
|
|
||||||
|
Ok(Json(result))
|
||||||
|
}
|
||||||
|
|
||||||
async fn app() -> anyhow::Result<()> {
|
async fn app() -> anyhow::Result<()> {
|
||||||
dotenv().ok();
|
dotenv().ok();
|
||||||
|
|
||||||
|
@ -119,31 +136,14 @@ async fn app() -> anyhow::Result<()> {
|
||||||
};
|
};
|
||||||
|
|
||||||
// Create google home fulfillment route
|
// Create google home fulfillment route
|
||||||
let fulfillment = Router::new().route(
|
let fulfillment = Router::new().route("/google_home", post(fulfillment));
|
||||||
"/google_home",
|
|
||||||
post(async move |user: User, Json(payload): Json<Request>| {
|
|
||||||
debug!(username = user.preferred_username, "{payload:#?}");
|
|
||||||
let gc = GoogleHome::new(&user.preferred_username);
|
|
||||||
let devices = device_manager.devices().await;
|
|
||||||
let result = match gc.handle_request(payload, &devices).await {
|
|
||||||
Ok(result) => result,
|
|
||||||
Err(err) => {
|
|
||||||
return ApiError::new(StatusCode::INTERNAL_SERVER_ERROR, err.into())
|
|
||||||
.into_response()
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
debug!(username = user.preferred_username, "{result:#?}");
|
|
||||||
|
|
||||||
(StatusCode::OK, Json(result)).into_response()
|
|
||||||
}),
|
|
||||||
);
|
|
||||||
|
|
||||||
// Combine together all the routes
|
// Combine together all the routes
|
||||||
let app = Router::new()
|
let app = Router::new()
|
||||||
.nest("/fulfillment", fulfillment)
|
.nest("/fulfillment", fulfillment)
|
||||||
.with_state(AppState {
|
.with_state(AppState {
|
||||||
openid_url: fulfillment_config.openid_url.clone(),
|
openid_url: fulfillment_config.openid_url.clone(),
|
||||||
|
device_manager,
|
||||||
});
|
});
|
||||||
|
|
||||||
// Start the web server
|
// Start the web server
|
||||||
|
|
Loading…
Reference in New Issue
Block a user