From 0323056d960ecdb0ad9361d163a0f820e8ee3abc Mon Sep 17 00:00:00 2001 From: Phil Crump Date: Sun, 6 May 2018 13:03:47 +0000 Subject: [PATCH] Add JSON OziMux UDP Output. --- Sockets.md | 1 + gateway.c | 10 +++++++++ global.h | 1 + habpack.c | 59 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ habpack.h | 2 ++ 5 files changed, 73 insertions(+) diff --git a/Sockets.md b/Sockets.md index 25b3905..628af3d 100755 --- a/Sockets.md +++ b/Sockets.md @@ -6,6 +6,7 @@ The LoRa Gateway provides some socket interfaces, configurable in gateway.txt: - DataPort - TCP/IP server socket, allowing a single client. Sends raw telemetry packets (e.g. $$...). - UDPPort - UDP client broadcast socket, sending raw telemetry. - OziPlotterPort - UDP client broadcast socket, sending basic telemetry reformatted as OziPlotter CSV format. +- OziMuxPort - UDP client broadcast socket, sending basic telemetry reformatted as OziMux JSON format. diff --git a/gateway.c b/gateway.c index f1a1bbb..8a91a1a 100644 --- a/gateway.c +++ b/gateway.c @@ -979,6 +979,14 @@ void ProcessLineHABpack(int Channel, received_t *Received) Config.Payloads[PayloadIndex].Altitude); UDPSend(OziSentence, Config.OziPlotterPort); } + + // Send out to any OziMux clients + if (Config.OziMuxPort > 0) + { + char OziSentence[512]; + Habpack_Telem_JSON(Received, OziSentence, 511); + UDPSend(OziSentence, Config.OziMuxPort); + } } @@ -1697,9 +1705,11 @@ void LoadConfigFile(void) RegisterConfigInteger(MainSection, -1, "DataPort", &Config.DataPort, NULL); // Raw data server RegisterConfigInteger(MainSection, -1, "UDPPort", &Config.UDPPort, NULL); // UDP Broadcast socket (raw data) RegisterConfigInteger(MainSection, -1, "OziPlotterPort", &Config.OziPlotterPort, NULL); // UDP Broadcast socket (OziPlotter format) + RegisterConfigInteger(MainSection, -1, "OziMuxPort", &Config.OziMuxPort, NULL); // UDP Broadcast socket (OziMux format) if (Config.UDPPort > 0) LogMessage("UDP Broadcast of raw packets on port %d\n", Config.UDPPort); if (Config.OziPlotterPort > 0) LogMessage("UDP Broadcast of OziPlotter packets on port %d\n", Config.OziPlotterPort); + if (Config.OziMuxPort > 0) LogMessage("UDP Broadcast of OziMux packets on port %d\n", Config.OziMuxPort); // Timeout for HAB Telnet uplink Config.HABTimeout = 4000; diff --git a/global.h b/global.h index 374c314..8dafe7e 100644 --- a/global.h +++ b/global.h @@ -117,6 +117,7 @@ struct TConfig int ServerPort; // JSON port for telemetry, settings int UDPPort; // UDP Broadcast port for raw received data packets int OziPlotterPort; // UDP Broadcast port for OziPlotter formatted packets + int OziMuxPort; // UDP Broadcast port for OziMux formatted packets int HABPort; // Telnet style port for comms with HAB int HABTimeout; // Timeout in ms for telnet uplink int HABChannel; // LoRa Channel for uplink diff --git a/habpack.c b/habpack.c index 718998d..7810325 100644 --- a/habpack.c +++ b/habpack.c @@ -301,6 +301,65 @@ void Habpack_Telem_UKHAS_String(received_t *Received, char *ukhas_string, uint32 ); } +void Habpack_Telem_JSON(received_t *Received, char *json_string, uint32_t max_length) +{ + uint32_t str_index; + habpack_telem_linklist_entry_t *walk_ptr; + + str_index = snprintf( + json_string, + max_length, + "{\"type\":\"PAYLOAD_TELEMETRY\"" + ); + + /* All other fields */ + walk_ptr = Received->Telemetry.habpack_extra; + while(walk_ptr != NULL) + { + str_index += snprintf( + &json_string[str_index], + (max_length - str_index), + ",\"%s\":", + walk_ptr->name + ); + if(walk_ptr->value_type == HB_VALUE_INTEGER) + { + str_index += snprintf( + &json_string[str_index], + (max_length - str_index), + "%"PRId64, + walk_ptr->value.integer + ); + } + else if(walk_ptr->value_type == HB_VALUE_REAL) + { + str_index += snprintf( + &json_string[str_index], + (max_length - str_index), + "%f", + walk_ptr->value.real + ); + } + else if(walk_ptr->value_type == HB_VALUE_STRING) + { + str_index += snprintf( + &json_string[str_index], + (max_length - str_index), + "\"%s\"", + walk_ptr->value.string + ); + } + + walk_ptr = walk_ptr->next; + } + + str_index += snprintf( + &json_string[str_index], + (max_length - str_index), + "}" + ); +} + void Habpack_Telem_Destroy(received_t *Received) { habpack_telem_linklist_entry_t *walk_ptr, *last_ptr; diff --git a/habpack.h b/habpack.h index a074b68..ab7aebd 100644 --- a/habpack.h +++ b/habpack.h @@ -45,4 +45,6 @@ int Habpack_Process_Message(received_t *Received); void Habpack_Telem_Destroy(received_t *Received); +void Habpack_Telem_JSON(received_t *Received, char *json_string, uint32_t max_length); + #endif /* __HABPACK_H__ */ \ No newline at end of file