Add recycle time and config for it

This commit is contained in:
Daniël Koek 2024-10-11 11:04:17 +01:00
parent c77f45f4f2
commit 9cf5ed87f8
3 changed files with 22 additions and 10 deletions

View file

@ -114,6 +114,7 @@ CONF_SUB_PACKET = "sub_packet"
CONF_SENT_SWITCH_STATE = "sent_switch_state"
CONF_REPEATER = "repeater"
CONF_NETWORK_ID = "network_id"
CONF_RECYCLE_TIME = "recycle_time"
def sensor_validation(cls: MockObjClass):
@ -149,6 +150,9 @@ CONFIG_SCHEMA = cv.All(
# to be able to repeater hop
cv.Required(CONF_NETWORK_ID): cv.int_range(min=0, max=255),
cv.Optional(CONF_REPEATER, default=False): cv.boolean,
cv.Optional(
CONF_RECYCLE_TIME, default="600s"
): cv.positive_time_period_seconds,
cv.Optional(CONF_SENSORS): cv.ensure_list(sensor_validation(Sensor)),
cv.Optional(CONF_BINARY_SENSORS): cv.ensure_list(
sensor_validation(BinarySensor)
@ -232,6 +236,7 @@ async def to_code(config):
cg.add(var.set_enable_lbt(config[CONF_ENABLE_LBT]))
cg.add(var.set_transmission_mode(config[CONF_TRANSMISSION_MODE]))
cg.add(var.set_enable_rssi(config[CONF_ENABLE_RSSI]))
cg.add(var.set_recycle_time(config[CONF_RECYCLE_TIME].total_seconds))
for sens_conf in config.get(CONF_SENSORS, ()):
sens_id = sens_conf[CONF_ID]
sensor_device = await cg.get_variable(sens_id)

View file

@ -270,7 +270,7 @@ void EbyteLoraComponent::setup() {
#ifdef USE_SENSOR
for (auto &sensor : this->sensors_) {
sensor.sensor->add_on_state_callback([this, &sensor](float x) {
this->updated_ = true;
this->need_send_info = true;
sensor.updated = true;
});
}
@ -278,7 +278,7 @@ void EbyteLoraComponent::setup() {
#ifdef USE_BINARY_SENSOR
for (auto &sensor : this->binary_sensors_) {
sensor.sensor->add_on_state_callback([this, &sensor](bool value) {
this->updated_ = true;
this->need_send_info = true;
sensor.updated = true;
});
}
@ -414,10 +414,12 @@ void EbyteLoraComponent::update() {
}
}
ESP_LOGD(TAG, "Full update done, config correct %s, mode %u ", YESNO(this->is_config_right()), this->get_mode_());
this->updated_ = true;
auto now = millis() / 1000;
if (this->last_key_time_ + this->repeater_request_recyle_time_ < now) {
this->resend_repeater_request_ = true;
if (this->last_key_time_ + this->recyle_time_ < now) {
ESP_LOGD(TAG, "Requesting all systems to send new info ");
this->request_repeater_info_update_needed_ = true;
this->need_send_info = true;
this->last_key_time_ = now;
}
}
@ -436,9 +438,9 @@ void EbyteLoraComponent::loop() {
this->process_(data);
}
if (this->resend_repeater_request_)
if (this->request_repeater_info_update_needed_)
this->request_repeater_info_();
if (this->updated_) {
if (this->need_send_info) {
this->send_data_(true);
}
}
@ -538,6 +540,8 @@ void EbyteLoraComponent::process_(std::vector<uint8_t> data) {
void EbyteLoraComponent::send_data_(bool all) {
if (!this->can_send_message_("send_data_"))
return;
// straight away we are all good for the next cycle
this->need_send_info = false;
std::vector<uint8_t> data;
data.push_back(network_id_);
#ifdef USE_SENSOR
@ -588,6 +592,7 @@ void EbyteLoraComponent::send_repeater_info_() {
void EbyteLoraComponent::request_repeater_info_() {
if (!this->can_send_message_("request_repeater_info_"))
return;
this->request_repeater_info_update_needed_ = false;
uint8_t data[2];
data[0] = REQUEST_REPEATER_INFO; // Request
data[1] = this->network_id_; // for unique id

View file

@ -87,6 +87,7 @@ class EbyteLoraComponent : public PollingComponent, public uart::UARTDevice {
void set_enable_lbt(EnableByte enable) { expected_config_.enable_lbt = enable; }
void set_transmission_mode(TransmissionMode mode) { expected_config_.transmission_mode = mode; }
void set_enable_rssi(EnableByte enable) { expected_config_.enable_rssi = enable; }
void set_recycle_time(uint32_t recycle_time) { this->recyle_time_ = recycle_time; }
// if enabled, will repeat any message it received as long as it isn't its own network id
void set_repeater(bool enable) { repeater_enabled_ = enable; }
// software network id, this will make sure that only items from that network id are being found
@ -108,9 +109,10 @@ class EbyteLoraComponent : public PollingComponent, public uart::UARTDevice {
bool can_send_message_(const char *info);
protected:
bool updated_{};
bool resend_repeater_request_{};
uint32_t repeater_request_recyle_time_{};
bool need_send_info{};
bool request_repeater_info_update_needed_{};
// for now do it every 600s
uint32_t recyle_time_ = 600;
uint32_t last_key_time_{};
void setup_conf_(std::vector<uint8_t> conf);
void process_(std::vector<uint8_t> data);