only send rrefs if value is changed

This commit is contained in:
damage 2024-02-01 23:54:36 +01:00
parent b3d99fd94b
commit 080cfc8230

View File

@ -24,6 +24,15 @@ const int MQTT_RREF_REQUEST_MIN_LENGTH = MQTT_RREF_REQUEST_LENGTH_ID + MQTT_RREF
int xPlaneSocket;
struct mosquitto *mqtt;
struct valueCacheElement
{
float value;
bool force;
};
struct valueCacheElement *valueCache = NULL;
int valueCacheCount = 0;
#define XPLANE_RREF_MAX_LENGTH 400
struct dref_struct_in
{
@ -49,7 +58,8 @@ int *strtoint(char *in, size_t start, size_t n)
return NULL;
}
for (int i = 0; i < n; i++) {
for (int i = 0; i < n; i++)
{
int position = offset - i;
if (in[position] >= '0' && in[position] <= '9')
{
@ -66,22 +76,27 @@ int *strtoint(char *in, size_t start, size_t n)
void requestRrefFromXPlane(const struct mosquitto_message *message)
{
int *id, *frequency;
int *index, *frequency;
if (message->payloadlen >= MQTT_RREF_REQUEST_MIN_LENGTH)
{
id = strtoint(message->payload, MQTT_RREF_REQUEST_START_ID, MQTT_RREF_REQUEST_LENGTH_ID);
index = strtoint(message->payload, MQTT_RREF_REQUEST_START_ID, MQTT_RREF_REQUEST_LENGTH_ID);
frequency = strtoint(message->payload, MQTT_RREF_REQUEST_START_FREQ, MQTT_RREF_REQUEST_LENGTH_FREQ);
if (id == NULL || frequency == NULL) {
if (index == NULL || frequency == NULL)
{
return;
}
if (*index < valueCacheCount)
{
valueCache[*index].force = true;
}
// prepare struct to send, make sure padding is with NULL bytes
struct dref_struct_in drefToRead = {
.dref_freq = *frequency,
.dref_sender_index = *id
};
.dref_sender_index = *index};
memset(drefToRead.dref_string, 0, sizeof(drefToRead.dref_string));
strncpy(drefToRead.dref_string, &((char *)message->payload)[MQTT_RREF_REQUEST_START_RREF], message->payloadlen - MQTT_RREF_REQUEST_START_RREF);
@ -93,7 +108,8 @@ void sendCommandToXPlane(const struct mosquitto_message *message)
{
char *command;
if (message->payloadlen > 0) {
if (message->payloadlen > 0)
{
sendToXPlane(xPlaneSocket, DEST_SERVER, DEST_PORT, "CMND", (char *)message->payload, message->payloadlen);
}
}
@ -167,12 +183,37 @@ int main()
struct dref_struct_out drefRead;
memcpy(&drefRead, &payload[i], sizeof(struct dref_struct_out));
printf("%d: %f\n", drefRead.dref_sender_index, drefRead.dref_flt_value);
// add new element to array
if (valueCache == NULL)
{
valueCacheCount = drefRead.dref_sender_index + 10;
valueCache = calloc(valueCacheCount, sizeof(struct valueCacheElement));
}
else if (drefRead.dref_sender_index >= valueCacheCount)
{
valueCacheCount = drefRead.dref_sender_index + 10;
valueCache = reallocarray(valueCache, valueCacheCount, sizeof(struct valueCacheElement));
}
else
{
// size of array is sufficient
}
sprintf(mqttTopic, "/xplane/rref/%d", drefRead.dref_sender_index);
sprintf(mqttPayload, "%f", drefRead.dref_flt_value);
if (valueCache[drefRead.dref_sender_index].force || valueCache[drefRead.dref_sender_index].value != drefRead.dref_flt_value)
{
valueCache[drefRead.dref_sender_index].value = drefRead.dref_flt_value;
printf("%d: %f\n", drefRead.dref_sender_index, drefRead.dref_flt_value);
sendMqtt(mqtt, mqttTopic, mqttPayload, strlen(mqttPayload));
sprintf(mqttTopic, "/xplane/rref/%d", drefRead.dref_sender_index);
sprintf(mqttPayload, "%f", drefRead.dref_flt_value);
sendMqtt(mqtt, mqttTopic, mqttPayload, strlen(mqttPayload));
valueCache[drefRead.dref_sender_index].force = false;
}
else
{
// neither changed nor forced
}
}
}
else
@ -190,7 +231,7 @@ int main()
exit(EXIT_FAILURE);
}
mosquitto_loop(mqtt, 100, 1);
mosquitto_loop(mqtt, 0, 1);
}
// bye bye (and no, I don't care about error anymore; I'll exit anyway?!)