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.

DataModel.h 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. #pragma once
  2. #include <memory>
  3. #include <vector>
  4. #include "PublisherData.h"
  5. #include <map>
  6. #include <chrono>
  7. #include <optional>
  8. #include <regex>
  9. #include <atomic>
  10. #include "ModbusDataPOC.h"
  11. #include "ModbusDataBender.h"
  12. #include "ts_map.h"
  13. #include "cappedstorage.h"
  14. const Eigen::MatrixXd dummyMat(0,0);
  15. const Eigen::VectorXd dummyVec(0);
  16. struct SavedData{
  17. Category cat;
  18. std::vector<uint16_t> data;
  19. SavedData(const Category c, const std::vector<uint16_t> d){
  20. cat = c;
  21. data = d;
  22. }
  23. friend std::ostream& operator<<(std::ostream& os, const SavedData& savedData);
  24. };
  25. class PublisherInterface;
  26. constexpr auto READ_BUFFER_SIZE = 4096;
  27. typedef std::vector<std::vector<float>> float_matrix;
  28. //Type for one dimensional permanent double values
  29. typedef ts_map<ModbusRegister, CappedStorage> permanent_data;
  30. using namespace std::chrono;
  31. class DataModel
  32. {
  33. public:
  34. //Sigleton Methods
  35. static std::unique_ptr<DataModel>& Instance();
  36. static void Destroy();
  37. static void Create();
  38. ~DataModel();
  39. //save Data inside application
  40. void savePublishedData(PublisherData&&);
  41. void makePermanent(ModbusRegister reg, bool biased);
  42. void saveModbusParameter(ParameterSpecification param);
  43. //getter
  44. std::vector<u_int> getSavedPublishedDataIndexes(const u_int id, const seconds newerThan);
  45. //---local file storage methods---
  46. //Stores in-programm data to local file system
  47. bool flush(std::stringstream &ss);
  48. uintmax_t readLogFile(std::stringstream& ss, long long fromTime);
  49. //Reads in from all files their content to ss, or a certain file, specified by its index
  50. //TODO: define readSince to read al data after that time point
  51. //size_t readSingleFile(std::stringstream& ss, const size_t index);
  52. uintmax_t readFromAllFiles(std::stringstream& ss, std::chrono::system_clock::time_point readSince = std::chrono::system_clock::from_time_t(0));
  53. uintmax_t readAllDataFiles(std::stringstream& ss);
  54. unsigned long removeStoredData(seconds olderThan);
  55. unsigned long removeStoredData();
  56. //saves collected data after a certain amount of time points
  57. void checkForFlushData();
  58. unsigned long long getStartTime(const unsigned int id);
  59. unsigned long long getStartTime();
  60. const char* getDataDir() { return dataDir; }
  61. const std::regex& getRegexPatternFile() { return regexPatternFile; }
  62. constexpr unsigned long long duration_to_sec_long(const std::chrono::system_clock::duration& t);
  63. constexpr unsigned long long timepoint_to_sec_long(const std::chrono::system_clock::time_point& t);
  64. const ts_map<ModbusRegister, SavedData> &getPublisherData() const;
  65. CappedStorage* getPermanentData(ModbusRegister reg);
  66. std::stringstream &readTemporaryData(std::stringstream &buffer);
  67. std::stringstream &readPermanentData(std::stringstream &buffer, bool retain);
  68. ts_queue<ParameterSpecification> &getAlerts();
  69. private:
  70. //Register to enqueue data wich are stored permanently
  71. permanent_data permanentStorage;
  72. //Temporary storage of the last readed parameter
  73. ts_map<ModbusRegister, SavedData> temporaryStorage;
  74. //Temporary saved alerts
  75. ts_queue<ParameterSpecification> alerts;
  76. static long permanentParamHistory;
  77. static int narrowBlock;
  78. const std::regex regexPatternFile = std::regex("data_\\w+");
  79. const std::string logFileName = "data_analysis.log";
  80. const std::string dateFormatLogger = "%Y-%m-%d %H:%M:%S";
  81. u_int nrOfDataPoints = 0;
  82. //static instance which can be accessed by calling DataModel::Instance()
  83. static std::unique_ptr<DataModel> instance;
  84. //local directory path to the data storage
  85. const char* dataDir = "data/";
  86. const char* logDir = "log/";
  87. //Data from publishers, one elemt in outer vector for one id
  88. //Each id consists of a vector of PublisherData (use map to make the id become a unique key)
  89. //ostream operators (read and write dataModel data from streams)
  90. friend std::ostream& operator<<(std::ostream& os, DataModel& dataModel);
  91. friend std::istream& operator>>(std::istream& is, DataModel& dataModel);
  92. };