12void Alpha3::dump_config() {
13 ESP_LOGCONFIG(TAG,
"ALPHA3");
14 LOG_SENSOR(
" ",
"Flow", this->flow_sensor_);
15 LOG_SENSOR(
" ",
"Head", this->head_sensor_);
16 LOG_SENSOR(
" ",
"Power", this->power_sensor_);
17 LOG_SENSOR(
" ",
"Current", this->current_sensor_);
18 LOG_SENSOR(
" ",
"Speed", this->speed_sensor_);
19 LOG_SENSOR(
" ",
"Voltage", this->voltage_sensor_);
26 if (sensor ==
nullptr)
29 const int16_t value_length = 4;
31 auto rel_offset = value_offset - response_offset;
32 if (rel_offset <= -value_length)
37 auto start_offset = std::max(0, rel_offset);
38 auto end_offset = std::min((int16_t) (rel_offset + value_length),
length);
39 auto copy_length = end_offset - start_offset;
40 auto buffer_offset = std::max(-rel_offset, 0);
41 std::memcpy(this->
buffer_ + buffer_offset, response + start_offset, copy_length);
43 if (rel_offset + value_length <=
length) {
47 float fvalue = *((
float *)
buffer);
53 return !std::memcmp(this->
response_type_, response_type, GENI_RESPONSE_TYPE_LENGTH);
59 if (length < GENI_RESPONSE_HEADER_LENGTH) {
63 if (response[0] != 36 || response[2] != 248 || response[3] != 231 || response[4] != 10) {
64 ESP_LOGW(TAG,
"[%s] response bytes %d %d %d %d %d don't match GENI HEADER", this->
parent_->
address_str(),
65 response[0], response[1], response[2], response[3], response[4]);
70 std::memcpy(this->
response_type_, response + 5, GENI_RESPONSE_TYPE_LENGTH);
73 auto extract_publish_sensor_value = [response,
length,
this](int16_t value_offset,
sensor::Sensor *sensor,
80 extract_publish_sensor_value(GENI_RESPONSE_FLOW_OFFSET, this->flow_sensor_, 3600.0F);
81 extract_publish_sensor_value(GENI_RESPONSE_HEAD_OFFSET, this->head_sensor_, .0001F);
84 extract_publish_sensor_value(GENI_RESPONSE_POWER_OFFSET, this->power_sensor_, 1.0F);
85 extract_publish_sensor_value(GENI_RESPONSE_CURRENT_OFFSET, this->current_sensor_, 1.0F);
86 extract_publish_sensor_value(GENI_RESPONSE_MOTOR_SPEED_OFFSET, this->speed_sensor_, 1.0F);
87 extract_publish_sensor_value(GENI_RESPONSE_VOLTAGE_AC_OFFSET, this->voltage_sensor_, 1.0F);
96void Alpha3::gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if, esp_ble_gattc_cb_param_t *param) {
98 case ESP_GATTC_OPEN_EVT: {
99 if (param->open.status == ESP_GATT_OK) {
106 case ESP_GATTC_CONNECT_EVT: {
107 if (std::memcmp(param->connect.remote_bda, this->parent_->get_remote_bda(), 6) != 0)
109 auto ret = esp_ble_set_encryption(param->connect.remote_bda, ESP_BLE_SEC_ENCRYPT);
111 ESP_LOGW(TAG,
"esp_ble_set_encryption failed, status=%x",
ret);
115 case ESP_GATTC_DISCONNECT_EVT: {
117 PUBLISH_STATE(this->flow_sensor_, NAN);
118 PUBLISH_STATE(this->head_sensor_, NAN);
119 PUBLISH_STATE(this->power_sensor_, NAN);
120 PUBLISH_STATE(this->current_sensor_, NAN);
121 PUBLISH_STATE(this->speed_sensor_, NAN);
122 PUBLISH_STATE(this->voltage_sensor_, NAN);
125 case ESP_GATTC_SEARCH_CMPL_EVT: {
127 if (chr ==
nullptr) {
128 ESP_LOGE(TAG,
"[%s] No GENI service found at device, not an Alpha3..?", this->
parent_->
address_str());
134 ESP_LOGW(TAG,
"esp_ble_gattc_register_for_notify failed, status=%d", status);
139 case ESP_GATTC_REG_FOR_NOTIFY_EVT: {
140 this->
node_state = espbt::ClientState::ESTABLISHED;
144 case ESP_GATTC_NOTIFY_EVT: {
145 if (param->notify.handle == this->geni_handle_) {
157 esp_ble_gattc_write_char(this->
parent_->
get_gattc_if(), this->parent_->get_conn_id(), this->geni_handle_,
len,
158 request, ESP_GATT_WRITE_TYPE_NO_RSP, ESP_GATT_AUTH_REQ_NONE);
164void Alpha3::update() {
165 if (this->
node_state != espbt::ClientState::ESTABLISHED) {
170 if (this->flow_sensor_ !=
nullptr || this->head_sensor_ !=
nullptr) {
171 uint8_t geni_request_flow_head[] = {39, 7, 231, 248, 10, 3, 93, 1, 33, 82, 31};
172 this->
send_request_(geni_request_flow_head,
sizeof(geni_request_flow_head));
175 if (this->power_sensor_ !=
nullptr || this->current_sensor_ !=
nullptr || this->speed_sensor_ !=
nullptr ||
176 this->voltage_sensor_ !=
nullptr) {
177 uint8_t geni_request_power[] = {39, 7, 231, 248, 10, 3, 87, 0, 69, 138, 205};
178 this->
send_request_(geni_request_power,
sizeof(geni_request_power));