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.

PublisherData.cpp 2.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. #include "PublisherData.h"
  2. #include "instreamOperator.h"
  3. #include <sstream>
  4. PublisherData::PublisherData(const int _publisherID, PublisherType pubType, float _value) :
  5. publisherID(_publisherID), publisherType(pubType),time_generated(system_clock::now()), values(std::vector<float>())
  6. {
  7. values.push_back(_value);
  8. }
  9. PublisherData::PublisherData(const int _publisherID, PublisherType pubType, std::vector<float> _values) :
  10. publisherID(_publisherID), publisherType(pubType), time_generated(system_clock::now()), values(_values)
  11. {
  12. }
  13. PublisherData::PublisherData() :
  14. publisherID(0), publisherType(PublisherType::NA), time_generated(system_clock::now()), values({ 0.0 })
  15. {
  16. }
  17. std::string PublisherData::toString() const{
  18. //auto timeNow = system_clock::to_time_t(time_generated);
  19. //char timestring[26];
  20. //ctime_s(timestring, sizeof timestring, &timeNow);
  21. //std::stringstream ss;
  22. //ss << "PublisherData from ID: " << publisherID << " at " << timestring;
  23. //unsigned int i = 0;
  24. //for (auto v : values) {
  25. // ss << " Value " << ++i << " <type: " << typeid(v).name() << "> " << ": " << v << std::endl;
  26. //}
  27. //return ss.str();
  28. }
  29. std::istream& operator>>(std::istream& is, char const* pat) {
  30. char ch;
  31. while (isspace(static_cast<unsigned char>(is.peek())))
  32. is.get(ch);
  33. while (*pat && is && *pat == is.peek() && is.get(ch)) {
  34. ++pat;
  35. }
  36. // if we didn't reach the end of the pattern, matching failed (mismatch, premature EOF, etc.)
  37. if (*pat) {
  38. is.setstate(std::ios::failbit);
  39. }
  40. return is;
  41. }
  42. std::ostream& operator<<(std::ostream& os, const PublisherData& data){
  43. //auto timeNow = system_clock::to_time_t(data.time_generated);
  44. //char timestring[26];
  45. //ctime_s(timestring, sizeof timestring, &timeNow);
  46. os << duration_cast<seconds>(data.time_generated.time_since_epoch()).count() << "_";
  47. for (unsigned int i = 0; i < data.values.size()-1; i++) {
  48. os << data.values[i] << ",";
  49. }
  50. os << data.values[data.values.size()-1] << ";";
  51. return os;
  52. }
  53. std::istream& operator>>(std::istream& is, PublisherData& data) {
  54. //size_t time;
  55. float value;
  56. is >> value;
  57. data.values.clear();
  58. data.values.push_back(value);
  59. //data.time_generated = std::chrono::system_clock::from_time_t(time);
  60. while (is.peek() == ',') {
  61. is >> "," >> value;
  62. data.values.push_back(value);
  63. }
  64. if (is.peek() == ';') {
  65. is.get();
  66. return is;
  67. }
  68. else
  69. throw std::invalid_argument("Stream object has invalid pattern (should be %d:%f,...,%f;)");
  70. }