chore: add get to configuration
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Andre Henriques 2023-07-09 19:43:46 +01:00
parent 578ed1373b
commit 9d1f6d0030
5 changed files with 254 additions and 13 deletions

81
flake.lock Normal file
View File

@ -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
}

53
flake.nix Normal file
View File

@ -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;
};
};
});
};
}

View File

@ -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<DBPool>) -> 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

View File

@ -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<LightConfigurationRequest>, db: &State<DBPool>
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<LightConfigurationRequest>, db: &State<DBPool>
json!(ApiResponse {code: 200, message: "Configuration was successful".to_string()}).to_string()
}
type Conn = PooledConnection<SqliteConnectionManager>;
fn get_conn(db: &State<DBPool>) -> Result<Conn, String> {
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<Statement<'a>, 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<bool, String> {
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= "<data>")]
pub async fn get_configure(data: Json<LightConfigurationRequest>, db: &State<DBPool>, action: &State<ASender<Action>>) -> 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<Data> = configuration.into_iter().map(|data| {
return data.unwrap();
}).collect();
json!(data).to_string()
}

View File

@ -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);
}