Oyster Harvest Area Closure Analysis 0.1
Prediction and analysis of NSW oyster harvest area closures.
Loading...
Searching...
No Matches
utils.h File Reference
#include <stdlib.h>
#include <string.h>
#include <curl/curl.h>
#include <stdint.h>
#include <cjson/cJSON.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <errno.h>
#include <math.h>
#include <log.h>
#include <libpq-fe.h>
Include dependency graph for utils.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  Utils_ReqData_TypeDef
 Holds HTTP response data before converting these data into cJSON objects. More...
 

Macros

#define USER_AGENT   "EnvMonitoring/0.1 (NSW Department of Primary Industries)"
 

Functions

size_t WriteMemoryCallback (void *contents, size_t size, size_t nmemb, void *userp)
 
int8_t MakeDirectory (const char *directory)
 Make directories (with error handling) More...
 
void WriteTimeseriesToFile (const char *filename, time_t *dates, double *values, int16_t max_n_values)
 Write timeseries data into a csv file. More...
 
void cJSON_Minify_Mod (char *json)
 Modified minify function from cJSON. More...
 
double Utils_PointsDistance (double latitude, double longitude, double station_latitude, double station_longitude)
 Distance between two points on earth. More...
 
void Utils_PrepareStatement (PGconn *psql_conn, const char *stmt_name, const char *stmt, const int nparams)
 

Macro Definition Documentation

◆ USER_AGENT

#define USER_AGENT   "EnvMonitoring/0.1 (NSW Department of Primary Industries)"

Function Documentation

◆ cJSON_Minify_Mod()

void cJSON_Minify_Mod ( char *  json)

Modified minify function from cJSON.

HTML minify function modified from cJSON library.

The main cJSON_Minify() function gets rid of all white space and '/' characters in a string. This however, causes sentances to appear without the nessessary spaces between words. This modified function keeps only one space between each character (if more than one space occurs) and maintains the spaces between words in a sentance. Additionally '/' characters are maintained as they are used as datetime delimiters.

See also
https://github.com/DaveGamble/cJSON/blob/master/cJSON.c#L2838
Parameters
jsonThe string to minify (parse).

◆ MakeDirectory()

int8_t MakeDirectory ( const char *  directory)

Make directories (with error handling)

Helper function to create directories with error handling.

MakeDirectory("my_directory");
MakeDirectory("my_directory/my_sub_directory");
int8_t MakeDirectory(const char *directory)
Make directories (with error handling)
Definition: utils.c:60
Parameters
directoryDirectory name you want to create.
Returns
Integer representing error status. 0 = OK ... 1 = ERROR

◆ Utils_PointsDistance()

double Utils_PointsDistance ( double  latitude,
double  longitude,
double  station_latitude,
double  station_longitude 
)

Distance between two points on earth.

Uses haversine formula to calculate the distance in km between two points on earth.

Used to calculate the distance between an POI and the closest Bureau of Meterology station.

Parameters
latitudeLatitude of point of interest.
longitudeLongitude of point of interest.
station_latitudeLatitude of station.
station_longitudeLongitude of station.
Returns
Distance between two points on earth in km.

◆ Utils_PrepareStatement()

void Utils_PrepareStatement ( PGconn *  psql_conn,
const char *  stmt_name,
const char *  stmt,
const int  nparams 
)

◆ WriteMemoryCallback()

size_t WriteMemoryCallback ( void *  contents,
size_t  size,
size_t  nmemb,
void *  userp 
)

Helper function to handle a CURL request response which normally gets written to stdout.

Callback function to save HTTP response data to a character array.

CURL defaults to printing to stdout. This function is added to CURL in the setup options (curl_easy_setupopt()). Memory is allocated and then filled with the response string. This character array is then used to create cJSON objects and other items.

MemoryStruct_TypeDef chunk;
chunk.memory = malloc(1);
chunk.size = 0;
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&chunk);
free(chunk.memory) // Don't forget to free allocation
size_t WriteMemoryCallback(void *contents, size_t size, size_t nmemb, void *userp)
Definition: utils.c:28
See also
https://curl.se/libcurl/c/getinmemory.html
Parameters
contentsThe items to add.
sizeThe size of items to add.
nmembThe number of members (data size).
userpThe struct to populate with data.
Returns
The size of received data.

◆ WriteTimeseriesToFile()

void WriteTimeseriesToFile ( const char *  filename,
time_t *  dates,
double *  values,
int16_t  max_n_values 
)

Write timeseries data into a csv file.

Helper function to handle writing timeseries data to a .csv file.

Writes timeseries data to a filename, exiting when the data is un-initialised '\0' or the maxiumum buffer size is reached. This function also converts UNIX time into local (human readable) time in ISO 8601 format.

// Build filename (directory requried)
char filename[100];
sprintf(filename, "%s/low.csv", directory);
dataset->low_tide_timestamps, // UNIX timestamps
dataset->low_tide_values, // Values
WW_FORECAST_RESPONSE_BUF); // Max number of values
#define WW_FORECAST_RESPONSE_BUF
Max response values.
Definition: forecast.h:22
void WriteTimeseriesToFile(const char *filename, time_t *dates, double *values, int16_t max_n_values)
Write timeseries data into a csv file.
Definition: utils.c:107
Parameters
filenameFile to write to.
datesDates (x values).
valuesData values (y values).
max_n_valuesMax buffer size.