blob: 1ccd3e774d8965f58ffcbb501d7f53fc3af9c68a [file] [log] [blame]
#include "tlbmc/redfish/routes/all_sensors.h"
#include <functional>
#include <memory>
#include <string>
#include <vector>
#include "absl/functional/bind_front.h"
#include "absl/time/time.h"
#include "nlohmann/json.hpp"
#include "tlbmc/redfish/app.h"
#include "tlbmc/redfish/request.h"
#include "tlbmc/redfish/response.h"
#include "tlbmc/redfish/routes/sensor.h"
#include "resource.pb.h"
#include "sensor.pb.h"
#include "tlbmc/sensors/sensor.h"
#include "tlbmc/store/store.h"
#include "tlbmc/time/time.h"
namespace milotic_tlbmc::all_sensors {
namespace {
using ::milotic_tlbmc::sensor::FillResponseWithSensorData;
void HandleAllSensors(const RedfishApp& app, const RedfishRequest& req,
RedfishResponse& resp) {
resp.SetKeyInJsonBody("/@odata.id", "/redfish/tlbmc/AllSensors");
resp.SetKeyInJsonBody("/@odata.type", "#TlBMCAllSensors.TlBMCAllSensors");
resp.SetKeyInJsonBody("/Name", "AllSensors");
resp.SetKeyInJsonBody("/Description", "All Sensors on the machine");
const Store& store = *app.GetStore();
std::vector<std::shared_ptr<const Sensor>> sensors = store.GetAllSensors();
nlohmann::json::array_t members;
for (const auto& sensor : sensors) {
nlohmann::json::object_t sensor_json;
members.push_back(sensor_json);
nlohmann::json::json_pointer sensor_pointer("/Members");
sensor_pointer = sensor_pointer / (members.size() - 1);
FillResponseWithSensorData(sensor, sensor_pointer, resp);
}
resp.SetKeyInJsonBody("/Members@odata.count", members.size());
}
void HandleAllSensorsWithHistory(const RedfishApp& app,
const RedfishRequest& req,
RedfishResponse& resp) {
resp.SetKeyInJsonBody("/@odata.id", "/redfish/tlbmc/AllSensorsHistory");
resp.SetKeyInJsonBody("/@odata.type", "#TlBMCAllSensors.TlBMCAllSensors");
resp.SetKeyInJsonBody("/Name", "AllSensorsHistory");
resp.SetKeyInJsonBody(
"/Description",
"All Sensors on the machine and their historical readings.");
const Store& store = *app.GetStore();
std::vector<std::shared_ptr<const Sensor>> sensors = store.GetAllSensors();
nlohmann::json::array_t members;
for (const auto& sensor : sensors) {
nlohmann::json::object_t sensor_json;
members.push_back(sensor_json);
nlohmann::json::json_pointer sensor_pointer("/Members");
sensor_pointer = sensor_pointer / (members.size() - 1);
FillResponseWithSensorData(sensor, sensor_pointer, resp);
nlohmann::json::array_t history;
for (const std::shared_ptr<const SensorValue>& value :
sensor->GetSensorDataHistory()) {
nlohmann::json::object_t data_point;
data_point["Reading"] = value->reading();
data_point["Timestamp"] =
absl::FormatTime(DecodeGoogleApiProto(value->timestamp()));
history.push_back(data_point);
}
resp.SetKeyInJsonBody(sensor_pointer / "Oem" / "History", history);
}
resp.SetKeyInJsonBody("/Members@odata.count", members.size());
}
} // namespace
void RegisterRoutes(RedfishApp& app) {
TLBMC_ROUTE(app, "/redfish/tlbmc/AllSensors/")
.methods(boost::beast::http::verb::get)(
absl::bind_front(HandleAllSensors, std::cref(app)));
TLBMC_ROUTE(app, "/redfish/tlbmc/AllSensorsHistory/")
.methods(boost::beast::http::verb::get)(
absl::bind_front(HandleAllSensorsWithHistory, std::cref(app)));
}
} // namespace milotic_tlbmc::all_sensors