From 5b39d38d8644b67b492741da21b81fbbe17a1466 Mon Sep 17 00:00:00 2001 From: lumapu Date: Thu, 12 Oct 2023 07:56:49 +0200 Subject: [PATCH] included dev-control again --- src/app.cpp | 14 +++++++---- src/hm/CommQueue.h | 9 ++++--- src/hm/Communication.h | 55 +++++++++++++++++++++++++++--------------- src/hm/hmInverter.h | 12 +++++++++ 4 files changed, 62 insertions(+), 28 deletions(-) diff --git a/src/app.cpp b/src/app.cpp index 2255aa14..0e0aec5f 100644 --- a/src/app.cpp +++ b/src/app.cpp @@ -200,8 +200,7 @@ void app::onNetwork(bool gotIp) { mNetworkConnected = gotIp; ah::Scheduler::resetTicker(); regularTickers(); //reinstall regular tickers - //every(std::bind(&app::tickSend, this), mConfig->nrf.sendInterval, "tSend"); - once(std::bind(&app::tickSend, this), 20, "tSend"); + every(std::bind(&app::tickSend, this), mConfig->nrf.sendInterval, "tSend"); mMqttReconnect = true; mSunrise = 0; // needs to be set to 0, to reinstall sunrise and ivComm tickers! //once(std::bind(&app::tickNtpUpdate, this), 2, "ntp2"); @@ -406,12 +405,17 @@ void app::tickMidnight(void) { void app::tickSend(void) { DPRINTLN(DBG_INFO, "tickSend"); + if(!mIVCommunicationOn) { + DPRINTLN(DBG_WARN, F("Time not set or it is night time, therefore no communication to the inverter!")); + return; + } + for (uint8_t i = 0; i < MAX_NUM_INVERTERS; i++) { Inverter<> *iv = mSys.getInverterByPos(i); if(NULL != iv) { - mCommunication.add(iv, 0x01); - mCommunication.add(iv, 0x05); - mCommunication.add(iv, 0x0b, false); + iv->tickSend([this, iv](uint8_t cmd) { + mCommunication.add(iv, cmd); + }); }; } diff --git a/src/hm/CommQueue.h b/src/hm/CommQueue.h index 5f13d03d..9c9da707 100644 --- a/src/hm/CommQueue.h +++ b/src/hm/CommQueue.h @@ -17,11 +17,11 @@ class CommQueue { void addImportant(Inverter<> *iv, uint8_t cmd, bool delOnPop = true) { dec(&mRdPtr); - mQueue[mRdPtr] = queue_s(iv, cmd, delOnPop); + mQueue[mRdPtr] = queue_s(iv, cmd, delOnPop, true); } void add(Inverter<> *iv, uint8_t cmd, bool delOnPop = true) { - mQueue[mWrPtr] = queue_s(iv, cmd, delOnPop); + mQueue[mWrPtr] = queue_s(iv, cmd, delOnPop, false); inc(&mWrPtr); } @@ -31,10 +31,11 @@ class CommQueue { uint8_t cmd; uint8_t attempts; bool delOnPop; + bool isDevControl; uint32_t ts; queue_s() {} - queue_s(Inverter<> *i, uint8_t c, bool d) : - iv(i), cmd(c), attempts(5), ts(0), delOnPop(d) {} + queue_s(Inverter<> *i, uint8_t c, bool d, bool dev) : + iv(i), cmd(c), attempts(5), ts(0), delOnPop(d), isDevControl(dev) {} }; protected: diff --git a/src/hm/Communication.h b/src/hm/Communication.h index ecb28ce0..e65484e7 100644 --- a/src/hm/Communication.h +++ b/src/hm/Communication.h @@ -16,6 +16,11 @@ class Communication : public CommQueue<> { mTimestamp = timestamp; } + void addImportant(Inverter<> *iv, uint8_t cmd, bool delOnPop = true) { + mState = States::RESET; // cancel current operation + CommQueue::addImportant(iv, cmd, delOnPop); + } + void loop() { get([this](bool valid, const queue_s *q) { if(!valid) @@ -23,6 +28,8 @@ class Communication : public CommQueue<> { switch(mState) { case States::RESET: + if(millis() < mWaitTimeout) + return; mMaxFrameId = 0; for(uint8_t i = 0; i < MAX_PAYLOAD_ENTRIES; i++) { mLocalBuf[i].len = 0; @@ -32,8 +39,13 @@ class Communication : public CommQueue<> { case States::START: setTs(mTimestamp); - q->iv->radio->prepareDevInformCmd(q->iv, q->cmd, q->ts, q->iv->alarmLastId, false); - mWaitTimeout = millis() + 1500; + if(q->isDevControl) { + if(ActivePowerContr == q->cmd) + q->iv->powerLimitAck = false; + q->iv->radio->sendControlPacket(q->iv, q->cmd, q->iv->powerLimit, false); + } else + q->iv->radio->prepareDevInformCmd(q->iv, q->cmd, q->ts, q->iv->alarmLastId, false); + mWaitTimeout = millis() + 500; setAttempt(); mState = States::WAIT; break; @@ -51,13 +63,10 @@ class Communication : public CommQueue<> { q->iv->radioStatistics.rxFailNoAnser++; // got nothing if((IV_HMS == q->iv->ivGen) || (IV_HMT == q->iv->ivGen)) { q->iv->radio->switchFrequency(q->iv, HOY_BOOT_FREQ_KHZ, WORK_FREQ_KHZ); - mWaitTimeout = millis() + 2000; - mState = States::GAP; - break; - } else { - mState = States::RESET; - break; + mWaitTimeout = millis() + 1000; } + mState = States::RESET; + break; } States nextState = States::RESET; @@ -83,7 +92,7 @@ class Communication : public CommQueue<> { parseFrame(p); nextState = States::CHECK_PACKAGE; } else if (p->packet[0] == (TX_REQ_DEVCONTROL + ALL_FRAMES)) // response from dev control command - parseDevCtrl(p); + parseDevCtrl(p, q); } q->iv->radio->mBufCtrl.pop(); @@ -93,12 +102,6 @@ class Communication : public CommQueue<> { } break; - case States::GAP: - if(millis() < mWaitTimeout) - return; - mState = States::RESET; - break; - case States::CHECK_PACKAGE: if(0 == mMaxFrameId) { DPRINT_IVID(DBG_WARN, q->iv->id); @@ -175,8 +178,22 @@ class Communication : public CommQueue<> { f->rssi = p->rssi; } - inline void parseDevCtrl(packet_t *p) { - //if((p->packet[12] == ActivePowerContr) && (p->packet[13] == 0x00)) + inline void parseDevCtrl(packet_t *p, const queue_s *q) { + if((p->packet[12] != ActivePowerContr) || (p->packet[13] != 0x00)) + return; + bool accepted = true; + if((p->packet[10] == 0x00) && (p->packet[11] == 0x00)) + q->iv->powerLimitAck = true; + else + accepted = false; + + DPRINT_IVID(DBG_INFO, q->iv->id); + DBGPRINT(F(" has ")); + if(!accepted) DBGPRINT(F("not ")); + DBGPRINT(F("accepted power limit set point ")); + DBGPRINT(String(q->iv->powerLimit[0])); + DBGPRINT(F(" with PowerLimitControl ")); + DBGPRINTLN(String(q->iv->powerLimit[1])); } inline void compilePayload(const queue_s *q) { @@ -234,7 +251,7 @@ class Communication : public CommQueue<> { private: enum class States : uint8_t { - RESET, START, WAIT, CHECK_FRAMES, CHECK_PACKAGE, GAP + RESET, START, WAIT, CHECK_FRAMES, CHECK_PACKAGE }; typedef struct { @@ -246,7 +263,7 @@ class Communication : public CommQueue<> { private: States mState = States::RESET; uint32_t *mTimestamp; - uint32_t mWaitTimeout; + uint32_t mWaitTimeout = 0; std::array mLocalBuf; uint8_t mMaxFrameId; uint8_t mPayload[150]; diff --git a/src/hm/hmInverter.h b/src/hm/hmInverter.h index 238e80e4..8dfb5156 100644 --- a/src/hm/hmInverter.h +++ b/src/hm/hmInverter.h @@ -15,6 +15,7 @@ #include "../hms/hmsDefines.h" #include #include +#include #include "../config/settings.h" #include "radio.h" @@ -180,6 +181,17 @@ class Inverter { // TODO: cleanup } + void tickSend(std::function cb) { + if((alarmLastId != alarmMesIndex) && (alarmMesIndex != 0)) + cb(AlarmData); // get last alarms + else if(0 == getFwVersion()) + cb(InverterDevInform_All); // get firmware version + else if(0 == getHwVersion()) + cb(InverterDevInform_Simple); // get hardware version + else + cb(RealTimeRunData_Debug); // get live data + } + template void enqueCommand(uint8_t cmd) { _commandQueue.push(std::make_shared(cmd));