Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MI - dev control plus debug (#4) #788

Merged
merged 5 commits into from
Mar 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions src/hm/hmInverter.h
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,10 @@ class Inverter {
template <typename T>
void enqueCommand(uint8_t cmd) {
_commandQueue.push(std::make_shared<T>(cmd));
DPRINTLN(DBG_INFO, F("(#") + String(id) + F(") enqueuedCmd: 0x") + String(cmd, HEX));
DPRINTHEAD(DBG_INFO, id);
//DBGPRINTLN(F("enqueuedCmd: 0x") + String(cmd, HEX));
DBGPRINT_TXT(TXT_ENQUCMD);
DBGHEXLN(cmd);
}

void setQueuedCmdFinished() {
Expand All @@ -154,7 +157,8 @@ class Inverter {
}

void clearCmdQueue() {
DPRINTLN(DBG_INFO, F("clearCmdQueue"));
DPRINTHEAD(DBG_INFO, id);
DBGPRINTLN_TXT(TXT_CLRQUE);
while (!_commandQueue.empty()) {
// Will destroy CommandAbstract Class Object (?)
_commandQueue.pop();
Expand Down
88 changes: 53 additions & 35 deletions src/hm/hmPayload.h
Original file line number Diff line number Diff line change
Expand Up @@ -117,11 +117,13 @@ class HmPayload {

iv->setQueuedCmdFinished(); // command failed
if (mSerialDebug)
DPRINTLN(DBG_INFO, F("enqueued cmd failed/timeout"));
DPRINTHEAD(DBG_INFO, iv->id);
DBGPRINTLN_TXT(TXT_TIMEOUT);
//DBGPRINTLN(F("enqueued cmd failed/timeout"));
if (mSerialDebug) {
DPRINT(DBG_INFO, F("(#"));
DBGPRINT(String(iv->id));
DBGPRINT(F(") no Payload received! (retransmits: "));
DPRINTHEAD(DBG_INFO, iv->id);
//DBGPRINT(F("no Payload received! (retransmits: "));
DBGPRINT_TXT(TXT_NOPYLD);
DBGPRINT(String(mPayload[iv->id].retransmits));
DBGPRINTLN(F(")"));
}
Expand All @@ -134,17 +136,17 @@ class HmPayload {

yield();
if (mSerialDebug) {
DPRINT(DBG_INFO, F("(#"));
DBGPRINT(String(iv->id));
DBGPRINT(F(") Requesting Inv SN "));
DPRINTHEAD(DBG_INFO, iv->id);
//DBGPRINT(F("Requesting Inv SN "));
DBGPRINT_TXT(TXT_INVSERNO);
DBGPRINTLN(String(iv->config->serial.u64, HEX));
}

if (iv->getDevControlRequest()) {
if (mSerialDebug) {
DPRINT(DBG_INFO, F("(#"));
DBGPRINT(String(iv->id));
DBGPRINT(F(") Devcontrol request 0x"));
DPRINTHEAD(DBG_INFO, iv->id);
//DBGPRINT(F("Devcontrol request 0x"));
DBGPRINT_TXT(TXT_DEVCTRL);
DBGPRINT(String(iv->devControlCmd, HEX));
DBGPRINT(F(" power limit "));
DBGPRINTLN(String(iv->powerLimit[0]));
Expand All @@ -155,9 +157,9 @@ class HmPayload {
//iv->enqueCommand<InfoCommand>(SystemConfigPara); // read back power limit
} else {
uint8_t cmd = iv->getQueuedCmd();
DPRINT(DBG_INFO, F("(#"));
DBGPRINT(String(iv->id));
DBGPRINT(F(") prepareDevInformCmd 0x"));
DPRINTHEAD(DBG_INFO, iv->id);
//DBGPRINT(F("prepareDevInformCmd 0x"));
DBGPRINT_TXT(TXT_GDEVINF);
DBGPRINTLN(String(cmd, HEX));
mSys->Radio.prepareDevInformCmd(iv->radioId.u64, cmd, mPayload[iv->id].ts, iv->alarmMesIndex, false);
mPayload[iv->id].txCmd = cmd;
Expand Down Expand Up @@ -200,14 +202,13 @@ class HmPayload {
mApp->setMqttPowerLimitAck(iv);
else
ok = false;
DPRINT(DBG_INFO, F("(#"));
DBGPRINT(String(iv->id));
DBGPRINT(F(" has "));
DPRINTHEAD(DBG_INFO,iv->id);
DBGPRINT(F("has "));
if(!ok) DBGPRINT(F("not "));
DBGPRINT(F("accepted power limit set point "));
DBGPRINT(String(iv->powerLimit[0]));
DBGPRINT(F(" with PowerLimitControl "));
DBGPRINT(String(iv->powerLimit[1]));
DBGPRINTLN(String(iv->powerLimit[1]));

iv->clearCmdQueue();
iv->enqueCommand<InfoCommand>(SystemConfigPara); // read back power limit
Expand Down Expand Up @@ -240,10 +241,14 @@ class HmPayload {
mPayload[iv->id].retransmits++;
if (iv->devControlCmd == Restart || iv->devControlCmd == CleanState_LockAndAlarm) {
// This is required to prevent retransmissions without answer.
DPRINTLN(DBG_INFO, F("Prevent retransmit on Restart / CleanState_LockAndAlarm..."));
//DPRINTLN(DBG_INFO, F("Prevent retransmit on Restart / CleanState_LockAndAlarm..."));
DPRINTHEAD(DBG_INFO, iv->id);
DBGPRINTLN_TXT(TXT_PREVSND);
mPayload[iv->id].retransmits = mMaxRetrans;
} else if(iv->devControlCmd == ActivePowerContr) {
DPRINTLN(DBG_INFO, F("retransmit power limit"));
//DPRINTLN(DBG_INFO, F("retransmit power limit"));
DPRINTHEAD(DBG_INFO, iv->id);
DBGPRINTLN_TXT(TXT_RESPLIM);
mSys->Radio.sendControlPacket(iv->radioId.u64, iv->devControlCmd, iv->powerLimit, true);
} else {
if(false == mPayload[iv->id].gotFragment) {
Expand All @@ -253,16 +258,18 @@ class HmPayload {
DPRINTLN(DBG_INFO, F("(#") + String(iv->id) + F(") prepareDevInformCmd 0x") + String(mPayload[iv->id].txCmd, HEX));
mSys->Radio.prepareDevInformCmd(iv->radioId.u64, mPayload[iv->id].txCmd, mPayload[iv->id].ts, iv->alarmMesIndex, true);
*/
DPRINT(DBG_INFO, F("(#"));
DBGPRINT(String(iv->id));
DBGPRINTLN(F(") nothing received"));
DPRINTHEAD(DBG_INFO, iv->id);
DBGPRINTLN_TXT(TXT_NOPYLD2);
mPayload[iv->id].retransmits = mMaxRetrans;
} else {
for (uint8_t i = 0; i < (mPayload[iv->id].maxPackId - 1); i++) {
if (mPayload[iv->id].len[i] == 0) {
DPRINT(DBG_WARN, F("Frame "));
DPRINTHEAD(DBG_WARN,iv->id);
DBGPRINT(F("Frame "));
DBGPRINT(String(i + 1));
DBGPRINTLN(F(" missing: Request Retransmit"));
//DBGPRINTLN(F(" missing: Request Retransmit"));
DBGPRINT_TXT(TXT_REQRETR);
DBGPRINT(F("\r\n"));
mSys->Radio.sendCmdPacket(iv->radioId.u64, TX_REQ_INFO, (SINGLE_FRAME + i), true);
break; // only request retransmit one frame per loop
}
Expand All @@ -275,20 +282,29 @@ class HmPayload {
} else if(!crcPass && pyldComplete) { // crc error on complete Payload
if (mPayload[iv->id].retransmits < mMaxRetrans) {
mPayload[iv->id].retransmits++;
DPRINTLN(DBG_WARN, F("CRC Error: Request Complete Retransmit"));
DPRINTHEAD(DBG_WARN,iv->id);
DBGPRINTLN_TXT(TXT_CRCERR);
mPayload[iv->id].txCmd = iv->getQueuedCmd();
DPRINT(DBG_INFO, F("(#"));
DBGPRINT(String(iv->id));
DBGPRINT(F(") prepareDevInformCmd 0x"));
DBGPRINTLN(String(mPayload[iv->id].txCmd, HEX));
DPRINTHEAD(DBG_INFO,iv->id);
DBGPRINT_TXT(TXT_GDEVINF);
//DBGPRINTLN(String(mPayload[iv->id].txCmd, HEX));
DBGHEXLN(mPayload[iv->id].txCmd);
mSys->Radio.prepareDevInformCmd(iv->radioId.u64, mPayload[iv->id].txCmd, mPayload[iv->id].ts, iv->alarmMesIndex, true);
}
} else { // payload complete
DPRINT(DBG_INFO, F("procPyld: cmd: 0x"));
DBGPRINTLN(String(mPayload[iv->id].txCmd, HEX));
//DBGPRINTLN(String(mPayload[iv->id].txCmd, HEX));
//DPRINT_INIT(DBG_INFO,TXT_PPYDCMD);
DBGHEXLN(mPayload[iv->id].txCmd);
DPRINT(DBG_INFO, F("procPyld: txid: 0x"));
DBGPRINTLN(String(mPayload[iv->id].txId, HEX));
DPRINTLN(DBG_DEBUG, F("procPyld: max: ") + String(mPayload[iv->id].maxPackId));
//DPRINT_INIT(DBG_INFO,TXT_PPYDTXI);
//DBGPRINTLN(String(mPayload[iv->id].txId, HEX));
DBGHEXLN(mPayload[iv->id].txId);
//DPRINT_INIT(DBG_DEBUG,TXT_PPYDMAX);
//DBGPRINTLN(String(mPayload[iv->id].maxPackId));
DPRINT(DBG_DEBUG, F("procPyld: max: ") + String(mPayload[iv->id].maxPackId));


record_t<> *rec = iv->getRecordStruct(mPayload[iv->id].txCmd); // choose the parser
mPayload[iv->id].complete = true;

Expand All @@ -312,7 +328,7 @@ class HmPayload {
}

if (NULL == rec) {
DPRINTLN(DBG_ERROR, F("record is NULL!"));
DPRINTLN_TXT(DBG_ERROR, TXT_NULLREC);
} else if ((rec->pyldLen == payloadLen) || (0 == rec->pyldLen)) {
if (mPayload[iv->id].txId == (TX_REQ_INFO + ALL_FRAMES))
mStat->rxSuccess++;
Expand Down Expand Up @@ -365,6 +381,8 @@ class HmPayload {

bool build(uint8_t id, bool *complete) {
DPRINTLN(DBG_VERBOSE, F("build"));
//DPRINTLN_TXT(DBG_VERBOSE, TXT_BUILD);

uint16_t crc = 0xffff, crcRcv = 0x0000;
if (mPayload[id].maxPackId > MAX_PAYLOAD_ENTRIES)
mPayload[id].maxPackId = MAX_PAYLOAD_ENTRIES;
Expand Down Expand Up @@ -393,8 +411,8 @@ class HmPayload {
}

void reset(uint8_t id) {
DPRINT(DBG_INFO, "resetPayload: id: ");
DBGPRINTLN(String(id));
DPRINTHEAD(DBG_INFO,id);
DBGPRINTLN_TXT(TXT_RSTPYLD);
memset(mPayload[id].len, 0, MAX_PAYLOAD_ENTRIES);
mPayload[id].txCmd = 0;
mPayload[id].gotFragment = false;
Expand Down
37 changes: 29 additions & 8 deletions src/hm/hmRadio.h
Original file line number Diff line number Diff line change
Expand Up @@ -175,18 +175,39 @@ class HmRadio {
mSerialDebug = true;
}

void sendControlPacket(uint64_t invId, uint8_t cmd, uint16_t *data, bool isRetransmit) {
void sendControlPacket(uint64_t invId, uint8_t cmd, uint16_t *data, bool isRetransmit, bool isNoMI = true) {
DPRINT(DBG_INFO, F("sendControlPacket cmd: 0x"));
DBGPRINTLN(String(cmd, HEX));
initPacket(invId, TX_REQ_DEVCONTROL, SINGLE_FRAME);
uint8_t cnt = 10;
mTxBuf[cnt++] = cmd; // cmd -> 0 on, 1 off, 2 restart, 11 active power, 12 reactive power, 13 power factor
mTxBuf[cnt++] = 0x00;
if(cmd >= ActivePowerContr && cmd <= PFSet) { // ActivePowerContr, ReactivePowerContr, PFSet
mTxBuf[cnt++] = ((data[0] * 10) >> 8) & 0xff; // power limit
mTxBuf[cnt++] = ((data[0] * 10) ) & 0xff; // power limit
mTxBuf[cnt++] = ((data[1] ) >> 8) & 0xff; // setting for persistens handlings
mTxBuf[cnt++] = ((data[1] ) ) & 0xff; // setting for persistens handling
if (isNoMI) {
mTxBuf[cnt++] = cmd; // cmd -> 0 on, 1 off, 2 restart, 11 active power, 12 reactive power, 13 power factor
mTxBuf[cnt++] = 0x00;
if(cmd >= ActivePowerContr && cmd <= PFSet) { // ActivePowerContr, ReactivePowerContr, PFSet
mTxBuf[cnt++] = ((data[0] * 10) >> 8) & 0xff; // power limit
mTxBuf[cnt++] = ((data[0] * 10) ) & 0xff; // power limit
mTxBuf[cnt++] = ((data[1] ) >> 8) & 0xff; // setting for persistens handlings
mTxBuf[cnt++] = ((data[1] ) ) & 0xff; // setting for persistens handling
}
} else { //MI 2nd gen. specific
switch (cmd) {
case TurnOn:
mTxBuf[9] = 0x55;
mTxBuf[10] = 0xaa;
break;
case TurnOff:
mTxBuf[9] = 0xaa;
mTxBuf[10] = 0x55;
break;
case ActivePowerContr:
cnt++;
mTxBuf[9] = 0x5a;
mTxBuf[10] = 0x5a;
mTxBuf[11] = data[0]; // power limit
break;
default:
return;
}
}
sendPacket(invId, cnt, isRetransmit, true);
}
Expand Down
Loading