get_stat_all.cpp 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. #include "get_stat_all.h"
  2. #include <status_codes.h>
  3. #include <iostream>
  4. #include <stdexcept>
  5. #include <vector>
  6. #define ID_FORMAT "Sensor id: "
  7. #define MIN_FORMAT "Minimum value: "
  8. #define MAX_FORMAT "Maximum value: "
  9. #define AVG_FORMAT "Average value: "
  10. #define MED_FORMAT "Median value: "
  11. using std::cout;
  12. using std::endl;
  13. using std::runtime_error;
  14. using std::vector;
  15. get_stat_all_res::get_stat_all_res()
  16. {
  17. }
  18. get_stat_all_res::~get_stat_all_res()
  19. {
  20. }
  21. command_type get_stat_all_res::get_command()
  22. {
  23. return command_type::GET_STAT_ALL_CMD;
  24. }
  25. int get_stat_all_res::perform(response *res)
  26. {
  27. if (res->response_u.get_stat_all.response != status::VALID)
  28. throw runtime_error(
  29. status_codes::get(res->response_u.read.response));
  30. sensor_stat *src = res->response_u.get_stat_all.stats.stats_val;
  31. size_t size = res->response_u.get_stat_all.stats.stats_len;
  32. vector<sensor_stat> stats(src, src + size);
  33. for (auto stat : stats) {
  34. cout << ID_FORMAT << stat.sensor_id << endl;
  35. cout << MIN_FORMAT << stat.min << endl;
  36. cout << MAX_FORMAT << stat.max << endl;
  37. cout << AVG_FORMAT << stat.avg << endl;
  38. cout << MED_FORMAT << stat.med << endl;
  39. }
  40. return status::VALID;
  41. }