-
Notifications
You must be signed in to change notification settings - Fork 0
/
HttpSyncTransaction.cpp
109 lines (81 loc) · 2.57 KB
/
HttpSyncTransaction.cpp
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
/*
* "Drinks" RFID Terminal
* Buy sodas with your company badge!
*
* Benoit Blanchon 2014 - MIT License
* https://github.com/bblanchon/DrinksRfidTerminal
*/
#include <Arduino.h>
#include <ArduinoJson.h>
#include "Configuration.h"
#include "HashBuilder.h"
#include "HttpSyncTransaction.h"
bool HttpSyncTransaction::send()
{
buffer[0] = 0;
return http.query("GET " API_PATH "/sync", buffer, buffer_size-1);
}
bool HttpSyncTransaction::parse()
{
//StaticJsonBuffer<JSON_OBJECT_SIZE(10)+JSON_ARRAY_SIZE(Catalog::MAX_PRODUCT_COUNT)> jsonBuffer;
StaticJsonBuffer<512> jsonBuffer; //buffer size is too small if we have more items...512 seems to be enough for now
Serial.println("Sync parse: root.success");
JsonObject& root = jsonBuffer.parseObject(buffer);
if (!root.success()) return false;
Serial.println("Sync parse: Header");
header = root["Header"];
if (header == NULL) return false;
Serial.println("Sync parse: DBID");
JsonArray& dbIDArray = root["DBID"];
if (!dbIDArray.success()) return false;
Serial.println("Sync parse: Products");
JsonArray& productsArray = root["Products"];
if (!productsArray.success()) return false;
Serial.println("Sync parse: For loop");
int count = productsArray.size();
for (int i = 0; i < count; i++)
{
products[i] = productsArray[i];
dbID[i] = dbIDArray[i];
}
products[count] = NULL;
Serial.print("Products count: "); Serial.println(productsArray.size());
Serial.println("Sync parse: Time");
time = root["Time"];
if (time == NULL) return false;
Serial.println("Sync parse: Hash");
hash = root["Hash"];
if (hash == NULL) return false;
return true;
}
bool HttpSyncTransaction::validate()
{
HashBuilder hashBuilder;
hashBuilder.print(header);
for (int i = 0; products[i] != NULL; i++)
hashBuilder.print(products[i]);
//for (int i = 0; dbID[i] != NULL; i++)
//hashBuilder.print(dbID[i]);
hashBuilder.print(time);
const char* hash2 = hashBuilder.getHash();
Serial.println("");
Serial.print("'");
Serial.print(hash);
Serial.print("' == '");
Serial.print(hash2);
Serial.print("' -> ");
Serial.println(!strcmp(hash, hash2));
return !strcmp(hash, hash2);
}
void HttpSyncTransaction::getCatalog(Catalog& catalog)
{
catalog.setHeader(header);
int i;
for (i = 0; i < MAX_PRODUCTS; i++)
{
if (products[i] == NULL) break;
catalog.setProduct(i, products[i]);
catalog.setProductDBID(i, dbID[i]);
}
catalog.setProductCount(i);
}