-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathHttpBuyTransaction.cpp
More file actions
74 lines (57 loc) · 1.85 KB
/
Copy pathHttpBuyTransaction.cpp
File metadata and controls
74 lines (57 loc) · 1.85 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
/*
* "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 "HttpBuyTransaction.h"
bool HttpBuyTransaction::send(char* badge, int product, unsigned long time)
{
char productString[2];
char timeString[11];
snprintf(productString, sizeof(productString), "%d", product);
snprintf(timeString, sizeof(timeString), "%lu", time);
HashBuilder hashBuilder;
hashBuilder.print(badge);
hashBuilder.print(productString);
hashBuilder.print(timeString);
StaticJsonBuffer<JSON_OBJECT_SIZE(4)> jsonBuffer;
JsonObject& json = jsonBuffer.createObject();
json["Badge"] = badge;
json["Hash"] = hashBuilder.getHash();
json["Product"] = productString;
json["Time"] = timeString;
json.printTo(buffer, sizeof(buffer));
return http.query("POST " API_PATH "/buy", buffer, sizeof(buffer));
}
bool HttpBuyTransaction::parse()
{
StaticJsonBuffer<JSON_OBJECT_SIZE(4)+JSON_ARRAY_SIZE(2)> jsonBuffer;
JsonObject& root = jsonBuffer.parseObject(buffer);
if (!root.success()) return false;
melody = root["Melody"];
if (melody == NULL) return false;
JsonArray& messageArray = root["Message"];
if (!messageArray.success()) return false;
messages[0] = messageArray[0];
messages[1] = messageArray[1];
time = root["Time"];
if (time == NULL) return false;
hash = root["Hash"];
if (hash == NULL) return false;
return true;
}
bool HttpBuyTransaction::validate()
{
HashBuilder hashBuilder;
hashBuilder.print(melody);
hashBuilder.print(messages[0]);
hashBuilder.print(messages[1]);
hashBuilder.print(time);
return strcasecmp(hash, hashBuilder.getHash()) == 0;
}