27 if (sensor ==
nullptr)
30 const int16_t value_length = 4;
32 auto rel_offset = value_offset - response_offset;
33 if (rel_offset <= -value_length)
38 auto start_offset = std::max(0, rel_offset);
39 auto end_offset = std::min((int16_t) (rel_offset + value_length),
length);
40 auto copy_length = end_offset - start_offset;
41 auto buffer_offset = std::max(-rel_offset, 0);
42 std::memcpy(this->
buffer_ + buffer_offset, response + start_offset, copy_length);
44 if (rel_offset + value_length <=
length) {
47 *((int32_t *) buffer) = ntohl(*((int32_t *) buffer));
48 float fvalue = *((
float *) buffer);
60 if (length < GENI_RESPONSE_HEADER_LENGTH) {
64 if (response[0] != 36 || response[2] != 248 || response[3] != 231 || response[4] != 10) {
65 ESP_LOGW(TAG,
"[%s] response bytes %d %d %d %d %d don't match GENI HEADER", this->
parent_->
address_str().c_str(),
66 response[0], response[1], response[2], response[3], response[4]);
71 std::memcpy(this->
response_type_, response + 5, GENI_RESPONSE_TYPE_LENGTH);
74 auto extract_publish_sensor_value = [response,
length,
this](int16_t value_offset,
sensor::Sensor *sensor,
81 extract_publish_sensor_value(GENI_RESPONSE_FLOW_OFFSET, this->
flow_sensor_, 3600.0F);
82 extract_publish_sensor_value(GENI_RESPONSE_HEAD_OFFSET, this->
head_sensor_, .0001F);
85 extract_publish_sensor_value(GENI_RESPONSE_POWER_OFFSET, this->
power_sensor_, 1.0F);
86 extract_publish_sensor_value(GENI_RESPONSE_CURRENT_OFFSET, this->
current_sensor_, 1.0F);
87 extract_publish_sensor_value(GENI_RESPONSE_MOTOR_SPEED_OFFSET, this->
speed_sensor_, 1.0F);
88 extract_publish_sensor_value(GENI_RESPONSE_VOLTAGE_AC_OFFSET, this->
voltage_sensor_, 1.0F);
99 case ESP_GATTC_OPEN_EVT: {
100 if (param->open.status == ESP_GATT_OK) {
107 case ESP_GATTC_CONNECT_EVT: {
108 if (std::memcmp(param->connect.remote_bda, this->parent_->get_remote_bda(), 6) != 0)
110 auto ret = esp_ble_set_encryption(param->connect.remote_bda, ESP_BLE_SEC_ENCRYPT);
112 ESP_LOGW(TAG,
"esp_ble_set_encryption failed, status=%x", ret);
116 case ESP_GATTC_DISCONNECT_EVT: {
132 case ESP_GATTC_SEARCH_CMPL_EVT: {
134 if (chr ==
nullptr) {
135 ESP_LOGE(TAG,
"[%s] No GENI service found at device, not an Alpha3..?", this->
parent_->
address_str().c_str());
141 ESP_LOGW(TAG,
"esp_ble_gattc_register_for_notify failed, status=%d",
status);
146 case ESP_GATTC_REG_FOR_NOTIFY_EVT: {
147 this->
node_state = espbt::ClientState::ESTABLISHED;
151 case ESP_GATTC_NOTIFY_EVT: {
152 if (param->notify.handle == this->geni_handle_) {
171 if (this->
node_state != espbt::ClientState::ESTABLISHED) {
172 ESP_LOGW(TAG,
"[%s] Cannot poll, not connected", this->
parent_->
address_str().c_str());
177 uint8_t geni_request_flow_head[] = {39, 7, 231, 248, 10, 3, 93, 1, 33, 82, 31};
178 this->
send_request_(geni_request_flow_head,
sizeof(geni_request_flow_head));
183 uint8_t geni_request_power[] = {39, 7, 231, 248, 10, 3, 87, 0, 69, 138, 205};
184 this->
send_request_(geni_request_power,
sizeof(geni_request_power));