mirror of
https://github.com/anklimov/lighthub
synced 2025-12-09 21:29:49 +03:00
core fixes, modbus, ac, pre-mapping, bins
This commit is contained in:
@@ -280,7 +280,7 @@ Item::Item(char *name) //Constructor
|
||||
buf[i]=0;
|
||||
itemArr = aJson.getObjectItem(items, buf);
|
||||
sub++;
|
||||
strncpy(defaultSubItem,sub,sizeof(defaultSubItem));
|
||||
strncpy(defaultSubItem,sub,sizeof(defaultSubItem)-1);
|
||||
defaultSuffixCode = retrieveCode (&pDefaultSubItem);
|
||||
if (!pDefaultSubItem) defaultSubItem[0] =0; //Zero string
|
||||
//debugSerial<<F("defaultSubItem: ")<<defaultSubItem<<F(" defaultSuffixCode:")<<defaultSuffixCode<<endl;
|
||||
@@ -761,7 +761,7 @@ int Item::Ctrl(itemCmd cmd, char* subItem, bool allowRecursion)
|
||||
|
||||
int fr = freeRam();
|
||||
|
||||
debugSerial<<F("RAM=")<<fr<<F(" Item=")<<itemArr->name<<F(" Sub=")<<subItem<<F(" Cmd:");
|
||||
debugSerial<<F("RAM=")<<fr<<F(" Item=")<<itemArr->name<<F(" Sub=")<<subItem<<F(" ");
|
||||
if (fr < 200)
|
||||
{
|
||||
errorSerial<<F("OutOfMemory!\n")<<endl;
|
||||
@@ -1281,6 +1281,15 @@ int Item::isActive() {
|
||||
}
|
||||
int cmd = getCmd();
|
||||
|
||||
if (driver) {
|
||||
short active = driver->isActive();
|
||||
if (active >= 0)
|
||||
{
|
||||
printActiveStatus(active);
|
||||
return active;
|
||||
}
|
||||
}
|
||||
// No driver - check command
|
||||
|
||||
if (itemType != CH_GROUP)
|
||||
// Simple check last command first
|
||||
@@ -1300,12 +1309,7 @@ int Item::isActive() {
|
||||
}
|
||||
|
||||
// Last time was not a command but parameters set. Looking inside
|
||||
if (driver) {
|
||||
bool active = driver->isActive();
|
||||
printActiveStatus(active);
|
||||
return active;
|
||||
}
|
||||
// No driver - check value
|
||||
|
||||
st.loadItem(this);
|
||||
|
||||
switch (itemType) {
|
||||
@@ -1431,7 +1435,7 @@ int Item::SendStatus(int sendFlags) {
|
||||
{
|
||||
char addrstr[48];
|
||||
char valstr[20] = "";
|
||||
char cmdstr[8] = "";
|
||||
char cmdstr[9] = "";
|
||||
st.debugOut();
|
||||
if (sendFlags & SEND_COMMAND)
|
||||
{
|
||||
@@ -1571,11 +1575,11 @@ int Item::SendStatus(int sendFlags) {
|
||||
strncat(addrstr, itemArr->name, sizeof(addrstr)-1);
|
||||
if (subItem)
|
||||
{
|
||||
strncat(addrstr, "/", sizeof(addrstr));
|
||||
strncat(addrstr, "/", sizeof(addrstr)-1);
|
||||
strncat(addrstr, subItem, sizeof(addrstr)-1);
|
||||
}
|
||||
strncat(addrstr, "/", sizeof(addrstr));
|
||||
strncat_P(addrstr, CMD_P, sizeof(addrstr));
|
||||
strncat(addrstr, "/", sizeof(addrstr)-1);
|
||||
strncat_P(addrstr, CMD_P, sizeof(addrstr)-1);
|
||||
|
||||
debugSerial<<F("Pub: ")<<addrstr<<F("->")<<cmdstr<<endl;
|
||||
if (mqttClient.connected() && !ethernetIdleCount)
|
||||
|
||||
@@ -168,7 +168,7 @@ class Item
|
||||
|
||||
|
||||
int checkFM();
|
||||
char defaultSubItem[10];
|
||||
char defaultSubItem[16];
|
||||
int defaultSuffixCode;
|
||||
|
||||
};
|
||||
|
||||
@@ -1129,15 +1129,39 @@ return false;
|
||||
}
|
||||
|
||||
|
||||
int itemCmd::doMapping(aJsonObject *mappingData)
|
||||
itemCmd itemCmd::doMapping(aJsonObject *mappingData)
|
||||
{
|
||||
if (isCommand())
|
||||
{
|
||||
switch (getCmd())
|
||||
{
|
||||
case CMD_AUTO:
|
||||
case CMD_ON:
|
||||
return itemCmd().Int((uint32_t)3);
|
||||
case CMD_OFF:
|
||||
return itemCmd().Int((uint32_t)0);
|
||||
case CMD_FAN:
|
||||
return itemCmd().Int((uint32_t)1);
|
||||
case CMD_HEAT:
|
||||
return itemCmd().Int((uint32_t)2);
|
||||
|
||||
return 0;
|
||||
case CMD_LOW:
|
||||
return itemCmd().Int((uint32_t)10);
|
||||
case CMD_MED:
|
||||
return itemCmd().Int((uint32_t)128);
|
||||
case CMD_HIGH:
|
||||
return itemCmd().Int((uint32_t)255);
|
||||
|
||||
default:
|
||||
return itemCmd().Int((uint32_t)0);
|
||||
}
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
int itemCmd::doReverseMapping (aJsonObject *mappingData)
|
||||
itemCmd itemCmd::doReverseMapping (aJsonObject *mappingData)
|
||||
|
||||
{
|
||||
return 0;
|
||||
return *this;
|
||||
}
|
||||
|
||||
int itemCmd::doMappingCmd(aJsonObject *mappingData)
|
||||
|
||||
@@ -220,8 +220,8 @@ public:
|
||||
itemCmd setChanType(short chanType);
|
||||
void debugOut();
|
||||
|
||||
int doMapping(aJsonObject *mappingData);
|
||||
int doReverseMapping (aJsonObject *mappingData);
|
||||
itemCmd doMapping(aJsonObject *mappingData);
|
||||
itemCmd doReverseMapping (aJsonObject *mappingData);
|
||||
int doMappingCmd(aJsonObject *mappingData);
|
||||
int doReverseMappingCmd (aJsonObject *mappingData);
|
||||
bool scale100();
|
||||
|
||||
@@ -186,11 +186,11 @@ byte getCRC(byte req[], size_t size){
|
||||
return crc;
|
||||
}
|
||||
|
||||
void SendData(byte req[], size_t size){
|
||||
void out_AC::SendData(byte req[], size_t size){
|
||||
AC_Serial.write(req, size - 1);
|
||||
AC_Serial.write(getCRC(req, size-1));
|
||||
//AC_Serial.flush();
|
||||
|
||||
item->setExt(millisNZ());
|
||||
debugSerial.print("AirCon<<");
|
||||
for (int i=0; i < size-1; i++)
|
||||
{
|
||||
@@ -485,9 +485,12 @@ int out_AC::Ctrl(itemCmd cmd, char* subItem , bool toExecute)
|
||||
data[10] = 77;
|
||||
data[11] = 95;
|
||||
AC_Serial.flush();
|
||||
uint32_t ts=item->getExt();
|
||||
while (ts && !isTimeOver(ts,millis(),100)) yield();
|
||||
SendData(data, sizeof(data)/sizeof(byte));
|
||||
//InsertData(data, sizeof(data)/sizeof(byte));
|
||||
//AC_Serial.flush();
|
||||
//item->setExt(millisNZ());
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
@@ -36,5 +36,6 @@ public:
|
||||
|
||||
protected:
|
||||
void InsertData(byte data[], size_t size);
|
||||
void SendData(byte req[], size_t size);
|
||||
};
|
||||
#endif
|
||||
|
||||
@@ -423,15 +423,12 @@ int out_Modbus::sendModbus(char * paramName, int32_t value, uint8_t regType)
|
||||
|
||||
switch(regType) {
|
||||
case PAR_U16:
|
||||
//res = node.writeSingleRegister(regObj->valueint,value);
|
||||
//break;
|
||||
case PAR_I16:
|
||||
case PAR_TENS:
|
||||
case PAR_100:
|
||||
res = node.writeSingleRegister(regObj->valueint,value);
|
||||
break;
|
||||
|
||||
|
||||
|
||||
break;
|
||||
case PAR_I32:
|
||||
case PAR_U32:
|
||||
@@ -440,11 +437,12 @@ int out_Modbus::sendModbus(char * paramName, int32_t value, uint8_t regType)
|
||||
break;
|
||||
|
||||
case PAR_U8L:
|
||||
res = node.writeSingleRegister(regObj->valueint,value & 0xFF);
|
||||
|
||||
case PAR_I8L:
|
||||
res = node.writeSingleRegister(regObj->valueint,value & 0xFF);
|
||||
break;
|
||||
|
||||
case PAR_U8H:
|
||||
case PAR_I8H:
|
||||
res = node.writeSingleRegister(regObj->valueint,(value & 0xFFFF)>> 8);
|
||||
break;
|
||||
}
|
||||
@@ -484,6 +482,7 @@ if (itemParametersObj && itemParametersObj->type ==aJson_Object)
|
||||
case 0: //fault
|
||||
execObj->subtype |= MB_SEND_ERROR;
|
||||
errorSerial<<F("MBus ")<<execObj->name<<F(" send error")<<endl;
|
||||
if ((execObj->subtype & 3) != MB_SEND_ATTEMPTS) execObj->subtype++;
|
||||
break;
|
||||
default: //param not found
|
||||
errorSerial<<F("MBus param ")<<execObj->name<<F(" not found")<<endl;
|
||||
@@ -507,6 +506,7 @@ if (itemParametersObj && itemParametersObj->type ==aJson_Object)
|
||||
while (execObj && execObj->type == aJson_Object)
|
||||
{
|
||||
if (execObj->subtype & MB_SEND_ERROR) execObj->subtype&=~ MB_SEND_ERROR;
|
||||
if ((execObj->subtype & 0x3) >= MB_SEND_ATTEMPTS) execObj->subtype&=~ MB_NEED_SEND;
|
||||
execObj=execObj->next;
|
||||
}
|
||||
}
|
||||
@@ -545,68 +545,39 @@ int out_Modbus::getChanType()
|
||||
return CH_MBUS;
|
||||
}
|
||||
|
||||
|
||||
//!Control unified Modbus item
|
||||
// Priority of selection sub-items control to:
|
||||
// 1. if defined standard suffix Code inside cmd
|
||||
// 2. custom textual subItem
|
||||
// 3. non-standard numeric suffix Code equal param id
|
||||
|
||||
int out_Modbus::Ctrl(itemCmd cmd, char* subItem, bool toExecute)
|
||||
int out_Modbus::sendItemCmd(aJsonObject *templateParamObj, itemCmd cmd)
|
||||
{
|
||||
int suffixCode = cmd.getSuffix();
|
||||
aJsonObject *templateParamObj = NULL;
|
||||
short mappedCmdVal = 0;
|
||||
|
||||
char * suffixStr = subItem;
|
||||
|
||||
// trying to find parameter in template with name == subItem (NB!! standard suffixes dint working here)
|
||||
if (subItem && strlen (subItem) && store) templateParamObj = aJson.getObjectItem(store->parameters, subItem);
|
||||
|
||||
if (!templateParamObj && store)
|
||||
{
|
||||
// Fallback - Trying to find template parameter where id == suffixCode
|
||||
templateParamObj = store->parameters->child;
|
||||
|
||||
while (templateParamObj)
|
||||
{
|
||||
//aJsonObject *regObj = aJson.getObjectItem(paramObj, "reg");
|
||||
aJsonObject *idObj = aJson.getObjectItem(templateParamObj, "id");
|
||||
if (idObj->type==aJson_Int && idObj->valueint == suffixCode)
|
||||
{
|
||||
suffixStr=templateParamObj->name;
|
||||
break;
|
||||
}
|
||||
|
||||
// aJsonObject *mapObj = aJson.getObjectItem(templateParamObj, "mapcmd");
|
||||
// if (mapObj && (mappedCmdVal = cmd.doReverseMappingCmd(mapObj))) break;
|
||||
|
||||
templateParamObj=templateParamObj->next;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if (templateParamObj)
|
||||
{
|
||||
int suffixCode = cmd.getSuffix();
|
||||
//bool isCommand = cmd.isCommand();
|
||||
if (!suffixCode) return 0;
|
||||
|
||||
char *suffixStr =templateParamObj->name;
|
||||
// We have find template for suffix or suffixCode
|
||||
long Value = 0;
|
||||
int8_t regType = PAR_I16;
|
||||
aJsonObject * typeObj = aJson.getObjectItem(templateParamObj, "type");
|
||||
aJsonObject * mapObj = aJson.getObjectItem(templateParamObj, "map");
|
||||
|
||||
if (typeObj && typeObj->type == aJson_String) regType=str2regSize(typeObj->valuestring);
|
||||
switch(regType) {
|
||||
case PAR_I16:
|
||||
case PAR_U16:
|
||||
case PAR_I32:
|
||||
case PAR_U32:
|
||||
case PAR_U8L:
|
||||
case PAR_U8H:
|
||||
Value=cmd.getInt();
|
||||
case PAR_U8H:
|
||||
case PAR_I8H:
|
||||
case PAR_I8L:
|
||||
|
||||
Value=cmd.doMapping(mapObj).getInt();
|
||||
break;
|
||||
case PAR_TENS:
|
||||
Value=cmd.getTens();
|
||||
Value=cmd.doMapping(mapObj).getTens();
|
||||
break;
|
||||
case PAR_100:
|
||||
Value=cmd.getTens_raw()*(100/TENS_BASE);
|
||||
Value=cmd.doMapping(mapObj).getTens_raw()*(100/TENS_BASE);
|
||||
}
|
||||
|
||||
debugSerial<<F("MB suffix:")<<suffixStr<< F(" Val: ")<<Value<<endl;
|
||||
@@ -643,49 +614,52 @@ if (itemParametersObj && itemParametersObj->type ==aJson_Object)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
return 1;
|
||||
}
|
||||
else return 0;
|
||||
}
|
||||
else errorSerial<<F("No template for ")<<subItem<<F("/")<<suffixCode<<endl;
|
||||
|
||||
//!Control unified Modbus item
|
||||
// Priority of selection sub-items control to:
|
||||
// 1. if defined standard suffix Code inside cmd
|
||||
// 2. custom textual subItem
|
||||
// 3. non-standard numeric suffix Code equal param id
|
||||
|
||||
if (cmd.isCommand() && !suffixCode) suffixCode=S_CMD; //if some known command find, but w/o correct suffix - got it
|
||||
|
||||
switch(suffixCode)
|
||||
int out_Modbus::Ctrl(itemCmd cmd, char* subItem, bool toExecute)
|
||||
{
|
||||
case S_NOTFOUND:
|
||||
// turn on and set
|
||||
toExecute = true;
|
||||
debugSerial<<F("Forced execution");
|
||||
case S_SET:
|
||||
//case S_ESET:
|
||||
if (!cmd.isValue()) return 0;
|
||||
if (!store) return -1;
|
||||
|
||||
//TODO
|
||||
return 1;
|
||||
//break;
|
||||
int suffixCode = cmd.getSuffix();
|
||||
aJsonObject *templateParamObj = NULL;
|
||||
int res = -1;
|
||||
|
||||
case S_CMD:
|
||||
switch (cmd.getCmd())
|
||||
{
|
||||
case CMD_ON:
|
||||
// trying to find parameter in template with name == subItem (NB!! standard suffixes dint working here)
|
||||
if (subItem && strlen (subItem))
|
||||
|
||||
{
|
||||
templateParamObj = aJson.getObjectItem(store->parameters, subItem);
|
||||
res= sendItemCmd(templateParamObj,cmd);
|
||||
}
|
||||
else
|
||||
|
||||
return 1;
|
||||
|
||||
case CMD_OFF:
|
||||
|
||||
return 1;
|
||||
|
||||
default:
|
||||
debugSerial<<F("Unknown cmd ")<<cmd.getCmd()<<endl;
|
||||
} //switch cmd
|
||||
|
||||
default:
|
||||
debugSerial<<F("Unknown suffix ")<<suffixCode<<endl;
|
||||
} //switch suffix
|
||||
|
||||
return 0;
|
||||
// No subitem, trying to find suffix with root item - (Trying to find template parameter where id == suffixCode)
|
||||
{
|
||||
templateParamObj = store->parameters->child;
|
||||
bool suffixFinded = false;
|
||||
while (templateParamObj)
|
||||
{
|
||||
aJsonObject *idObj = aJson.getObjectItem(templateParamObj, "id");
|
||||
if (idObj->type==aJson_Int && idObj->valueint == suffixCode)
|
||||
{
|
||||
res= sendItemCmd(templateParamObj,cmd);
|
||||
suffixFinded = true;
|
||||
}
|
||||
templateParamObj=templateParamObj->next;
|
||||
}
|
||||
if (!suffixFinded) errorSerial<<F("No template for ")<<subItem<<F(" or suffix ")<<suffixCode<<endl;
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
@@ -22,8 +22,9 @@ public:
|
||||
aJsonObject * parameters;
|
||||
};
|
||||
|
||||
#define MB_NEED_SEND 1
|
||||
#define MB_SEND_ERROR 2
|
||||
#define MB_NEED_SEND 8
|
||||
#define MB_SEND_ERROR 4
|
||||
#define MB_SEND_ATTEMPTS 3
|
||||
|
||||
|
||||
class out_Modbus : public abstractOut {
|
||||
@@ -46,5 +47,6 @@ protected:
|
||||
void pollModbus(aJsonObject * reg, int regType);
|
||||
void initLine();
|
||||
int sendModbus(char * paramName, int32_t value, uint8_t regType);
|
||||
int sendItemCmd(aJsonObject *templateParamObj, itemCmd cmd);
|
||||
};
|
||||
#endif
|
||||
|
||||
@@ -10,7 +10,7 @@
|
||||
class out_pwm : public colorChannel {
|
||||
public:
|
||||
|
||||
out_pwm(Item * _item):colorChannel(_item){};
|
||||
out_pwm(Item * _item):colorChannel(_item){numChannels=0;};
|
||||
int Setup() override;
|
||||
int Stop() override;
|
||||
int Status() override;
|
||||
|
||||
@@ -142,7 +142,7 @@ itemCmd getNumber(char **chan) {
|
||||
}
|
||||
}
|
||||
|
||||
if (!fractlen) val.Int(atol(*chan));
|
||||
if (!fractlen) val.Int((int32_t) atol(*chan));
|
||||
else if (fractlen<=TENS_FRACT_LEN && intlen+TENS_FRACT_LEN<=9)
|
||||
{
|
||||
long intpart = atol(*chan);
|
||||
@@ -162,7 +162,10 @@ itemCmd getNumber(char **chan) {
|
||||
|
||||
#if defined(ARDUINO_ARCH_ESP32) || defined(ESP8266)
|
||||
unsigned long freeRam ()
|
||||
{return system_get_free_heap_size();}
|
||||
{
|
||||
return esp_get_free_heap_size();//heap_caps_get_free_size();
|
||||
//return system_get_free_heap_size();
|
||||
}
|
||||
#endif
|
||||
|
||||
#if defined(__AVR__)
|
||||
|
||||
Reference in New Issue
Block a user