From ea6a9c155c533a9fdc781359a13e0b19e533dc1d Mon Sep 17 00:00:00 2001 From: lumapu Date: Sat, 17 Dec 2022 13:26:25 +0100 Subject: [PATCH] changed sunrise / sunset calculation, angle is now `-3.5` instead of original `-0.83` improved scheduler (removed -1 from `reload`) #483 improved reboot flag in `app.h` fixed #493 no MQTT payload once display is defined --- src/CHANGES.md | 6 ++++++ src/app.cpp | 23 ++--------------------- src/app.h | 32 +++++++++++++++----------------- src/defines.h | 2 +- src/hm/hmInverter.h | 19 +++++++++++-------- src/hm/hmRadio.h | 2 +- src/publisher/pubMqtt.h | 4 ++-- src/utils/helper.cpp | 2 +- src/utils/scheduler.h | 2 +- src/utils/sun.h | 2 +- src/web/RestApi.h | 8 ++++++-- src/web/html/serial.html | 21 +++++++++++++++------ 12 files changed, 62 insertions(+), 61 deletions(-) diff --git a/src/CHANGES.md b/src/CHANGES.md index 0fec9ed9..7ee05094 100644 --- a/src/CHANGES.md +++ b/src/CHANGES.md @@ -1,5 +1,11 @@ # Changelog +## 0.5.54 +* changed sunrise / sunset calculation, angle is now `-3.5` instead of original `-0.83` +* improved scheduler (removed -1 from `reload`) #483 +* improved reboot flag in `app.h` +* fixed #493 no MQTT payload once display is defined + ## 0.5.53 * Mono-Display: show values in offline mode #498 * improved wifi class #483 diff --git a/src/app.cpp b/src/app.cpp index 6a679e1a..5fec587e 100644 --- a/src/app.cpp +++ b/src/app.cpp @@ -30,8 +30,6 @@ void app::setup() { mSettings.getPtr(mConfig); DPRINTLN(DBG_INFO, F("Settings valid: ") + String((mSettings.getValid()) ? F("true") : F("false"))); - - everySec(std::bind(&app::tickSecond, this)); every(std::bind(&app::tickSend, this), mConfig->nrf.sendInterval); #if !defined(AP_ONLY) once(std::bind(&app::tickNtpUpdate, this), 2); @@ -44,6 +42,8 @@ void app::setup() { mSys = new HmSystemType(); mSys->enableDebug(); mSys->setup(mConfig->nrf.amplifierPower, mConfig->nrf.pinIrq, mConfig->nrf.pinCe, mConfig->nrf.pinCs); + mPayload.addListener(std::bind(&app::payloadEventListener, this, std::placeholders::_1)); + #if !defined(AP_ONLY) mMqtt.setup(&mConfig->mqtt, mConfig->sys.deviceName, mVersion, mSys, &mTimestamp, &mSunrise, &mSunset); @@ -62,7 +62,6 @@ void app::setup() { // when WiFi is in client mode, then enable mqtt broker #if !defined(AP_ONLY) if (mConfig->mqtt.broker[0] > 0) { - mPayload.addListener(std::bind(&PubMqttType::payloadEventListener, &mMqtt, std::placeholders::_1)); everySec(std::bind(&PubMqttType::tickerSecond, &mMqtt)); everyMin(std::bind(&PubMqttType::tickerMinute, &mMqtt)); mMqtt.setSubscriptionCb(std::bind(&app::mqttSubRxCb, this, std::placeholders::_1)); @@ -75,19 +74,10 @@ void app::setup() { everySec(std::bind(&WebType::tickSecond, &mWeb)); mApi.setup(this, mSys, mWeb.getWebSrvPtr(), mConfig); - /*mApi.registerCb(apiCbScanNetworks, std::bind(&app::scanAvailNetworks, this)); - #if !defined(AP_ONLY) - mApi.registerCb(apiCbMqttTxCnt, std::bind(&PubMqttType::getTxCnt, &mMqtt)); - mApi.registerCb(apiCbMqttRxCnt, std::bind(&PubMqttType::getRxCnt, &mMqtt)); - mApi.registerCb(apiCbMqttIsCon, std::bind(&PubMqttType::isConnected, &mMqtt)); - mApi.registerCb(apiCbMqttDiscvry, std::bind(&PubMqttType::sendDiscoveryConfig, &mMqtt)); - //mApi.registerCb(apiCbMqttDiscvry, std::bind(&app::setMqttDiscoveryFlag, this)); - #endif*/ // Plugins #if defined(ENA_NOKIA) || defined(ENA_SSD1306) mMonoDisplay.setup(mSys, &mTimestamp); - mPayload.addListener(std::bind(&MonoDisplayType::payloadEventListener, &mMonoDisplay, std::placeholders::_1)); everySec(std::bind(&MonoDisplayType::tickerSecond, &mMonoDisplay)); #endif @@ -107,11 +97,6 @@ void app::loop(void) { mWeb.loop(); - if (mFlagSendDiscoveryConfig) { - mFlagSendDiscoveryConfig = false; - mMqtt.sendDiscoveryConfig(); - } - mSys->Radio.loop(); yield(); @@ -253,10 +238,6 @@ void app::handleIntr(void) { void app::resetSystem(void) { snprintf(mVersion, 12, "%d.%d.%d", VERSION_MAJOR, VERSION_MINOR, VERSION_PATCH); - mShouldReboot = false; - mUpdateNtp = false; - mFlagSendDiscoveryConfig = false; - #ifdef AP_ONLY mTimestamp = 1; #else diff --git a/src/app.h b/src/app.h index ec708310..b7848224 100644 --- a/src/app.h +++ b/src/app.h @@ -94,7 +94,7 @@ class app : public IApp, public ah::Scheduler { } void setRebootFlag() { - mShouldReboot = true; + once(std::bind(&app::tickReboot, this), 1); } const char *getVersion() { @@ -118,7 +118,7 @@ class app : public IApp, public ah::Scheduler { } void setMqttDiscoveryFlag() { - mFlagSendDiscoveryConfig = true; + once(std::bind(&PubMqttType::sendDiscoveryConfig, &mMqtt), 1); } bool getMqttIsConnected() { @@ -153,32 +153,33 @@ class app : public IApp, public ah::Scheduler { void setTimestamp(uint32_t newTime) { DPRINTLN(DBG_DEBUG, F("setTimestamp: ") + String(newTime)); if(0 == newTime) - mUpdateNtp = true; + mWifi.getNtpTime(); else Scheduler::setTimestamp(newTime); } HmSystemType *mSys; - bool mShouldReboot; private: void resetSystem(void); + void payloadEventListener(uint8_t cmd) { + #if !defined(AP_ONLY) + mMqtt.payloadEventListener(cmd); + #endif + #if defined(ENA_NOKIA) || defined(ENA_SSD1306) + mMonoDisplay.payloadEventListener(cmd); + #endif + } + void mqttSubRxCb(JsonObject obj); void setupLed(void); void updateLed(void); - void tickSecond(void) { - if (mShouldReboot) { - DPRINTLN(DBG_INFO, F("Rebooting...")); - ESP.restart(); - } - - if (mUpdateNtp) { - mUpdateNtp = false; - mWifi.getNtpTime(); - } + void tickReboot(void) { + DPRINTLN(DBG_INFO, F("Rebooting...")); + ESP.restart(); } void tickNtpUpdate(void); @@ -206,9 +207,6 @@ class app : public IApp, public ah::Scheduler { DPRINTLN(DBG_VERBOSE, F(" - frag: ") + String(frag)); } - bool mUpdateNtp; - bool mFlagSendDiscoveryConfig; - bool mShowRebootRequest; ahoywifi mWifi; diff --git a/src/defines.h b/src/defines.h index 04a8dd88..cde85689 100644 --- a/src/defines.h +++ b/src/defines.h @@ -13,7 +13,7 @@ //------------------------------------- #define VERSION_MAJOR 0 #define VERSION_MINOR 5 -#define VERSION_PATCH 53 +#define VERSION_PATCH 54 //------------------------------------- typedef struct { diff --git a/src/hm/hmInverter.h b/src/hm/hmInverter.h index 2494a979..8c1fccc8 100644 --- a/src/hm/hmInverter.h +++ b/src/hm/hmInverter.h @@ -109,7 +109,6 @@ class Inverter { uint8_t id; // unique id uint8_t type; // integer which refers to inverter type uint16_t alarmMesIndex; // Last recorded Alarm Message Index - uint16_t fwVersion; // Firmware Version from Info Command Request uint16_t powerLimit[2]; // limit power output float actPowerLimit; // actual power limit uint8_t devControlCmd; // carries the requested cmd @@ -130,7 +129,6 @@ class Inverter { devControlRequest = false; devControlCmd = InitDataState; initialized = false; - fwVersion = 0; lastAlarmMsg = "nothing"; alarmMesIndex = 0; } @@ -153,6 +151,7 @@ class Inverter { } void clearCmdQueue() { + DPRINTLN(DBG_INFO, F("clearCmdQueue")); while (!_commandQueue.empty()) { // Will destroy CommandAbstract Class Object (?) _commandQueue.pop(); @@ -161,7 +160,7 @@ class Inverter { uint8_t getQueuedCmd() { if (_commandQueue.empty()) { - if (fwVersion == 0) + if (getFwVersion() == 0) enqueCommand(InverterDevInform_All); enqueCommand(RealTimeRunData_Debug); if (actPowerLimit == 0xffff) @@ -264,11 +263,7 @@ class Inverter { } else if (rec->assign == InfoAssignment) { DPRINTLN(DBG_DEBUG, "add info"); - // get at least the firmware version and save it to the inverter object - if (getPosByChFld(0, FLD_FW_VERSION, rec) == pos){ - fwVersion = rec->record[pos]; - DPRINT(DBG_DEBUG, F("Inverter FW-Version: ") + String(fwVersion)); - } + // eg. fw version ... } else if (rec->assign == SystemConfigParaAssignment) { DPRINTLN(DBG_DEBUG, "add config"); @@ -295,6 +290,8 @@ class Inverter { DPRINTLN(DBG_VERBOSE, F("hmInverter.h:getValue")); if(NULL == rec) return 0; + if(pos > rec->length) + return 0; return rec->record[pos]; } @@ -323,6 +320,12 @@ class Inverter { return false; } + uint16_t getFwVersion() { + record_t<> *rec = getRecordStruct(InverterDevInform_All); + uint8_t pos = getPosByChFld(CH0, FLD_FW_VERSION, rec); + return getValue(pos, rec); + } + uint32_t getLastTs(record_t<> *rec) { DPRINTLN(DBG_VERBOSE, F("hmInverter.h:getLastTs")); return rec->ts; diff --git a/src/hm/hmRadio.h b/src/hm/hmRadio.h index 90eaa40f..a28a6f04 100644 --- a/src/hm/hmRadio.h +++ b/src/hm/hmRadio.h @@ -221,7 +221,7 @@ class HmRadio { } void sendTimePacket(uint64_t invId, uint8_t cmd, uint32_t ts, uint16_t alarmMesId) { - DPRINTLN(DBG_VERBOSE, F("sendTimePacket")); + DPRINTLN(DBG_INFO, F("sendTimePacket ") + String(cmd, HEX)); sendCmdPacket(invId, TX_REQ_INFO, ALL_FRAMES, false); mTxBuf[10] = cmd; // cid mTxBuf[11] = 0x00; diff --git a/src/publisher/pubMqtt.h b/src/publisher/pubMqtt.h index dfea49a1..cf98eec2 100644 --- a/src/publisher/pubMqtt.h +++ b/src/publisher/pubMqtt.h @@ -140,8 +140,8 @@ class PubMqtt { if(!mClient.connected()) return; - char topic[MQTT_TOPIC_LEN + 2]; - snprintf(topic, (MQTT_TOPIC_LEN + 2), "%s/%s", mCfgMqtt->topic, subTopic); + char topic[(MQTT_TOPIC_LEN << 1) + 2]; + snprintf(topic, ((MQTT_TOPIC_LEN << 1) + 2), "%s/%s", mCfgMqtt->topic, subTopic); if(addTopic) mClient.publish(topic, QOS_0, retained, payload); else diff --git a/src/utils/helper.cpp b/src/utils/helper.cpp index cf0e13d3..bc027bae 100644 --- a/src/utils/helper.cpp +++ b/src/utils/helper.cpp @@ -9,7 +9,7 @@ namespace ah { void ip2Arr(uint8_t ip[], const char *ipStr) { memset(ip, 0, 4); char *tmp = new char[strlen(ipStr)+1]; - strncpy(tmp, ipStr, strlen(ipStr)+1); + snprintf(tmp, strlen(ipStr), ipStr); char *p = strtok(tmp, "."); uint8_t i = 0; while(NULL != p) { diff --git a/src/utils/scheduler.h b/src/utils/scheduler.h index fc684ba9..331f3e73 100644 --- a/src/utils/scheduler.h +++ b/src/utils/scheduler.h @@ -111,7 +111,7 @@ namespace ah { if(0 == p->d.reload) p = mStack.rem(p); else { - p->d.timeout = p->d.reload - 1; + p->d.timeout = p->d.reload; p = mStack.get(p); } } diff --git a/src/utils/sun.h b/src/utils/sun.h index c66149c2..03a91f70 100644 --- a/src/utils/sun.h +++ b/src/utils/sun.h @@ -25,7 +25,7 @@ namespace ah { // Declination of the sun double delta = ASIN(SIN(lambda) * SIN(23.44)); // Hour angle - double omega = ACOS((SIN(-0.83) - SIN(lat) * SIN(delta)) / (COS(lat) * COS(delta))); + double omega = ACOS((SIN(-3.5) - SIN(lat) * SIN(delta)) / (COS(lat) * COS(delta))); //(SIN(-0.83) // Calculate sunrise and sunset double Jrise = Jtransit - omega / 360; double Jset = Jtransit + omega / 360; diff --git a/src/web/RestApi.h b/src/web/RestApi.h index ef87281f..21650081 100644 --- a/src/web/RestApi.h +++ b/src/web/RestApi.h @@ -251,7 +251,7 @@ class RestApi { obj2[F("name")] = String(iv->config->name); obj2[F("serial")] = String(iv->config->serial.u64, HEX); obj2[F("channels")] = iv->channels; - obj2[F("version")] = String(iv->fwVersion); + obj2[F("version")] = String(iv->getFwVersion()); for(uint8_t j = 0; j < iv->channels; j ++) { obj2[F("ch_max_power")][j] = iv->config->chMaxPwr[j]; @@ -357,7 +357,7 @@ class RestApi { invObj[F("enabled")] = (bool)iv->config->enabled; invObj[F("id")] = i; invObj[F("name")] = String(iv->config->name); - invObj[F("version")] = String(iv->fwVersion); + invObj[F("version")] = String(iv->getFwVersion()); invObj[F("is_avail")] = iv->isAvailable(mApp->getTimestamp(), rec); invObj[F("is_producing")] = iv->isProducing(mApp->getTimestamp(), rec); invObj[F("ts_last_success")] = iv->getLastTs(rec); @@ -501,6 +501,10 @@ class RestApi { iv->devControlCmd = ActivePowerContr; iv->devControlRequest = true; } + else if(F("dev") == jsonIn[F("cmd")]) { + DPRINTLN(DBG_INFO, F("dev cmd")); + iv->enqueCommand(jsonIn[F("val")].as()); + } else { jsonOut[F("error")] = F("unknown cmd: '") + jsonIn["cmd"].as() + "'"; return false; diff --git a/src/web/html/serial.html b/src/web/html/serial.html index ccbd1156..2064da38 100644 --- a/src/web/html/serial.html +++ b/src/web/html/serial.html @@ -35,12 +35,6 @@
-
- - - -
-



@@ -58,6 +52,13 @@

+
+ + + + +
+

Ctrl result: n/a

@@ -185,6 +186,14 @@ getAjax("/api/ctrl", ctrlCb, "POST", JSON.stringify(obj)); }); + document.getElementById("alarminfo").addEventListener("click", function() { + var obj = new Object(); + obj.id = get_selected_iv(); + obj.cmd = "dev"; + obj.val = 0x11; + getAjax("/api/ctrl", ctrlCb, "POST", JSON.stringify(obj)); + }); + document.getElementById("sendpwrlim").addEventListener("click", function() { var val = parseInt(document.getElementsByName('pwrlimval')[0].value); var cmd = document.getElementsByName('pwrlimctrl')[0].value;