master resets slaves

This commit is contained in:
2025-05-18 15:10:17 +02:00
parent 17da15d69f
commit 199168550d
3 changed files with 131 additions and 13 deletions

114
simulatesimulator/stress.c Normal file
View File

@ -0,0 +1,114 @@
#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
#define LOOPS 1000000
struct mosquitto *mqtt;
int airspeed = 100;
int heading = 0;
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);
}
}
int loop = 0;
int main() {
char mqttPayload[MAX_MQTT_PAYLOAD_SIZE];
mqtt = connectMqtt(MQTT_USER1, MQTT_PASS, onMessage);
if (mqtt == NULL) {
return -1;
}
recvMqtt(mqtt, "/xplane/meta/cmnd");
while (1) {
loop++;
if (loop > LOOPS) {
sprintf(mqttPayload, "%03d", heading);
sendMqtt(mqtt, "/xplane/rref/1", mqttPayload, strlen(mqttPayload));
sprintf(mqttPayload, "%03d", airspeed);
sendMqtt(mqtt, "/xplane/rref/2", mqttPayload, strlen(mqttPayload));
heading++;
if (heading >= 360) {
heading = 0;
}
airspeed++;
if (airspeed >= MAX_AIRSPEED) {
airspeed = 100;
}
loop = 0;
}
mosquitto_loop(mqtt, 0, 1);
}
disconnectMqtt(mqtt);
}