4 #include <lwip/sockets.h> 11 static const char *
const TAG =
"alpha3";
14 ESP_LOGCONFIG(TAG,
"ALPHA3");
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)
35 if (rel_offset >= 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);
54 return !std::memcmp(this->
response_type_, response_type, GENI_RESPONSE_TYPE_LENGTH);
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);
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_) {
165 request, ESP_GATT_WRITE_TYPE_NO_RSP, ESP_GATT_AUTH_REQ_NONE);
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));
sensor::Sensor * power_sensor_
void handle_geni_response_(const uint8_t *response, uint16_t length)
void gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if, esp_ble_gattc_cb_param_t *param) override
bool is_current_response_type_(const uint8_t *response_type)
sensor::Sensor * flow_sensor_
uint8_t * get_remote_bda()
void extract_publish_sensor_value_(const uint8_t *response, int16_t length, int16_t response_offset, int16_t value_offset, sensor::Sensor *sensor, float factor)
void dump_config() override
uint16_t get_conn_id() const
std::string address_str() const
sensor::Sensor * voltage_sensor_
sensor::Sensor * speed_sensor_
void publish_state(float state)
Publish a new state to the front-end.
void send_request_(uint8_t *request, size_t len)
BLECharacteristic * get_characteristic(espbt::ESPBTUUID service, espbt::ESPBTUUID chr)
Implementation of SPI Controller mode.
Base-class for all sensors.
uint8_t response_type_[GENI_RESPONSE_TYPE_LENGTH]
esphome::sensor::Sensor * sensor
sensor::Sensor * head_sensor_
void IRAM_ATTR HOT delay(uint32_t ms)
espbt::ClientState node_state
sensor::Sensor * current_sensor_