1 Commits
Author SHA1 Message Date
Paul Dino Jones b3331ba599 Add thread support to gilles 2023-04-15 08:32:25 +00:00
16 changed files with 178 additions and 2063 deletions
-10
View File
@@ -1,10 +0,0 @@
{
argtable arg_parse
Memcheck:Leak
...
...
...
fun:arg_parse
...
...
}
+1 -2
View File
@@ -24,8 +24,7 @@ add_subdirectory(src/gilles/helper)
add_subdirectory(src/gilles/slog) add_subdirectory(src/gilles/slog)
add_executable(gilles src/gilles/gilles.c) add_executable(gilles src/gilles/gilles.c)
target_link_libraries(gilles xdg-basedir m ncursesw argtable2 config gameloop helper slog simulatorapi eclipse-paho-mqtt-c::paho-mqtt3c pthread hoel jansson) target_link_libraries(gilles m ncursesw argtable2 config gameloop helper slog simulatorapi eclipse-paho-mqtt-c::paho-mqtt3c pthread)
add_compile_definitions(gilles SIMMAP_ALL)
# used for enabling additional compiler options if supported # used for enabling additional compiler options if supported
include(CheckCXXCompilerFlag) include(CheckCXXCompilerFlag)
+3
View File
@@ -3,6 +3,9 @@ set(gameloop_source_files
gameloop.h gameloop.h
) )
set(LIBXML_INCLUDE_DIR /usr/include/libxml2)
include_directories("." ${LIBXML_INCLUDE_DIR})
add_library(gameloop STATIC ${gameloop_source_files}) add_library(gameloop STATIC ${gameloop_source_files})
-865
View File
@@ -1,865 +0,0 @@
#include <stdbool.h>
#include <stdio.h>
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <ncurses.h>
#include <signal.h>
#include <time.h>
#include <sys/time.h>
#include <MQTTClient.h>
#include <poll.h>
#include <termios.h>
#include <hoel.h>
#include <jansson.h>
#include "session.h"
#include "gameloop.h"
#include "../helper/parameters.h"
#include "../helper/confighelper.h"
#include "../helper/dirhelper.h"
#include "../simulatorapi/simapi/simapi/simdata.h"
#include "../simulatorapi/simapi/simapi/simmapper.h"
#include "../slog/slog.h"
#define DEFAULT_UPDATE_RATE 60
#define DATA_UPDATE_RATE 4
#define SESSIONS_SCREEN 1
#define STINTS_SCREEN 2
#define LAPS_SCREEN 3
char datestring[30];
WINDOW* win1;
WINDOW* win2;
WINDOW* win3;
WINDOW* win4;
int winx, winy;
int win23y, win23x;
void handle_winch(int sig)
{
endwin();
refresh();
clear();
getmaxyx(stdscr, winx, winy);
//win23y = winy/3;
//win23x = winx/3;
//win1 = newwin(winx,winy,0,0);
//win2 = newwin(win23x,win23y,1,win23y-1);
//win3 = newwin(win23x,win23y,1,win23y*2-1);
//win4 = newwin(winx-win23x-2,winy-win23y,win23x+1,win23y-1);
refresh();
}
void rectangle(int y1, int x1, int y2, int x2)
{
mvhline(y1, x1, 0, x2-x1);
mvhline(y2, x1, 0, x2-x1);
mvvline(y1, x1, 0, y2-y1);
mvvline(y1, x2, 0, y2-y1);
mvaddch(y1, x1, ACS_ULCORNER);
mvaddch(y2, x1, ACS_LLCORNER);
mvaddch(y1, x2, ACS_URCORNER);
mvaddch(y2, x2, ACS_LRCORNER);
}
int curses_init()
{
initscr();
start_color();
init_pair(1,COLOR_GREEN,0);
init_pair(2,COLOR_YELLOW,0);
init_pair(3,COLOR_MAGENTA,0);
init_pair(4,COLOR_WHITE,0);
getmaxyx(stdscr, winx, winy);
win1 = newwin(winx,winy,0,0);
//win23y = winy/3;
//win23x = winx/3;
//win2 = newwin(win23x,win23y,1,win23y-1);
//win3 = newwin(win23x,win23y,1,win23y*2-1);
//win4 = newwin(winx-win23x-2,winy-win23y,win23x+1,win23y-1);
wbkgd(win1,COLOR_PAIR(1));
wbkgd(win2,COLOR_PAIR(1));
wbkgd(win3,COLOR_PAIR(1));
wbkgd(win4,COLOR_PAIR(1));
signal(SIGWINCH, handle_winch);
cbreak();
noecho();
box(win1, 0, 0);
box(win2, 0, 0);
box(win3, 0, 0);
box(win4, 0, 0);
}
char * removeSpacesFromStr(char *string)
{
int non_space_count = 0;
for (int i = 0; string[i] != '\0'; i++)
{
if (string[i] != ' ')
{
string[non_space_count] = string[i];
non_space_count++;
}
}
string[non_space_count] = '\0';
return string;
}
void update_date()
{
time_t rawtime;
struct tm * timeinfo;
time ( &rawtime );
timeinfo = localtime ( &rawtime );
sprintf(datestring, "%.24s", asctime (timeinfo));
}
void print_result(struct _h_result result) {
int col, row, i;
char buf[64];
slogi("rows: %d, col: %d", result.nb_rows, result.nb_columns);
for (row = 0; row<result.nb_rows; row++) {
for (col=0; col<result.nb_columns; col++) {
switch(result.data[row][col].type) {
case HOEL_COL_TYPE_INT:
printf("| %d ", ((struct _h_type_int *)result.data[row][col].t_data)->value);
break;
case HOEL_COL_TYPE_DOUBLE:
printf("| %f ", ((struct _h_type_double *)result.data[row][col].t_data)->value);
break;
case HOEL_COL_TYPE_TEXT:
printf("| %s ", ((struct _h_type_text *)result.data[row][col].t_data)->value);
break;
case HOEL_COL_TYPE_BLOB:
for (i=0; i<((struct _h_type_blob *)result.data[row][col].t_data)->length; i++) {
printf("%c", *((char*)(((struct _h_type_blob *)result.data[row][col].t_data)->value+i)));
if (i%80 == 0 && i>0) {
printf("\n");
}
}
break;
case HOEL_COL_TYPE_DATE:
strftime(buf, 64, "%Y-%m-%d %H:%M:%S", &((struct _h_type_datetime *)result.data[row][col].t_data)->value);
printf("| %s ", buf);
case HOEL_COL_TYPE_NULL:
printf("| [null] ");
break;
}
}
printf("|\n");
}
}
void get_row_results(struct _h_result result, DBField* fields, void* rows, size_t rowsize) {
int col, row, i;
char buf[64];
//sess->hasdata = true;
//sess->rows = result.nb_rows;
slogi("rows: %d, col: %d", result.nb_rows, result.nb_columns);
char* aaa = (char *) rows;
for (row = 0; row<result.nb_rows; row++) {
char* aa = (char*)aaa + (rowsize * row);
for (col=0; col<result.nb_columns; col++) {
char* a = (char*) aa + fields[col].offset;
switch(result.data[row][col].type) {
case HOEL_COL_TYPE_INT:
int bb = ((struct _h_type_int *)result.data[row][col].t_data)->value;
*(int*) a = bb;
break;
case HOEL_COL_TYPE_DOUBLE:
double cc = ((struct _h_type_double *)result.data[row][col].t_data)->value;
*(double*) a = cc;
break;
case HOEL_COL_TYPE_TEXT:
char* ddd = ((struct _h_type_text *)result.data[row][col].t_data)->value;
memcpy(a, ddd, fields[col].size);
break;
case HOEL_COL_TYPE_BLOB:
for (i=0; i<((struct _h_type_blob *)result.data[row][col].t_data)->length; i++) {
printf("%c", *((char*)(((struct _h_type_blob *)result.data[row][col].t_data)->value+i)));
if (i%80 == 0 && i>0) {
printf("\n");
}
}
break;
case HOEL_COL_TYPE_DATE:
strftime(a, fields[col].size, "%Y-%m-%d %H:%M:%S", &((struct _h_type_datetime *)result.data[row][col].t_data)->value);
//strftime(buf, 64, "%Y-%m-%d %H:%M:%S", &((struct _h_type_datetime *)result.data[row][col].t_data)->value);
printf("| %s ", buf);
case HOEL_COL_TYPE_NULL:
printf("| [null] ");
break;
}
}
printf("|\n");
}
}
int getsessions(struct _h_connection* conn, const char* sessionname, SessionDbo* sess)
{
struct _h_result result;
struct _h_data * data;
char* query = malloc(99 * sizeof(char));
slogt("Performing query");
//sprintf(query, "select session_id, event_id, event_type, duration_min, elapsed_ms, laps, air_temp, road_temp, start_grip, current_grip, is_finished, http_port from %s", "Sessions");
sprintf(query, "select session_id, event_id, event_type, laps FROM %s", "Sessions");
if (h_query_select(conn, query, &result) == H_OK) {
sess->rows = malloc(sizeof(SessionRowData) * result.nb_rows);
get_row_results(result, sess->fields, sess->rows, sizeof(SessionRowData));
//get_session_result(result, sess);
h_clean_result(&result);
} else {
printf("Error executing query\n");
}
free(query);
return result.nb_rows;
}
int getstints(struct _h_connection* conn, const char* sessionname, StintDbo* stint, int use_id)
{
struct _h_result result;
struct _h_data * data;
char* query = malloc(150 * sizeof(char));
slogt("Performing query stints");
sprintf(query, "select stint_id, driver_id, team_member_id, session_id, car_id, game_car_id, laps, valid_laps, best_lap_id FROM %s WHERE session_id=%i", "Stints", use_id);
if (h_query_select(conn, query, &result) == H_OK) {
stint->rows = malloc(sizeof(StintRowData) * result.nb_rows);
get_row_results(result, stint->fields, stint->rows, sizeof(StintRowData));
//get_stint_result(result, stint);
h_clean_result(&result);
} else {
printf("Error executing query\n");
}
free(query);
return result.nb_rows;
}
int getlaps(struct _h_connection* conn, const char* sessionname, LapDbo* laps, int use_id)
{
struct _h_result result;
struct _h_data * data;
char* query = malloc(150 * sizeof(char));
slogt("Performing query laps");
sprintf(query, "select * FROM %s WHERE stint_id=%i", "Laps", use_id);
if (h_query_select(conn, query, &result) == H_OK) {
laps->rows = malloc(sizeof(LapRowData) * result.nb_rows);
get_row_results(result, laps->fields, laps->rows, sizeof(LapRowData));
//get_stint_result(result, stint);
h_clean_result(&result);
} else {
printf("Error executing query\n");
}
free(query);
return result.nb_rows;
}
//int getsessions(struct _h_connection* conn, const char* carname)
//{
// json_t *j_result;
// char* where_clause = h_build_where_clause(conn, "car_name=%s", carname);
// json_t* j_query = json_pack("{sss[s]s{s{ssss}}}","table", "sessions", "columns", "session_id", "session_name", "laps",
// "where", " ", "operator", "raw",
// "value", where_clause);
//
// slogi("Looking for car named %s", carname);
// //char* qq;
// int res = h_select(conn, j_query, &j_result, NULL);
// //slogi("here your query: %s", qq);
// // Deallocate j_query since it won't be needed anymore
// json_decref(j_query);
// h_free(where_clause);
// int session_id = -1;
// int sessions = 0;
// // Test query execution result
// if (res == H_OK) {
// // Print result
// char* dump = json_dumps(j_result, JSON_INDENT(2));
// slogi("json select result is\n%s", dump);
// int index1 = json_array_size(j_result);
// sessions = index1;
// if (index1 == 0)
// {
// slogw("no car by this name");
// }
// else {
// for (int k = 0; k < index1; k++)
// {
// json_t* jj = json_array_get(j_result, k);
// session_id = json_integer_value(json_object_get(jj, "session_id"));
// int laps = json_integer_value(json_object_get(jj, "laps"));
// slogt("found session %i with %i laps", session_id, laps);
// }
// }
// // Deallocate data result
// json_decref(j_result);
// free(dump);
// } else {
// sloge("Error executing select query: %d", res);
// }
// return sessions;
//}
void* browseloop(Parameters* p)
{
struct _h_result result;
struct _h_connection * conn;
char* connectionstring = "host=zorak.brak dbname=gilles user=test password=thisisatest";
conn = h_connect_pgsql(connectionstring);
slogt("Starting analyzer");
curses_init();
timeout(DEFAULT_UPDATE_RATE);
SessionDbo sess;
DBField sessid;
sessid.type = HOEL_COL_TYPE_INT;
sessid.offset = 0;
sessid.colnum = 0;
DBField laps;
laps.type = HOEL_COL_TYPE_INT;
laps.offset = sizeof(int);
laps.offset = offsetof(SessionRowData, laps);
laps.colnum = 1;
DBField eventid;
eventid.type = HOEL_COL_TYPE_INT;
eventid.offset = offsetof(SessionRowData, event_id);
DBField eventtype;
eventtype.type = HOEL_COL_TYPE_INT;
eventtype.offset = offsetof(SessionRowData, event_type);
//SessionFields sf;
sess.fields[0] = sessid;
sess.fields[1] = eventid;
sess.fields[2] = eventtype;
sess.fields[3] = laps;
//sf.session_id = sessid;
//sf.laps = laps;
//sess.fields = sf;
int action = 0;
int selection = 1;
int lastselection = 1;
int selection1 = 0;
int selection2 = 0;
int sessions = 0;
int lapsresults = 0;
int curresults = 0;
int stintsid = 0;
StintDbo stints;
DBField stintid;
stintid.type = HOEL_COL_TYPE_INT;
stintid.offset = 0;
DBField driverid;
driverid.type = HOEL_COL_TYPE_INT;
driverid.offset = offsetof(StintRowData, driver_id);
DBField teammemberid;
teammemberid.type = HOEL_COL_TYPE_INT;
teammemberid.offset = offsetof(StintRowData, team_member_id);
DBField sessionidstint;
sessionidstint.type = HOEL_COL_TYPE_INT;
sessionidstint.offset = offsetof(StintRowData, session_id);
DBField carid;
carid.type = HOEL_COL_TYPE_INT;
carid.offset = offsetof(StintRowData, car_id);
DBField gamecarid;
gamecarid.type = HOEL_COL_TYPE_INT;
gamecarid.offset = offsetof(StintRowData, game_car_id);
DBField stintlaps;
stintlaps.type = HOEL_COL_TYPE_INT;
stintlaps.offset = offsetof(StintRowData, laps);
DBField validlaps;
validlaps.type = HOEL_COL_TYPE_INT;
validlaps.offset = offsetof(StintRowData, valid_laps);
DBField bestlapid;
bestlapid.type = HOEL_COL_TYPE_INT;
bestlapid.offset = offsetof(StintRowData, best_lap_id);
stints.fields[0] = stintid;
stints.fields[1] = driverid;
stints.fields[2] = teammemberid;
stints.fields[3] = sessionidstint;
stints.fields[4] = carid;
stints.fields[5] = gamecarid;
stints.fields[6] = stintlaps;
stints.fields[7] = validlaps;
stints.fields[8] = bestlapid;
LapDbo lapsdb;
DBField lapsdbid;
lapsdbid.type = HOEL_COL_TYPE_INT;
lapsdbid.offset = 0;
DBField lapsdbstintid;
lapsdbstintid.type = HOEL_COL_TYPE_INT;
lapsdbstintid.offset = offsetof(LapRowData, stint_id);
DBField sector_1;
sector_1.type = HOEL_COL_TYPE_INT;
sector_1.offset = offsetof(LapRowData, sector_1);
DBField sector_2;
sector_2.type = HOEL_COL_TYPE_INT;
sector_2.offset = offsetof(LapRowData, sector_2);
DBField sector_3;
sector_3.type = HOEL_COL_TYPE_INT;
sector_3.offset = offsetof(LapRowData, sector_3);
DBField grip;
grip.type = HOEL_COL_TYPE_DOUBLE;
grip.offset = offsetof(LapRowData, grip);
DBField tyrec;
tyrec.type = HOEL_COL_TYPE_TEXT;
tyrec.offset = offsetof(LapRowData, tyre);
tyrec.size = sizeof(unsigned char)*10;
DBField time;
time.type = HOEL_COL_TYPE_INT;
time.offset = offsetof(LapRowData, time);
DBField cuts;
cuts.type = HOEL_COL_TYPE_INT;
cuts.offset = offsetof(LapRowData, cuts);
DBField carcrashes;
carcrashes.type = HOEL_COL_TYPE_INT;
carcrashes.offset = offsetof(LapRowData, crashes);
DBField maxspeed;
maxspeed.type = HOEL_COL_TYPE_INT;
maxspeed.offset = offsetof(LapRowData, max_speed);
DBField avgspeed;
avgspeed.type = HOEL_COL_TYPE_INT;
avgspeed.offset = offsetof(LapRowData, avg_speed);
DBField lapsdbfinishedat;
lapsdbfinishedat.type = HOEL_COL_TYPE_DATE;
lapsdbfinishedat.offset = offsetof(LapRowData, finished_at);
lapsdbfinishedat.size = sizeof(PDBTimeStamp);
lapsdb.fields[0] = lapsdbid;
lapsdb.fields[1] = lapsdbstintid;
lapsdb.fields[2] = sector_1;
lapsdb.fields[3] = sector_2;
lapsdb.fields[4] = sector_3;
lapsdb.fields[5] = grip;
lapsdb.fields[6] = tyrec;
lapsdb.fields[7] = time;
lapsdb.fields[8] = cuts;
lapsdb.fields[9] = carcrashes;
lapsdb.fields[10] = maxspeed;
lapsdb.fields[11] = avgspeed;
lapsdb.fields[12] = lapsdbfinishedat;
//slogt("sessions has %i rows", sess.numrows);
action = 2;
int go = true;
char lastsimstatus = false;
int screen = SESSIONS_SCREEN;
char ch;
box(win1, 0, 0);
wrefresh(win1);
int useid = 0;
while (go == true)
{
if (lastselection != selection)
{
action = 1;
lastselection = selection;
}
if (action == 2)
{
slogt("going to perform an action");
sessions = getsessions(conn, "Sessions", &sess);
curresults = sessions;
}
if (action == 3)
{
slogt("going to perform an action");
stintsid = getstints(conn, "Stints", &stints, useid);
curresults = stintsid;
}
if (action == 4)
{
slogt("going to perform an action");
lapsresults = getlaps(conn, "laps", &lapsdb, useid);
curresults = lapsresults;
}
if (action > 0)
{
wclear(win1);
wprintw(win1, "\n");
switch(screen) {
case SESSIONS_SCREEN:
for(int i=0; i<sessions+1; i++)
{
if (i == 0)
{
char spacer[4];
sprintf(spacer, "\n");
waddstr(win1, spacer);
char selectind[4];
char clastlap[26];
sprintf(clastlap, " session name ");
waddstr(win1, clastlap);
wprintw(win1, " ");
char cbestlap[14];
sprintf(cbestlap, " laps ");
waddstr(win1, cbestlap);
wprintw(win1, "\n");
}
else
{
char spacer[4];
sprintf(spacer, "\n");
waddstr(win1, spacer);
char selectind[6];
if ( i == selection )
{
useid = sess.rows[i-1].session_id;
sprintf(selectind, "_*_");
waddstr(win1, selectind);
}
else
{
sprintf(selectind, "___");
waddstr(win1, selectind);
}
int maxstrlen = 20;
wprintw(win1, " %i ", sess.rows[i-1].session_id);
char clastlap[14];
sprintf(clastlap, "session name");
waddstr(win1, clastlap);
wprintw(win1, " ");
char cbestlap[14];
sprintf(cbestlap, "laps %i", sess.rows[i-1].laps);
waddstr(win1, cbestlap);
wprintw(win1, "\n");
}
//box(win1, 0, 0);
//wrefresh(win1);
}
break;
case STINTS_SCREEN:
if (stintsid == 0)
{
break;
}
for(int i=0; i<stintsid+1; i++)
{
if (i == 0)
{
char spacer[4];
sprintf(spacer, "\n");
waddstr(win1, spacer);
char selectind[4];
char clastlap[26];
sprintf(clastlap, " stint name ");
waddstr(win1, clastlap);
wprintw(win1, " ");
char cbestlap[14];
sprintf(cbestlap, " laps ");
waddstr(win1, cbestlap);
wprintw(win1, "\n");
}
else
{
char spacer[4];
sprintf(spacer, "\n");
waddstr(win1, spacer);
char selectind[4];
if ( i == selection )
{
sprintf(selectind, "_*_");
waddstr(win1, selectind);
useid = stints.rows[i-i].stint_id;
}
else
{
sprintf(selectind, "___");
waddstr(win1, selectind);
}
int maxstrlen = 20;
wprintw(win1, " %i ", stints.rows[i-1].stint_id);
char clastlap[14];
sprintf(clastlap, "stint name");
waddstr(win1, clastlap);
wprintw(win1, " ");
char cbestlap[14];
sprintf(cbestlap, "laps %i", stints.rows[i-1].driver_id);
waddstr(win1, cbestlap);
wprintw(win1, "\n");
}
}
break;
//wattrset(win4, COLOR_PAIR(1));
case LAPS_SCREEN:
if (lapsresults == 0)
{
break;
}
for(int i=0; i<lapsresults+1; i++)
{
if (i == 0)
{
char spacer[4];
sprintf(spacer, "\n");
waddstr(win1, spacer);
char selectind[4];
char clastlap[16];
sprintf(clastlap, " lap name ");
waddstr(win1, clastlap);
wprintw(win1, " ");
char cbestlap[14];
sprintf(cbestlap, " tyre ");
waddstr(win1, cbestlap);
wprintw(win1, "\n");
}
else
{
char spacer[4];
sprintf(spacer, "\n");
waddstr(win1, spacer);
char selectind[4];
if ( i == selection )
{
sprintf(selectind, "_*_");
waddstr(win1, selectind);
}
else
{
sprintf(selectind, "___");
waddstr(win1, selectind);
}
char selectind1[4];
if ( i == selection1 )
{
sprintf(selectind1, "_1_");
waddstr(win1, selectind1);
}
else if ( i == selection2 )
{
sprintf(selectind1, "_2_");
waddstr(win1, selectind1);
}
else
{
sprintf(selectind1, "___");
waddstr(win1, selectind1);
}
int maxstrlen = 20;
wprintw(win1, " %i ", lapsdb.rows[i-1].lap_id);
char clastlap[14];
sprintf(clastlap, "lap name");
waddstr(win1, clastlap);
wprintw(win1, " ");
char cbestlap[14];
sprintf(cbestlap, "laps %i", lapsdb.rows[i-1].max_speed);
waddstr(win1, cbestlap);
wprintw(win1, " ");
char gripstr[14];
sprintf(gripstr, "%s", lapsdb.rows[i-1].tyre);
waddstr(win1, gripstr);
wprintw(win1, " ");
char finstr[30];
sprintf(finstr, " %s ", lapsdb.rows[i-1].finished_at);
waddstr(win1, finstr);
wprintw(win1, "\n");
}
}
break;
//wattrset(win4, COLOR_PAIR(1));
}
action = 0;
}
box(win1, 0, 0);
//box(win2, 0, 0);
//box(win3, 0, 0);
//box(win4, 0, 0);
wrefresh(win1);
//wrefresh(win2);
//wrefresh(win3);
//wrefresh(win4);
scanf("%c", &ch);
if(ch == 'q')
{
go = false;
}
if (ch == 'b')
{
switch(screen) {
case STINTS_SCREEN:
action = 2;
screen = SESSIONS_SCREEN;
break;
case LAPS_SCREEN:
action = 3;
screen = STINTS_SCREEN;
break;
}
selection = 1;
lastselection = 1;
}
if (ch == 'e')
{
switch(screen) {
case SESSIONS_SCREEN:
action = 3;
screen = STINTS_SCREEN;
break;
case STINTS_SCREEN:
action = 4;
screen = LAPS_SCREEN;
break;
}
selection = 1;
lastselection = 1;
}
if (ch == 'B')
{
selection++;
if (selection > curresults)
{
selection = curresults;
}
}
if (ch == 'A')
{
selection--;
if (selection <= 1)
{
selection = 1;
}
}
if (ch == '1')
{
selection1 = selection;
action = 1;
}
if (ch == '2')
{
selection2 = selection;
action = 1;
}
}
wrefresh(win4);
delwin(win4);
endwin();
wrefresh(win3);
delwin(win3);
endwin();
wrefresh(win2);
delwin(win2);
endwin();
wrefresh(win1);
delwin(win1);
endwin();
h_close_db(conn);
h_clean_connection(conn);
//return 0;
}
-4
View File
@@ -1,4 +0,0 @@
#include "../helper/parameters.h"
#include "../helper/confighelper.h"
int browseloop(Parameters* p);
+82 -803
View File
@@ -1,30 +1,19 @@
#include <stdbool.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <unistd.h>
#include <ncurses.h> #include <ncurses.h>
#include <signal.h> #include <signal.h>
#include <time.h> #include <time.h>
#include <sys/time.h>
#include <MQTTClient.h> #include <MQTTClient.h>
#include <poll.h>
#include <termios.h>
#include <hoel.h>
#include <jansson.h>
#include "gameloop.h" #include "gameloop.h"
#include "../helper/parameters.h" #include "../helper/parameters.h"
#include "../helper/confighelper.h" #include "../helper/confighelper.h"
#include "../helper/dirhelper.h"
#include "../simulatorapi/simapi/simapi/simdata.h" #include "../simulatorapi/simapi/simapi/simdata.h"
#include "../simulatorapi/simapi/simapi/simmapper.h" #include "../simulatorapi/simapi/simapi/simmapper.h"
#include "../slog/slog.h" #include "../slog/slog.h"
#define DEFAULT_UPDATE_RATE 60 #define DEFAULT_UPDATE_RATE 100
#define DATA_UPDATE_RATE 32
#define TRACK_SAMPLE_RATE 4
#define SIM_CHECK_RATE 1
#define ADDRESS "tcp://localhost:1883" #define ADDRESS "tcp://localhost:1883"
#define CLIENTID "gilles" #define CLIENTID "gilles"
@@ -44,8 +33,6 @@ int winx, winy;
int win23y, win23x; int win23y, win23x;
void handle_winch(int sig) void handle_winch(int sig)
{ {
endwin(); endwin();
@@ -133,174 +120,18 @@ void update_date()
sprintf(datestring, "%.24s", asctime (timeinfo)); sprintf(datestring, "%.24s", asctime (timeinfo));
} }
int mainloop(Parameters* p)
{
pthread_t ui_thread;
pthread_t mqtt_thread;
pthread_t mysql_thread;
SimData* simdata = malloc(sizeof(SimData));
SimMap* simmap = malloc(sizeof(SimMap));
struct termios newsettings, canonicalmode;
tcgetattr(0, &canonicalmode);
newsettings = canonicalmode;
newsettings.c_lflag &= (~ICANON & ~ECHO);
newsettings.c_cc[VMIN] = 1;
newsettings.c_cc[VTIME] = 0;
tcsetattr(0, TCSANOW, &newsettings);
char ch;
struct pollfd mypoll = { STDIN_FILENO, POLLIN|POLLPRI };
p->simdata = simdata;
p->simmap = simmap;
fprintf(stdout, "Searching for sim data... Press q to quit...\n");
double update_rate = SIM_CHECK_RATE;
int go = true;
char lastsimstatus = false;
while (go == true)
{
getSim(simdata, simmap, &p->simon, &p->sim);
if (p->simon == true) {
p->program_state = 1;
if (p->cli == true)
{
if (pthread_create(&ui_thread, NULL, &clilooper, p) != 0)
{
printf("Uh-oh!\n");
return -1;
}
}
else
{
if (pthread_create(&ui_thread, NULL, &looper, p) != 0)
{
printf("Uh-oh!\n");
return -1;
}
}
//if (p->mqtt == true)
//{
// if (pthread_create(&mqtt_thread, NULL, &b4madmqtt, p) != 0)
// {
// printf("Uh-oh!\n");
// return -1;
// }
//}
if (p->mysql == true)
{
if (pthread_create(&mysql_thread, NULL, &simviewmysql, p) != 0)
{
printf("Uh-oh!\n");
return -1;
}
}
pthread_join(ui_thread, NULL);
p->program_state = -1;
//if (p->mqtt == true)
//{
// pthread_join(mqtt_thread, NULL);
//}
if (p->mysql == true)
{
pthread_join(mysql_thread, NULL);
}
}
if (p->simon == true)
{
p->simon = false;
fprintf(stdout, "Searching for sim data... Press q again to quit...\n");
sleep(2);
}
if( poll(&mypoll, 1, 1000.0/update_rate) )
{
scanf("%c", &ch);
if(ch == 'q')
{
go = false;
}
}
}
fprintf(stdout, "\n");
fflush(stdout);
tcsetattr(0, TCSANOW, &canonicalmode);
free(simdata);
free(simmap);
return 0;
}
void* clilooper(void* thargs)
{
Parameters* p = (Parameters*) thargs;
SimData* simdata = p->simdata;
SimMap* simmap = p->simmap;
struct termios newsettings, canonicalmode;
tcgetattr(0, &canonicalmode);
newsettings = canonicalmode;
newsettings.c_lflag &= (~ICANON & ~ECHO);
newsettings.c_cc[VMIN] = 1;
newsettings.c_cc[VTIME] = 0;
tcsetattr(0, TCSANOW, &newsettings);
char ch;
struct pollfd mypoll = { STDIN_FILENO, POLLIN|POLLPRI };
fprintf(stdout, "Press q to quit...\n");
fprintf(stdout, "Press c for a useful readout of car telemetry...\n");
fprintf(stdout, "Press s for basic sesion information...\n");
fprintf(stdout, "Press l for basic lap / stint information...\n");
double update_rate = DEFAULT_UPDATE_RATE;
int go = true;
char lastsimstatus = false;
while (go == true && simdata->simstatus > 1)
{
simdatamap(simdata, simmap, p->sim);
if( poll(&mypoll, 1, 1000.0/update_rate) )
{
scanf("%c", &ch);
if(ch == 'q')
{
go = false;
}
if(ch == 'c')
{
slogi("speed: %i gear: %i", simdata->velocity, simdata->gear);
}
if(ch == 's')
{
slogi("status: %i", simdata->simstatus);
}
}
}
fprintf(stdout, "\n");
fflush(stdout);
tcsetattr(0, TCSANOW, &canonicalmode);
return 0;
}
void* looper(void* thargs) void* looper(void* thargs)
{ {
Parameters* p = (Parameters*) thargs; Parameters* p = (Parameters*) thargs;
SimData* simdata = p->simdata; SimData* simdata = malloc(sizeof(SimData));
SimMap* simmap = p->simmap; SimMap* simmap = malloc(sizeof(SimMap));
int error = siminit(simdata, simmap, 1);
if (error != GILLES_ERROR_NONE)
{
slogf("Fatal error getting simulator data");
//return error;
}
curses_init(); curses_init();
@@ -310,7 +141,7 @@ void* looper(void* thargs)
char lastsimstatus = false; char lastsimstatus = false;
while (go == true) while (go == true)
{ {
simdatamap(simdata, simmap, p->sim); simdatamap(simdata, simmap, 1);
wclear(win1); wclear(win1);
wclear(win2); wclear(win2);
@@ -717,650 +548,98 @@ void* looper(void* thargs)
delwin(win1); delwin(win1);
endwin(); endwin();
free(simdata);
free(simmap);
//return 0; //return 0;
} }
int getLastInsertID(struct _h_connection* conn) void* b4madmqtt(void* thargs)
{
json_t* last_id = (h_last_insert_id(conn));
int id = json_integer_value(last_id);
json_decref(last_id);
return id;
}
int adddriver(struct _h_connection* conn, int driverid, const char* drivername)
{
if (driverid > 0)
{
return driverid;
}
json_t *root = json_object();
json_t *json_arr = json_array();
json_object_set_new( root, "table", json_string("drivers") );
json_object_set_new( root, "values", json_arr );
json_t* values = json_object();
json_object_set_new(values, "driver_name", json_string(drivername));
json_object_set_new(values, "prev_name", json_string(drivername));
json_object_set_new(values, "steam64_id", json_integer(17));
json_object_set_new(values, "country", json_string("USA"));
json_array_append(json_arr, values);
int res = h_insert(conn, root, NULL);
json_decref(root);
json_decref(values);
driverid = getLastInsertID(conn);
return driverid;
}
int addtrackconfig(struct _h_connection* conn, int trackconfigid, const char* track, int length)
{
//track_config_id | track_name | config_name | display_name | country | city | length
if (trackconfigid > 0)
{
return trackconfigid;
}
json_t *root = json_object();
json_t *json_arr = json_array();
json_object_set_new( root, "table", json_string(track) );
json_object_set_new( root, "values", json_arr );
json_t* values = json_object();
json_object_set_new(values, "track_name", json_string(track));
json_object_set_new(values, "config_name", json_string("default"));
json_object_set_new(values, "display_name", json_string(track));
json_object_set_new(values, "country", json_string("USA"));
json_object_set_new(values, "city", json_string("USA"));
json_object_set_new(values, "length", json_integer(length));
json_array_append(json_arr, values);
int res = h_insert(conn, root, NULL);
trackconfigid = getLastInsertID(conn);
json_decref(root);
json_decref(values);
return trackconfigid;
}
int addevent(struct _h_connection* conn, int track_config)
{
json_t *root = json_object();
json_t *json_arr = json_array();
json_object_set_new( root, "table", json_string("events") );
json_object_set_new( root, "values", json_arr );
json_t* values = json_object();
json_object_set_new(values, "track_config_id", json_integer(track_config));
json_object_set_new(values, "event_name", json_string("default"));
//event_id | server_name | track_config_id | event_name | team_event | active | livery_preview | use_number | practice_duration | quali_duration | race_duration | race_duration_type | race_wait_time | race_extra_laps | reverse_grid_positions
json_array_append(json_arr, values);
int res = h_insert(conn, root, NULL);
json_decref(root);
json_decref(values);
return getLastInsertID(conn);
}
int addsession(struct _h_connection* conn, int eventid, int eventtype, int airtemp, int tracktemp)
{
// session_id | event_id | event_type | track_time | session_name
// | start_time | duration_min | elapsed_ms | laps | weather |
// air_temp | road_temp | start_grip |
json_t *root = json_object();
json_t *json_arr = json_array();
json_object_set_new( root, "table", json_string("sessions") );
json_object_set_new( root, "values", json_arr );
json_t* values = json_object();
json_object_set_new(values, "event_id", json_integer(eventid));
json_object_set_new(values, "event_type", json_integer(1));
json_object_set_new(values, "duration_min", json_integer(60));
json_object_set_new(values, "start_time", json_string("NOW()"));
json_object_set_new(values, "session_name", json_string("default"));
json_object_set_new(values, "air_temp", json_integer(airtemp));
json_object_set_new(values, "road_temp", json_integer(tracktemp));
json_object_set_new(values, "weather", json_string("Windy"));
json_object_set_new(values, "http_port", json_integer(0));
json_array_append(json_arr, values);
int res = h_insert(conn, root, NULL);
slogt("session insert response: %i", res);
json_decref(root);
json_decref(values);
return getLastInsertID(conn);
}
int addstint(struct _h_connection* conn, int sessionid, int driverid, int carid)
{
json_t *root = json_object();
json_t *json_arr = json_array();
json_object_set_new( root, "table", json_string("stints") );
json_object_set_new( root, "values", json_arr );
json_t* values = json_object();
json_object_set_new(values, "driver_id", json_integer(driverid));
//team_member_id
json_object_set_new(values, "session_id", json_integer(sessionid));
json_object_set_new(values, "car_id", json_integer(carid));
json_object_set_new(values, "game_car_id", json_integer(carid));
json_object_set_new(values, "laps", json_integer(0));
json_object_set_new(values, "valid_laps", json_integer(0));
// best_lap_id
json_object_set_new(values, "is_finished", json_integer(0));
json_object_set_new(values, "started_at", json_string("NOW()") );
json_array_append(json_arr, values);
int res = h_insert(conn, root, NULL);
slogt("stint insert response: %i", res);
json_decref(root);
json_decref(values);
return getLastInsertID(conn);
}
int closesession(struct _h_connection* conn, int sessionid)
{
slogt("closing previous session ", sessionid);
// best_lap_id
// session_id | event_id | event_type | track_time | session_name
// | start_time | duration_min | elapsed_ms | laps | weather |
// air_temp | road_temp | start_grip | end_grip | is_finished
char* query = malloc((sizeof(char)*100));
sprintf(query, "UPDATE %s SET is_finished=1, finished_at=NOW() WHERE session_id=%i",
"sessions", sessionid);
int res1 = h_query_update(conn, query);
slogt("running query %s", query);
free(query);
slogt("closed previous session");
return res1;
}
int closestint(struct _h_connection* conn, int stintid, int stintlaps, int validstintlaps)
{
// best_lap_id
slogt("closing previous stint ", stintid);
char* query = malloc((sizeof(char)*146));
sprintf(query, "UPDATE %s SET %s = %i, %s = %i, is_finished=1, finished_at=NOW() WHERE stint_id=%i",
"stints", "laps", stintlaps, "valid_laps", validstintlaps, stintid);
int res1 = h_query_update(conn, query);
slogt("running query %s", query);
free(query);
slogt("closed previous stint");
return res1;
}
int closelap(struct _h_connection* conn, int lapid, int sector1, int sector2, int sector3, int cuts, int crashes, int maxspeed, int avgspeed,
double fbraketemp, double rbraketemp, double ftyrewear, double rtyrewear, double ftyretemp, double rtyretemp, double ftyrepress, double rtyrepress)
{
// stint laps
// lap_id | stint_id | sector_1 | sector_2 | sector_3 | grip | tyre | time | cuts | crashes
// max_speed | avg_speed | finished_at
slogt("closing previous lap ", lapid);
char* query = malloc((sizeof(char)*360));
sprintf(query, "UPDATE %s SET %s=%i, %s=%i, %s=%i, %s=%f, %s=%f, %s=%f, %s=%f, %s=%f, %s=%f, %s=%f, %s=%f, finished_at=NOW() WHERE lap_id=%i;",
"laps", "sector_1", sector1, "sector_2", sector2, "sector_3", sector3, "front_tyre_temp", ftyretemp, "rear_tyre_temp", rtyretemp,
"front_tyre_wear", ftyrewear, "rear_tyre_wear", rtyrewear, "front_tyre_press", ftyrepress, "rear_tyre_press", rtyrepress,
"front_brake_temp", fbraketemp, "rear_brake_temp", rbraketemp, lapid);
int res1 = h_query_update(conn, query);
slogt("running query %s", query);
free(query);
slogt("closed previous lap");
return res1;
}
int addstintlap(struct _h_connection* conn, int stintid)
{
// stint laps
// stint_lap_id | stint_id | sector_1 | sector_2 | sector_3 | grip | tyre | time | cuts | crashes
// max_speed | avg_speed | finished_at
json_t *root = json_object();
json_t *json_arr = json_array();
json_object_set_new( root, "table", json_string("laps") );
json_object_set_new( root, "values", json_arr );
json_t* values = json_object();
json_object_set_new(values, "stint_id", json_integer(stintid));
json_object_set_new(values, "tyre", json_string("Vintage") );
json_array_append(json_arr, values);
int res = h_insert(conn, root, NULL);
slogt("stint lap insert response: %i", res);
json_decref(root);
json_decref(values);
return getLastInsertID(conn);
}
int addcar(struct _h_connection* conn, int carid, const char* carname)
{
// car_id | display_name | car_name | manufacturer | car_class
if (carid > 0)
{
return carid;
}
json_t *root = json_object();
json_t *json_arr = json_array();
json_object_set_new( root, "table", json_string("cars") );
json_object_set_new( root, "values", json_arr );
json_t* values = json_object();
json_object_set_new(values, "display_name", json_string(carname));
json_object_set_new(values, "car_name", json_string(carname));
json_object_set_new(values, "manufacturer", json_string("Unknown"));
json_object_set_new(values, "car_class", json_string("Unknown"));
json_array_append(json_arr, values);
int res = h_insert(conn, root, NULL);
json_decref(root);
json_decref(values);
carid = getLastInsertID(conn);
return carid;
}
int addtelemetry(struct _h_connection* conn, int points, int stintid)
{
json_t *root = json_object();
json_t *json_arr = json_array();
json_object_set_new( root, "table", json_string("telemetry") );
json_object_set_new( root, "values", json_arr );
json_t* values = json_object();
json_object_set_new(values, "lap_id", json_integer(stintid));
json_object_set_new(values, "points", json_integer(points));
json_array_append(json_arr, values);
int res = h_insert(conn, root, NULL);
json_decref(root);
json_decref(values);
int telemid = getLastInsertID(conn);
return telemid;
}
int updatetelemetry(struct _h_connection* conn, int telemid, int size, const char* column, void* data)
{
//char *pp = malloc((size*2)+1);
char output[(size * 2) + 1];
char *ppp = &output[0];
unsigned char *p = data;
int i;
for (i=0; i<size; i++) {
ppp += sprintf(ppp, "%02hhX", p[i]);
//snprintf(pp, (size*2)+1, "%s%02hhX", pp, p[i]);
}
char* query = malloc((sizeof(char)*71)+(sizeof(column))+(size*2)+1);
sprintf(query, "UPDATE telemetry SET %s = decode('%s', 'hex') WHERE telemetry_id = %i", column, &output, telemid);
int res1 = h_query_update(conn, query);
//int res1 = h_insert(conn, j_query, NULL);
slogt("got res %i", res1);
free(query);
return res1;
}
int gettrack(struct _h_connection* conn, const char* trackname)
{
json_t *j_result;
char* where_clause = h_build_where_clause(conn, "config_name=%s AND track_name=%s", "default", trackname);
json_t* j_query = json_pack("{sss[s]s{s{ssss}}}", "table", "track_config", "columns", "track_config_id", "where", " ", "operator", "raw",
"value", where_clause);
//char* qq;
int res = h_select(conn, j_query, &j_result, NULL);
//slogi("here your query: %s", qq);
// Deallocate j_query since it won't be needed anymore
json_decref(j_query);
h_free(where_clause);
int track_config = -1;
// Test query execution result
if (res == H_OK) {
// Print result
char* dump = json_dumps(j_result, JSON_INDENT(2));
slogi("json select result is\n%s", dump);
int index1 = json_array_size(j_result);
if (index1 == 0)
{
slogw("no config for this track");
}
else {
json_t* jj = json_array_get(j_result, index1-1);
track_config = json_integer_value(json_object_get(jj, "track_config_id"));
}
// Deallocate data result
json_decref(j_result);
free(dump);
} else {
sloge("Error executing select query: %d", res);
}
return track_config;
}
int getdriver(struct _h_connection* conn, const char* driver_name)
{
json_t *j_result;
char* where_clause = h_build_where_clause(conn, "driver_name=%s", driver_name);
json_t* j_query = json_pack("{sss[s]s{s{ssss}}}","table", "drivers", "columns", "driver_id", "where", " ", "operator", "raw",
"value", where_clause);
slogi("Looking for driver named %s", driver_name);
//char* qq;
int res = h_select(conn, j_query, &j_result, NULL);
//slogi("here your query: %s", qq);
// Deallocate j_query since it won't be needed anymore
json_decref(j_query);
h_free(where_clause);
int driver_id = -1;
// Test query execution result
if (res == H_OK) {
// Print result
char* dump = json_dumps(j_result, JSON_INDENT(2));
slogi("json select result is\n%s", dump);
int index1 = json_array_size(j_result);
if (index1 == 0)
{
slogw("no driver by this name");
}
else {
json_t* jj = json_array_get(j_result, index1-1);
driver_id = json_integer_value(json_object_get(jj, "driver_id"));
}
// Deallocate data result
json_decref(j_result);
free(dump);
} else {
sloge("Error executing select query: %d", res);
}
return driver_id;
}
int getcar(struct _h_connection* conn, const char* carname)
{
json_t *j_result;
char* where_clause = h_build_where_clause(conn, "car_name=%s", carname);
json_t* j_query = json_pack("{sss[s]s{s{ssss}}}","table", "cars", "columns", "car_id", "where", " ", "operator", "raw",
"value", where_clause);
slogi("Looking for car named %s", carname);
//char* qq;
int res = h_select(conn, j_query, &j_result, NULL);
//slogi("here your query: %s", qq);
// Deallocate j_query since it won't be needed anymore
json_decref(j_query);
h_free(where_clause);
int car_id = -1;
// Test query execution result
if (res == H_OK) {
// Print result
char* dump = json_dumps(j_result, JSON_INDENT(2));
slogi("json select result is\n%s", dump);
int index1 = json_array_size(j_result);
if (index1 == 0)
{
slogw("no car by this name");
}
else {
json_t* jj = json_array_get(j_result, index1-1);
car_id = json_integer_value(json_object_get(jj, "car_id"));
}
// Deallocate data result
json_decref(j_result);
free(dump);
} else {
sloge("Error executing select query: %d", res);
}
return car_id;
}
void print_bytes(void *ptr, int size)
{
char *pp = malloc((size*2)+1);
char output[(size * 2) + 1];
char *ppp = &output[0];
unsigned char *p = ptr;
int i;
for (i=0; i<size; i++) {
slogt("%02hhX", p[i]);
ppp += sprintf(ppp, "%02X", p[i]);
//snprintf(pp, (size*2)+1, "%s%02hhX", pp, p[i]);
}
slogt("\n");
slogt("bytes %s", output);
}
void* simviewmysql(void* thargs)
{ {
Parameters* p = (Parameters*) thargs; Parameters* p = (Parameters*) thargs;
SimData* simdata = p->simdata; SimData* simdata = malloc(sizeof(SimData));
SimMap* simmap = p->simmap; SimMap* simmap = malloc(sizeof(SimMap));
struct _h_result result; int error = siminit(simdata, simmap, 1);
struct _h_connection * conn; if (error != GILLES_ERROR_NONE)
char* connectionstring = "host=zorak.brak dbname=gilles user=test password=thisisatest"; {
conn = h_connect_pgsql(connectionstring); slogf("Fatal error getting simulator data");
//return error;
}
bool mqtt = p->mqtt;
bool mqtt_connected = false;
int trackconfig = gettrack(conn, simdata->track); MQTTClient client;
trackconfig = addtrackconfig(conn, trackconfig, simdata->track, simdata->trackdistancearound); MQTTClient_connectOptions conn_opts = MQTTClient_connectOptions_initializer;
int eventid = addevent(conn, trackconfig); MQTTClient_message pubmsg = MQTTClient_message_initializer;
int driverid = getdriver(conn, simdata->driver); MQTTClient_deliveryToken token;
driverid = adddriver(conn, driverid, simdata->driver); int rc;
int carid = getcar(conn, simdata->car);
carid = addcar(conn, carid, simdata->car);
// ?? close last session // Create a new MQTT client
MQTTClient_create(&client, ADDRESS, CLIENTID,
MQTTCLIENT_PERSISTENCE_NONE, NULL);
int pitstatus = simdata->inpit; // Connect to the MQTT server
int sessionstatus = 0; if (mqtt == true)
int lastpitstatus = pitstatus; {
int lastsessionstatus = 0; conn_opts.keepAliveInterval = 20;
int lap = 0; conn_opts.cleansession = 1;
int lastlap = 0; if ((rc = MQTTClient_connect(client, &conn_opts)) != MQTTCLIENT_SUCCESS) {
int sector = 0; MQTTClient_disconnect(client, 10000);
int lastsector = 0; sloge("Failed to connect, return code %d", rc);
//exit(-1);
}
mqtt_connected = true;
}
int stintid = 0; int go = false;
int stintlapid = 0; if (mqtt_connected == true)
int sessionid = 0; {
sessionid = addsession(conn, eventid, simdata->session, simdata->airtemp, simdata->tracktemp); go = true;
stintid = addstint(conn, sessionid, driverid, carid); }
stintlapid = addstintlap(conn, stintid);
sessionstatus = simdata->session;
lastsessionstatus = sessionstatus;
lap = simdata->lap;
lastlap = lap;
double update_rate = DATA_UPDATE_RATE;
struct pollfd mypoll = { STDIN_FILENO, POLLIN|POLLPRI };
int go = true;
char lastsimstatus = false; char lastsimstatus = false;
int tick = 0; while (go == true && p->program_state == 1)
int track_samples = simdata->trackspline / TRACK_SAMPLE_RATE;
slogt("track samples %i", track_samples);
int stintlaps = 0;
int validstintlaps = 0;
bool validind = true;
int* speeddata = malloc(track_samples * sizeof(simdata->velocity));
int* rpmdata = malloc(track_samples * sizeof(simdata->rpms));
int* geardata = malloc(track_samples * sizeof(simdata->gear));
double* steerdata = malloc(track_samples * sizeof(simdata->steer));
double* acceldata = malloc(track_samples * sizeof(simdata->gas));
double* brakedata = malloc(track_samples * sizeof(simdata->brake));
//double* tyretemp0 = malloc(track_samples * sizeof(simdata->tyretemp[0]));
//double* tyretemp1 = malloc(track_samples * sizeof(simdata->tyretemp[0]));
//double* tyretemp2 = malloc(track_samples * sizeof(simdata->tyretemp[0]));
//double* tyretemp3 = malloc(track_samples * sizeof(simdata->tyretemp[0]));
//double* tyrepressure0 = malloc(track_samples * sizeof(simdata->tyrepressure[0]));
//double* tyrepressure1 = malloc(track_samples * sizeof(simdata->tyrepressure[0]));
//double* tyrepressure2 = malloc(track_samples * sizeof(simdata->tyrepressure[0]));
//double* tyrepressure3 = malloc(track_samples * sizeof(simdata->tyrepressure[0]));
//double* braketemp0 = malloc(track_samples * sizeof(simdata->braketemp[0]));
//double* braketemp1 = malloc(track_samples * sizeof(simdata->braketemp[0]));
//double* braketemp2 = malloc(track_samples * sizeof(simdata->braketemp[0]));
//double* braketemp3 = malloc(track_samples * sizeof(simdata->braketemp[0]));
int sectortimes[4];
if (p->program_state < 0)
{ {
go = false; simdatamap(simdata, simmap, 1);
char* newdatestring;
char simstatus = (simdata->simstatus > 0) ? true : false;
if (simdata->simstatus > 0 && simstatus != lastsimstatus)
{
update_date();
newdatestring = removeSpacesFromStr(datestring);
} }
lastsimstatus = simstatus;
while (go == true) if (mqtt_connected == true && simdata->simstatus > 0)
{ {
char payloads[5][60];
sprintf(payloads[0], "telemetry,lap=%i,session=%s gas=%04f", simdata->lap, newdatestring, simdata->gas);
sprintf(payloads[1], "telemetry,lap=%i,session=%s brake=%04f", simdata->lap, newdatestring, simdata->brake);
sprintf(payloads[2], "telemetry,lap=%i,session=%s steer=%04f", simdata->lap, newdatestring, simdata->brake);
sprintf(payloads[3], "telemetry,lap=%i,session=%s gear=%04i", simdata->lap, newdatestring, simdata->gear);
sprintf(payloads[4], "telemetry,lap=%i,session=%s speed=%04i", simdata->lap, newdatestring, simdata->velocity);
slogt("tick %i", tick); for (int k =0; k < 5; k++)
int pos = (int) track_samples * simdata->playerspline;
slogt("pos %f normpos %i of samples %i", simdata->playerspline, pos, track_samples);
steerdata[pos] = simdata->steer;
acceldata[pos] = simdata->gas;
brakedata[pos] = simdata->brake;
speeddata[pos] = simdata->velocity;
rpmdata[pos] = simdata->rpms;
geardata[pos] = simdata->gear;
//tyretemp0[pos] = simdata->tyretemp[0];
//tyretemp1[pos] = simdata->tyretemp[1];
//tyretemp2[pos] = simdata->tyretemp[2];
//tyretemp3[pos] = simdata->tyretemp[3];
//tyrepressure0[pos] = simdata->tyrepressure[0];
//tyrepressure1[pos] = simdata->tyrepressure[1];
//tyrepressure2[pos] = simdata->tyrepressure[2];
//tyrepressure3[pos] = simdata->tyrepressure[3];
//braketemp0[pos] = simdata->braketemp[0];
//braketemp1[pos] = simdata->braketemp[1];
//braketemp2[pos] = simdata->braketemp[2];
//braketemp3[pos] = simdata->braketemp[3];
sessionstatus = simdata->session;
lap = simdata->lap;
sector = simdata->sectorindex;
if (simdata->lapisvalid == false && validind == true)
{ {
validind = false; pubmsg.payload = payloads[k];
} pubmsg.payloadlen = strlen(payloads[k]);
sectortimes[simdata->sectorindex] = simdata->lastsectorinms; pubmsg.qos = QOS;
if (sessionstatus != lastsessionstatus) pubmsg.retained = 0;
{ MQTTClient_publishMessage(client, TOPIC, &pubmsg, &token);
closestint(conn, stintid, stintlaps, validstintlaps);
closesession(conn, sessionid);
if (sessionstatus > 1)
{
sessionid = addsession(conn, eventid, simdata->session, simdata->airtemp, simdata->tracktemp);
}
//pitstatus = 1;
stintlaps = 0;
validstintlaps = 0;
}
pitstatus = simdata->inpit;
if (simdata->inpit == true && pitstatus != lastpitstatus)
{
//pitstatus = 1;
//}
//if (pitstatus = 0 && pitstatus != lastpitstatus)
//{
// close last stint
closestint(conn, stintid, stintlaps, validstintlaps);
stintid = addstint(conn, sessionid, driverid, carid);
stintlaps = 0;
validstintlaps = 0;
}
if (lap != lastlap)
{
slogt("New lap detected");
stintlaps++;
if (validind == true)
{
validstintlaps++;
}
double fbraketemp = (simdata->braketemp[0] + simdata->braketemp[1])/2;
double rbraketemp = (simdata->braketemp[2] + simdata->braketemp[3])/2;
double ftyrewear = (simdata->tyrewear[0] + simdata->tyrewear[1])/2;
double rtyrewear = (simdata->tyrewear[2] + simdata->tyrewear[3])/2;
double ftyretemp = (simdata->tyretemp[0] + simdata->tyretemp[1])/2;
double rtyretemp = (simdata->tyretemp[2] + simdata->tyretemp[3])/2;
double ftyrepress = (simdata->tyrepressure[0] + simdata->tyrepressure[1])/2;
double rtyrepress = (simdata->tyrepressure[2] + simdata->tyrepressure[3])/2;
closelap(conn, stintlapid, sectortimes[1], sectortimes[2], simdata->lastsectorinms, 0, 0, 0, 0, fbraketemp, rbraketemp, ftyrewear, rtyrewear, ftyretemp, rtyretemp, ftyrepress, rtyrepress);
stintlapid = addstintlap(conn, stintid);
int telemid = addtelemetry(conn, track_samples, stintlapid);
int b = updatetelemetry(conn, telemid, track_samples*sizeof(double), "steer", steerdata);
b = updatetelemetry(conn, telemid, track_samples*sizeof(double), "accel", acceldata);
b = updatetelemetry(conn, telemid, track_samples*sizeof(double), "brake", brakedata);
b = updatetelemetry(conn, telemid, track_samples*sizeof(int), "rpms", rpmdata);
b = updatetelemetry(conn, telemid, track_samples*sizeof(int), "gear", geardata);
b = updatetelemetry(conn, telemid, track_samples*sizeof(int), "speed", speeddata);
tick = 0;
// assume lap is valid until it isn't
validind = true;
}
lastpitstatus = pitstatus;
lastsessionstatus = sessionstatus;
lastsector = sector;
lastlap = lap;
tick++;
poll(&mypoll, 1, 1000 / DATA_UPDATE_RATE);
if (p->program_state < 0)
{
closestint(conn, stintid, stintlaps, validstintlaps);
closesession(conn, sessionid);
go = false;
} }
} }
}
if (mqtt_connected == true)
{
MQTTClient_disconnect(client, 10000);
}
MQTTClient_destroy(&client);
h_close_db(conn); free(simdata);
h_clean_connection(conn); free(simmap);
//return 0;
} }
-3
View File
@@ -1,8 +1,5 @@
#include "../helper/parameters.h" #include "../helper/parameters.h"
#include "../helper/confighelper.h" #include "../helper/confighelper.h"
int mainloop(Parameters* p);
void* clilooper(void* params);
void* looper(void* params); void* looper(void* params);
void* b4madmqtt(void* params); void* b4madmqtt(void* params);
void* simviewmysql(void* params);
-130
View File
@@ -1,130 +0,0 @@
#ifndef _SESSION_H
#define _SESSION_H
#include <stddef.h>
#include <stdbool.h>
typedef struct DBData DBData;
typedef struct DBField DBField;
typedef struct SessionFieldsData SessionFieldsData;
typedef struct SessionFields SessionFields;
typedef struct Session Session;
typedef unsigned char PDBTimeStamp[26];
struct DBField
{
size_t offset;
size_t size;
int colnum;
int type;
char name[20];
};
//struct SessionFields
//{
// DBField session_id;
// DBField laps;
//};
typedef struct SessionRowData
{
int session_id;
int event_id;
int event_type;
//char track_time[5];
//char session_name[50];
//char start_time[6];
//int duration_min;
//int elapsed_ms;
int laps;
//char weather[100];
//double air_temp;
//double road_temp;
//double start_grip;
//double current_grip;
//int is_finished;
//char finish_time[6];
//char last_activity[6];
//int http_port;
}
SessionRowData;
//struct SessionFieldsData
//{
// int session_id;
// int laps;
//};
typedef struct StintRowData
{
int stint_id;
int driver_id;
int team_member_id;
int session_id;
int car_id;
int game_car_id;
int laps;
int valid_laps;
int best_lap_id;
}
StintRowData;
typedef struct LapRowData
{
int lap_id;
int stint_id;
int sector_1;
int sector_2;
int sector_3;
double grip;
unsigned char tyre[10];
int time;
int cuts;
int crashes;
int max_speed;
int avg_speed;
double f_tyre_temp;
double r_tyre_temp;
double f_tyre_wear;
double r_tyre_wear;
double f_tyre_press;
double r_tyre_press;
double f_brake_temp;
double r_brake_temp;
PDBTimeStamp finished_at;
}
LapRowData;
typedef struct SessionDbo
{
int numrows;
bool hasdata;
DBField fields[4];
SessionRowData* rows;
}
SessionDbo;
typedef struct StintDbo
{
int numrows;
bool hasdata;
DBField fields[9];
StintRowData* rows;
}
StintDbo;
typedef struct LapDbo
{
int numrows;
bool hasdata;
DBField fields[21];
LapRowData* rows;
}
LapDbo;
#endif
+34 -36
View File
@@ -1,4 +1,3 @@
#include <stdbool.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <sys/stat.h> #include <sys/stat.h>
@@ -6,7 +5,6 @@
#include <unistd.h> #include <unistd.h>
#include <libconfig.h> #include <libconfig.h>
#include <pthread.h> #include <pthread.h>
#include <basedir_fs.h>
#include "gameloop/gameloop.h" #include "gameloop/gameloop.h"
#include "helper/parameters.h" #include "helper/parameters.h"
@@ -14,6 +12,7 @@
#include "helper/confighelper.h" #include "helper/confighelper.h"
#include "slog/slog.h" #include "slog/slog.h"
int create_dir(char* dir) int create_dir(char* dir)
{ {
struct stat st = {0}; struct stat st = {0};
@@ -23,37 +22,25 @@ int create_dir(char* dir)
} }
} }
char* create_user_dir(const char* dirtype) char* create_user_dir(char* dirtype)
{ {
char* config_dir_str = ( char* ) malloc(1 + strlen(dirtype) + strlen("/gilles")); char* home_dir_str = gethome();
strcpy(config_dir_str, dirtype); char* config_dir_str = ( char* ) malloc(1 + strlen(home_dir_str) + strlen(dirtype) + strlen("gilles/"));
strcat(config_dir_str, "/gilles"); strcpy(config_dir_str, home_dir_str);
strcat(config_dir_str, dirtype);
strcat(config_dir_str, "gilles/");
create_dir(config_dir_str); create_dir(config_dir_str);
free(config_dir_str); free(config_dir_str);
} }
char* get_config_file(const char* confpath, xdgHandle* xdg)
{
if(strcmp(confpath, "") != 0)
{
fprintf(stderr, "no config path specified");
return strdup(confpath);
}
const char* relpath = "gilles/gilles.config";
const char* confpath1 = xdgConfigFind(relpath, xdg);
slogi("path is %s", confpath1);
return strdup(confpath1);
}
int main(int argc, char** argv) int main(int argc, char** argv)
{ {
Parameters* p = malloc(sizeof(Parameters)); Parameters* p = malloc(sizeof(Parameters));
GillesSettings* gs = malloc(sizeof(GillesSettings));; GillesSettings* gs = malloc(sizeof(GillesSettings));;
ConfigError ppe = getParameters(argc, argv, p); ConfigError ppe = getParameters(argc, argv, p);
if (ppe == E_SUCCESS_AND_EXIT) if (ppe == E_SUCCESS_AND_EXIT)
{ {
@@ -65,9 +52,13 @@ int main(int argc, char** argv)
char* home_dir_str = gethome(); char* home_dir_str = gethome();
create_user_dir("/.config/"); create_user_dir("/.config/");
create_user_dir("/.cache/"); create_user_dir("/.cache/");
char* config_file_str = ( char* ) malloc(1 + strlen(home_dir_str) + strlen("/.config/") + strlen("gilles/gilles.config"));
char* cache_dir_str = ( char* ) malloc(1 + strlen(home_dir_str) + strlen("/.cache/gilles/")); char* cache_dir_str = ( char* ) malloc(1 + strlen(home_dir_str) + strlen("/.cache/gilles/"));
strcpy(config_file_str, home_dir_str);
strcat(config_file_str, "/.config/");
strcpy(cache_dir_str, home_dir_str); strcpy(cache_dir_str, home_dir_str);
strcat(cache_dir_str, "/.cache/gilles/"); strcat(cache_dir_str, "/.cache/gilles/");
strcat(config_file_str, "gilles/gilles.config");
slog_config_t slgCfg; slog_config_t slgCfg;
slog_config_get(&slgCfg); slog_config_get(&slgCfg);
@@ -91,32 +82,39 @@ int main(int argc, char** argv)
slog_disable(SLOG_DEBUG); slog_disable(SLOG_DEBUG);
} }
xdgHandle xdg; pthread_t ui_thread;
if(!xdgInitHandle(&xdg)) pthread_t mqtt_thread;
{
slogf("Function xdgInitHandle() failed, is $HOME unset?");
}
char* config_file_str = get_config_file("/home/paul/.config/gilles/gilles.config", &xdg);
loadconfig(config_file_str, p); if (pthread_create(&ui_thread, NULL, &looper, p) != 0)
//slogi("mysql user is %s", p->mysql_user); {
printf("Uh-oh!\n");
return -1;
}
if (p->mqtt == true)
{
if (pthread_create(&mqtt_thread, NULL, &b4madmqtt, p) != 0)
{
printf("Uh-oh!\n");
return -1;
}
}
pthread_join(ui_thread, NULL);
p->program_state = -1;
if (p->mqtt == true)
{
pthread_join(mqtt_thread, NULL);
}
free(config_file_str); free(config_file_str);
free(cache_dir_str); free(cache_dir_str);
xdgWipeHandle(&xdg);
mainloop(p);
//free(config_file_str);
//free(cache_dir_str);
//free(simmap);
//free(simdata);
configcleanup: configcleanup:
//config_destroy(&cfg); //config_destroy(&cfg);
cleanup_final: cleanup_final:
freeparams(p);
free(gs); free(gs);
free(p); free(p);
exit(0); exit(0);
+18 -51
View File
@@ -2,70 +2,37 @@
#include <stdbool.h> #include <stdbool.h>
#include <string.h> #include <string.h>
#include <stdint.h> #include <stdint.h>
#include <stdlib.h>
#include "confighelper.h" #include "confighelper.h"
#include "../slog/slog.h" #include "../slog/slog.h"
//int strtogame(const char* game, GillesSettings* gs) int strtogame(const char* game, GillesSettings* gs)
//{
// slogd("Checking for %s in list of supported simulators.", game);
// if (strcmp(game, "ac") == 0)
// {
// slogd("Setting simulator to Assetto Corsa");
// gs->sim_name = SIMULATOR_ASSETTO_CORSA;
// }
// else
// if (strcmp(game, "test") == 0)
// {
// slogd("Setting simulator to Test Data");
// gs->sim_name = SIMULATOR_GILLES_TEST;
// }
// else
// {
// slogi("%s does not appear to be a supported simulator.", game);
// return GILLES_ERROR_INVALID_SIM;
// }
// return GILLES_ERROR_NONE;
//}
int loadmysql(config_t* cfg, Parameters* p)
{ {
config_setting_t* config_mysql_array = NULL; slogd("Checking for %s in list of supported simulators.", game);
config_mysql_array = config_lookup(cfg, "mysql"); if (strcmp(game, "ac") == 0)
config_setting_t* config_mysql = NULL;
config_mysql = config_setting_get_elem(config_mysql_array, 0);
if (config_mysql == NULL) {
slogi("found no mysql settings");
}
const char* temp;
config_setting_lookup_string(config_mysql, "user", &temp);
p->mysql_user = strdup(temp);
return 0;
}
int loadconfig(const char* config_file_str, Parameters* p)
{
config_t cfg;
config_init(&cfg);
if (!config_read_file(&cfg, config_file_str))
{ {
fprintf(stderr, "%s:%d - %s\n", config_error_file(&cfg), config_error_line(&cfg), config_error_text(&cfg)); slogd("Setting simulator to Assetto Corsa");
gs->sim_name = SIMULATOR_ASSETTO_CORSA;
}
else
if (strcmp(game, "test") == 0)
{
slogd("Setting simulator to Test Data");
gs->sim_name = SIMULATOR_GILLES_TEST;
} }
else else
{ {
slogi("Parsing config file"); slogi("%s does not appear to be a supported simulator.", game);
if (p->mysql == true) return GILLES_ERROR_INVALID_SIM;
{
loadmysql(&cfg, p);
}
} }
return GILLES_ERROR_NONE;
}
config_destroy(&cfg);
int loadconfig(const char* config_file)
{
return 0; return 0;
} }
+7 -2
View File
@@ -8,6 +8,13 @@
#include "parameters.h" #include "parameters.h"
typedef enum
{
SIMULATOR_GILLES_TEST = 0,
SIMULATOR_ASSETTO_CORSA = 1
}
Simulator;
typedef enum typedef enum
{ {
SIMULATOR_UPDATE_DEFAULT = 0, SIMULATOR_UPDATE_DEFAULT = 0,
@@ -34,8 +41,6 @@ typedef struct
} }
GillesSettings; GillesSettings;
int loadconfig(const char* config_file_str, Parameters* p);
int strtogame(const char* game, GillesSettings* gs); int strtogame(const char* game, GillesSettings* gs);
#endif #endif
+5 -56
View File
@@ -3,23 +3,16 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <stdbool.h> #include <stdbool.h>
#include <dirent.h>
#include <pwd.h> #include <pwd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <time.h> #include <time.h>
#include <string.h> #include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#if defined(OS_WIN)
#include <windows.h>
#else
#include <dirent.h> // for *Nix directory access
#include <unistd.h>
#endif
char* gethome() char* gethome()
{ {
char* homedir = getenv("HOME"); char* homedir = getenv("HOME");
@@ -207,47 +200,3 @@ bool does_directory_exist(char* path, char* dirname)
return answer; return answer;
} }
bool file_exists(const char* file)
{
if (file == NULL) { return false; }
#if defined(OS_WIN)
#if defined(WIN_API)
// if you want the WinAPI, versus CRT
if (strnlen(file, MAX_PATH+1) > MAX_PATH) {
// ... throw error here or ...
return false;
}
DWORD res = GetFileAttributesA(file);
return (res != INVALID_FILE_ATTRIBUTES &&
!(res & FILE_ATTRIBUTE_DIRECTORY));
#else
// Use Win CRT
struct stat fi;
if (_stat(file, &fi) == 0) {
#if defined(S_ISSOCK)
// sockets come back as a 'file' on some systems
// so make sure it's not a socket or directory
// (in other words, make sure it's an actual file)
return !(S_ISDIR(fi.st_mode)) &&
!(S_ISSOCK(fi.st_mode));
#else
return !(S_ISDIR(fi.st_mode));
#endif
}
return false;
#endif
#else
struct stat fi;
if (stat(file, &fi) == 0) {
#if defined(S_ISSOCK)
return !(S_ISDIR(fi.st_mode)) &&
!(S_ISSOCK(fi.st_mode));
#else
return !(S_ISDIR(fi.st_mode));
#endif
}
return false;
#endif
}
-1
View File
@@ -8,6 +8,5 @@ char* str2md5(const char* str, int length);
bool does_directory_exist(char* path, char* dirname); bool does_directory_exist(char* path, char* dirname);
void restrict_folders_to_cache(char* path, int cachesize); void restrict_folders_to_cache(char* path, int cachesize);
void delete_dir(char* path); void delete_dir(char* path);
bool file_exists(const char* file);
#endif #endif
+18 -71
View File
@@ -8,22 +8,6 @@
#include <argtable2.h> #include <argtable2.h>
#include <regex.h> #include <regex.h>
int freeparams(Parameters* p)
{
if(p->sim_string != NULL)
{
free(p->sim_string);
}
if (p->mysql == true)
{
if(p->mysql_user != NULL)
{
free(p->mysql_user);
}
}
return 0;
}
ConfigError getParameters(int argc, char** argv, Parameters* p) ConfigError getParameters(int argc, char** argv, Parameters* p)
{ {
@@ -31,101 +15,64 @@ ConfigError getParameters(int argc, char** argv, Parameters* p)
// set return structure defaults // set return structure defaults
p->program_action = 0; p->program_action = 0;
p->cli = false;
p->mqtt = false; p->mqtt = false;
p->mysql = false;
p->simon = false;
p->verbosity_count = 0; p->verbosity_count = 0;
// setup argument handling structures // setup argument handling structures
const char* progname = "Gilles"; const char* progname = "gilles";
struct arg_lit* arg_verbosity0 = arg_litn("v","verbose", 0, 2, "increase logging verbosity"); struct arg_lit* arg_verbosity = arg_litn("v","verbose", 0, 2, "increase logging verbosity");
struct arg_lit* arg_verbosity1 = arg_litn("v","verbose", 0, 2, "increase logging verbosity");
struct arg_rex* cmd1 = arg_rex1(NULL, NULL, "play", NULL, REG_ICASE, NULL); struct arg_rex* cmd1 = arg_rex1(NULL, NULL, "play", NULL, REG_ICASE, NULL);
struct arg_rex* cmd2 = arg_rex1(NULL, NULL, "browse", NULL, REG_ICASE, NULL);
struct arg_str* arg_sim = arg_strn("s", "sim", "<gamename>", 0, 1, NULL); struct arg_str* arg_sim = arg_strn("s", "sim", "<gamename>", 0, 1, NULL);
struct arg_lit* arg_cli = arg_lit0("c", "textui", "text only ui"); struct arg_lit* arg_mqtt = arg_lit0("S", NULL, "send data to local mqtt server");
struct arg_lit* arg_mqtt = arg_lit0("Q", "mqtt", "send data to local mqtt server with connection settings speciifed in config"); struct arg_lit* help = arg_litn(NULL,"help", 0, 1, "print this help and exit");
struct arg_lit* arg_mysql = arg_lit0("M", "mysql", "send data to local mysql server with connection settings specified in config"); struct arg_lit* vers = arg_litn(NULL,"version", 0, 1, "print version information and exit");
struct arg_lit* help0 = arg_litn(NULL,"help", 0, 1, "print this help and exit"); struct arg_end* end = arg_end(20);
struct arg_lit* vers0 = arg_litn(NULL,"version", 0, 1, "print version information and exit"); void* argtable[] = {cmd1,arg_sim,arg_verbosity,arg_mqtt,help,vers,end};
struct arg_end* end0 = arg_end(20); int nerrors;
struct arg_lit* help1 = arg_litn(NULL,"help", 0, 1, "print this help and exit");
struct arg_lit* vers1 = arg_litn(NULL,"version", 0, 1, "print version information and exit");
struct arg_end* end1 = arg_end(20);
void* argtable0[] = {cmd1,arg_sim,arg_verbosity0,arg_cli,arg_mqtt,arg_mysql,help0,vers0,end0};
void* argtable1[] = {cmd2,arg_verbosity1,help1,vers1,end1};
int nerrors0;
int nerrors1;
if (arg_nullcheck(argtable0) != 0) if (arg_nullcheck(argtable) != 0)
{
printf("%s: insufficient memory\n",progname);
goto cleanup;
}
if (arg_nullcheck(argtable1) != 0)
{ {
printf("%s: insufficient memory\n",progname); printf("%s: insufficient memory\n",progname);
goto cleanup; goto cleanup;
} }
nerrors0 = arg_parse(argc,argv,argtable0); nerrors = arg_parse(argc,argv,argtable);
nerrors1 = arg_parse(argc,argv,argtable1);
if (nerrors0==0) if (nerrors==0)
{ {
p->program_action = A_PLAY; p->program_action = A_PLAY;
p->sim_string = strdup(arg_sim->sval[0]); p->sim_string = arg_sim->sval[0];
p->verbosity_count = arg_verbosity0->count; p->verbosity_count = arg_verbosity->count;
if (arg_cli->count > 0)
{
p->cli = true;
}
if (arg_mqtt->count > 0) if (arg_mqtt->count > 0)
{ {
p->mqtt = true; p->mqtt = true;
} }
if (arg_mysql->count > 0)
{
p->mysql = true;
}
exitcode = E_SUCCESS_AND_DO;
}
if (nerrors1==0)
{
p->program_action = A_BROWSE;
//p->sim_string = strdup(arg_sim->sval[0]);
p->verbosity_count = arg_verbosity1->count;
exitcode = E_SUCCESS_AND_DO; exitcode = E_SUCCESS_AND_DO;
} }
// interpret some special cases before we go through trouble of reading the config file // interpret some special cases before we go through trouble of reading the config file
if (help0->count > 0) if (help->count > 0)
{ {
printf("Usage: %s\n", progname); printf("Usage: %s\n", progname);
arg_print_syntax(stdout,argtable0,"\n"); printf("\nReport bugs on the github github.com/spacefreak18/csimtelem.\n");
arg_print_syntax(stdout,argtable1,"\n");
printf("\nReport bugs on the github github.com/spacefreak18/gilles.\n");
exitcode = E_SUCCESS_AND_EXIT; exitcode = E_SUCCESS_AND_EXIT;
goto cleanup; goto cleanup;
} }
if (vers0->count > 0) if (vers->count > 0)
{ {
printf("%s Simulator Monitor\n",progname); printf("%s Simulator Hardware Manager\n",progname);
printf("October 2022, Paul Dino Jones\n"); printf("October 2022, Paul Dino Jones\n");
exitcode = E_SUCCESS_AND_EXIT; exitcode = E_SUCCESS_AND_EXIT;
goto cleanup; goto cleanup;
} }
cleanup: cleanup:
arg_freetable(argtable0,sizeof(argtable0)/sizeof(argtable0[0])); arg_freetable(argtable,sizeof(argtable)/sizeof(argtable[0]));
arg_freetable(argtable1,sizeof(argtable1)/sizeof(argtable1[0]));
return exitcode; return exitcode;
} }
+1 -20
View File
@@ -3,37 +3,19 @@
#include <stdbool.h> #include <stdbool.h>
#include "../simulatorapi/simapi/simapi/simapi.h"
#include "../simulatorapi/simapi/simapi/simdata.h"
#include "../simulatorapi/simapi/simapi/simmapper.h"
typedef struct typedef struct
{ {
char* sim_string;
int program_action; int program_action;
int program_state; int program_state;
const char* sim_string;
bool cli;
bool mqtt; bool mqtt;
bool mysql;
bool simon;
int verbosity_count; int verbosity_count;
char* mysql_user;
char* mysql_serv;
char* mysql_dbnm;
char* mysql_pass;
Simulator sim;
SimData* simdata;
SimMap* simmap;
} }
Parameters; Parameters;
typedef enum typedef enum
{ {
A_PLAY = 0, A_PLAY = 0,
A_BROWSE = 0
} }
ProgramAction; ProgramAction;
@@ -46,7 +28,6 @@ typedef enum
ConfigError; ConfigError;
ConfigError getParameters(int argc, char** argv, Parameters* p); ConfigError getParameters(int argc, char** argv, Parameters* p);
int freeparams(Parameters* p);
struct _errordesc struct _errordesc
{ {