Add thread support to gilles
This commit is contained in:
parent
e2a0979b95
commit
b3331ba599
|
|
@ -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 ncursesw 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)
|
||||||
|
|
|
||||||
|
|
@ -120,82 +120,28 @@ void update_date()
|
||||||
sprintf(datestring, "%.24s", asctime (timeinfo));
|
sprintf(datestring, "%.24s", asctime (timeinfo));
|
||||||
}
|
}
|
||||||
|
|
||||||
int looper(Simulator simulator, Parameters* p)
|
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;
|
char lastsimstatus = false;
|
||||||
while (go == true)
|
while (go == true)
|
||||||
{
|
{
|
||||||
simdatamap(simdata, simmap, simulator);
|
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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
wclear(win1);
|
wclear(win1);
|
||||||
wclear(win2);
|
wclear(win2);
|
||||||
|
|
@ -602,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);
|
||||||
|
|
@ -611,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);
|
||||||
|
|
|
||||||
|
|
@ -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;
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue