Compare commits
5 Commits
4042ebb93d
...
main
Author | SHA1 | Date | |
---|---|---|---|
8c819ba562 | |||
080cfc8230 | |||
b3d99fd94b | |||
ada95d1d9c | |||
b21ce7d6ac |
7
README.md
Normal file
7
README.md
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
## build
|
||||||
|
```bash
|
||||||
|
apt install libmosquitto-dev
|
||||||
|
cp config.h.example config.h
|
||||||
|
# edit config.h
|
||||||
|
make
|
||||||
|
```
|
2
config.h.example
Normal file
2
config.h.example
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
#define MQTT_USER1 "<user>"
|
||||||
|
#define MQTT_PASS "<pass>"
|
1
mqtt.c
1
mqtt.c
@ -32,6 +32,7 @@ struct mosquitto *connectMqtt(char *user, char *pass, void (*messageCallback)(st
|
|||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// FIXME: I am hard coded :(
|
||||||
err = mosquitto_connect(mqtt, "openhab.sugarland.lan", 1883, 10);
|
err = mosquitto_connect(mqtt, "openhab.sugarland.lan", 1883, 10);
|
||||||
if (err)
|
if (err)
|
||||||
{
|
{
|
||||||
|
87
readref.c
87
readref.c
@ -24,6 +24,15 @@ 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
|
||||||
{
|
{
|
||||||
@ -49,7 +58,8 @@ 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')
|
||||||
{
|
{
|
||||||
@ -64,35 +74,57 @@ int *strtoint(char *in, size_t start, size_t n)
|
|||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
void onMessage(struct mosquitto *mqtt, void *obj, const struct mosquitto_message *message)
|
void requestRrefFromXPlane(const struct mosquitto_message *message)
|
||||||
{
|
{
|
||||||
int *id, *frequency;
|
int *index, *frequency;
|
||||||
char rref[XPLANE_RREF_MAX_LENGTH];
|
|
||||||
|
|
||||||
printf("Received message %.*s on topic %s\n", message->payloadlen, (char *)message->payload, message->topic);
|
|
||||||
|
|
||||||
if (strcmp(message->topic, "/xplane/meta/rref") == 0)
|
|
||||||
{
|
|
||||||
printf("found rref request\n");
|
|
||||||
if (message->payloadlen >= MQTT_RREF_REQUEST_MIN_LENGTH)
|
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);
|
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;
|
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 = *id
|
.dref_sender_index = *index};
|
||||||
};
|
|
||||||
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);
|
||||||
|
|
||||||
sendToXPlane(xPlaneSocket, DEST_SERVER, DEST_PORT, "RREF", (char *)&drefToRead, sizeof(drefToRead));
|
sendToXPlane(xPlaneSocket, DEST_SERVER, DEST_PORT, "RREF", (char *)&drefToRead, sizeof(drefToRead));
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void sendCommandToXPlane(const struct mosquitto_message *message)
|
||||||
|
{
|
||||||
|
char *command;
|
||||||
|
|
||||||
|
if (message->payloadlen > 0)
|
||||||
|
{
|
||||||
|
sendToXPlane(xPlaneSocket, DEST_SERVER, DEST_PORT, "CMND", (char *)message->payload, message->payloadlen);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void onMessage(struct mosquitto *mqtt, void *obj, const struct mosquitto_message *message)
|
||||||
|
{
|
||||||
|
printf("Received message %.*s on topic %s\n", message->payloadlen, (char *)message->payload, message->topic);
|
||||||
|
|
||||||
|
if (strcmp(message->topic, "/xplane/meta/rref") == 0)
|
||||||
|
{
|
||||||
|
requestRrefFromXPlane(message);
|
||||||
|
}
|
||||||
|
else if (strcmp(message->topic, "/xplane/meta/cmnd") == 0)
|
||||||
|
{
|
||||||
|
sendCommandToXPlane(message);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@ -110,7 +142,7 @@ int main()
|
|||||||
}
|
}
|
||||||
|
|
||||||
// mqtt
|
// mqtt
|
||||||
mqtt = connectMqtt(MQTT_USER, MQTT_PASS, onMessage);
|
mqtt = connectMqtt(MQTT_USER1, MQTT_PASS, onMessage);
|
||||||
if (mqtt == NULL)
|
if (mqtt == NULL)
|
||||||
{
|
{
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
@ -151,12 +183,37 @@ 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
|
||||||
|
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)
|
||||||
|
{
|
||||||
|
valueCache[drefRead.dref_sender_index].value = drefRead.dref_flt_value;
|
||||||
printf("%d: %f\n", drefRead.dref_sender_index, 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);
|
sprintf(mqttTopic, "/xplane/rref/%d", drefRead.dref_sender_index);
|
||||||
sprintf(mqttPayload, "%f", drefRead.dref_flt_value);
|
sprintf(mqttPayload, "%f", drefRead.dref_flt_value);
|
||||||
|
|
||||||
sendMqtt(mqtt, mqttTopic, mqttPayload, strlen(mqttPayload));
|
sendMqtt(mqtt, mqttTopic, mqttPayload, strlen(mqttPayload));
|
||||||
|
valueCache[drefRead.dref_sender_index].force = false;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// neither changed nor forced
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
@ -174,7 +231,7 @@ int main()
|
|||||||
exit(EXIT_FAILURE);
|
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?!)
|
// bye bye (and no, I don't care about error anymore; I'll exit anyway?!)
|
||||||
|
Reference in New Issue
Block a user