Compare commits
No commits in common. "080cfc8230dd6bf40b274a069dbe6b74b1d78f47" and "ada95d1d9cd3ab99049a49287054efa4c35a4803" have entirely different histories.
080cfc8230
...
ada95d1d9c
67
readref.c
67
readref.c
@ -24,15 +24,6 @@ const int MQTT_RREF_REQUEST_MIN_LENGTH = MQTT_RREF_REQUEST_LENGTH_ID + MQTT_RREF
|
|||||||
int xPlaneSocket;
|
int xPlaneSocket;
|
||||||
struct mosquitto *mqtt;
|
struct mosquitto *mqtt;
|
||||||
|
|
||||||
struct valueCacheElement
|
|
||||||
{
|
|
||||||
float value;
|
|
||||||
bool force;
|
|
||||||
};
|
|
||||||
|
|
||||||
struct valueCacheElement *valueCache = NULL;
|
|
||||||
int valueCacheCount = 0;
|
|
||||||
|
|
||||||
#define XPLANE_RREF_MAX_LENGTH 400
|
#define XPLANE_RREF_MAX_LENGTH 400
|
||||||
struct dref_struct_in
|
struct dref_struct_in
|
||||||
{
|
{
|
||||||
@ -58,8 +49,7 @@ int *strtoint(char *in, size_t start, size_t n)
|
|||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (int i = 0; i < n; i++)
|
for (int i = 0; i < n; i++) {
|
||||||
{
|
|
||||||
int position = offset - i;
|
int position = offset - i;
|
||||||
if (in[position] >= '0' && in[position] <= '9')
|
if (in[position] >= '0' && in[position] <= '9')
|
||||||
{
|
{
|
||||||
@ -76,27 +66,22 @@ int *strtoint(char *in, size_t start, size_t n)
|
|||||||
|
|
||||||
void requestRrefFromXPlane(const struct mosquitto_message *message)
|
void requestRrefFromXPlane(const struct mosquitto_message *message)
|
||||||
{
|
{
|
||||||
int *index, *frequency;
|
int *id, *frequency;
|
||||||
|
|
||||||
if (message->payloadlen >= MQTT_RREF_REQUEST_MIN_LENGTH)
|
if (message->payloadlen >= MQTT_RREF_REQUEST_MIN_LENGTH)
|
||||||
{
|
{
|
||||||
index = strtoint(message->payload, MQTT_RREF_REQUEST_START_ID, MQTT_RREF_REQUEST_LENGTH_ID);
|
id = 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);
|
frequency = strtoint(message->payload, MQTT_RREF_REQUEST_START_FREQ, MQTT_RREF_REQUEST_LENGTH_FREQ);
|
||||||
|
|
||||||
if (index == NULL || frequency == NULL)
|
if (id == NULL || frequency == NULL) {
|
||||||
{
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (*index < valueCacheCount)
|
|
||||||
{
|
|
||||||
valueCache[*index].force = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
// prepare struct to send, make sure padding is with NULL bytes
|
// prepare struct to send, make sure padding is with NULL bytes
|
||||||
struct dref_struct_in drefToRead = {
|
struct dref_struct_in drefToRead = {
|
||||||
.dref_freq = *frequency,
|
.dref_freq = *frequency,
|
||||||
.dref_sender_index = *index};
|
.dref_sender_index = *id
|
||||||
|
};
|
||||||
memset(drefToRead.dref_string, 0, sizeof(drefToRead.dref_string));
|
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);
|
strncpy(drefToRead.dref_string, &((char *)message->payload)[MQTT_RREF_REQUEST_START_RREF], message->payloadlen - MQTT_RREF_REQUEST_START_RREF);
|
||||||
|
|
||||||
@ -108,8 +93,7 @@ void sendCommandToXPlane(const struct mosquitto_message *message)
|
|||||||
{
|
{
|
||||||
char *command;
|
char *command;
|
||||||
|
|
||||||
if (message->payloadlen > 0)
|
if (message->payloadlen > 0) {
|
||||||
{
|
|
||||||
sendToXPlane(xPlaneSocket, DEST_SERVER, DEST_PORT, "CMND", (char *)message->payload, message->payloadlen);
|
sendToXPlane(xPlaneSocket, DEST_SERVER, DEST_PORT, "CMND", (char *)message->payload, message->payloadlen);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -142,7 +126,7 @@ int main()
|
|||||||
}
|
}
|
||||||
|
|
||||||
// mqtt
|
// mqtt
|
||||||
mqtt = connectMqtt(MQTT_USER1, MQTT_PASS, onMessage);
|
mqtt = connectMqtt(MQTT_USER, MQTT_PASS, onMessage);
|
||||||
if (mqtt == NULL)
|
if (mqtt == NULL)
|
||||||
{
|
{
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
@ -183,37 +167,12 @@ int main()
|
|||||||
struct dref_struct_out drefRead;
|
struct dref_struct_out drefRead;
|
||||||
memcpy(&drefRead, &payload[i], sizeof(struct dref_struct_out));
|
memcpy(&drefRead, &payload[i], sizeof(struct dref_struct_out));
|
||||||
|
|
||||||
// add new element to array
|
printf("%d: %f\n", drefRead.dref_sender_index, drefRead.dref_flt_value);
|
||||||
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
|
|
||||||
}
|
|
||||||
|
|
||||||
if (valueCache[drefRead.dref_sender_index].force || valueCache[drefRead.dref_sender_index].value != drefRead.dref_flt_value)
|
sprintf(mqttTopic, "/xplane/rref/%d", drefRead.dref_sender_index);
|
||||||
{
|
sprintf(mqttPayload, "%f", 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);
|
|
||||||
|
|
||||||
sprintf(mqttTopic, "/xplane/rref/%d", drefRead.dref_sender_index);
|
sendMqtt(mqtt, mqttTopic, mqttPayload, strlen(mqttPayload));
|
||||||
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
|
else
|
||||||
@ -231,7 +190,7 @@ int main()
|
|||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
|
|
||||||
mosquitto_loop(mqtt, 0, 1);
|
mosquitto_loop(mqtt, 100, 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
// bye bye (and no, I don't care about error anymore; I'll exit anyway?!)
|
// bye bye (and no, I don't care about error anymore; I'll exit anyway?!)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user