-
Notifications
You must be signed in to change notification settings - Fork 0
/
HttpBuyTransaction.cpp
152 lines (118 loc) · 4.57 KB
/
HttpBuyTransaction.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
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
/*
* "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[6];
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(6)+12> jsonBuffer;
JsonObject& json = jsonBuffer.createObject();
json["Tid"] = "3"; //////////---------------------------------------> Change HERE
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::sendForBalance(char* badge, unsigned long time)
{
char timeString[11];
snprintf(timeString, sizeof(timeString), "%lu", time);
HashBuilder hashBuilder;
hashBuilder.print(badge);
hashBuilder.print(timeString);
StaticJsonBuffer<JSON_OBJECT_SIZE(6)+12> jsonBuffer;
JsonObject& json = jsonBuffer.createObject();
json["Tid"] = "3"; //////////---------------------------------------> Change HERE
json["Badge"] = badge;
json["Hash"] = hashBuilder.getHash();
json["Time"] = timeString;
json.printTo(buffer, sizeof(buffer));
return http.query("POST " API_PATH "/balance", buffer, sizeof(buffer));
}
bool HttpBuyTransaction::sendForLocker(char* badge, unsigned long time)
{
char timeString[11];
snprintf(timeString, sizeof(timeString), "%lu", time);
HashBuilder hashBuilder;
hashBuilder.print(badge);
hashBuilder.print(timeString);
StaticJsonBuffer<JSON_OBJECT_SIZE(6)+12> jsonBuffer;
JsonObject& json = jsonBuffer.createObject();
json["Tid"] = "3"; //////////---------------------------------------> Change HERE
json["Badge"] = badge;
json["Hash"] = hashBuilder.getHash();
json["Time"] = timeString;
json.printTo(buffer, sizeof(buffer));
return http.query("POST " API_PATH "/locker", buffer, sizeof(buffer));
}
bool HttpBuyTransaction::sendForUser(char* badge, unsigned long time)
{
char timeString[11];
snprintf(timeString, sizeof(timeString), "%lu", time);
HashBuilder hashBuilder;
hashBuilder.print(badge);
hashBuilder.print(timeString);
StaticJsonBuffer<JSON_OBJECT_SIZE(4)> jsonBuffer;
JsonObject& json = jsonBuffer.createObject();
json["Tid"] = "3"; //////////---------------------------------------> Change HERE
json["Badge"] = badge;
json["Hash"] = hashBuilder.getHash();
json["Time"] = timeString;
json.printTo(buffer, sizeof(buffer));
return http.query("POST " API_PATH "/user", buffer, sizeof(buffer));
}
bool HttpBuyTransaction::parse()
{
StaticJsonBuffer<JSON_OBJECT_SIZE(4)+JSON_ARRAY_SIZE(20)> jsonBuffer;
JsonObject& root = jsonBuffer.parseObject(buffer);
if (!root.success()) {Serial.println("JSON error"); error = "JSON error"; return false;}
melody = root["Melody"];
if (melody == NULL) {Serial.println("No melody sent"); error = "No melody sent"; return false;}
JsonArray& messageArray = root["Message"];
if (!messageArray.success()) return false;
//messages[0] = messageArray[0];
//messages[1] = messageArray[1];
messages.clear();
for (int i = 0; i < messageArray.size(); i++)
messages.emplace_back(messageArray.get<char*>(i));
time = root["Time"];
if (time == NULL) {Serial.println("No time sent"); error = "No time sent"; return false;}
hash = root["Hash"];
if (hash == NULL) {Serial.println("No hash sent"); error = "No hash sent"; return false;}
return true;
}
bool HttpBuyTransaction::validate()
{
HashBuilder hashBuilder;
hashBuilder.print(melody);
for (auto & m : messages)
hashBuilder.print(m.c_str());
hashBuilder.print(time);
if(strcasecmp(hash, hashBuilder.getHash()) == 0)
return true;
else
{
Serial.print("Hash incorrect received: ");
Serial.print(hash); Serial.print(" expecting "); Serial.println(hashBuilder.getHash());
Serial.print("Time: "); Serial.println(time);
error = "Hash incorrect";
return false;
}
}