Rettet til så det passer overens med de data som API'et forventer at modtage
This commit is contained in:
@@ -6,21 +6,25 @@ class FilamentGuardApiClient
|
||||
{
|
||||
public:
|
||||
bool sendTagRead(
|
||||
const String& json) const;
|
||||
const String& json,
|
||||
int& responseCode,
|
||||
String& responseBody) const;
|
||||
|
||||
bool sendTagEvent(
|
||||
const String& json) const;
|
||||
const String& json,
|
||||
int& responseCode,
|
||||
String& responseBody) const;
|
||||
|
||||
bool sendLogEntry(
|
||||
const String& json) const;
|
||||
|
||||
private:
|
||||
static constexpr const char* TagReadEndpointUrl =
|
||||
"http://filamentguard.maximuss.dk/";
|
||||
"http://192.168.1.6:5011/api/Tag/detected";
|
||||
|
||||
static constexpr const char* TagEventEndpointUrl =
|
||||
"http://filamentguard.maximuss.dk/";
|
||||
"http://192.168.1.6:5011/api/Tag/removed";
|
||||
|
||||
static constexpr const char* LogEndpointUrl =
|
||||
"http://filamentguard.maximuss.dk/";
|
||||
};
|
||||
"http://192.168.1.6:5011/api/Log/create";
|
||||
};
|
||||
|
||||
@@ -19,6 +19,7 @@ struct FilamentGuardLogEntry
|
||||
String category;
|
||||
String message;
|
||||
String data;
|
||||
String tagUid;
|
||||
|
||||
unsigned long uptimeMilliseconds = 0;
|
||||
};
|
||||
};
|
||||
|
||||
@@ -16,6 +16,9 @@ public:
|
||||
void setDeviceInfo(
|
||||
const FilamentGuardDeviceInfo& deviceInfo);
|
||||
|
||||
void setTagUid(
|
||||
const String& tagUid);
|
||||
|
||||
void info(
|
||||
const String& category,
|
||||
const String& message,
|
||||
@@ -37,6 +40,7 @@ private:
|
||||
size_t queueCount = 0;
|
||||
|
||||
String firmwareVersion;
|
||||
String tagUid;
|
||||
FilamentGuardDeviceInfo deviceInfo;
|
||||
|
||||
bool hasDeviceInfo = false;
|
||||
@@ -60,4 +64,4 @@ private:
|
||||
FilamentGuardLogEntry& logEntry) const;
|
||||
|
||||
void dequeue();
|
||||
};
|
||||
};
|
||||
|
||||
@@ -86,4 +86,4 @@ private:
|
||||
|
||||
void sendTagRead(
|
||||
const FilamentGuardTagRead& tagRead) const;
|
||||
};
|
||||
};
|
||||
|
||||
@@ -5,11 +5,26 @@
|
||||
|
||||
namespace
|
||||
{
|
||||
constexpr uint16_t HttpTimeoutMilliseconds =
|
||||
15000;
|
||||
|
||||
bool postJson(
|
||||
const char* endpointUrl,
|
||||
const String& json,
|
||||
const char* description)
|
||||
const char* description,
|
||||
int* returnedResponseCode = nullptr,
|
||||
String* returnedResponseBody = nullptr)
|
||||
{
|
||||
if (returnedResponseCode != nullptr)
|
||||
{
|
||||
*returnedResponseCode = 0;
|
||||
}
|
||||
|
||||
if (returnedResponseBody != nullptr)
|
||||
{
|
||||
returnedResponseBody->clear();
|
||||
}
|
||||
|
||||
if (WiFi.status() != WL_CONNECTED)
|
||||
{
|
||||
Serial.print(description);
|
||||
@@ -43,18 +58,33 @@ namespace
|
||||
"Content-Type",
|
||||
"application/json");
|
||||
|
||||
httpClient.setTimeout(
|
||||
HttpTimeoutMilliseconds);
|
||||
|
||||
const int responseCode =
|
||||
httpClient.POST(json);
|
||||
|
||||
if (returnedResponseCode != nullptr)
|
||||
{
|
||||
*returnedResponseCode = responseCode;
|
||||
}
|
||||
|
||||
if (responseCode <= 0)
|
||||
{
|
||||
const String errorMessage =
|
||||
httpClient.errorToString(
|
||||
responseCode);
|
||||
|
||||
if (returnedResponseBody != nullptr)
|
||||
{
|
||||
*returnedResponseBody = errorMessage;
|
||||
}
|
||||
|
||||
Serial.print(description);
|
||||
Serial.print(
|
||||
" fejlede. HTTPClient-fejl: ");
|
||||
|
||||
Serial.println(
|
||||
httpClient.errorToString(
|
||||
responseCode));
|
||||
Serial.println(errorMessage);
|
||||
|
||||
httpClient.end();
|
||||
|
||||
@@ -64,6 +94,11 @@ namespace
|
||||
const String responseBody =
|
||||
httpClient.getString();
|
||||
|
||||
if (returnedResponseBody != nullptr)
|
||||
{
|
||||
*returnedResponseBody = responseBody;
|
||||
}
|
||||
|
||||
const bool successful =
|
||||
responseCode >= 200 &&
|
||||
responseCode < 300;
|
||||
@@ -93,21 +128,29 @@ namespace
|
||||
}
|
||||
|
||||
bool FilamentGuardApiClient::sendTagRead(
|
||||
const String& json) const
|
||||
const String& json,
|
||||
int& responseCode,
|
||||
String& responseBody) const
|
||||
{
|
||||
return postJson(
|
||||
TagReadEndpointUrl,
|
||||
json,
|
||||
"tagregistrering");
|
||||
"tagregistrering",
|
||||
&responseCode,
|
||||
&responseBody);
|
||||
}
|
||||
|
||||
bool FilamentGuardApiClient::sendTagEvent(
|
||||
const String& json) const
|
||||
const String& json,
|
||||
int& responseCode,
|
||||
String& responseBody) const
|
||||
{
|
||||
return postJson(
|
||||
TagEventEndpointUrl,
|
||||
json,
|
||||
"taghændelse");
|
||||
"tagfjernelse",
|
||||
&responseCode,
|
||||
&responseBody);
|
||||
}
|
||||
|
||||
bool FilamentGuardApiClient::sendLogEntry(
|
||||
@@ -117,4 +160,4 @@ bool FilamentGuardApiClient::sendLogEntry(
|
||||
LogEndpointUrl,
|
||||
json,
|
||||
"log");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -29,6 +29,9 @@ String FilamentGuardLogEntrySerializer::serialize(
|
||||
document["data"] =
|
||||
logEntry.data;
|
||||
|
||||
document["tagUid"] =
|
||||
logEntry.tagUid;
|
||||
|
||||
document["uptimeMilliseconds"] =
|
||||
logEntry.uptimeMilliseconds;
|
||||
|
||||
@@ -55,4 +58,4 @@ const char* FilamentGuardLogEntrySerializer::logLevelToString(
|
||||
default:
|
||||
return "Info";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -39,6 +39,13 @@ void FilamentGuardLogger::setDeviceInfo(
|
||||
hasDeviceInfo = true;
|
||||
}
|
||||
|
||||
void FilamentGuardLogger::setTagUid(
|
||||
const String& newTagUid)
|
||||
{
|
||||
tagUid =
|
||||
newTagUid;
|
||||
}
|
||||
|
||||
void FilamentGuardLogger::info(
|
||||
const String& category,
|
||||
const String& message,
|
||||
@@ -149,6 +156,9 @@ void FilamentGuardLogger::log(
|
||||
logEntry.data =
|
||||
data;
|
||||
|
||||
logEntry.tagUid =
|
||||
tagUid;
|
||||
|
||||
logEntry.uptimeMilliseconds =
|
||||
millis();
|
||||
|
||||
@@ -242,4 +252,4 @@ void FilamentGuardLogger::dequeue()
|
||||
QueueSize;
|
||||
|
||||
--queueCount;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -290,12 +290,15 @@ void Pn5180Reader::handleTagMissing()
|
||||
activeProtocol ==
|
||||
TagProtocol::Iso15693;
|
||||
|
||||
const String tagData =
|
||||
String("{\"tagUid\":\"") +
|
||||
const String tagUid =
|
||||
formatUid(
|
||||
lastUid,
|
||||
lastUidLength,
|
||||
reverseOrder) +
|
||||
reverseOrder);
|
||||
|
||||
const String tagData =
|
||||
String("{\"tagUid\":\"") +
|
||||
tagUid +
|
||||
"\"}";
|
||||
|
||||
logger.info(
|
||||
@@ -303,6 +306,45 @@ void Pn5180Reader::handleTagMissing()
|
||||
"NFC-tag fjernet.",
|
||||
tagData);
|
||||
|
||||
const String eventJson =
|
||||
String("{\"device\":{") +
|
||||
"\"deviceId\":\"" +
|
||||
deviceInfo.deviceId +
|
||||
"\",\"hostname\":\"" +
|
||||
deviceInfo.hostname +
|
||||
"\"},\"tag\":{" +
|
||||
"\"tagUid\":\"" +
|
||||
tagUid +
|
||||
"\"}}";
|
||||
|
||||
int responseCode = 0;
|
||||
String responseBody;
|
||||
|
||||
const bool successful =
|
||||
apiClient.sendTagEvent(
|
||||
eventJson,
|
||||
responseCode,
|
||||
responseBody);
|
||||
|
||||
const String responseMessage =
|
||||
String("Tag removed API-svar. HTTP-status: ") +
|
||||
responseCode;
|
||||
|
||||
if (successful)
|
||||
{
|
||||
logger.info(
|
||||
"Api",
|
||||
responseMessage,
|
||||
responseBody);
|
||||
}
|
||||
else
|
||||
{
|
||||
logger.error(
|
||||
"Api",
|
||||
responseMessage,
|
||||
responseBody);
|
||||
}
|
||||
|
||||
clearLastUid();
|
||||
}
|
||||
|
||||
@@ -343,6 +385,13 @@ void Pn5180Reader::rememberUid(
|
||||
activeProtocol =
|
||||
protocol;
|
||||
|
||||
logger.setTagUid(
|
||||
formatUid(
|
||||
uid,
|
||||
uidLength,
|
||||
protocol ==
|
||||
TagProtocol::Iso15693));
|
||||
|
||||
missingScanCount = 0;
|
||||
}
|
||||
|
||||
@@ -357,6 +406,9 @@ void Pn5180Reader::clearLastUid()
|
||||
activeProtocol =
|
||||
TagProtocol::None;
|
||||
|
||||
logger.setTagUid(
|
||||
String());
|
||||
|
||||
missingScanCount = 0;
|
||||
}
|
||||
|
||||
@@ -543,6 +595,31 @@ void Pn5180Reader::sendTagRead(
|
||||
tagReadSerializer.serialize(
|
||||
tagRead);
|
||||
|
||||
apiClient.sendTagRead(
|
||||
json);
|
||||
}
|
||||
int responseCode = 0;
|
||||
String responseBody;
|
||||
|
||||
const bool successful =
|
||||
apiClient.sendTagRead(
|
||||
json,
|
||||
responseCode,
|
||||
responseBody);
|
||||
|
||||
const String message =
|
||||
String("Tag detected API-svar. HTTP-status: ") +
|
||||
responseCode;
|
||||
|
||||
if (successful)
|
||||
{
|
||||
logger.info(
|
||||
"Api",
|
||||
message,
|
||||
responseBody);
|
||||
}
|
||||
else
|
||||
{
|
||||
logger.error(
|
||||
"Api",
|
||||
message,
|
||||
responseBody);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user