Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Realtime export of payload data in KML format. #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions gateway.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "urlencode.h"
#include "base64.h"
#include "ssdv.h"
#include "kml.h"
#include "global.h"


Expand Down Expand Up @@ -586,6 +587,7 @@ void LoadConfigFile()
Config.ftpUser[0] = '\0';
Config.ftpPassword[0] = '\0';
Config.ftpFolder[0] = '\0';
Config.EnableKML = 0;

if ((fp = fopen(filename, "r")) == NULL)
{
Expand All @@ -604,6 +606,8 @@ void LoadConfigFile()
ReadString(fp, "ftpPassword", Config.ftpPassword, sizeof(Config.ftpPassword), 0);
ReadString(fp, "ftpFolder", Config.ftpFolder, sizeof(Config.ftpFolder), 0);

ReadBoolean(fp, "EnableKML", 0, &Config.EnableKML);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don`t make this a boolean. I would like to add different log levels, to record full telemetry and/or KML and/or RSSI / frequency offset etc. Perhaps change the name to "LogLevel" and use a bitmask. Thanks.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This configuration entry is only for enabling KML output (like Habitat output which is a boolean too), and in this current changeset the data which is exported in the kml is not configurable (lat,lon and altitude). When new features for kml export will be implemented I suggest that we add configuration variable for these features, but since we don't know what may be supported in the future I suggest that we don't add configuration entries which are not supported. It would be nice to implement logging with the features you mentioned but this is out of scope of this kml feature.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree that an EnableKML bolean seems consistent with the other features and perhaps LogTelemetry/LogKML/LogRSSI boleans if/when the ability to log these values is added in future?


for (Channel=0; Channel<=1; Channel++)
{
// Defaults
Expand Down Expand Up @@ -1070,6 +1074,12 @@ int main(int argc, char **argv)
DoPositionCalcs(Channel);

Config.LoRaDevices[Channel].TelemetryCount++;
if (Config.EnableKML)
UpdatePayloadKML(Config.LoRaDevices[Channel].Payload, Config.LoRaDevices[Channel].Seconds,
Config.LoRaDevices[Channel].Latitude, Config.LoRaDevices[Channel].Longitude,
Config.LoRaDevices[Channel].Altitude);



Message[strlen(Message+1)] = '\0';
LogMessage("Ch %d: %s\n", Channel, Message+1);
Expand Down Expand Up @@ -1111,6 +1121,12 @@ int main(int argc, char **argv)
DoPositionCalcs(Channel);

Config.LoRaDevices[Channel].TelemetryCount++;
if (Config.EnableKML)
UpdatePayloadKML(Payloads[SourceID].Payload, Config.LoRaDevices[Channel].Seconds,
Config.LoRaDevices[Channel].Latitude, Config.LoRaDevices[Channel].Longitude,
Config.LoRaDevices[Channel].Altitude);



LogMessage("Ch %d: Sender %d Source %d (%s) Position %8.5lf, %8.5lf, %05u\n",
Channel,
Expand Down
3 changes: 2 additions & 1 deletion global.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ struct TConfig
char ftpPassword[32];
char ftpFolder[64];
struct TLoRaDevice LoRaDevices[2];
int EnableKML;
};

extern struct TConfig Config;
extern struct TConfig Config;
125 changes: 125 additions & 0 deletions kml.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
/* RealTime KML export of the payload data */
/* Based on https://ukhas.org.uk/using_google_earth */

#include <unistd.h> // UNIX standard function definitions
#include <stdio.h> // Standard input/output definitions
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>

#include "kml.h"
#include "global.h"

#define KML_LINE_SIZE 256 //Maximum size of a kml line

/* KML file footer */
const char footer_str[] =
" </coordinates>\n" \
" </LineString>\n" \
" </Placemark>\n" \
" </Document>\n" \
"</kml>\n";

/* Write the header of the KML file for the specified payload */
void writeHeader(FILE* fp, char * payload) {
fprintf(fp,"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
fprintf(fp," <kml xmlns=\"http://www.opengis.net/kml/2.2\" xmlns:gx=\"http://www.google.com/kml/ext/2.2\" " );
fprintf(fp," xmlns:kml=\"http://www.opengis.net/kml/2.2\" xmlns:atom=\"http://www.w3.org/2005/Atom\">\n");
fprintf(fp," <Document>\n <name>%s</name>\n", payload);
fprintf(fp," <open>1</open>\n");
fprintf(fp," <Placemark>\n");
fprintf(fp," <name>%s-track</name>\n", payload);
fprintf(fp," <LineString>\n");
fprintf(fp," <extrude>1</extrude>\n");
fprintf(fp," <tessellate>1</tessellate>\n");
fprintf(fp," <altitudeMode>absolute</altitudeMode>\n");
fprintf(fp," <coordinates>\n");

}

/* Write the footer of the KML file */
void writeFooter(FILE* fp) {
fprintf(fp,"%s", footer_str);
}

/* Write the KML for refreshing the track of the specified payload */
void writeRefreshKML(FILE* fp, char * payload) {
fprintf(fp,"<kml xmlns=\"http://earth.google.com/kml/2.0\">\n");
fprintf(fp," <Document>\n");
fprintf(fp," <NetworkLink>\n");
fprintf(fp," <name>%s</name>\n", payload);
fprintf(fp," <Url>\n");
fprintf(fp," <href>%s.kml</href>\n", payload);
fprintf(fp," <refreshMode>onInterval</refreshMode>\n");
fprintf(fp," <refreshInterval>5</refreshInterval>\n");
fprintf(fp," </Url>\n");
fprintf(fp," </NetworkLink>\n");
fprintf(fp," </Document>\n");
fprintf(fp,"</kml>\n");
}

/* Update the KML file of the payload with the specified data */
void UpdatePayloadKML(char * payload, unsigned int seconds, double latitude, double longitude, unsigned int altitude) {
FILE * fp;
char filename[256];
char line[KML_LINE_SIZE];
struct stat st;
int res;

/* Discard empty positions */
if (latitude == 0.0 && longitude == 0.0)
return;
/* Path to the KML file for this payload */
sprintf(filename, "%s.kml", payload);

/* Check if the KML exists already */
res = stat(filename, &st);

/* Prepare the KML line for the payload data */
snprintf(line, KML_LINE_SIZE, " %f,%f,%d\n",
longitude, latitude, altitude);

/* If the KML doesn't exists */
if (res < 0) {
/* Create a KML file which will refresh the payload KML at regular interval */
char filename_refresh[256];
FILE * fp_refresh;
sprintf(filename_refresh, "%s-refresh.kml", payload);
fp_refresh = fopen(filename_refresh, "w");
if (fp_refresh) {
writeRefreshKML(fp_refresh,payload);
fclose(fp_refresh);
} else {
LogMessage("Can't open %s file for writing\n", filename_refresh);
}
/* Create the KML file for the payload */

fp = fopen(filename, "a");
if (!fp) {
LogMessage("Can't open %s file for writing\n", filename);
return;
} else {
LogMessage("Created %s file\n", filename);
}
/* Add the header, new data line and footer */
writeHeader(fp, payload);
fwrite(line, 1, strlen(line), fp);
writeFooter(fp);

} else {
/* KML already exists : update only the file with new data */
fp = fopen(filename, "r+");
if (!fp) {
LogMessage("Can't open %s file for writing\n", filename);
return;
}
/* Write new data just before the position of the footer in the KML */
fseek(fp, -strlen(footer_str), SEEK_END);
fwrite(line, 1, strlen(line), fp);
writeFooter(fp);

}
fclose(fp);
}


4 changes: 4 additions & 0 deletions kml.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/* RealTime KML export of the payload data */
/* Based on https://ukhas.org.uk/using_google_earth */

void UpdatePayloadKML(char * payload, unsigned int seconds, double latitude, double longitude, unsigned int altitude);
33 changes: 18 additions & 15 deletions makefile
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
gateway: gateway.o urlencode.o base64.o ssdv.o ssdv.h global.h
cc -o gateway gateway.o urlencode.o base64.o ssdv.o -lm -lwiringPi -lwiringPiDev -lcurl -lncurses -lpthread

gateway.o: gateway.c global.h
gcc -c gateway.c

ssdv.o: ssdv.c ssdv.h global.h
gcc -c ssdv.c

urlencode.o: urlencode.c
gcc -c urlencode.c

base64.o: base64.c
gcc -c base64.c

gateway: gateway.o urlencode.o base64.o ssdv.o ssdv.h kml.o global.h
cc -o gateway gateway.o urlencode.o base64.o ssdv.o kml.o -lm -lwiringPi -lwiringPiDev -lcurl -lncurses -lpthread

gateway.o: gateway.c global.h
gcc -c gateway.c

ssdv.o: ssdv.c ssdv.h global.h
gcc -c ssdv.c

urlencode.o: urlencode.c
gcc -c urlencode.c

base64.o: base64.c
gcc -c base64.c

kml.o: kml.c kml.h global.h
gcc -c kml.c