-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathalerter.cpp
More file actions
87 lines (81 loc) · 2.8 KB
/
alerter.cpp
File metadata and controls
87 lines (81 loc) · 2.8 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
79
80
81
82
83
84
85
86
87
#include <nlohmann/json.hpp>
#include <fstream>
#include <iostream>
#include "alerter.h"
#include "alert.h"
#include "manager.h"
using json = nlohmann::json;
using namespace std;
// create buffer from alert object
vector<uint8_t> Alerter::makeAlertBuffer(int type, float distance,
float relativeVelocity)
{
Alert alert(false, 1, type, distance, relativeVelocity);
vector<uint8_t> serialized = alert.serialize();
return serialized;
}
void Alerter::destroyAlertBuffer(char *buffer)
{
delete[] buffer;
}
// create alerts buffer to send
vector<vector<uint8_t>> Alerter::sendAlerts(
const vector<ObjectInformation> &output)
{
vector<vector<uint8_t>> alerts;
for (const ObjectInformation &objectInformation : output) {
if (isSendAlert(objectInformation)) {
vector<uint8_t> alertBuffer = makeAlertBuffer(
objectInformation.type, objectInformation.distance,
objectInformation.velocity.value());
alerts.push_back(alertBuffer);
}
}
return alerts;
}
// Check whether to send alert
bool Alerter::isSendAlert(const ObjectInformation &objectInformation)
{
return objectInformation.distance < MIN_LEGAL_DISTANCE;
}
// create json file form the alert buffer
void Alerter::makeFileJSON()
{
json j;
j["endianness"] = "little";
j["fields"] = json::array();
// field AlertDetails
json alertDetailsJson;
alertDetailsJson["name"] = "AlertDetails";
alertDetailsJson["size"] = 8;
alertDetailsJson["type"] = "bit_field";
alertDetailsJson["fields"] = json::array(
{{{"name", "MessageType"}, {"size", 1}, {"type", "unsigned_int"}},
{{"name", "Level"}, {"size", 3}, {"type", "unsigned_int"}},
{{"name", "ObjectType"}, {"size", 4}, {"type", "unsigned_int"}}});
// add AlertDetails to JSON
j["fields"].push_back(alertDetailsJson);
// add ObjectDistance
json objectDistanceJson;
objectDistanceJson["name"] = "ObjectDistance";
objectDistanceJson["size"] = 32;
objectDistanceJson["type"] = "float_fixed";
j["fields"].push_back(objectDistanceJson);
// add relativeVelocity
json relativeVelocityJson;
relativeVelocityJson["name"] = "relativeVelocity";
relativeVelocityJson["size"] = 32;
relativeVelocityJson["type"] = "float_fixed";
j["fields"].push_back(relativeVelocityJson);
// Write the JSON to the file
ofstream output_file("../alert.json");
if (output_file.is_open()) {
// 4 = Indent level for easier reading
output_file << j.dump(4);
output_file.close();
}
else {
LogManager::logErrorMessage(ErrorType::FILE_ERROR,
"open file for writing");
}
}