Digitalisierte Elektroverteilung zur permanenten Verbraucherüberwachung
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

cappedstorage.cpp 1.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. #include "cappedstorage.h"
  2. #include <cstring>
  3. #include <iostream>
  4. #include <easylogging++.h>
  5. #include "SystemConfig.h"
  6. CappedStorage::CappedStorage(bool _biased): biased(_biased)
  7. {
  8. if(biased){
  9. X.resize(0, 2);
  10. }
  11. else
  12. X.resize(0, 1);
  13. accessMtx = std::make_unique<std::mutex>();
  14. }
  15. void CappedStorage::lock(){
  16. accessMtx->lock();
  17. }
  18. void CappedStorage::unlock(){
  19. accessMtx->unlock();
  20. }
  21. void CappedStorage::store(const std::vector<uint16_t>& d)
  22. {
  23. std::scoped_lock lock(*accessMtx);
  24. float value = 0;
  25. std::memcpy(&value, d.data(), 4);
  26. const double time = c::time_point_cast<c::seconds>(c::system_clock::now()).time_since_epoch().count();
  27. X.conservativeResize(X.rows()+1, X.cols());
  28. y.conservativeResize(y.rows()+1, 1);
  29. y(y.rows()-1, 0) = value;
  30. if(biased){
  31. X(X.rows()-1, 0) = 1;
  32. X(X.rows()-1, 1) = time;
  33. }
  34. else
  35. X(X.rows()-1, 0) = time;
  36. }
  37. void CappedStorage::clear()
  38. {
  39. std::scoped_lock lock(*accessMtx);
  40. X.resize(0, Eigen::NoChange_t());
  41. }
  42. //Stream operator for 1 dimensional X Matrix
  43. std::ostream& operator<<(std::ostream& os, const CappedStorage& c){
  44. std::scoped_lock lock(*(c.accessMtx));
  45. int col =0;
  46. if(c.biased){
  47. col = 1;
  48. }
  49. for (int i = 0; i < c.X.rows(); i++) {
  50. os << c.X(i, col) << ",";
  51. }
  52. os << std::endl;
  53. for (int i = 0; i < c.y.size(); i++) {
  54. os << c.y(i, 0) << ",";
  55. }
  56. os << std::endl;
  57. return os;
  58. }