cmd_read_all.cpp 947 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. #include "cmd_read_all.h"
  2. #include <exception>
  3. using std::exception;
  4. cmd_read_all::cmd_read_all()
  5. {
  6. sensors = nullptr;
  7. }
  8. cmd_read_all::~cmd_read_all()
  9. {
  10. if (sensors != nullptr)
  11. delete[] sensors;
  12. }
  13. command_type cmd_read_all::get_command()
  14. {
  15. return command_type::READ_ALL_CMD;
  16. }
  17. response cmd_read_all::perform(request *req)
  18. {
  19. vector<sensor_data> *data;
  20. int sensor_id;
  21. memset(&res, 0, sizeof res);
  22. res.type = get_command();
  23. try {
  24. data = get_user_db(req->request_u.everything.session_key);
  25. } catch (exception &e) {
  26. res.response_u.read_all.response = status::INVALID_SESSION_KEY;
  27. return res;
  28. }
  29. if (sensors != nullptr)
  30. delete[] sensors;
  31. sensors = new sensor_data[data->size()];
  32. memcpy(sensors, data->data(), data->size() * sizeof(sensor_data));
  33. res.response_u.read_all.data.data_val = sensors;
  34. res.response_u.read_all.data.data_len = data->size();
  35. res.response_u.read_all.response = status::VALID;
  36. return res;
  37. }