-
Notifications
You must be signed in to change notification settings - Fork 1
/
parser.c
65 lines (53 loc) · 2.33 KB
/
parser.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <cjson/cJSON.h>
#include <time.h>
#include "parser.h"
char *strptime(const char *s, const char *format, struct tm *tm);
void parse_aemo_request(char *ptr, struct AEMO *aemo, char *region)
{
const cJSON *regions;
const cJSON *parameter;
cJSON *NEM = cJSON_Parse(ptr);
if (NEM == NULL) {
printf("Unable to parse JSON file\r\n");
return;
}
regions = cJSON_GetObjectItemCaseSensitive(NEM, "ELEC_NEM_SUMMARY");
if (regions == NULL) {
printf("Cannot find ELEC_NEM_SUMMARY object\r\n");
return;
}
cJSON_ArrayForEach(parameter, regions)
{
cJSON *name = cJSON_GetObjectItemCaseSensitive(parameter, "REGIONID");
if (name != NULL) {
//printf("Region %s\n",name->valuestring);
if (strcmp(name->valuestring, region) == 0) {
cJSON *settlement = cJSON_GetObjectItemCaseSensitive(parameter, "SETTLEMENTDATE");
if (settlement != NULL) {
/* String in the format of 2020-12-19T15:10:00 */
if (strptime((char *)settlement->valuestring, "%Y-%m-%dT%H:%M:%S", &aemo->settlement) == NULL)
printf("Unable to parse settlement time\r\n");
}
cJSON *price = cJSON_GetObjectItemCaseSensitive(parameter, "PRICE");
if (price != NULL) aemo->price = price->valuedouble;
else printf("Can't find %s\r\n", price->string);
cJSON *totaldemand = cJSON_GetObjectItemCaseSensitive(parameter, "TOTALDEMAND");
if (totaldemand != NULL) aemo->totaldemand = totaldemand->valuedouble;
else printf("Can't find %s\r\n", totaldemand->string);
cJSON *netinterchange = cJSON_GetObjectItemCaseSensitive(parameter, "NETINTERCHANGE");
if (netinterchange != NULL) aemo->netinterchange = netinterchange->valuedouble;
else printf("Can't find %s\r\n", netinterchange->string);
cJSON *scheduledgeneration = cJSON_GetObjectItemCaseSensitive(parameter, "SCHEDULEDGENERATION");
if (scheduledgeneration != NULL) aemo->scheduledgeneration = scheduledgeneration->valuedouble;
else printf("Can't find %s\r\n", scheduledgeneration->string);
cJSON *semischeduledgeneration = cJSON_GetObjectItemCaseSensitive(parameter, "SEMISCHEDULEDGENERATION");
if (semischeduledgeneration != NULL) aemo->semischeduledgeneration = semischeduledgeneration->valuedouble;
else printf("Can't find %s\r\n", semischeduledgeneration->string);
}
}
}
cJSON_Delete(NEM);
}