/* * PumpCommand.cpp * * Created on: Jul 27, 2021 * Author: Andreas Berthoud */ #include "PumpCommand.hpp" #include "pump_control.hpp" #include "main.h" PumpResponse::PumpResponse(com_channel_type_t com_channel_type, uint16_t response_identifier, bool was_successful, bool is_on) : Response(com_channel_type, response_identifier) { uint8_t * next_free_payload = this->payload_ptr + this->get_payload_size(); next_free_payload[0] = (uint8_t)was_successful; next_free_payload[1] = (uint8_t)is_on; this->add_to_payload_size(2); } PumpRequest::PumpRequest(com_channel_type_t com_channel_type, uint8_t * payload_ptr, uint16_t size) : Request(com_channel_type, payload_ptr, size) { uint16_t expected_length = this->buffer_offset + 3; if (expected_length != size) { this->has_error = true; return; } uint8_t * data_ptr = payload_ptr + this->buffer_offset; this->pump_command = static_cast(data_ptr[0]); this->timeout = data_ptr[1] << 8 | data_ptr[2]; } PumpResponse * PumpRequest::execute_request(com_channel_type_t com_channel_type, uint16_t response_identifier) { bool was_successful = true; if (this->pump_command >= pump_command_max) { was_successful = false; } else { switch (this->pump_command) { case pump_command_turn_off: pump::turn_on(this->timeout); break; case pump_command_turn_on: pump::turn_off(); break; case pump_command_get_state: break; default: was_successful = false; break; } } bool is_on = pump::is_on(); return new PumpResponse(com_channel_type, response_identifier, was_successful, is_on); }