From 9d1f6d0030cc83f67d656125bf5fdb986e510796 Mon Sep 17 00:00:00 2001 From: Andre Henriques Date: Sun, 9 Jul 2023 19:43:46 +0100 Subject: [PATCH] chore: add get to configuration --- flake.lock | 81 +++++++++++++++++++++++++++++++++ flake.nix | 53 ++++++++++++++++++++++ src/animations.rs | 10 +---- src/configure.rs | 112 ++++++++++++++++++++++++++++++++++++++++++++-- src/utils.rs | 11 ++++- 5 files changed, 254 insertions(+), 13 deletions(-) create mode 100644 flake.lock create mode 100644 flake.nix diff --git a/flake.lock b/flake.lock new file mode 100644 index 0000000..d43f6db --- /dev/null +++ b/flake.lock @@ -0,0 +1,81 @@ +{ + "nodes": { + "flake-utils": { + "inputs": { + "systems": "systems" + }, + "locked": { + "lastModified": 1681202837, + "narHash": "sha256-H+Rh19JDwRtpVPAWp64F+rlEtxUWBAQW28eAi3SRSzg=", + "owner": "numtide", + "repo": "flake-utils", + "rev": "cfacdce06f30d2b68473a46042957675eebb3401", + "type": "github" + }, + "original": { + "owner": "numtide", + "repo": "flake-utils", + "type": "github" + } + }, + "nixpkgs": { + "locked": { + "lastModified": 1688925019, + "narHash": "sha256-281HjmJycKt8rZ0/vpYTtJuZrQl6mpGNlUFf8cebmeA=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "2b356dae6208d422236c4cdc48f3bed749f9daea", + "type": "github" + }, + "original": { + "owner": "NixOS", + "repo": "nixpkgs", + "type": "github" + } + }, + "root": { + "inputs": { + "nixpkgs": "nixpkgs", + "rust-overlay": "rust-overlay" + } + }, + "rust-overlay": { + "inputs": { + "flake-utils": "flake-utils", + "nixpkgs": [ + "nixpkgs" + ] + }, + "locked": { + "lastModified": 1688870171, + "narHash": "sha256-8tD8fheWPa7TaJoxzcU3iHkCrQQpOpdMN+HYqgZ1N5A=", + "owner": "oxalica", + "repo": "rust-overlay", + "rev": "5a932f10ac4bd59047d6e8b5780750ec76ea988a", + "type": "github" + }, + "original": { + "owner": "oxalica", + "repo": "rust-overlay", + "type": "github" + } + }, + "systems": { + "locked": { + "lastModified": 1681028828, + "narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=", + "owner": "nix-systems", + "repo": "default", + "rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e", + "type": "github" + }, + "original": { + "owner": "nix-systems", + "repo": "default", + "type": "github" + } + } + }, + "root": "root", + "version": 7 +} diff --git a/flake.nix b/flake.nix new file mode 100644 index 0000000..0b55b19 --- /dev/null +++ b/flake.nix @@ -0,0 +1,53 @@ +{ + description = "monitors discord to controll lights"; + + inputs = { + nixpkgs.url = "github:NixOS/nixpkgs"; + rust-overlay.url = "github:oxalica/rust-overlay"; + rust-overlay.inputs.nixpkgs.follows = "nixpkgs"; + }; + + outputs = { self, nixpkgs, rust-overlay }: + let + # Systems supported + allSystems = [ + "x86_64-linux" # 64-bit Intel/AMD Linux + "aarch64-linux" # 64-bit ARM Linux + "x86_64-darwin" # 64-bit Intel macOS + "aarch64-darwin" # 64-bit ARM macOS + ]; + + # Helper to provide system-specific attributes + forAllSystems = f: nixpkgs.lib.genAttrs allSystems (system: f { + pkgs = import nixpkgs { inherit system; }; + }); + in + { + packages = forAllSystems ({ pkgs }: { + default = let + stdenv = pkgs.stdenv; + lib = pkgs.lib; + in + pkgs.rustPlatform.buildRustPackage { + pname = "lights"; + version = "0.1"; + src = ./.; + LIBCLANG_PATH="${pkgs.llvmPackages.libclang.lib}/lib"; + # doCheck = false; + # RUST_BACKTRACE=1; + buildInputs = with pkgs; [ + ]; + nativeBuildInputs = with pkgs; [ + pkg-config + clang + llvmPackages.libclang + rust-analyzer + ]; + cargoLock = { + lockFile = ./Cargo.lock; + }; + }; + }); + }; +} + diff --git a/src/animations.rs b/src/animations.rs index 4a07d60..096616b 100644 --- a/src/animations.rs +++ b/src/animations.rs @@ -8,7 +8,7 @@ use rocket::{ State, }; -use crate::DBPool; +use crate::{DBPool, utils::{get_error_message, create_message}}; use crate::{ utils::{ApiResponse, ApiResponseActiveList}, AReceiver, ASender, Action, DBConn, RenderMessage, @@ -570,14 +570,6 @@ pub async fn get_animation_request(name: &str, db: &State) -> String { } } -fn create_message(code: u32, message: &str) -> String { - return json!(ApiResponse {code, message: message.to_string()}).to_string(); -} - -fn get_error_message(message: &str) -> String { - return create_message(500, message); -} - /** * * Start animations diff --git a/src/configure.rs b/src/configure.rs index 8c60150..a586c68 100644 --- a/src/configure.rs +++ b/src/configure.rs @@ -1,6 +1,8 @@ -use rocket::{serde::{json::{Json, serde_json::json}, Deserialize, Serialize}, State}; +use r2d2::PooledConnection; +use r2d2_sqlite::{SqliteConnectionManager, rusqlite::Statement}; +use rocket::{serde::{json::{Json, serde_json::json, self}, Deserialize, Serialize}, State}; -use crate::{utils::ApiResponse, Action, ASender}; +use crate::{utils::{ApiResponse, get_error_message}, Action, ASender}; use crate::DBPool; #[derive(Debug, Deserialize, Serialize, Clone)] @@ -54,7 +56,7 @@ pub async fn configure(data: Json, db: &State return json!(ApiResponse {code: 500, message: "Clould not configure. Err: 3".to_string()}).to_string(); } - let version_iter = version_iter.ok().unwrap(); + let version_iter = version_iter.unwrap(); let mut recreate = false; @@ -100,3 +102,107 @@ pub async fn configure(data: Json, db: &State json!(ApiResponse {code: 200, message: "Configuration was successful".to_string()}).to_string() } + +type Conn = PooledConnection; + +fn get_conn(db: &State) -> Result { + let conn = db.get(); + if let Ok(conn) = conn { + return Ok(conn); + } + Err(json!(ApiResponse {code: 500, message: "Clould not conncect to the db.".to_string()}).to_string()) +} + +fn prepare_statement<'a>(conn: &'a Conn, stat: &str) -> Result, String> { + let stmt = conn.prepare(stat); + if stmt.is_err() || stmt.as_ref().ok().is_none() { + return Err(get_error_message("Could not construct statetment")); + } + let stmt: Statement<'a> = stmt.unwrap(); + return Ok(stmt); +} + +fn is_configured(conn: &Conn) -> Result { + let mut stmt = prepare_statement(conn, "SELECT configured from meta")?; + + struct Data { + configured: bool, + } + + let version_iter = stmt.query_map([], |row| { + Ok(Data { + configured: row.get(0)? + }) + }); + + if version_iter.is_err() || version_iter.as_ref().ok().is_none() { + return Err(json!(ApiResponse {code: 500, message: "Clould not get data of db".to_string()}).to_string()); + } + + let mut version_iter = version_iter.ok().unwrap(); + + if version_iter.by_ref().count() > 1 { + return Err(json!(ApiResponse {code: 500, message: "Change something is very wrong!".to_string()}).to_string()); + } + + return Ok(version_iter.next().unwrap().unwrap().configured); +} + +#[get("/configure", data= "")] +pub async fn get_configure(data: Json, db: &State, action: &State>) -> String { + + let conn = get_conn(db); + if let Err(e) = conn { + return e; + } + let conn = conn.unwrap(); + + let configured = is_configured(&conn); + if let Err(e) = configured { + return e; + } + let configured = configured.unwrap(); + + if !configured { + return get_error_message("Not yet configured"); + } + + let stmt = prepare_statement(&conn, "SELECT id ledcount directionX directionY directionZ tags from configuration;"); + if let Err(e) = stmt { + return e; + } + let mut stmt = stmt.unwrap(); + + #[derive(Deserialize, Serialize)] + struct Data { + id: u32, + ledcount: u32, + direction_x: u32, + direction_y: u32, + direction_z: u32, + tags: String, + } + + let configuration = stmt.query_map([], |row| { + Ok(Data { + id: row.get(0)?, + ledcount: row.get(1)?, + direction_x: row.get(2)?, + direction_y: row.get(3)?, + direction_z: row.get(4)?, + tags: row.get(5)?, + }) + }); + + if configuration.is_err() || configuration.as_ref().ok().is_none() { + return json!(ApiResponse {code: 500, message: "Clould not get data of db".to_string()}).to_string(); + } + + let configuration = configuration.unwrap(); + + let data: Vec = configuration.into_iter().map(|data| { + return data.unwrap(); + }).collect(); + + json!(data).to_string() +} diff --git a/src/utils.rs b/src/utils.rs index 912a376..9455fa4 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -1,4 +1,4 @@ -use rocket::{serde::{Deserialize, Serialize}, Responder}; +use rocket::{serde::{Deserialize, Serialize, json::{self, serde_json::json}}, Responder}; #[derive(Deserialize, Serialize)] #[serde(crate="rocket::serde")] @@ -16,3 +16,12 @@ pub struct ApiResponseActiveList { #[derive(Responder)] pub struct ApiResponder(String); + + +pub fn create_message(code: u32, message: &str) -> String { + return json!(ApiResponse {code, message: message.to_string()}).to_string(); +} + +pub fn get_error_message(message: &str) -> String { + return create_message(500, message); +}