| #include "tlbmc/redfish/routes/high_frequency_sensors.h" |
| |
| #include <functional> |
| #include <memory> |
| #include <string> |
| #include <vector> |
| |
| #include "absl/functional/bind_front.h" |
| #include "absl/strings/escaping.h" |
| #include "absl/time/time.h" |
| #include "tlbmc/redfish/app.h" |
| #include "tlbmc/redfish/request.h" |
| #include "tlbmc/redfish/response.h" |
| #include "hft_sensor.pb.h" |
| #include "tlbmc/sensors/sensor.h" |
| #include "tlbmc/time/time.h" |
| |
| namespace milotic_tlbmc::hf_sensors { |
| |
| namespace { |
| |
| // TODO(nanzhou): Add support for sensor type, units, and devpaths. |
| SensorDescriptor GetSensorDescriptorFromSensor(const Sensor& sensor) { |
| SensorDescriptor descriptor; |
| descriptor.mutable_id()->set_name(sensor.GetKey()); |
| descriptor.set_source(SENSOR_SOURCE_BMC); |
| return descriptor; |
| } |
| |
| // An optimization can be made here so that every 10s we update the binary proto |
| // once for all clients, instead of doing it for each client on demand. |
| void HandleSensor(const RedfishApp& app, const RedfishRequest& req, |
| RedfishResponse& resp) { |
| resp.SetKeyInJsonBody("/@odata.id", |
| "/redfish/v1/Oem/Google/HighFrequencySensors"); |
| resp.SetKeyInJsonBody("/@odata.type", "#Oem.Google.HighFrequencySensors"); |
| resp.SetKeyInJsonBody("/Name", "High Frequency Sensor Readings"); |
| resp.SetKeyInJsonBody("/Description", |
| "Provides access to high-frequency sensor readings"); |
| std::vector<std::shared_ptr<const Sensor>> all_sensors = |
| app.GetStore()->GetAllSensors(); |
| HighFrequencySensorsReadingsBatch batch; |
| for (const auto& sensor : all_sensors) { |
| HighFrequencySensorsReadings* readings = batch.add_readings(); |
| *readings->mutable_sensor_descriptor() = |
| GetSensorDescriptorFromSensor(*sensor); |
| for (const auto& sensor_data : sensor->GetSensorDataHistory()) { |
| HighFrequencySensorReading* reading = |
| readings->add_timestamped_readings(); |
| if (sensor_data == nullptr) { |
| continue; |
| } |
| reading->set_reading(sensor_data->reading()); |
| reading->set_timestamp_ns( |
| absl::ToUnixNanos(DecodeGoogleApiProto(sensor_data->timestamp()))); |
| } |
| } |
| std::string encoded_batch; |
| absl::Base64Escape(batch.SerializeAsString(), &encoded_batch); |
| resp.SetKeyInJsonBody("/HighFrequencySensorData", encoded_batch); |
| } |
| |
| } // namespace |
| |
| void RegisterRoutes(RedfishApp& app) { |
| TLBMC_ROUTE(app, "/redfish/v1/Oem/Google/HighFrequencySensors/") |
| .methods(boost::beast::http::verb::get)( |
| absl::bind_front(HandleSensor, std::cref(app))); |
| } |
| |
| } // namespace milotic_tlbmc::hf_sensors |