Compare commits
8
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b3331ba599 | ||
|
|
e2a0979b95 | ||
|
|
db7548cb1a | ||
|
|
ad132c0574 | ||
|
|
9908326ac5 | ||
|
|
c16a758e94 | ||
|
|
f8670ad393 | ||
|
|
170ad25020 |
+1
-1
@@ -24,7 +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 m ncurses argtable2 config gameloop helper slog simulatorapi eclipse-paho-mqtt-c::paho-mqtt3c)
|
target_link_libraries(gilles m ncursesw argtable2 config gameloop helper slog simulatorapi eclipse-paho-mqtt-c::paho-mqtt3c pthread)
|
||||||
|
|
||||||
# used for enabling additional compiler options if supported
|
# used for enabling additional compiler options if supported
|
||||||
include(CheckCXXCompilerFlag)
|
include(CheckCXXCompilerFlag)
|
||||||
|
|||||||
@@ -11,7 +11,7 @@ ncurses based telemetry monitor for racing sims
|
|||||||
- argtable2
|
- argtable2
|
||||||
- libconfig
|
- libconfig
|
||||||
- ncurses
|
- ncurses
|
||||||
- slog (static)
|
- [slog](https://github.com/kala13x/slog) (static)
|
||||||
- [simshmbridge](https://github.com/spacefreak18/simshmbridge)
|
- [simshmbridge](https://github.com/spacefreak18/simshmbridge)
|
||||||
- [simapi](https://github.com/spacefreak18/simapi)
|
- [simapi](https://github.com/spacefreak18/simapi)
|
||||||
|
|
||||||
|
|||||||
+119
-51
@@ -3,6 +3,7 @@
|
|||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <ncurses.h>
|
#include <ncurses.h>
|
||||||
#include <signal.h>
|
#include <signal.h>
|
||||||
|
#include <time.h>
|
||||||
#include <MQTTClient.h>
|
#include <MQTTClient.h>
|
||||||
|
|
||||||
#include "gameloop.h"
|
#include "gameloop.h"
|
||||||
@@ -21,6 +22,8 @@
|
|||||||
#define QOS 0
|
#define QOS 0
|
||||||
#define TIMEOUT 10000L
|
#define TIMEOUT 10000L
|
||||||
|
|
||||||
|
char datestring[30];
|
||||||
|
|
||||||
WINDOW* win1;
|
WINDOW* win1;
|
||||||
WINDOW* win2;
|
WINDOW* win2;
|
||||||
WINDOW* win3;
|
WINDOW* win3;
|
||||||
@@ -91,73 +94,54 @@ int curses_init()
|
|||||||
box(win4, 0, 0);
|
box(win4, 0, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
int looper(Simulator simulator, Parameters* p)
|
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* looper(void* thargs)
|
||||||
|
{
|
||||||
|
Parameters* p = (Parameters*) thargs;
|
||||||
SimData* simdata = malloc(sizeof(SimData));
|
SimData* simdata = malloc(sizeof(SimData));
|
||||||
SimMap* simmap = malloc(sizeof(SimMap));
|
SimMap* simmap = malloc(sizeof(SimMap));
|
||||||
|
|
||||||
int error = siminit(simdata, simmap, simulator);
|
int error = siminit(simdata, simmap, 1);
|
||||||
|
|
||||||
if (error != GILLES_ERROR_NONE)
|
if (error != GILLES_ERROR_NONE)
|
||||||
{
|
{
|
||||||
slogf("Fatal error getting simulator data");
|
slogf("Fatal error getting simulator data");
|
||||||
return error;
|
//return error;
|
||||||
}
|
}
|
||||||
|
|
||||||
curses_init();
|
curses_init();
|
||||||
|
|
||||||
timeout(DEFAULT_UPDATE_RATE);
|
timeout(DEFAULT_UPDATE_RATE);
|
||||||
|
|
||||||
bool mqtt = p->mqtt;
|
|
||||||
bool mqtt_connected = false;
|
|
||||||
|
|
||||||
MQTTClient client;
|
|
||||||
MQTTClient_connectOptions conn_opts = MQTTClient_connectOptions_initializer;
|
|
||||||
MQTTClient_message pubmsg = MQTTClient_message_initializer;
|
|
||||||
MQTTClient_deliveryToken token;
|
|
||||||
int rc;
|
|
||||||
|
|
||||||
// Create a new MQTT client
|
|
||||||
MQTTClient_create(&client, ADDRESS, CLIENTID,
|
|
||||||
MQTTCLIENT_PERSISTENCE_NONE, NULL);
|
|
||||||
|
|
||||||
// Connect to the MQTT server
|
|
||||||
if (mqtt == true)
|
|
||||||
{
|
|
||||||
conn_opts.keepAliveInterval = 20;
|
|
||||||
conn_opts.cleansession = 1;
|
|
||||||
if ((rc = MQTTClient_connect(client, &conn_opts)) != MQTTCLIENT_SUCCESS) {
|
|
||||||
MQTTClient_disconnect(client, 10000);
|
|
||||||
sloge("Failed to connect, return code %d", rc);
|
|
||||||
//exit(-1);
|
|
||||||
}
|
|
||||||
mqtt_connected = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
int go = true;
|
int go = true;
|
||||||
|
char lastsimstatus = false;
|
||||||
while (go == true)
|
while (go == true)
|
||||||
{
|
{
|
||||||
simdatamap(simdata, simmap, simulator);
|
simdatamap(simdata, simmap, 1);
|
||||||
|
|
||||||
if (mqtt_connected == true)
|
|
||||||
{
|
|
||||||
char payloads[6][20];
|
|
||||||
sprintf(payloads[0], "gas, lap=%i, %04f", simdata->lap, simdata->gas);
|
|
||||||
sprintf(payloads[1], "brake, lap=%i, %04f", simdata->lap, simdata->brake);
|
|
||||||
sprintf(payloads[2], "steer, lap=%i, %04f", simdata->lap, simdata->brake);
|
|
||||||
sprintf(payloads[3], "gear, lap=%i, %04i", simdata->lap, simdata->gear);
|
|
||||||
sprintf(payloads[4], "speed, lap=%i, %04i", simdata->lap, simdata->velocity);
|
|
||||||
|
|
||||||
for (int k =0; k < 6; k++)
|
|
||||||
{
|
|
||||||
pubmsg.payload = payloads[k];
|
|
||||||
pubmsg.payloadlen = strlen(payloads[k]);
|
|
||||||
pubmsg.qos = QOS;
|
|
||||||
pubmsg.retained = 0;
|
|
||||||
MQTTClient_publishMessage(client, TOPIC, &pubmsg, &token);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
wclear(win1);
|
wclear(win1);
|
||||||
wclear(win2);
|
wclear(win2);
|
||||||
@@ -564,6 +548,90 @@ int looper(Simulator simulator, Parameters* p)
|
|||||||
delwin(win1);
|
delwin(win1);
|
||||||
endwin();
|
endwin();
|
||||||
|
|
||||||
|
free(simdata);
|
||||||
|
free(simmap);
|
||||||
|
|
||||||
|
//return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
void* b4madmqtt(void* thargs)
|
||||||
|
{
|
||||||
|
Parameters* p = (Parameters*) thargs;
|
||||||
|
SimData* simdata = malloc(sizeof(SimData));
|
||||||
|
SimMap* simmap = malloc(sizeof(SimMap));
|
||||||
|
|
||||||
|
int error = siminit(simdata, simmap, 1);
|
||||||
|
if (error != GILLES_ERROR_NONE)
|
||||||
|
{
|
||||||
|
slogf("Fatal error getting simulator data");
|
||||||
|
//return error;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool mqtt = p->mqtt;
|
||||||
|
bool mqtt_connected = false;
|
||||||
|
|
||||||
|
MQTTClient client;
|
||||||
|
MQTTClient_connectOptions conn_opts = MQTTClient_connectOptions_initializer;
|
||||||
|
MQTTClient_message pubmsg = MQTTClient_message_initializer;
|
||||||
|
MQTTClient_deliveryToken token;
|
||||||
|
int rc;
|
||||||
|
|
||||||
|
// Create a new MQTT client
|
||||||
|
MQTTClient_create(&client, ADDRESS, CLIENTID,
|
||||||
|
MQTTCLIENT_PERSISTENCE_NONE, NULL);
|
||||||
|
|
||||||
|
// Connect to the MQTT server
|
||||||
|
if (mqtt == true)
|
||||||
|
{
|
||||||
|
conn_opts.keepAliveInterval = 20;
|
||||||
|
conn_opts.cleansession = 1;
|
||||||
|
if ((rc = MQTTClient_connect(client, &conn_opts)) != MQTTCLIENT_SUCCESS) {
|
||||||
|
MQTTClient_disconnect(client, 10000);
|
||||||
|
sloge("Failed to connect, return code %d", rc);
|
||||||
|
//exit(-1);
|
||||||
|
}
|
||||||
|
mqtt_connected = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
int go = false;
|
||||||
|
if (mqtt_connected == true)
|
||||||
|
{
|
||||||
|
go = true;
|
||||||
|
}
|
||||||
|
char lastsimstatus = false;
|
||||||
|
while (go == true && p->program_state == 1)
|
||||||
|
{
|
||||||
|
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;
|
||||||
|
|
||||||
|
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);
|
||||||
|
|
||||||
|
for (int k =0; k < 5; k++)
|
||||||
|
{
|
||||||
|
pubmsg.payload = payloads[k];
|
||||||
|
pubmsg.payloadlen = strlen(payloads[k]);
|
||||||
|
pubmsg.qos = QOS;
|
||||||
|
pubmsg.retained = 0;
|
||||||
|
MQTTClient_publishMessage(client, TOPIC, &pubmsg, &token);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
if (mqtt_connected == true)
|
if (mqtt_connected == true)
|
||||||
{
|
{
|
||||||
MQTTClient_disconnect(client, 10000);
|
MQTTClient_disconnect(client, 10000);
|
||||||
@@ -573,5 +641,5 @@ int looper(Simulator simulator, Parameters* p)
|
|||||||
free(simdata);
|
free(simdata);
|
||||||
free(simmap);
|
free(simmap);
|
||||||
|
|
||||||
return 0;
|
//return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
#include "../helper/parameters.h"
|
#include "../helper/parameters.h"
|
||||||
#include "../helper/confighelper.h"
|
#include "../helper/confighelper.h"
|
||||||
|
|
||||||
int looper (Simulator simulator, Parameters* p);
|
void* looper(void* params);
|
||||||
|
void* b4madmqtt(void* params);
|
||||||
|
|||||||
+25
-1
@@ -4,6 +4,7 @@
|
|||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <libconfig.h>
|
#include <libconfig.h>
|
||||||
|
#include <pthread.h>
|
||||||
|
|
||||||
#include "gameloop/gameloop.h"
|
#include "gameloop/gameloop.h"
|
||||||
#include "helper/parameters.h"
|
#include "helper/parameters.h"
|
||||||
@@ -46,6 +47,7 @@ int main(int argc, char** argv)
|
|||||||
goto cleanup_final;
|
goto cleanup_final;
|
||||||
}
|
}
|
||||||
gs->program_action = p->program_action;
|
gs->program_action = p->program_action;
|
||||||
|
p->program_state = 1;
|
||||||
|
|
||||||
char* home_dir_str = gethome();
|
char* home_dir_str = gethome();
|
||||||
create_user_dir("/.config/");
|
create_user_dir("/.config/");
|
||||||
@@ -80,7 +82,29 @@ int main(int argc, char** argv)
|
|||||||
slog_disable(SLOG_DEBUG);
|
slog_disable(SLOG_DEBUG);
|
||||||
}
|
}
|
||||||
|
|
||||||
looper(1, p);
|
pthread_t ui_thread;
|
||||||
|
pthread_t mqtt_thread;
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
|
|||||||
@@ -6,6 +6,7 @@
|
|||||||
typedef struct
|
typedef struct
|
||||||
{
|
{
|
||||||
int program_action;
|
int program_action;
|
||||||
|
int program_state;
|
||||||
const char* sim_string;
|
const char* sim_string;
|
||||||
bool mqtt;
|
bool mqtt;
|
||||||
int verbosity_count;
|
int verbosity_count;
|
||||||
|
|||||||
Submodule src/gilles/simulatorapi/simapi updated: 85c063eac3...618920440e
Reference in New Issue
Block a user