cmd_load.cpp 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. #include "cmd_load.h"
  2. #include <exception>
  3. using std::exception;
  4. cmd_load::cmd_load()
  5. {
  6. }
  7. cmd_load::~cmd_load()
  8. {
  9. }
  10. command_type cmd_load::get_command()
  11. {
  12. return command_type::LOAD_CMD;
  13. }
  14. response cmd_load::perform(request *req)
  15. {
  16. memset(&res, 0, sizeof res);
  17. res.type = get_command();
  18. vector<sensor_data> *data;
  19. try {
  20. data = get_user_db(req->request_u.everything.session_key);
  21. } catch (exception &e) {
  22. res.response_u.active.response = status::INVALID_SESSION_KEY;
  23. return res;
  24. }
  25. if (data->size() != 0) {
  26. res.response_u.active.response = status::INVALID_REQUEST;
  27. return res;
  28. }
  29. for (int i = 0; i < req->request_u.load.sensors.sensors_len; i++) {
  30. sensor_data insert_sensor;
  31. sensor_data *sensor =
  32. &req->request_u.load.sensors.sensors_val[i];
  33. float *values = new float[sensor->values.values_len];
  34. memcpy(values, sensor->values.values_val,
  35. sensor->values.values_len * sizeof(float));
  36. insert_sensor.sensor_id = sensor->sensor_id;
  37. insert_sensor.values.values_len = sensor->values.values_len;
  38. insert_sensor.values.values_val = values;
  39. data->push_back(insert_sensor);
  40. }
  41. res.response_u.active.response = status::VALID;
  42. return res;
  43. }