mirror of
https://github.com/lumapu/ahoy.git
synced 2025-05-02 19:55:54 +02:00
Merge branch 'hotfix_relase_0.8.140' into development03
This commit is contained in:
commit
d155ab1edf
2 changed files with 43 additions and 18 deletions
|
@ -153,10 +153,13 @@ void app::onNetwork(bool gotIp) {
|
||||||
if(gotIp) {
|
if(gotIp) {
|
||||||
ah::Scheduler::resetTicker();
|
ah::Scheduler::resetTicker();
|
||||||
regularTickers(); //reinstall regular tickers
|
regularTickers(); //reinstall regular tickers
|
||||||
|
if(!mConfig->inst.startWithoutTime) // already set in regularTickers
|
||||||
every(std::bind(&app::tickSend, this), mConfig->inst.sendInterval, "tSend");
|
every(std::bind(&app::tickSend, this), mConfig->inst.sendInterval, "tSend");
|
||||||
mTickerInstallOnce = true;
|
mTickerInstallOnce = true;
|
||||||
mSunrise = 0; // needs to be set to 0, to reinstall sunrise and ivComm tickers!
|
mSunrise = 0; // needs to be set to 0, to reinstall sunrise and ivComm tickers!
|
||||||
once(std::bind(&app::tickNtpUpdate, this), 2, "ntp2");
|
|
||||||
|
uint32_t nextTrig = (mTimestamp < 0x1337) ? 0 : (mConfig->ntp.interval * 60);
|
||||||
|
once(std::bind(&app::tickNtpUpdate, this), nextTrig, "ntp");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -237,6 +240,7 @@ void app::tickNtpUpdate(void) {
|
||||||
nxtTrig = mConfig->ntp.interval * 60; // check again in configured interval
|
nxtTrig = mConfig->ntp.interval * 60; // check again in configured interval
|
||||||
mNtpReceived = false;
|
mNtpReceived = false;
|
||||||
}
|
}
|
||||||
|
yield();
|
||||||
|
|
||||||
updateNtp();
|
updateNtp();
|
||||||
|
|
||||||
|
|
|
@ -11,9 +11,9 @@
|
||||||
#include "../utils/helper.h"
|
#include "../utils/helper.h"
|
||||||
#include "AhoyWifiAp.h"
|
#include "AhoyWifiAp.h"
|
||||||
#include "AsyncJson.h"
|
#include "AsyncJson.h"
|
||||||
|
#include <lwip/dns.h>
|
||||||
|
|
||||||
#define NTP_PACKET_SIZE 48
|
#define NTP_PACKET_SIZE 48
|
||||||
|
|
||||||
class AhoyNetwork {
|
class AhoyNetwork {
|
||||||
public:
|
public:
|
||||||
typedef std::function<void(bool)> OnNetworkCB;
|
typedef std::function<void(bool)> OnNetworkCB;
|
||||||
|
@ -26,6 +26,8 @@ class AhoyNetwork {
|
||||||
mOnNetworkCB = onNetworkCB;
|
mOnNetworkCB = onNetworkCB;
|
||||||
mOnTimeCB = onTimeCB;
|
mOnTimeCB = onTimeCB;
|
||||||
|
|
||||||
|
mNtpIp = IPAddress(0, 0, 0, 0);
|
||||||
|
|
||||||
if('\0' == mConfig->sys.deviceName[0])
|
if('\0' == mConfig->sys.deviceName[0])
|
||||||
snprintf(mConfig->sys.deviceName, DEVNAME_LEN, "%s", DEF_DEVICE_NAME);
|
snprintf(mConfig->sys.deviceName, DEVNAME_LEN, "%s", DEF_DEVICE_NAME);
|
||||||
|
|
||||||
|
@ -55,16 +57,37 @@ class AhoyNetwork {
|
||||||
return ((mStatus == NetworkState::CONNECTED) || (mStatus == NetworkState::GOT_IP));
|
return ((mStatus == NetworkState::CONNECTED) || (mStatus == NetworkState::GOT_IP));
|
||||||
}
|
}
|
||||||
|
|
||||||
bool updateNtpTime(void) {
|
static void dnsCallback(const char *name, const ip_addr_t *ipaddr, void *pClass) {
|
||||||
if(NetworkState::GOT_IP != mStatus)
|
AhoyNetwork *obj = static_cast<AhoyNetwork*>(pClass);
|
||||||
return false;
|
if (ipaddr) {
|
||||||
|
obj->mNtpIp = ipaddr->u_addr.ip4.addr;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void updateNtpTime() {
|
||||||
|
if(mNtpIp != 0) {
|
||||||
|
startNtpUpdate();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
ip_addr_t ipaddr;
|
||||||
|
mNtpIp = WiFi.gatewayIP();
|
||||||
|
// dns_gethostbyname runs asynchronous and sets the member mNtpIp which is then checked on
|
||||||
|
// next call of updateNtpTime
|
||||||
|
err_t err = dns_gethostbyname(mConfig->ntp.addr, &ipaddr, dnsCallback, this);
|
||||||
|
|
||||||
|
if (err == ERR_OK) {
|
||||||
|
mNtpIp = ipaddr.u_addr.ip4.addr;
|
||||||
|
startNtpUpdate();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
protected:
|
||||||
|
void startNtpUpdate() {
|
||||||
|
DPRINTLN(DBG_INFO, F("get time from: ") + mNtpIp.toString());
|
||||||
if (!mUdp.connected()) {
|
if (!mUdp.connected()) {
|
||||||
IPAddress timeServer;
|
if (!mUdp.connect(mNtpIp, mConfig->ntp.port))
|
||||||
if (!WiFi.hostByName(mConfig->ntp.addr, timeServer))
|
return;
|
||||||
return false;
|
|
||||||
if (!mUdp.connect(timeServer, mConfig->ntp.port))
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
mUdp.onPacket([this](AsyncUDPPacket packet) {
|
mUdp.onPacket([this](AsyncUDPPacket packet) {
|
||||||
|
@ -72,7 +95,8 @@ class AhoyNetwork {
|
||||||
});
|
});
|
||||||
sendNTPpacket();
|
sendNTPpacket();
|
||||||
|
|
||||||
return true;
|
// reset to start with DNS lookup next time again
|
||||||
|
mNtpIp = IPAddress(0, 0, 0, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
@ -185,7 +209,7 @@ class AhoyNetwork {
|
||||||
std::swap(sort[i], sort[j]);
|
std::swap(sort[i], sort[j]);
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
protected:
|
||||||
void sendNTPpacket(void) {
|
void sendNTPpacket(void) {
|
||||||
uint8_t buf[NTP_PACKET_SIZE];
|
uint8_t buf[NTP_PACKET_SIZE];
|
||||||
memset(buf, 0, NTP_PACKET_SIZE);
|
memset(buf, 0, NTP_PACKET_SIZE);
|
||||||
|
@ -194,11 +218,6 @@ class AhoyNetwork {
|
||||||
buf[1] = 0; // Stratum
|
buf[1] = 0; // Stratum
|
||||||
buf[2] = 6; // Max Interval between messages in seconds
|
buf[2] = 6; // Max Interval between messages in seconds
|
||||||
buf[3] = 0xEC; // Clock Precision
|
buf[3] = 0xEC; // Clock Precision
|
||||||
// bytes 4 - 11 are for Root Delay and Dispersion and were set to 0 by memset
|
|
||||||
buf[12] = 49; // four-byte reference ID identifying
|
|
||||||
buf[13] = 0x4E;
|
|
||||||
buf[14] = 49;
|
|
||||||
buf[15] = 52;
|
|
||||||
|
|
||||||
mUdp.write(buf, NTP_PACKET_SIZE);
|
mUdp.write(buf, NTP_PACKET_SIZE);
|
||||||
}
|
}
|
||||||
|
@ -245,6 +264,8 @@ class AhoyNetwork {
|
||||||
AhoyWifiAp mAp;
|
AhoyWifiAp mAp;
|
||||||
DNSServer mDns;
|
DNSServer mDns;
|
||||||
|
|
||||||
|
IPAddress mNtpIp;
|
||||||
|
|
||||||
AsyncUDP mUdp; // for time server
|
AsyncUDP mUdp; // for time server
|
||||||
#if defined(ESP8266)
|
#if defined(ESP8266)
|
||||||
WiFiEventHandler wifiConnectHandler, wifiDisconnectHandler, wifiGotIPHandler;
|
WiFiEventHandler wifiConnectHandler, wifiDisconnectHandler, wifiGotIPHandler;
|
||||||
|
|
Loading…
Add table
Reference in a new issue