Skip to content

Commit

Permalink
Implement uptime device prop, fix #52
Browse files Browse the repository at this point in the history
  • Loading branch information
marvinroger committed Apr 2, 2016
1 parent f23700a commit d6319f8
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 1 deletion.
19 changes: 19 additions & 0 deletions src/Homie/Boot/BootNormal.cpp
Expand Up @@ -7,6 +7,7 @@ BootNormal::BootNormal()
, _lastWifiReconnectAttempt(0)
, _lastMqttReconnectAttempt(0)
, _lastSignalSent(0)
, _lastUptimeSent(0)
, _setupFunctionCalled(false)
, _wifiConnectNotified(false)
, _wifiDisconnectNotified(true)
Expand Down Expand Up @@ -395,6 +396,24 @@ void BootNormal::loop() {
}
}

if (now - this->_lastUptimeSent >= UPTIME_SEND_INTERVAL || this->_lastUptimeSent == 0) {
Clock.tick();
Logger.log(F("Sending uptime, currently "));
Logger.log(String(Clock.getSeconds()));
Logger.logln(F(" seconds..."));

char uptimeStr[10 + 1];
itoa(Clock.getSeconds(), uptimeStr, 10);

strcpy(MqttClient.getTopicBuffer(), Config.get().mqtt.baseTopic);
strcat(MqttClient.getTopicBuffer(), Config.get().deviceId);
strcat_P(MqttClient.getTopicBuffer(), PSTR("/$uptime"));

if (MqttClient.publish(uptimeStr, true)) {
this->_lastUptimeSent = now;
}
}

this->_interface->loopFunction();

MqttClient.loop();
Expand Down
2 changes: 2 additions & 0 deletions src/Homie/Boot/BootNormal.hpp
Expand Up @@ -12,6 +12,7 @@
#include "../Helpers.hpp"
#include "../Config.hpp"
#include "../Blinker.hpp"
#include "../Clock.hpp"
#include "../Logger.hpp"
#include "Boot.hpp"

Expand All @@ -31,6 +32,7 @@ namespace HomieInternals {
unsigned long _lastWifiReconnectAttempt;
unsigned long _lastMqttReconnectAttempt;
unsigned long _lastSignalSent;
unsigned long _lastUptimeSent;
bool _setupFunctionCalled;
bool _wifiConnectNotified;
bool _wifiDisconnectNotified;
Expand Down
21 changes: 21 additions & 0 deletions src/Homie/Clock.cpp
@@ -0,0 +1,21 @@
#include "Clock.hpp"

using namespace HomieInternals;

ClockClass::ClockClass()
: _seconds(0)
, _lastTick(0)
{
}

void ClockClass::tick() {
unsigned long now = millis();
this->_seconds += (now - this->_lastTick) / 1000UL;
this->_lastTick = now;
}

unsigned long ClockClass::getSeconds() {
return this->_seconds;
}

ClockClass HomieInternals::Clock;
18 changes: 18 additions & 0 deletions src/Homie/Clock.hpp
@@ -0,0 +1,18 @@
#pragma once

#include "Arduino.h"

namespace HomieInternals {
class ClockClass {
public:
ClockClass();
void tick();
unsigned long getSeconds();

private:
unsigned long _seconds;
unsigned long _lastTick;
};

extern ClockClass Clock;
}
3 changes: 2 additions & 1 deletion src/Homie/Constants.hpp
Expand Up @@ -20,7 +20,8 @@ namespace HomieInternals {
const unsigned int CONFIG_SCAN_INTERVAL = 20 * 1000;
const unsigned int WIFI_RECONNECT_INTERVAL = 20 * 1000;
const unsigned int MQTT_RECONNECT_INTERVAL = 5 * 1000;
const unsigned long SIGNAL_QUALITY_SEND_INTERVAL = 300 * 1000;
const unsigned long SIGNAL_QUALITY_SEND_INTERVAL = 5 * 60 * 1000;
const unsigned long UPTIME_SEND_INTERVAL = 2 * 60 * 1000;

const float LED_WIFI_DELAY = 1;
const float LED_MQTT_DELAY = 0.2;
Expand Down

0 comments on commit d6319f8

Please sign in to comment.