Improve dgr initialization failure handling

This commit is contained in:
Paul C Diem 2020-02-24 18:40:44 -06:00
parent a550fe3ac7
commit 9c5e5b689e

View File

@ -22,7 +22,7 @@
*/ */
#ifdef USE_DEVICE_GROUPS #ifdef USE_DEVICE_GROUPS
//#define DEVICE_GROUPS_DEBUG #define DEVICE_GROUPS_DEBUG
extern bool udp_connected; extern bool udp_connected;
@ -49,7 +49,8 @@ struct device_group {
struct device_group * device_groups; struct device_group * device_groups;
uint16_t outgoing_sequence = 0; uint16_t outgoing_sequence = 0;
bool device_groups_active = false; bool device_groups_initialized = false;
bool device_groups_initialization_failed = false;
bool building_status_message = false; bool building_status_message = false;
bool processing_remote_device_message = false; bool processing_remote_device_message = false;
bool waiting_for_acks; bool waiting_for_acks;
@ -57,12 +58,11 @@ bool udp_was_connected = false;
void DeviceGroupsInit(void) void DeviceGroupsInit(void)
{ {
/* // Initialize the device information for each device group. The group name is the MQTT group topic.
Initialize the device information for each device group. The group name is the MQTT group topic.
*/
device_groups = (struct device_group *)calloc(device_group_count, sizeof(struct device_group)); device_groups = (struct device_group *)calloc(device_group_count, sizeof(struct device_group));
if (device_groups == nullptr) { if (device_groups == nullptr) {
AddLog_P2(LOG_LEVEL_ERROR, PSTR("DGR: error allocating %u-element device group array"), device_group_count); AddLog_P2(LOG_LEVEL_ERROR, PSTR("DGR: error allocating %u-element device group array"), device_group_count);
device_groups_initialization_failed = true;
return; return;
} }
@ -80,7 +80,7 @@ void DeviceGroupsInit(void)
Settings.device_group_share_in = Settings.device_group_share_out = 0xffffffff; Settings.device_group_share_in = Settings.device_group_share_out = 0xffffffff;
} }
device_groups_active = true; device_groups_initialized = true;
} }
char * IPAddressToString(const IPAddress& ip_address) char * IPAddressToString(const IPAddress& ip_address)
@ -133,8 +133,8 @@ void SendDeviceGroupPacket(IPAddress ip, char * packet, int len, const char * la
void _SendDeviceGroupMessage(uint8_t device_group_index, DeviceGroupMessageType message_type, ...) void _SendDeviceGroupMessage(uint8_t device_group_index, DeviceGroupMessageType message_type, ...)
{ {
// If device groups are not active, ignore this request. // If device groups are not enabled, ignore this request.
if (!device_groups_active) return; if (!Settings.flag4.device_groups_enabled) return;
// If UDP is not set up, ignore this request. // If UDP is not set up, ignore this request.
if (!udp_connected) return; if (!udp_connected) return;
@ -164,7 +164,7 @@ void _SendDeviceGroupMessage(uint8_t device_group_index, DeviceGroupMessageType
AddLog_P2(LOG_LEVEL_DEBUG, PSTR("Building device group %s full status packet"), device_group->group_name); AddLog_P2(LOG_LEVEL_DEBUG, PSTR("Building device group %s full status packet"), device_group->group_name);
#endif // DEVICE_GROUPS_DEBUG #endif // DEVICE_GROUPS_DEBUG
device_group->message_length = 0; device_group->message_length = 0;
_SendDeviceGroupMessage(device_group_index, DGR_MSGTYP_PARTIAL_UPDATE, DGR_ITEM_POWER, power); SendDeviceGroupMessage(device_group_index, DGR_MSGTYP_PARTIAL_UPDATE, DGR_ITEM_POWER, power);
XdrvMailbox.command_code = DGR_ITEM_STATUS; XdrvMailbox.command_code = DGR_ITEM_STATUS;
XdrvCall(FUNC_DEVICE_GROUP_REQUEST); XdrvCall(FUNC_DEVICE_GROUP_REQUEST);
building_status_message = false; building_status_message = false;
@ -390,9 +390,6 @@ void _SendDeviceGroupMessage(uint8_t device_group_index, DeviceGroupMessageType
void ProcessDeviceGroupMessage(char * packet, int packet_length) void ProcessDeviceGroupMessage(char * packet, int packet_length)
{ {
// If device groups are not active, ignore this request.
if (!device_groups_active) return;
// Make the group name a null-terminated string. // Make the group name a null-terminated string.
char * message_group_name = packet + sizeof(DEVICE_GROUP_MESSAGE) - 1; char * message_group_name = packet + sizeof(DEVICE_GROUP_MESSAGE) - 1;
char * message_ptr = strchr(message_group_name, ' '); char * message_ptr = strchr(message_group_name, ' ');
@ -617,14 +614,12 @@ void DeviceGroupsLoop(void)
{ {
if (!Settings.flag4.device_groups_enabled) return; if (!Settings.flag4.device_groups_enabled) return;
if (udp_connected) { if (udp_connected) {
if (!device_groups_active) {
DeviceGroupsInit();
if (!device_groups_active) return;
}
if (!udp_was_connected) { if (!udp_was_connected) {
udp_was_connected = true; udp_was_connected = true;
if (!device_groups_initialized) DeviceGroupsInit();
if (device_groups_initialization_failed) return;
for (uint32_t device_group_index = 0; device_group_index < device_group_count; device_group_index++) { for (uint32_t device_group_index = 0; device_group_index < device_group_count; device_group_index++) {
device_group * device_group = &device_groups[device_group_index]; device_group * device_group = &device_groups[device_group_index];
char * message_ptr = &device_group->message[device_group->message_header_length]; char * message_ptr = &device_group->message[device_group->message_header_length];
@ -635,12 +630,14 @@ void DeviceGroupsLoop(void)
*message_ptr++ = 0; *message_ptr++ = 0;
device_group->message_length = message_ptr - device_group->message; device_group->message_length = message_ptr - device_group->message;
device_group->initial_status_requests_remaining = 5; device_group->initial_status_requests_remaining = 5;
device_group->next_ack_check_time = millis() + 2000; device_group->next_ack_check_time = millis() + 1000;
} }
waiting_for_acks = true; waiting_for_acks = true;
} }
if (device_groups_initialization_failed) return;
if (waiting_for_acks) { if (waiting_for_acks) {
uint32_t now = millis(); uint32_t now = millis();
waiting_for_acks = false; waiting_for_acks = false;