-
Notifications
You must be signed in to change notification settings - Fork 3
/
json_messages.c
665 lines (544 loc) · 22.8 KB
/
json_messages.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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
/*
* Copyright (c) 2017 Shaun Feakes - All rights reserved
*
* You may use redistribute and/or modify this code under the terms of
* the GNU General Public License version 2 as published by the
* Free Software Foundation. For the terms of this license,
* see <http://www.gnu.org/licenses/>.
*
* You are free to use this software under the terms of the GNU General
* Public License, but WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*
* https://github.com/sfeakes/aqualinkd
*/
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include "aqualink.h"
#include "config.h"
//#include "aq_programmer.h"
#include "utils.h"
//#include "web_server.h"
#include "json_messages.h"
//#include "domoticz.h"
#include "aq_mqtt.h"
#include "SWG_device.h"
#include "GPIO_device.h"
#include "version.h"
//#define test_message "{\"type\": \"status\",\"version\": \"8157 REV MMM\",\"date\": \"09/01/16 THU\",\"time\": \"1:16 PM\",\"temp_units\": \"F\",\"air_temp\": \"96\",\"pool_temp\": \"86\",\"spa_temp\": \" \",\"battery\": \"ok\",\"pool_htr_set_pnt\": \"85\",\"spa_htr_set_pnt\": \"99\",\"freeze_protection\": \"off\",\"frz_protect_set_pnt\": \"0\",\"leds\": {\"pump\": \"on\",\"spa\": \"off\",\"aux1\": \"off\",\"aux2\": \"off\",\"aux3\": \"off\",\"aux4\": \"off\",\"aux5\": \"off\",\"aux6\": \"off\",\"aux7\": \"off\",\"pool_heater\": \"off\",\"spa_heater\": \"off\",\"solar_heater\": \"off\"}}"
//#define test_labels "{\"type\": \"aux_labels\",\"aux1_label\": \"Cleaner\",\"aux2_label\": \"Waterfall\",\"aux3_label\": \"Spa Blower\",\"aux4_label\": \"Pool Light\",\"aux5_label\": \"Spa Light\",\"aux6_label\": \"Unassigned\",\"aux7_label\": \"Unassigned\"}"
//#define test_message "{\"type\": \"status\",\"version\":\"xx\",\"time\":\"xx\",\"air_temp\":\"0\",\"pool_temp\":\"0\",\"spa_temp\":\"0\",\"pool_htr_set_pnt\":\"0\",\"spa_htr_set_pnt\":\"0\",\"frz_protect_set_pnt\":\"0\",\"temp_units\":\"f\",\"battery\":\"ok\",\"leds\":{\"Filter_Pump\": \"on\",\"Spa_Mode\": \"on\",\"Aux_1\": \"on\",\"Aux_2\": \"on\",\"Aux_3\": \"on\",\"Aux_4\": \"on\",\"Aux_5\": \"on\",\"Aux_6\": \"on\",\"Aux_7\": \"on\",\"Pool_Heater\": \"on\",\"Spa_Heater\": \"on\",\"Solar_Heater\": \"on\"}}"
//{"type": "aux_labels","Pool Pump": "Pool Pump","Spa Mode": "Spa Mode","Cleaner": "Aux 1","Waterfall": "Aux 2","Spa Blower": "Aux 2","Pool Light": "Aux 4","Spa Light ": "Aux 5","Aux 6": "Aux 6","Aux 7": "Aux 7","Heater": "Heater","Heater": "Heater","Solar Heater": "Solar Heater","(null)": "(null)"}
const char* SWGstatus2test(unsigned char status)
{
switch (status) {
case SWG_STATUS_OFF:
return "Off";
break;
case SWG_STATUS_OFFLINE:
return "Offline";
break;
case SWG_STATUS_ON:
return "Generating Salt";
break;
case SWG_STATUS_NO_FLOW:
return "No flow";
break;
case SWG_STATUS_LOW_SALT:
return "Low Salt";
break;
case SWG_STATUS_HI_SALT:
return "High Salt";
break;
case SWG_STATUS_CLEAN_CELL:
return "Clean Cell";
break;
case SWG_STATUS_TURNING_OFF:
return "Turning Off";
break;
case SWG_STATUS_HIGH_CURRENT:
return "High Current";
break;
case SWG_STATUS_LOW_VOLTS:
return "Low Volts";
break;
case SWG_STATUS_LOW_TEMP:
return "Low Temp";
break;
case SWG_STATUS_CHECK_PCB:
return "Check PCB";
break;
}
return "Unknown";
}
/*
const char* getStatus(const struct aqualinkdata *aqdata)
{
if (aqdata->active_thread.thread_id != 0) {
return JSON_PROGRAMMING;
}
if (aqdata->display_message != NULL && stristr(aqdata->display_message, "SERVICE") != NULL ) {
return JSON_SERVICE;
}
return JSON_READY;
}
*/
/*
int build_mqtt_status_JSON(char* buffer, int size, int idx, int nvalue, float tvalue)
{
memset(&buffer[0], 0, size);
int length = 0;
if (tvalue == TEMP_UNKNOWN) {
length = sprintf(buffer, "{\"idx\":%d,\"nvalue\":%d,\"svalue\":\"\"}", idx, nvalue);
} else {
length = sprintf(buffer, "{\"idx\":%d,\"nvalue\":%d,\"stype\":\"SetPoint\",\"svalue\":\"%.2f\"}", idx, nvalue, tvalue);
}
buffer[length] = '\0';
return strlen(buffer);
}
int build_mqtt_status_message_JSON(char* buffer, int size, int idx, int nvalue, char *svalue)
{
memset(&buffer[0], 0, size);
int length = 0;
//json.htm?type=command¶m=udevice&idx=IDX&nvalue=LEVEL&svalue=TEXT
length = sprintf(buffer, "{\"idx\":%d,\"nvalue\":%d,\"svalue\":\"%s\"}", idx, nvalue, svalue);
buffer[length] = '\0';
return strlen(buffer);
}
*/
int build_aqualink_error_status_JSON(char* buffer, int size, char *msg)
{
//return snprintf(buffer, size, "{\"type\": \"error\",\"status\":\"%s\"}", msg);
return snprintf(buffer, size, "{\"type\": \"status\",\"status\":\"%s\",\"version\":\"xx\",\"time\":\"xx\",\"air_temp\":\"0\",\"pool_temp\":\"0\",\"spa_temp\":\"0\",\"pool_htr_set_pnt\":\"0\",\"spa_htr_set_pnt\":\"0\",\"frz_protect_set_pnt\":\"0\",\"temp_units\":\"f\",\"battery\":\"ok\",\"leds\":{\"Filter_Pump\": \"off\",\"Spa_Mode\": \"off\",\"Aux_1\": \"off\",\"Aux_2\": \"off\",\"Aux_3\": \"off\",\"Aux_4\": \"off\",\"Aux_5\": \"off\",\"Aux_6\": \"off\",\"Aux_7\": \"off\",\"Pool_Heater\": \"off\",\"Spa_Heater\": \"off\",\"Solar_Heater\": \"off\"}}", msg);
}
bool homekit_fahrenheit(bool homekit)
{
return (homekit && _apconfig_.temp_units==FAHRENHEIT);
}
int _build_gpio_device_JSON(const struct gpiodata *gpiodata, char* buffer, int size, bool homekit)
{
int i;
int length=0;
//bool homekit_f = homekit_fahrenheit(homekit);
char id[20];
for (i=0; i < gpiodata->num_devices; i++ ) {
sprintf(id, "%s%d",GPIO_TOPIC,_gpiodata_.devices[i].pin);
length += sprintf(buffer+length, "{\"type\": \"switch\", \"id\": \"%s\", \"name\": \"%s\", \"state\": \"%s\", \"status\": \"%s\", \"int_status\": \"%d\" },",
id,
_gpiodata_.devices[i].name,
is_gpiodevice_on(&_gpiodata_.devices[i])?JSON_ON:JSON_OFF,
is_gpiodevice_on(&_gpiodata_.devices[i])?JSON_ON:JSON_OFF,
is_gpiodevice_on(&_gpiodata_.devices[i])?MQTT_ON:MQTT_OFF);
}
if (buffer[length-1] == ',') {
buffer[length-1] = ' ';
length--;
}
return length;
}
int _build_swg_device_JSON(const struct apdata *aqdata, char* buffer, int size, bool homekit)
{
int length = 0;
bool homekit_f = homekit_fahrenheit(homekit);
length += sprintf(buffer+length, "{\"type\": \"setpoint_swg\", \"id\": \"%s\", \"setpoint_id\": \"%s\", \"name\": \"%s\", \"state\": \"%s\", \"status\": \"%s\", \"spvalue\": \"%.*f\", \"value\": \"%.*f\", \"extended_status\": \"%d\" },",
SWG_TOPIC,
((homekit_f)?SWG_PERCENT_F_TOPIC:SWG_PERCENT_TOPIC),
"Salt Water Generator",
aqdata->status == SWG_STATUS_ON?JSON_ON:JSON_OFF,
((aqdata->connected && (aqdata->Percent > 0) && (aqdata->status < SWG_STATUS_TURNING_OFF) )?JSON_ON:JSON_OFF),
((homekit)?2:0),
((homekit_f)?degFtoC(aqdata->Percent):aqdata->Percent),
((homekit)?2:0),
((homekit_f)?degFtoC(aqdata->Percent):aqdata->Percent),
aqdata->status);
length += sprintf(buffer+length, "{\"type\": \"value\", \"id\": \"%s\", \"name\": \"%s\", \"state\": \"%s\", \"value\": \"%.*f\" },",
((homekit_f)?SWG_PERCENT_F_TOPIC:SWG_PERCENT_TOPIC),
"Salt Water Generator Percent",
"on",
((homekit_f)?2:0),
((homekit_f)?degFtoC(aqdata->Percent):aqdata->Percent));
if ( aqdata->PPM != TEMP_UNKNOWN ) {
length += sprintf(buffer+length, "{\"type\": \"value\", \"id\": \"%s\", \"name\": \"%s\", \"state\": \"%s\", \"value\": \"%.*f\" },",
((homekit_f)?SWG_PPM_F_TOPIC:SWG_PPM_TOPIC),
"Salt Level PPM",
"on",
((homekit_f)?2:0),((homekit_f)?roundf(degFtoC(aqdata->PPM)):aqdata->PPM));
}
if ( aqdata->Ph != TEMP_UNKNOWN ) {
length += sprintf(buffer+length, "{\"type\": \"value\", \"id\": \"%s\", \"name\": \"%s\", \"state\": \"%s\", \"value\": \"%d\" },",
SWG_PH_TOPIC,
"Ph level",
"on",
aqdata->Ph);
}
if ( aqdata->ORP != TEMP_UNKNOWN ) {
length += sprintf(buffer+length, "{\"type\": \"value\", \"id\": \"%s\", \"name\": \"%s\", \"state\": \"%s\", \"value\": \"%d\" },",
SWG_ORP_TOPIC,
"ORP level",
"on",
aqdata->ORP);
}
length += sprintf(buffer+length, "{\"type\": \"switch\", \"id\": \"%s\", \"name\": \"%s\", \"state\": \"%s\", \"status\": \"%s\" }",
SWG_BOOST_TOPIC,
"Boost pool",
aqdata->boost == true?JSON_ON:JSON_OFF,
aqdata->boost == true?JSON_ON:JSON_OFF);
return length;
}
int build_gpio_device_JSON(const struct gpiodata *gpiodata, char* buffer, int size, bool homekit)
{
int length=0;
//bool homekit_f = homekit_fahrenheit(homekit);
length += sprintf(buffer+length, "{\"type\": \"devices\"");
length += sprintf(buffer+length, ",\"version\":\"%s\"",AQUAPURED_VERSION );
length += sprintf(buffer+length, ",\"name\":\"%s\"",AQUAPURED_NAME );
length += sprintf(buffer+length, ",\"status\":\"%s\"",_apdata_.display_message );
length += sprintf(buffer+length, ", \"devices\": [");
length += _build_gpio_device_JSON(gpiodata, buffer+length, size-length, homekit);
if (buffer[length-1] == ',')
length--;
length += sprintf(buffer+length, "]}");
logMessage(LOG_DEBUG, "build_device_JSON %d of %d", length, size);
buffer[length] = '\0';
return strlen(buffer);
}
int build_swg_device_JSON(const struct apdata *aqdata, char* buffer, int size, bool homekit)
{
int length=0;
//bool homekit_f = homekit_fahrenheit(homekit);
length += sprintf(buffer+length, "{\"type\": \"devices\"");
length += sprintf(buffer+length, ",\"version\":\"%s\"",AQUAPURED_VERSION );
length += sprintf(buffer+length, ",\"name\":\"%s\"",AQUAPURED_NAME );
length += sprintf(buffer+length, ",\"fullstatus\":\"%s\"",SWGstatus2test(aqdata->status));
length += sprintf(buffer+length, ", \"devices\": [");
length += _build_swg_device_JSON(aqdata, buffer+length, size-length, homekit);
if (buffer[length-1] == ',')
length--;
length += sprintf(buffer+length, "]}");
logMessage(LOG_DEBUG, "build_device_JSON %d of %d", length, size);
buffer[length] = '\0';
return strlen(buffer);
}
int build_device_JSON(const struct apdata *aqdata, const struct gpiodata *gpiodata, char* buffer, int size, bool homekit)
{
int length=0;
//bool homekit_f = homekit_fahrenheit(homekit);
length += sprintf(buffer+length, "{\"type\": \"devices\"");
length += sprintf(buffer+length, ",\"version\":\"%s\"",AQUAPURED_VERSION );
length += sprintf(buffer+length, ",\"name\":\"%s\"",AQUAPURED_NAME );
length += sprintf(buffer+length, ",\"fullstatus\":\"%s\"",SWGstatus2test(aqdata->status));
length += sprintf(buffer+length, ", \"devices\": [");
length += _build_swg_device_JSON(aqdata, buffer+length, size-length, homekit);
length += sprintf(buffer+length, ",");
length += _build_gpio_device_JSON(gpiodata, buffer+length, size-length, homekit);
if (buffer[length-1] == ',')
length--;
length += sprintf(buffer+length, "]}");
logMessage(LOG_DEBUG, "build_device_JSON %d of %d", length, size);
buffer[length] = '\0';
return strlen(buffer);
}
//int build_status_JSON(struct aqualinkdata *aqdata, char* buffer, int size)
int build_device_JSON_OLD(const struct apdata *aqdata, char* buffer, int size, bool homekit)
{
memset(&buffer[0], 0, size);
int length = 0;
//int i;
bool homekit_f = homekit_fahrenheit(homekit);
length += sprintf(buffer+length, "{\"type\": \"devices\"");
length += sprintf(buffer+length, ",\"version\":\"%s\"",AQUAPURED_VERSION );
length += sprintf(buffer+length, ",\"name\":\"%s\"",AQUAPURED_NAME );
length += sprintf(buffer+length, ",\"fullstatus\":\"%s\"",SWGstatus2test(aqdata->status));
length += sprintf(buffer+length, ", \"devices\": [");
//if ( aqdata->Percent != TEMP_UNKNOWN ) {
length += sprintf(buffer+length, "{\"type\": \"setpoint_swg\", \"id\": \"%s\", \"setpoint_id\": \"%s\", \"name\": \"%s\", \"state\": \"%s\", \"status\": \"%s\", \"spvalue\": \"%.*f\", \"value\": \"%.*f\", \"extended_status\": \"%d\" },",
SWG_TOPIC,
((homekit_f)?SWG_PERCENT_F_TOPIC:SWG_PERCENT_TOPIC),
"Salt Water Generator",
aqdata->status == SWG_STATUS_ON?JSON_ON:JSON_OFF,
((aqdata->connected && (aqdata->Percent > 0) && (aqdata->status < SWG_STATUS_TURNING_OFF) )?JSON_ON:JSON_OFF),
((homekit)?2:0),
((homekit_f)?degFtoC(aqdata->Percent):aqdata->Percent),
((homekit)?2:0),
((homekit_f)?degFtoC(aqdata->Percent):aqdata->Percent),
aqdata->status);
//length += sprintf(buffer+length, "{\"type\": \"value\", \"id\": \"%s\", \"name\": \"%s\", \"state\": \"%s\", \"value\": \"%d\" },",
length += sprintf(buffer+length, "{\"type\": \"value\", \"id\": \"%s\", \"name\": \"%s\", \"state\": \"%s\", \"value\": \"%.*f\" },",
((homekit_f)?SWG_PERCENT_F_TOPIC:SWG_PERCENT_TOPIC),
"Salt Water Generator Percent",
"on",
((homekit_f)?2:0),
((homekit_f)?degFtoC(aqdata->Percent):aqdata->Percent));
//}
if ( aqdata->PPM != TEMP_UNKNOWN ) {
length += sprintf(buffer+length, "{\"type\": \"value\", \"id\": \"%s\", \"name\": \"%s\", \"state\": \"%s\", \"value\": \"%.*f\" },",
((homekit_f)?SWG_PPM_F_TOPIC:SWG_PPM_TOPIC),
"Salt Level PPM",
"on",
((homekit)?2:0),
((homekit_f)?roundf(degFtoC(aqdata->PPM)):aqdata->PPM));
/*
length += sprintf(buffer+length, "{\"type\": \"value\", \"id\": \"%s\", \"name\": \"%s\", \"state\": \"%s\", \"value\": \"%d\" },",
SWG_PPM_TOPIC,
"Salt Level PPM",
"on",
aqdata->swg_ppm);
*/
}
length += sprintf(buffer+length, "{\"type\": \"switch\", \"id\": \"%s\", \"name\": \"%s\", \"state\": \"%s\", \"status\": \"%s\" },",
SWG_BOOST_TOPIC,
"Salt Water Generator Boost",
aqdata->boost == true?JSON_ON:JSON_OFF,
aqdata->boost == true?JSON_ON:JSON_OFF);
if (buffer[length-1] == ',')
length--;
length += sprintf(buffer+length, "]}");
logMessage(LOG_DEBUG, "build_device_JSON %d of %d", length, size);
buffer[length] = '\0';
return strlen(buffer);
}
int build_dz_mqtt_status_message_JSON(char* buffer, int size, int idx, int nvalue, char *svalue)
{
memset(&buffer[0], 0, size);
int length = 0;
//json.htm?type=command¶m=udevice&idx=IDX&nvalue=LEVEL&svalue=TEXT
length = sprintf(buffer, "{\"idx\":%d,\"nvalue\":%d,\"svalue\":\"%s\"}", idx, nvalue, svalue);
buffer[length] = '\0';
return strlen(buffer);
}
/*
int build_aquapure_status_JSON(struct apdata *aqdata, char* buffer, int size)
{
//strncpy(buffer, test_message, strlen(test_message)+1);
//return strlen(test_message);
//char buffer2[600];
memset(&buffer[0], 0, size);
int length = 0;
int i;
length += sprintf(buffer+length, "{\"type\": \"status\"");
length += sprintf(buffer+length, ",\"version\":\"%s\"",AQUAPURED_VERSION );
length += sprintf(buffer+length, ",\"name\":\"%s\"",AQUAPURED_NAME );
length += sprintf(buffer+length, ",\"fullstatus\":\"%s\"",SWGstatus2test(aqdata->status));
if ( aqdata->Percent != TEMP_UNKNOWN )
length += sprintf(buffer+length, ",\"swg_percent\":\"%d\"",aqdata->Percent );
if ( aqdata->PPM != TEMP_UNKNOWN )
length += sprintf(buffer+length, ",\"swg_ppm\":\"%d\"",aqdata->PPM );
length += sprintf(buffer+length, ",\"leds\":{\"%s\": \"%s\"}", SWG_TOPIC, aqdata->status == 0x00?JSON_ON:JSON_OFF);
length += sprintf(buffer+length, "}" );
buffer[length] = '\0';
return strlen(buffer);
}
*/
/*
int build_aux_labels_JSON(struct aqualinkdata *aqdata, char* buffer, int size)
{
memset(&buffer[0], 0, size);
int length = 0;
int i;
length += sprintf(buffer+length, "{\"type\": \"aux_labels\"");
for (i=0; i < TOTAL_BUTTONS; i++)
{
length += sprintf(buffer+length, ",\"%s\": \"%s\"", aqdata->aqbuttons[i].name, aqdata->aqbuttons[i].label);
}
length += sprintf(buffer+length, "}");
return length;
//printf("%s\n",buffer);
//return strlen(buffer);
}
// WS Received '{"parameter":"SPA_HTR","value":99}'
// WS Received '{"command":"KEY_HTR_POOL"}'
// WS Received '{"command":"GET_AUX_LABELS"}'
bool parseJSONwebrequest(char *buffer, struct JSONwebrequest *request)
{
int i=0;
int found=0;
bool reading = false;
request->first.key = NULL;
request->first.value = NULL;
request->second.key = NULL;
request->second.value = NULL;
int length = strlen(buffer);
while ( i < length )
{
//printf ("Reading %c",buffer[i]);
switch (buffer[i]) {
case '{':
case '"':
case '}':
case ':':
case ',':
case ' ':
// Ignore space , : if reading a string
if (reading == true && buffer[i] != ' ' && buffer[i] != ',' && buffer[i] != ':'){
//printf (" <- END");
reading = false;
buffer[i] = '\0';
found++;
}
break;
default:
if (reading == false) {
//printf (" <- START");
reading = true;
switch(found) {
case 0:
request->first.key = &buffer[i];
break;
case 1:
request->first.value = &buffer[i];
break;
case 2:
request->second.key = &buffer[i];
break;
case 3:
request->second.value = &buffer[i];
break;
}
}
break;
}
//printf ("\n");
if (found >= 4)
break;
i++;
}
return true;
}
*/
bool parseJSONwebrequest(char *buffer, struct JSONwebrequest *request)
{
int i=0;
int found=0;
bool reading = false;
request->first.key = NULL;
request->first.value = NULL;
request->second.key = NULL;
request->second.value = NULL;
request->third.key = NULL;
request->third.value = NULL;
int length = strlen(buffer);
while ( i < length )
{
//printf ("Reading %c",buffer[i]);
switch (buffer[i]) {
case '{':
case '"':
case '}':
case ':':
case ',':
case ' ':
// Ignore space , : if reading a string
if (reading == true && buffer[i] != ' ' && buffer[i] != ',' && buffer[i] != ':'){
//printf (" <- END");
reading = false;
buffer[i] = '\0';
found++;
}
break;
default:
if (reading == false) {
//printf (" <- START");
reading = true;
switch(found) {
case 0:
request->first.key = &buffer[i];
break;
case 1:
request->first.value = &buffer[i];
break;
case 2:
request->second.key = &buffer[i];
break;
case 3:
request->second.value = &buffer[i];
break;
case 4:
request->third.key = &buffer[i];
break;
case 5:
request->third.value = &buffer[i];
break;
}
}
break;
}
//printf ("\n");
// if (found >= 4)
if (found >= 6)
break;
i++;
}
return true;
}
char *jsontok(jsontoken *jt) {
//int i = jt->position;
int j;
jt->key_ptr = NULL;
jt->val_ptr = NULL;
jt->key_len = 0;
jt->val_len = 0;
bool subset = 0;
bool inquotes = false;
while(jt->json[jt->pos] == ' ' || jt->json[jt->pos] == '{' || jt->json[jt->pos] == '"' || jt->json[jt->pos] <= 31) {
if(jt->json[jt->pos] == '"')
inquotes = !inquotes;
jt->pos++;
}
jt->key_ptr = (char *)&jt->json[jt->pos];
while (jt->pos < jt->json_len ) {
switch (jt->json[jt->pos]){
case '{':
case '[':
subset++;
break;
case '}':
case ']':
subset--;
break;
case '"':
inquotes = !inquotes;
break;
case ':':
// force move to value and error if no key
if (subset==0 && !inquotes && jt->key_ptr != NULL && jt->key_len == 0) {
j=jt->pos-1;
while(jt->json[j] == ' ' || jt->json[j] == '"' || jt->json[j] <= 31){j--;}
jt->key_len = &jt->json[j+1] - jt->key_ptr;
j=jt->pos+1;
while(jt->json[j] == ' ' || jt->json[j] == '"' || jt->json[j] <= 31) {j++;}
jt->val_ptr = (char *)&jt->json[j];
} else if (subset==0 && !inquotes) {
printf("Don't know what to do, received ':' out of sequance\n");
}
break;
case ',':
// We need to end if value is started, error if not
if (subset==0 && !inquotes && jt->key_len != 0 && jt->val_len == 0 && jt->val_ptr != NULL) {
j=jt->pos-1;
while(jt->json[j] == ' ' || jt->json[j] == '"' || jt->json[j] <= 31){j--;}
jt->val_len = &jt->json[j+1] - jt->val_ptr;
jt->pos++;
// Handle blank value
if (jt->val_len < 0) jt->val_len=0;
return (char *)&jt->json[jt->pos+1];
} else if (subset==0 && !inquotes) {
printf("Don't know what to do, received ','\n");
}
break;
}
jt->pos++;
}
if (jt->val_ptr != NULL) {
j=jt->pos-1;
while(jt->json[j] == '}' || jt->json[j] == ' ' || jt->json[j] == '"' || jt->json[j] <= 31){j--;}
jt->val_len = &jt->json[j+1] - jt->val_ptr;
// Handle blank value
if (jt->val_len < 0) jt->val_len=0;
return (char *)&jt->json[jt->pos+1];
}
return NULL;
}