-
Notifications
You must be signed in to change notification settings - Fork 1
/
aemo.c
294 lines (236 loc) · 6.75 KB
/
aemo.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <curl/curl.h>
#include <time.h>
#include <stdbool.h>
#include <unistd.h>
#include <libgen.h>
#include <signal.h>
#include "http.h"
#include "MQTTClient.h"
#include "mqtt.h"
#include "parser.h"
#define IDLE 0
#define FETCH 1
bool exitflag;
static void print_usage(char *prg)
{
fprintf(stderr, "Usage: %s region [options]\n\n",prg);
fprintf(stderr, "Regions: NSW1, QLD1, SA1, TAS1, VIC1\n\n");
fprintf(stderr, "Options:\n");
fprintf(stderr, " -l <filename> Log to file\n");
fprintf(stderr, " -m <broker URI> Log to MQTT Broker\n");
fprintf(stderr, " -t <topic> MQTT topic\n");
fprintf(stderr, " -u <username> Username for MQTT Broker\n");
fprintf(stderr, " -p <password> Password for MQTT Broker\n");
fprintf(stderr, "\n");
}
int print_aemo_data(struct AEMO *aemo)
{
printf("\r\n");
printf(" Settlement Period: %04d-%02d-%02d %02d:%02d:%02d\r\n",
aemo->settlement.tm_year + 1900,
aemo->settlement.tm_mon + 1,
aemo->settlement.tm_mday,
aemo->settlement.tm_hour,
aemo->settlement.tm_min,
aemo->settlement.tm_sec);
printf(" Price: $%.02f\r\n",aemo->price);
printf(" Total Demand: %.02f MW\r\n",aemo->totaldemand);
printf(" Scheduled Generation (Baseload): %.02f MW\r\n",aemo->scheduledgeneration);
printf(" Semi Scheduled Generation (Renewable): %.02f MW\r\n",aemo->semischeduledgeneration);
printf(" Export: %.02f MW\r\n",aemo->netinterchange);
fflush(stdout);
}
int log_prices_file(FILE *fhandle, struct AEMO *aemo, int number_tries)
{
time_t now;
struct tm timeinfo;
time(&now);
localtime_r(&now, &timeinfo);
fprintf(fhandle,"%04d-%02d-%02d %02d:%02d:%02d,",
timeinfo.tm_year + 1900,
timeinfo.tm_mon + 1,
timeinfo.tm_mday,
timeinfo.tm_hour,
timeinfo.tm_min,
timeinfo.tm_sec);
fprintf(fhandle,"%d,", number_tries);
fprintf(fhandle,"%04d-%02d-%02d %02d:%02d:%02d,",
aemo->settlement.tm_year + 1900,
aemo->settlement.tm_mon + 1,
aemo->settlement.tm_mday,
aemo->settlement.tm_hour,
aemo->settlement.tm_min,
aemo->settlement.tm_sec);
fprintf(fhandle,"%.02f,%.02f,%.02f,%.02f,%.02f\r\n",
aemo->price,
aemo->totaldemand,
aemo->netinterchange,
aemo->scheduledgeneration,
aemo->semischeduledgeneration);
fflush(fhandle);
}
int log_prices_mqtt(MQTTClient client, char * topic, struct AEMO *aemo)
{
unsigned char mqtt_str[800];
sprintf(mqtt_str,"{\"price\":%.02f,\"totaldemand\":%.02f,\"netinterchange\":%.02f,\"scheduledgeneration\":%.02f,\"semischeduledgeneration\":%.02f}",
aemo->price,
aemo->totaldemand,
aemo->netinterchange,
aemo->scheduledgeneration,
aemo->semischeduledgeneration);
MQTT_pub(client, topic ,mqtt_str);
}
void ctrlc_handler(int s) {
exitflag = 1;
}
int main(int argc, char **argv)
{
char * logfilename = NULL;
bool logtofile = false;
char * mqttbrokerURI = NULL;
char topic[] = {"electricity/5min"};
char * mqtttopic = topic;
char * mqttusername = NULL;
char * mqttpassword = NULL;
char * nemregion = NULL;
bool logtomqtt = false;
printf("AEMO <-> MQTT Connector\r\n");
int opt;
while ((opt = getopt(argc, argv, "l:m:t:u:p:?")) != -1) {
switch (opt) {
case 'l':
logfilename = (char *)optarg;
logtofile = true;
break;
case 'm':
mqttbrokerURI = (char *)optarg;
logtomqtt = true;
break;
case 't':
mqtttopic = (char *)optarg;
break;
case 'u':
mqttusername = (char *)optarg;
break;
case 'p':
mqttpassword = (char *)optarg;
break;
default:
print_usage(basename(argv[0]));
exit(1);
break;
}
}
if (optind >= argc) {
printf("No region specified\r\n");
print_usage(basename(argv[0]));
exit(1);
}
if (argv[optind] != NULL){
nemregion = (char *)argv[optind];
}
printf("Region = %s\r\n", nemregion);
CURLcode res;
unsigned char number_tries;
char *data;
struct buffer out_buf = {
.data = data,
.pos = 0
};
struct AEMO aemo;
time_t now;
struct tm timeinfo;
int previous_period;
unsigned int state = IDLE;
FILE *fhandle;
if (logtofile) {
printf("Logging to %s\r\n",logfilename);
fhandle = fopen(logfilename,"a+");
if (fhandle == NULL) {
printf("Unable to open %s for writing\r\n",logfilename);
exit(1);
}
}
MQTTClient client;
if (logtomqtt) {
printf("Connecting to broker: %s\r\n",mqttbrokerURI);
printf("Publishing to topic: %s\r\n",mqtttopic);
if (mqttusername) printf("Username: %s\r\n",mqttusername);
//printf("Password: %s\r\n",mqttpassword);
client = MQTT_connect(mqttbrokerURI, mqttusername, mqttpassword);
}
/* Init CTRL-C handler */
exitflag = 0;
struct sigaction sigIntHandler;
sigIntHandler.sa_handler = ctrlc_handler;
sigemptyset(&sigIntHandler.sa_mask);
sigIntHandler.sa_flags = 0;
sigaction(SIGINT, &sigIntHandler, NULL);
/* Populate data */
out_buf.data = malloc(16384);
out_buf.pos = 0;
res = http_json_request(&out_buf);
if(res == CURLE_OK) {
parse_aemo_request(out_buf.data, &aemo, nemregion);
printf("Current settlement period %04d-%02d-%02d %02d:%02d:%02d\r\n",
aemo.settlement.tm_year + 1900,
aemo.settlement.tm_mon + 1,
aemo.settlement.tm_mday,
aemo.settlement.tm_hour,
aemo.settlement.tm_min,
aemo.settlement.tm_sec);
previous_period = aemo.settlement.tm_min;
} else {
printf("Failed to download AEMO ELEC_NEM_SUMMARY\r\n");
}
while (!exitflag) {
/* Get Time */
time(&now);
localtime_r(&now, &timeinfo);
switch (state) {
case IDLE:
/* 20 seconds after a 5 minute period, start fetching a new JSON file */
if ((!(timeinfo.tm_min % 5)) & (timeinfo.tm_sec == 20)) {
state = FETCH;
number_tries = 0;
printf("Fetching data for next settlement period");
}
break;
case FETCH:
/* Start fetching a new JSON file. We keep trying every 5 seconds until */
/* the settlement time is different from the previous period */
/* Allocate a modest buffer now, we can realloc later if needed */
out_buf.data = malloc(16384);
out_buf.pos = 0;
/* Fetch JSON file */
res = http_json_request(&out_buf);
if(res == CURLE_OK) {
/* If HTTP request was successful, parse request */
parse_aemo_request(out_buf.data, &aemo, nemregion);
/* Print a dot each time we make a HTTP request */
printf(".");
fflush(stdout);
number_tries++;
if (aemo.settlement.tm_min != previous_period) {
/* Change in settlement time, log new period */
previous_period = aemo.settlement.tm_min;
print_aemo_data(&aemo);
if (logtofile) log_prices_file(fhandle, &aemo, number_tries);
if (logtomqtt) log_prices_mqtt(client, mqtttopic, &aemo);
/* Success, go back to IDLE */
state = IDLE;
}
}
/* No luck, we will try again in five */
sleep(5);
break;
}
sleep(1);
}
printf("\r\nClosing...\r\n");
if (logtofile) fclose(fhandle);
if (logtomqtt) MQTT_disconnect(client);
}