105 lines
2.5 KiB
C
105 lines
2.5 KiB
C
|
#include <unistd.h>
|
||
|
#include <stdio.h>
|
||
|
#include <mosquitto.h>
|
||
|
#include <string.h>
|
||
|
#include <unistd.h>
|
||
|
|
||
|
#include "mqtt.h"
|
||
|
#include "config.h"
|
||
|
|
||
|
#define MAX_AIRSPEED 385
|
||
|
#define MAX_MQTT_PAYLOAD_SIZE 512
|
||
|
|
||
|
struct mosquitto *mqtt;
|
||
|
|
||
|
int airspeed = 0;
|
||
|
int heading = 0;
|
||
|
|
||
|
struct test {
|
||
|
char *foo;
|
||
|
};
|
||
|
|
||
|
void onMessage(struct mosquitto *mqtt, void *obj, const struct mosquitto_message *message) {
|
||
|
char *mqttTopic;
|
||
|
char mqttPayload[MAX_MQTT_PAYLOAD_SIZE];
|
||
|
|
||
|
if (strcmp(message->topic, "/xplane/meta/rref") == 0) {
|
||
|
//requestRrefFromXPlane(message);
|
||
|
} else if (strcmp(message->topic, "/xplane/meta/cmnd") == 0) {
|
||
|
mqttTopic = NULL;
|
||
|
memset(mqttPayload, 0, sizeof(mqttPayload));
|
||
|
|
||
|
printf("CMD: %s\n", (char *)message->payload);
|
||
|
if (strcmp((char *)message->payload, "sim/autopilot/airspeed_up") == 0) {
|
||
|
airspeed++;
|
||
|
if (airspeed > MAX_AIRSPEED) {
|
||
|
airspeed = MAX_AIRSPEED;
|
||
|
} else {
|
||
|
// airspeed changed
|
||
|
mqttTopic = "/xplane/rref/2";
|
||
|
sprintf(mqttPayload, "%03d", airspeed);
|
||
|
}
|
||
|
printf("Airspeed: %d\n", airspeed);
|
||
|
} else if (strcmp((char *)message->payload, "sim/autopilot/airspeed_down") == 0) {
|
||
|
airspeed--;
|
||
|
if (airspeed < 0) {
|
||
|
airspeed = 0;
|
||
|
} else {
|
||
|
mqttTopic = "/xplane/rref/2";
|
||
|
sprintf(mqttPayload, "%03d", airspeed);
|
||
|
}
|
||
|
printf("Airspeed: %d\n", airspeed);
|
||
|
} else if (strcmp((char *)message->payload, "sim/autopilot/heading_up") == 0) {
|
||
|
heading++;
|
||
|
if (heading > 360) {
|
||
|
heading = 1;
|
||
|
}
|
||
|
mqttTopic = "/xplane/rref/1";
|
||
|
sprintf(mqttPayload, "%03d", heading);
|
||
|
printf("Heading: %d\n", heading);
|
||
|
} else if (strcmp((char *)message->payload, "sim/autopilot/heading_down") == 0) {
|
||
|
heading--;
|
||
|
if (heading < 0) {
|
||
|
heading = 359;
|
||
|
}
|
||
|
mqttTopic = "/xplane/rref/1";
|
||
|
sprintf(mqttPayload, "%03d", heading);
|
||
|
printf("Heading: %d\n", heading);
|
||
|
} else {
|
||
|
printf("unkown command %s\n", (char *)message->payload);
|
||
|
}
|
||
|
|
||
|
if (mqttTopic != NULL && mqttPayload != NULL) {
|
||
|
sendMqtt(mqtt, mqttTopic, mqttPayload, strlen(mqttPayload));
|
||
|
}
|
||
|
} else if (strcmp(message->topic, "/xplane/meta/log") == 0) {
|
||
|
printf("Log: %s\n", (char *)message->payload);
|
||
|
} else {
|
||
|
fprintf(stderr, "Unknown topic %s\n", message->topic);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void doit(struct test *blah) {
|
||
|
blah->foo = "moep";
|
||
|
}
|
||
|
|
||
|
int main() {
|
||
|
struct test bar = { "baz" };
|
||
|
doit(&bar);
|
||
|
printf("foo is %s\n", bar.foo);
|
||
|
|
||
|
mqtt = connectMqtt(MQTT_USER1, MQTT_PASS, onMessage);
|
||
|
if (mqtt == NULL) {
|
||
|
return -1;
|
||
|
}
|
||
|
|
||
|
recvMqtt(mqtt, "/xplane/meta/cmnd");
|
||
|
|
||
|
while (1) {
|
||
|
mosquitto_loop(mqtt, 0, 1);
|
||
|
usleep(10 * 1000);
|
||
|
}
|
||
|
|
||
|
disconnectMqtt(mqtt);
|
||
|
}
|