moved functions to seperate file and query the 4 main table data (throttle, brake, speed and steering)
This commit is contained in:
56
src/array_utils.c
Normal file
56
src/array_utils.c
Normal file
@@ -0,0 +1,56 @@
|
|||||||
|
#include <duckdb.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
#include "arrays.h"
|
||||||
|
|
||||||
|
void allocate_array_for_db_data(float_array *array, const char *table, duckdb_connection *conn) {
|
||||||
|
char query[100];
|
||||||
|
sprintf(query, "Select count(*) from \"%s\"", table);
|
||||||
|
duckdb_result result;
|
||||||
|
duckdb_query(*conn, query, &result);
|
||||||
|
|
||||||
|
duckdb_data_chunk chunk = duckdb_fetch_chunk(result);
|
||||||
|
if (!chunk) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
duckdb_vector col1 = duckdb_data_chunk_get_vector(chunk, 0);
|
||||||
|
int32_t *col1_data = (int32_t *)duckdb_vector_get_data(col1);
|
||||||
|
uint64_t *col1_validity = duckdb_vector_get_validity(col1);
|
||||||
|
|
||||||
|
if (duckdb_validity_row_is_valid(col1_validity, 0)) {
|
||||||
|
printf("capycity of array: %d\n", col1_data[0]);
|
||||||
|
float_array_allocate(array, col1_data[0]);
|
||||||
|
}
|
||||||
|
duckdb_destroy_data_chunk(&chunk);
|
||||||
|
duckdb_destroy_result(&result);
|
||||||
|
}
|
||||||
|
|
||||||
|
void get_data_from_db(const char *query, duckdb_connection *conn, float_array *info) {
|
||||||
|
duckdb_result result;
|
||||||
|
|
||||||
|
duckdb_query(*conn, query, &result);
|
||||||
|
|
||||||
|
while (true) {
|
||||||
|
duckdb_data_chunk data_chunk = duckdb_fetch_chunk(result);
|
||||||
|
if (!data_chunk) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
idx_t row_count = duckdb_data_chunk_get_size(data_chunk); // number of rows from the data chunk
|
||||||
|
|
||||||
|
// set column
|
||||||
|
duckdb_vector col1 = duckdb_data_chunk_get_vector(data_chunk, 0);
|
||||||
|
void *col1_data = duckdb_vector_get_data(col1);
|
||||||
|
uint64_t *col1_validity = duckdb_vector_get_validity(col1);
|
||||||
|
|
||||||
|
for (idx_t row = 0; row < row_count; row++) {
|
||||||
|
if (duckdb_validity_row_is_valid(col1_validity, row)) {
|
||||||
|
float_array_set(info, ((float *)col1_data)[row], -1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
duckdb_destroy_data_chunk(&data_chunk);
|
||||||
|
}
|
||||||
|
duckdb_destroy_result(&result);
|
||||||
|
}
|
||||||
6
src/array_utils.h
Normal file
6
src/array_utils.h
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
#include <duckdb.h>
|
||||||
|
|
||||||
|
#include "arrays.h"
|
||||||
|
|
||||||
|
void allocate_array_for_db_data(float_array *array, const char *table, duckdb_connection *conn);
|
||||||
|
void get_data_from_db(const char *query, duckdb_connection *conn, float_array *info);
|
||||||
85
src/main.c
85
src/main.c
@@ -5,6 +5,7 @@
|
|||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
|
|
||||||
|
#include "array_utils.h"
|
||||||
#include "arrays.h"
|
#include "arrays.h"
|
||||||
#include "exit_code.h"
|
#include "exit_code.h"
|
||||||
#include "lapinfo.h"
|
#include "lapinfo.h"
|
||||||
@@ -31,58 +32,6 @@ bool is_valid_path(const char *path) {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void allocate_array_for_db_data(float_array *array, const char *table, duckdb_connection *conn) {
|
|
||||||
char query[100];
|
|
||||||
sprintf(query, "Select count(*) from \"%s\"", table);
|
|
||||||
duckdb_result result;
|
|
||||||
duckdb_query(*conn, query, &result);
|
|
||||||
|
|
||||||
duckdb_data_chunk chunk = duckdb_fetch_chunk(result);
|
|
||||||
if (!chunk) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
duckdb_vector col1 = duckdb_data_chunk_get_vector(chunk, 0);
|
|
||||||
int32_t *col1_data = (int32_t *)duckdb_vector_get_data(col1);
|
|
||||||
uint64_t *col1_validity = duckdb_vector_get_validity(col1);
|
|
||||||
|
|
||||||
if (duckdb_validity_row_is_valid(col1_validity, 0)) {
|
|
||||||
printf("capycity of array: %d", col1_data[0]);
|
|
||||||
float_array_allocate(array, col1_data[0]);
|
|
||||||
}
|
|
||||||
duckdb_destroy_data_chunk(&chunk);
|
|
||||||
duckdb_destroy_result(&result);
|
|
||||||
}
|
|
||||||
|
|
||||||
void get_data_from_db(const char *query, duckdb_connection *conn, float_array *info) {
|
|
||||||
duckdb_result result;
|
|
||||||
|
|
||||||
duckdb_query(*conn, query, &result);
|
|
||||||
|
|
||||||
while (true) {
|
|
||||||
duckdb_data_chunk data_chunk = duckdb_fetch_chunk(result);
|
|
||||||
if (!data_chunk) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
idx_t row_count = duckdb_data_chunk_get_size(data_chunk); // number of rows from the data chunk
|
|
||||||
|
|
||||||
// set column
|
|
||||||
duckdb_vector col1 = duckdb_data_chunk_get_vector(data_chunk, 0);
|
|
||||||
void *col1_data = duckdb_vector_get_data(col1);
|
|
||||||
uint64_t *col1_validity = duckdb_vector_get_validity(col1);
|
|
||||||
|
|
||||||
for (idx_t row = 0; row < row_count; row++) {
|
|
||||||
if (duckdb_validity_row_is_valid(col1_validity, row)) {
|
|
||||||
float_array_set(info, ((float *)col1_data)[row], -1);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
duckdb_destroy_data_chunk(&data_chunk);
|
|
||||||
}
|
|
||||||
duckdb_destroy_result(&result);
|
|
||||||
}
|
|
||||||
|
|
||||||
int main(int argc, char **argv) {
|
int main(int argc, char **argv) {
|
||||||
duckdb_database db;
|
duckdb_database db;
|
||||||
duckdb_connection conn;
|
duckdb_connection conn;
|
||||||
@@ -195,10 +144,38 @@ int main(int argc, char **argv) {
|
|||||||
allocate_array_for_db_data(&throttle_data_all, "Throttle Pos", &conn);
|
allocate_array_for_db_data(&throttle_data_all, "Throttle Pos", &conn);
|
||||||
get_data_from_db("Select value as FLOAT from \"Throttle Pos\"\n", &conn, &throttle_data_all);
|
get_data_from_db("Select value as FLOAT from \"Throttle Pos\"\n", &conn, &throttle_data_all);
|
||||||
for (int32_t i = 0; i < throttle_data_all.length; ++i) {
|
for (int32_t i = 0; i < throttle_data_all.length; ++i) {
|
||||||
printf(" %f", float_array_get(&throttle_data_all, i));
|
// printf(" %f", float_array_get(&throttle_data_all, i));
|
||||||
}
|
}
|
||||||
|
|
||||||
printf("Shutting down HardCompound!\n");
|
// brake
|
||||||
|
float_array brake_data_all;
|
||||||
|
float_array_init(&brake_data_all);
|
||||||
|
allocate_array_for_db_data(&brake_data_all, "Brake Pos", &conn);
|
||||||
|
get_data_from_db("Select value as FLOAT from \"Brake Pos\"\n", &conn, &brake_data_all);
|
||||||
|
for (int32_t i = 0; i < brake_data_all.length; ++i) {
|
||||||
|
// printf(" %f", float_array_get(&brake_data_all, i));
|
||||||
|
}
|
||||||
|
|
||||||
|
// steering
|
||||||
|
float_array steering_data_all;
|
||||||
|
float_array_init(&steering_data_all);
|
||||||
|
allocate_array_for_db_data(&steering_data_all, "Steering Pos", &conn);
|
||||||
|
get_data_from_db("Select value as FLOAT from \"Steering Pos\"\n", &conn, &steering_data_all);
|
||||||
|
for (int32_t i = 0; i < steering_data_all.length; ++i) {
|
||||||
|
// printf(" %f", float_array_get(&steering_data_all, i));
|
||||||
|
}
|
||||||
|
// select avg(((value1+value2+value3+value4)/4)*3.6) as speed from "Wheel Speed"
|
||||||
|
// speed
|
||||||
|
float_array speed_data_all;
|
||||||
|
float_array_init(&speed_data_all);
|
||||||
|
allocate_array_for_db_data(&speed_data_all, "Wheel Speed", &conn);
|
||||||
|
get_data_from_db("select ((value1+value2+value3+value4)/4)*3.6 as speed from \"Wheel Speed\"", &conn,
|
||||||
|
&speed_data_all);
|
||||||
|
for (int32_t i = 0; i < speed_data_all.length; ++i) {
|
||||||
|
// printf(" %f", float_array_get(&speed_data_all, i));
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("\nShutting down HardCompound!\n");
|
||||||
|
|
||||||
printf("Closing database and any connection.\n");
|
printf("Closing database and any connection.\n");
|
||||||
goto cleanup;
|
goto cleanup;
|
||||||
|
|||||||
Reference in New Issue
Block a user