-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathHttpSyncTransaction.cpp
More file actions
78 lines (58 loc) · 1.61 KB
/
Copy pathHttpSyncTransaction.cpp
File metadata and controls
78 lines (58 loc) · 1.61 KB
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
/*
* "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, sizeof(buffer));
}
bool HttpSyncTransaction::parse()
{
StaticJsonBuffer<JSON_OBJECT_SIZE(4)+JSON_ARRAY_SIZE(Catalog::MAX_PRODUCT_COUNT)> jsonBuffer;
JsonObject& root = jsonBuffer.parseObject(buffer);
if (!root.success()) return false;
header = root["Header"];
if (header == NULL) return false;
JsonArray& productsArray = root["Products"];
if (!productsArray.success()) return false;
int count = productsArray.size();
for (int i = 0; i < count; i++)
{
products[i] = productsArray[i];
}
products[count] = NULL;
time = root["Time"];
if (time == NULL) return false;
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]);
hashBuilder.print(time);
return strcasecmp(hash, hashBuilder.getHash()) == 0;
}
void HttpSyncTransaction::getCatalog(Catalog& catalog)
{
catalog.setHeader(header);
int i;
for (i = 0; i < 4; i++)
{
if (products[i] == NULL) break;
catalog.setProduct(i, products[i]);
}
catalog.setProductCount(i);
}