Compare commits
3
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
0ac2ce935c | ||
|
|
ae7e54c253 | ||
|
|
fab3a5e631 |
+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 ncursesw argtable2 config gameloop helper slog simulatorapi eclipse-paho-mqtt-c::paho-mqtt3c pthread)
|
target_link_libraries(gilles m ncursesw argtable2 config gameloop helper slog simulatorapi eclipse-paho-mqtt-c::paho-mqtt3c json-c)
|
||||||
|
|
||||||
# used for enabling additional compiler options if supported
|
# used for enabling additional compiler options if supported
|
||||||
include(CheckCXXCompilerFlag)
|
include(CheckCXXCompilerFlag)
|
||||||
|
|||||||
+121
-90
@@ -4,6 +4,7 @@
|
|||||||
#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 "gameloop.h"
|
#include "gameloop.h"
|
||||||
@@ -13,6 +14,8 @@
|
|||||||
#include "../simulatorapi/simapi/simapi/simmapper.h"
|
#include "../simulatorapi/simapi/simapi/simmapper.h"
|
||||||
#include "../slog/slog.h"
|
#include "../slog/slog.h"
|
||||||
|
|
||||||
|
#include <json-c/json.h>
|
||||||
|
|
||||||
#define DEFAULT_UPDATE_RATE 100
|
#define DEFAULT_UPDATE_RATE 100
|
||||||
|
|
||||||
#define ADDRESS "tcp://localhost:1883"
|
#define ADDRESS "tcp://localhost:1883"
|
||||||
@@ -33,6 +36,8 @@ int winx, winy;
|
|||||||
|
|
||||||
int win23y, win23x;
|
int win23y, win23x;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
void handle_winch(int sig)
|
void handle_winch(int sig)
|
||||||
{
|
{
|
||||||
endwin();
|
endwin();
|
||||||
@@ -120,28 +125,138 @@ void update_date()
|
|||||||
sprintf(datestring, "%.24s", asctime (timeinfo));
|
sprintf(datestring, "%.24s", asctime (timeinfo));
|
||||||
}
|
}
|
||||||
|
|
||||||
void* looper(void* thargs)
|
int looper(Simulator simulator, Parameters* p)
|
||||||
{
|
{
|
||||||
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, 1);
|
int error = siminit(simdata, simmap, simulator);
|
||||||
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;
|
||||||
|
|
||||||
|
long unix_time_start;
|
||||||
|
char time_buff[11];
|
||||||
|
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, 1);
|
simdatamap(simdata, simmap, simulator);
|
||||||
|
|
||||||
|
char* newdatestring;
|
||||||
|
char simstatus = (simdata->simstatus > 0) ? true : false;
|
||||||
|
if (simdata->simstatus > 0 && simstatus != lastsimstatus)
|
||||||
|
{
|
||||||
|
//update_date();
|
||||||
|
unix_time_start = (unsigned long) time(NULL);
|
||||||
|
sprintf(time_buff, "%lu", unix_time_start);
|
||||||
|
//sprintf(time_buff, "%lu", (unsigned long)time(NULL));
|
||||||
|
//newdatestring = removeSpacesFromStr(datestring);
|
||||||
|
}
|
||||||
|
lastsimstatus = simstatus;
|
||||||
|
|
||||||
|
if (mqtt_connected == true && simdata->simstatus > 0)
|
||||||
|
{
|
||||||
|
json_object *root = json_object_new_object();
|
||||||
|
if (!root)
|
||||||
|
return 1;
|
||||||
|
|
||||||
|
json_object *child = json_object_new_object();
|
||||||
|
|
||||||
|
struct timeval tv;
|
||||||
|
gettimeofday(&tv, NULL);
|
||||||
|
unsigned long long millisecondsSinceEpoch = (unsigned long long)(tv.tv_sec) * 1000 + (unsigned long long)(tv.tv_usec) / 1000;
|
||||||
|
|
||||||
|
json_object_object_add(root, "time", json_object_new_int64(millisecondsSinceEpoch)); //unix time milliseconds
|
||||||
|
|
||||||
|
const char* topic_root = "racing/gilles/TuxRacerX";
|
||||||
|
//const char* game_name = "Assetto Corsa (64 bit)";
|
||||||
|
const char* game_name = "assetto_64_bit";
|
||||||
|
const char* session_type = "Practice";
|
||||||
|
|
||||||
|
json_object_object_add(child, "CarModel", json_object_new_string(simdata->car));
|
||||||
|
json_object_object_add(child, "GameName", json_object_new_string(game_name));
|
||||||
|
json_object_object_add(child, "SessionId", json_object_new_int(unix_time_start));
|
||||||
|
json_object_object_add(child, "SessionTypeName", json_object_new_string("Practice"));
|
||||||
|
json_object_object_add(child, "TrackCode", json_object_new_string(simdata->track));
|
||||||
|
|
||||||
|
json_object_object_add(child, "Clutch", json_object_new_double(simdata->clutch));
|
||||||
|
json_object_object_add(child, "Brake", json_object_new_double(simdata->brake));
|
||||||
|
json_object_object_add(child, "Throtte", json_object_new_double(simdata->gas));
|
||||||
|
json_object_object_add(child, "HandBrake", json_object_new_double(simdata->handbrake));
|
||||||
|
json_object_object_add(child, "SteeringAngle", json_object_new_double(simdata->steer));
|
||||||
|
json_object_object_add(child, "Rpms", json_object_new_int(simdata->rpms));
|
||||||
|
json_object_object_add(child, "Gear", json_object_new_int(simdata->gear));
|
||||||
|
json_object_object_add(child, "SpeedMs", json_object_new_double(simdata->velocity * 0.2777778));
|
||||||
|
json_object_object_add(child, "DistanceRoundTrack", json_object_new_double(simdata->trackdistancearound));
|
||||||
|
json_object_object_add(child, "WorldPosition_x", json_object_new_double(simdata->worldposx));
|
||||||
|
json_object_object_add(child, "WorldPosition_y", json_object_new_double(simdata->worldposy));
|
||||||
|
json_object_object_add(child, "WorldPosition_z", json_object_new_double(simdata->worldposz));
|
||||||
|
json_object_object_add(child, "CurrentLap", json_object_new_int(simdata->playerlaps));
|
||||||
|
json_object_object_add(child, "CurrentLapTime", json_object_new_int(simdata->currentlapinseconds));
|
||||||
|
json_object_object_add(child, "LapTimePrevious", json_object_new_int(simdata->lastlapinseconds));
|
||||||
|
json_object_object_add(child, "CurrentLapIsValid", json_object_new_int(simdata->lapisvalid));
|
||||||
|
json_object_object_add(child, "PreviousLapWasValid", json_object_new_int(1));
|
||||||
|
|
||||||
|
json_object_object_add(root, "telemetry", child);
|
||||||
|
|
||||||
|
slogi(json_object_to_json_string_ext(root, JSON_C_TO_STRING_PRETTY));
|
||||||
|
|
||||||
|
// TODO: generate this topic string only once
|
||||||
|
char* topic = ( char* ) malloc(1 + strlen(topic_root) + strlen("/") + strlen(game_name) + strlen("/") + strlen(session_type)
|
||||||
|
+ strlen("/") + strlen(simdata->car) + strlen("/") + strlen(simdata->track) + strlen("/") + 11);
|
||||||
|
strcpy(topic, topic_root);
|
||||||
|
strcat(topic, "/");
|
||||||
|
strcat(topic, time_buff);
|
||||||
|
strcat(topic, "/");
|
||||||
|
strcat(topic, game_name);
|
||||||
|
strcat(topic, "/");
|
||||||
|
strcat(topic, simdata->track);
|
||||||
|
strcat(topic, "/");
|
||||||
|
strcat(topic, simdata->car);
|
||||||
|
strcat(topic, "/");
|
||||||
|
strcat(topic, session_type);
|
||||||
|
|
||||||
|
pubmsg.payload = json_object_to_json_string_ext(root, JSON_C_TO_STRING_PRETTY);
|
||||||
|
pubmsg.payloadlen = strlen(json_object_to_json_string_ext(root, JSON_C_TO_STRING_PRETTY));
|
||||||
|
pubmsg.qos= QOS;
|
||||||
|
pubmsg.retained = 0;
|
||||||
|
|
||||||
|
MQTTClient_publishMessage(client, topic, &pubmsg, &token);
|
||||||
|
}
|
||||||
|
|
||||||
wclear(win1);
|
wclear(win1);
|
||||||
wclear(win2);
|
wclear(win2);
|
||||||
@@ -548,90 +663,6 @@ void* looper(void* thargs)
|
|||||||
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);
|
||||||
@@ -641,5 +672,5 @@ void* b4madmqtt(void* thargs)
|
|||||||
free(simdata);
|
free(simdata);
|
||||||
free(simmap);
|
free(simmap);
|
||||||
|
|
||||||
//return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,4 @@
|
|||||||
#include "../helper/parameters.h"
|
#include "../helper/parameters.h"
|
||||||
#include "../helper/confighelper.h"
|
#include "../helper/confighelper.h"
|
||||||
|
|
||||||
void* looper(void* params);
|
int looper (Simulator simulator, Parameters* p);
|
||||||
void* b4madmqtt(void* params);
|
|
||||||
|
|||||||
+1
-25
@@ -4,7 +4,6 @@
|
|||||||
#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"
|
||||||
@@ -47,7 +46,6 @@ 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/");
|
||||||
@@ -82,29 +80,7 @@ int main(int argc, char** argv)
|
|||||||
slog_disable(SLOG_DEBUG);
|
slog_disable(SLOG_DEBUG);
|
||||||
}
|
}
|
||||||
|
|
||||||
pthread_t ui_thread;
|
looper(1, p);
|
||||||
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,7 +6,6 @@
|
|||||||
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: 618920440e...db3cd3eec3
Reference in New Issue
Block a user