From 64d9d1313744543e6e4546a07765db2e2db11e6b Mon Sep 17 00:00:00 2001 From: Andrey Klimov Date: Sun, 2 May 2021 02:39:31 +0300 Subject: [PATCH] Multi-ventilation - alpha --- lighthub/item.cpp | 41 ++++++--- lighthub/item.h | 2 +- lighthub/modules/out_multivent.cpp | 141 ++++++++++++++++------------- lighthub/modules/out_multivent.h | 2 +- 4 files changed, 107 insertions(+), 79 deletions(-) diff --git a/lighthub/item.cpp b/lighthub/item.cpp index fdb1b4f..682d71a 100644 --- a/lighthub/item.cpp +++ b/lighthub/item.cpp @@ -648,6 +648,7 @@ int Item::Ctrl(itemCmd cmd, char* subItem, bool allowRecursion) bool chActive = (isActive()>0); bool toExecute = chActive; // execute if channel is active now bool scale100 = false; + debugSerial<0) prevCmd=CMD_ON; - - if (subitemCmd && subitemCmd != prevCmd) + if (!prevCmd && chActive) prevCmd=CMD_ON; + + if (subitemCmd) //Find some COMMAND in subitem + { + if (subitemCmd != prevCmd) { debugSerial<itemArg || !item->itemArg->type != aJson_Array) return; - gatesObj = aJson.getArrayItem(item->itemArg,1); + if (!item || !item->itemArg || item->itemArg->type != aJson_Object) return; + gatesObj = item->itemArg; } int out_Multivent::Setup() @@ -23,31 +23,36 @@ abstractOut::Setup(); //getConfig(); //Expand Argument storage to 2 -for (int i = aJson.getArraySize(item->itemArg); i < 2; i++) - aJson.addItemToArray(item->itemArg, aJson.createItem( (long int) 0)); +//for (int i = aJson.getArraySize(item->itemArg); i < 2; i++) +// aJson.addItemToArray(item->itemArg, aJson.createItem( (long int) 0)); //Allocate objects to store persistent data in config tree -if (gatesObj) +if (gatesObj /*&& aJson.getArraySize(item->itemArg)>=2*/) { aJsonObject * i = gatesObj->child; while (i) { + if (i->name && *i->name) + { aJsonObject * setObj = aJson.getObjectItem(i, "set"); if (!setObj) aJson.addNumberToObject(i, "set", (long int) -1); aJsonObject * cmdObj = aJson.getObjectItem(i, "cmd"); - if (!setObj) aJson.addNumberToObject(i, "cmd", (long int) -1); + if (!cmdObj) aJson.addNumberToObject(i, "cmd", (long int) -1); aJsonObject * outObj = aJson.getObjectItem(i, "out"); - if (!setObj) aJson.addNumberToObject(i, "out", (long int) -1); - + if (!outObj) aJson.addNumberToObject(i, "out", (long int) -1); + } i=i->next; } + debugSerial << F ("MultiVent init")<< endl; + driverStatus = CST_INITIALIZED; + return 1; } -debugSerial << F ("MultiVent init")<< endl; -driverStatus = CST_INITIALIZED; -return 1; +debugSerial << F ("MultiVent config failed")<< endl; +return 0; + } int out_Multivent::Stop() @@ -96,91 +101,97 @@ int suffixCode = cmd.getSuffix(); if (cmd.isCommand() && !suffixCode) suffixCode=S_CMD; //if some known command find, but w/o correct suffix - got it aJsonObject * i = NULL; -aJsonObject * currentGateObj = NULL; if (gatesObj) i = gatesObj->child; // Pass 1 - calculate summ air value, max value etc int activeV = 0; int totalV = 0; -int gateV = 0; -//char * gateEmit = NULL; -int gateCmd = 0; int maxV=0; +int maxRequestedV=0; +int maxPercent=0; + while (i) { + aJsonObject * setObj=aJson.getObjectItem(i, "set"); aJsonObject * cmdObj=aJson.getObjectItem(i, "cmd"); - if (!setObj || !cmdObj || setObj->type!=aJson_Int || cmdObj->type!=aJson_Int) continue; + if (setObj && cmdObj && setObj->type==aJson_Int && cmdObj->type==aJson_Int) + { - int V =aJson.getObjectItem(i,"V")->valueint; - int requestedV=0; + int V =aJson.getObjectItem(i,"V")->valueint; + int requestedV=0; - if (subItem && !strcmp (i->name,subItem)) - { - currentGateObj=i; - gateV=V; - // gateEmit=aJson.getObjectItem(i,"emit")->valuestring; - - gateCmd=cmdObj->valueint; - if (cmdObj && cmd.isCommand()) - { - cmdObj->valueint = cmd.getCmd(); - //publishTopic(i->name,cmdObj->valueint,"/set"); - item->SendStatusImmediate(SEND_COMMAND,i->name); - } - if (setObj && cmd.isValue()) - { - setObj->valueint = cmd.getPercents255(); - //publishTopic(i->name,setObj->valueint,"/set"); - item->SendStatusImmediate(SEND_PARAMETERS,i->name); - } - } + if (subItem && !strcmp (i->name,subItem)) + { + if (cmdObj && cmd.isCommand()) + { + cmdObj->valueint = cmd.getCmd(); + //publishTopic(i->name,cmdObj->valueint,"/set"); + if (isNotRetainingStatus()) item->SendStatusImmediate(cmd,SEND_COMMAND,i->name); + } + if (setObj && cmd.isValue()) + { + setObj->valueint = cmd.getPercents255(); + //publishTopic(i->name,setObj->valueint,"/set"); + if (isNotRetainingStatus()) item->SendStatusImmediate(cmd,SEND_PARAMETERS,i->name); + } + } - if (cmdObj->valueint != CMD_OFF) - { - requestedV=V*setObj->valueint; - activeV+=requestedV; - } - totalV+=V; - // numberGates++; - - if(requestedV>maxV) maxV=requestedV; + if (cmdObj->valueint != CMD_OFF) + { + requestedV=V*setObj->valueint; + activeV+=requestedV; + } + totalV+=V; + //if(requestedV>maxRequestedV) + if (setObj->valueint>maxPercent) + { + maxRequestedV=requestedV; + maxV=V; + maxPercent=setObj->valueint; + } + } i=i->next; } -int fanV=activeV/totalV; -debugSerial << F("Total V:")<child; //Pass 2: re-distribute airflow while (i) { -// nGate++; int V =aJson.getObjectItem(i,"V")->valueint; aJsonObject * outObj=aJson.getObjectItem(i, "out"); aJsonObject * setObj=aJson.getObjectItem(i, "set"); aJsonObject * cmdObj=aJson.getObjectItem(i, "cmd"); - if (!outObj || !setObj || !cmdObj || outObj->type!=aJson_Int || setObj->type!=aJson_Int || cmdObj->type!=aJson_Int) continue; - int out = 0; - if (cmdObj->valueint != CMD_OFF) - { - int requestedV=V*setObj->valueint; - int out = requestedV*255/maxV; - debugSerial<name,(" Req:")<valueint) + if (outObj && setObj && cmdObj && outObj->type==aJson_Int && setObj->type==aJson_Int && cmdObj->type==aJson_Int && V) + { + long int out = 0; + if (cmdObj->valueint != CMD_OFF && maxRequestedV) { - //report out - executeCommand(i,-1,itemCmd().Percents255(out)); - outObj->valueint=out; - } - + int requestedV=V*setObj->valueint; + out = (( long)requestedV*255L)/(( long)V)*( long)maxV/( long)maxRequestedV; + debugSerial<name<<(" Req:")<valueint) + { + //report out + executeCommand(i,-1,itemCmd().Percents255(out)); + outObj->valueint=out; + } + } i=i->next; } diff --git a/lighthub/modules/out_multivent.h b/lighthub/modules/out_multivent.h index 2e9fdb9..cc7a68f 100644 --- a/lighthub/modules/out_multivent.h +++ b/lighthub/modules/out_multivent.h @@ -6,7 +6,7 @@ #include "itemCmd.h" -static int8_t motorQuote = 0; +//static int8_t motorQuote = 0; class out_Multivent : public abstractOut { public: