homieiot / homie-esp8266

💡 ESP8266 framework for Homie, a lightweight MQTT convention for the IoT
http://homieiot.github.io/homie-esp8266
MIT License
1.36k stars 308 forks source link

Proposal: $implementation/reboot #693

Open maruel opened 3 years ago

maruel commented 3 years ago

There's already $implementation/reset (which doesn't seem to work on 3.0.1) but none to reboot. It'd be useful to do a end-to-end test after flashing over serial to prepare for OTA update later.

I think the implementation would be relatively simple but I want to double check with owners if they would want such patch.

I think it would boil down to the following:

https://github.com/homieiot/homie-esp8266/blob/develop/src/Homie/Boot/BootNormal.hpp

A new enum SUB_IMPLEMENTATION_REBOOT

https://github.com/homieiot/homie-esp8266/blob/develop/src/Homie/Boot/BootNormal.cpp

In BootNormal::_advertise():

    case AdvertisementProgress::GlobalStep::SUB_IMPLEMENTATION_REBOOT:
      packetId = Interface::get().getMqttClient().subscribe(_prefixMqttTopic(PSTR("/$implementation/reboot")), 1);
      if (packetId != 0) _advertisementProgress.globalStep = AdvertisementProgress::GlobalStep::SUB_IMPLEMENTATION_RESET;
      break;

Then a new function that would be called in BootNormal::_onMqttMessage():

bool HomieInternals::BootNormal::__handleReboot(char * topic, char * payload, const AsyncMqttClientMessageProperties& properties, size_t len, size_t index, size_t total) {
  if (
    _mqttTopicLevelsCount == 3
    && strcmp_P(_mqttTopicLevels.get()[1], PSTR("$implementation")) == 0
    && strcmp_P(_mqttTopicLevels.get()[2], PSTR("reboot")) == 0
    && strcmp_P(_mqttPayloadBuffer.get(), PSTR("true")) == 0
    ) {
    Interface::get().getMqttClient().publish(_prefixMqttTopic(PSTR("/$implementation/reboot")), 1, true, "");
    Interface::get().getLogger() << F("Flagged for reboot by network") << endl;
   _flaggedForReboot = true;
    return true;
  }
  return false;
}

https://github.com/homieiot/homie-esp8266/blob/develop/docs/others/homie-implementation-specifics.md Update to add the new topic.