Socket (syslog,artnet) optim. Relay pwm thermo fix

This commit is contained in:
2022-01-03 20:42:46 +03:00
parent 1316835cad
commit 0c3108c620
20 changed files with 29210 additions and 21621 deletions

View File

@@ -346,7 +346,7 @@ float Item::getFloatArg(short n) //Return arg float or first array element if Ar
else if (itemArg->type == aJson_Float) return itemArg->valuefloat;
else return 0;
}
if ((itemArg->type == aJson_Array) && ( n < aJson.getArraySize(itemArg)))
{
aJsonObject * obj = aJson.getArrayItem(itemArg, n);
@@ -691,7 +691,8 @@ return false;
//Main routine to control Item
int Item::Ctrl(itemCmd cmd, char* subItem, bool allowRecursion)
{
{
uint32_t time=millis();
int suffixCode = cmd.getSuffix();
bool operation = isNotRetainingStatus();
@@ -721,7 +722,7 @@ int Item::Ctrl(itemCmd cmd, char* subItem, bool allowRecursion)
}
cmd.debugOut();
//debugSerial<<endl;
if (subItem && subItem[0] == '$') {debugSerial<<F("Skipped homie stuff")<<endl;return -4; }
if (!itemArr) return -1;
/// DELAYED COMMANDS processing
@@ -888,9 +889,9 @@ int Item::Ctrl(itemCmd cmd, char* subItem, bool allowRecursion)
{
if (allowRecursion && itemArg->type == aJson_Array && operation)
{
digGroup(itemArg,&cmd,subItem);
res=1;
digGroup(itemArg,&cmd,subItem);
}
res=1;
// Post-processing of group command - converting HALT,REST,XON,XOFF to conventional ON/OFF for status
switch (cmd.getCmd()) {

View File

@@ -406,7 +406,7 @@ else
cmd_parse((char *)payload);
return;// -4;
}
/*
if (subItem = strchr(itemName, '/'))
{
*subItem = 0;
@@ -421,6 +421,12 @@ else
//return
item.Ctrl((char *)payload,subItem);
} //valid item
*/
if (itemName[0]=='$') return;// -6; //Skipping homie stuff
Item item(itemName);
if (item.isValid()) item.Ctrl((char *)payload);
return;// -7;
}
@@ -489,7 +495,7 @@ void setupSyslog()
if (lanStatus<HAVE_IP_ADDRESS) return;
if (!root) return;
udpSyslogClient.begin(SYSLOG_LOCAL_SOCKET);
// udpSyslogClient.begin(SYSLOG_LOCAL_SOCKET);
udpSyslogArr = aJson.getObjectItem(root, "syslog");
if (udpSyslogArr && (n = aJson.getArraySize(udpSyslogArr))) {
@@ -498,6 +504,8 @@ void setupSyslog()
_inet_ntoa_r(Ethernet.localIP(),syslogDeviceHostname,sizeof(syslogDeviceHostname));
infoSerial<<F("Syslog params:")<<syslogServer<<":"<<syslogPort<<":"<<syslogDeviceHostname<<endl;
udpSyslogClient.begin(SYSLOG_LOCAL_SOCKET);
udpSyslog.server(syslogServer, syslogPort);
udpSyslog.deviceHostname(syslogDeviceHostname);
@@ -547,7 +555,7 @@ lan_status lanLoop() {
mdnsName[8+4]='\0';
if (!MDNS.begin(mdnsName))
errorSerial<<("Error setting up MDNS responder!")<<endl;
errorSerial<<F("Error setting up MDNS responder!")<<endl;
else infoSerial<<F("mDNS responder started: ")<<mdnsName<<F(".local")<<endl;
MDNS.addService("http", "tcp", OTA_PORT);
@@ -880,7 +888,9 @@ void ip_ready_config_loaded_connecting_to_broker() {
infoSerial<<F("Device Name:")<<deviceName<<endl;
#if defined (MDNS_ENABLE) && ! defined (WIFI_ENABLE)
mdns.setName(deviceName);
if (!mdns.setName(deviceName))
errorSerial<<("Error updating MDNS name!")<<endl;
else infoSerial<<F("mDNS name updated: ")<<deviceName<<F(".local")<<endl;
#endif
//debugSerial<<F("N:")<<n<<endl;
@@ -1059,6 +1069,7 @@ if (WiFi.status() == WL_CONNECTED) {
} else {
infoSerial<<F("Got IP address:");
printIPAddress(Ethernet.localIP());
infoSerial<<endl;
lanStatus = HAVE_IP_ADDRESS;
}
}//DHCP
@@ -1067,12 +1078,17 @@ if (WiFi.status() == WL_CONNECTED) {
char mdnsName[32] = "LightHub";
SetBytes(sysConf.mac+4,2,mdnsName+8);
mdns.begin(Ethernet.localIP(), mdnsName);
if(!mdns.begin(Ethernet.localIP(), mdnsName))
errorSerial<<F("Error setting up MDNS responder!")<<endl;
else infoSerial<<F("mDNS responder started.")<<endl;
char txtRecord[32] = "\x10mac=";
SetBytes(sysConf.mac,6,txtRecord+5);
strncat(mdnsName,"._http",sizeof(mdnsName));
mdns.addServiceRecord(mdnsName, OTA_PORT, MDNSServiceTCP, txtRecord);
if (!mdns.addServiceRecord(mdnsName, OTA_PORT, MDNSServiceTCP, txtRecord))
errorSerial<<("Error setting up service record!")<<endl;
else infoSerial<<F("Service record: ")<<mdnsName<<F(".local")<<endl;
#endif
#endif //Ethernet
@@ -1239,6 +1255,7 @@ setupSyslog();
if (artnetMaxCh>maxChannels) artnetMaxCh=maxChannels;
}
infoSerial<<F("Artnet start. Channels:")<<artnetMinCh<<F("-")<<artnetMaxCh<<endl;
artnetSetup();
artnetSetChans(artnetMinCh,artnetMaxCh);
//artnetInitialized=true;
}
@@ -1349,6 +1366,14 @@ void printConfigSummary() {
infoSerial<<F("\n1-wire ");
printBool(owArr);
#endif
#ifdef SYSLOG_ENABLE
infoSerial<<F("\nSyslog ");
printBool(syslogInitialized);
#endif
#ifdef _artnet
infoSerial<<F("\nArtnet ");
printBool(artnet);
#endif
infoSerial << endl;
infoSerial<<F("RAM=")<<freeRam()<<endl;
@@ -2003,9 +2028,9 @@ void setup_main() {
mqttClient.setCallback(mqttCallback);
#ifdef _artnet
artnetSetup();
#endif
//#ifdef _artnet
// artnetSetup();
//#endif
#if defined(WIFI_ENABLE) and not defined(WIFI_MANAGER_DISABLE)
// WiFiManager wifiManager;
@@ -2176,8 +2201,19 @@ infoSerial<<F("\n(+)MDNS");
#else
infoSerial<<F("\n(-)MDNS");
#endif
infoSerial<<endl;
#ifndef RELAY_DISABLE
infoSerial<<F("\n(+)PWM_RELAY");
#else
infoSerial<<F("\n(-)PWM_RELAY");
#endif
#ifndef MULTIVENT_DISABLE
infoSerial<<F("\n(+)MULTIVENT");
#else
infoSerial<<F("\n(-)MULTIVENT");
#endif
infoSerial<<endl;
// WDT_Disable( WDT ) ;
#if defined(__SAM3X8E__)

View File

@@ -22,7 +22,7 @@ void out_relay::getConfig()
inverted=true;
}
if(pin==0 || pin>=PINS_COUNT) pin=32;
period = item->getFloatArg(1)*1000.0;
if (!period) period = 5000UL;
@@ -37,13 +37,17 @@ int out_relay::Setup()
abstractOut::Setup();
debugSerial<<F("Relay-Out #")<<pin<<F(" init")<<endl;
if (!item ) return 0;
pinMode(pin, OUTPUT);
item->setExt(0);
digitalWrite(pin,INACTIVE);
if (item) item->setExt(0);
//if (item->getCmd()) item->setFlag(SEND_COMMAND);
//if (item->itemVal) item->setFlag(SEND_PARAMETERS);
driverStatus = CST_INITIALIZED;
if (!item->isActive())
{
item->setExt(millisNZ());
}
return 1;
}
@@ -60,7 +64,7 @@ int out_relay::Status()
return driverStatus;
}
const char action_P[] PROGMEM = "/action";
const char action_P[] PROGMEM = "action";
const char cooling_P[] PROGMEM = "cooling";
const char heating_P[] PROGMEM = "heating";
const char drying_P[] PROGMEM = "drying";
@@ -71,13 +75,13 @@ const char off_P[] PROGMEM = "off";
void out_relay::relay(bool state)
{
char subtopic[10];
char subtopic[10]="/";
char val[10];
digitalWrite(pin,(state)?ACTIVE:INACTIVE);
if (period<1000) return;
debugSerial<<F("Out ")<<pin<<F(" is ")<<(state)<<endl;
strcpy_P(subtopic,action_P);
strcat_P(subtopic,action_P);
short cmd=item->getCmd();
if (state)
switch(cmd)
@@ -103,21 +107,26 @@ if (state)
if (cmd==CMD_OFF) strcpy_P(val,off_P);
else strcpy_P(val,idle_P);
publishTopic(item->itemArr->name,val,subtopic);
debugSerial << F("pub action ") << publishTopic(item->itemArr->name,val,subtopic)<<F(":")<<item->itemArr->name<<subtopic<<F("=>")<<val<<endl;
}
bool getPinVal(uint8_t pin)
{return (0!=(*portOutputRegister( digitalPinToPort(pin) ) & digitalPinToBitMask(pin)));}
{
return (0!=(*portOutputRegister( digitalPinToPort(pin) ) & digitalPinToBitMask(pin)));
}
int out_relay::Poll(short cause)
{
if (!item) return 0;
itemCmd st;
st.loadItem(item);
int val = st.getPercents255();
short cmd = st.getCmd();
uint32_t timer = item->getExt();
bool needToOff = isTimeOver(timer,millis(),period*val/255);
if (timer && isTimeOver(timer,millis(),period))
{
@@ -125,12 +134,15 @@ uint32_t timer = item->getExt();
if (val && (getPinVal(pin) == INACTIVE)) relay(true);
}
else if (timer && (getPinVal(pin) == ACTIVE) && isTimeOver(timer,millis(),period*val/255))
else if (timer && (getPinVal(pin) == ACTIVE) && needToOff)
{
relay(false);
if (!item->isActive()) item->setExt(0);
}
else if (timer && val && (getPinVal(pin) == INACTIVE) && !needToOff)
relay(true);
return 0;
};
@@ -140,6 +152,7 @@ int out_relay::Ctrl(itemCmd cmd, char* subItem, bool toExecute)
{
debugSerial<<F("relayCtr: ");
cmd.debugOut();
if (! strcmp_P(subItem,action_P)) return 0;
int suffixCode;
if (cmd.isCommand()) suffixCode = S_CMD;
else suffixCode = cmd.getSuffix();
@@ -157,7 +170,7 @@ case S_SET:
if (!item->getExt())
{
item->setExt(millisNZ());
relay(true);
//relay(true);
}
}
else
@@ -180,7 +193,7 @@ case S_CMD:
if (!item->getExt())
{
item->setExt(millisNZ());
relay(true);
//relay(true);
}
return 1;

View File

@@ -558,7 +558,12 @@ RebootFunc();
bool isTimeOver(uint32_t timestamp, uint32_t currTime, uint32_t time, uint32_t modulo)
{
uint32_t endTime=(timestamp + time) % modulo;
uint32_t endTime;
if (!time) return true;
if (modulo) endTime = (timestamp + time) % modulo;
else endTime = timestamp + time;
return ((currTime>endTime) && (currTime <timestamp)) ||
((timestamp<endTime) && ((currTime>endTime) || (currTime <timestamp)));
}

View File

@@ -67,7 +67,7 @@ char* setTopic(char* buf, int8_t buflen, topicType tt, const char* suffix = NULL
void printUlongValueToStr(char *valstr, unsigned long value);
void scan_i2c_bus();
void softRebootFunc();
bool isTimeOver(uint32_t timestamp, uint32_t currTime, uint32_t time, uint32_t modulo = 0xFFFFFFFF);
bool isTimeOver(uint32_t timestamp, uint32_t currTime, uint32_t time, uint32_t modulo = 0);
//bool executeCommand(aJsonObject* cmd, int8_t toggle = -1, char* defCmd = NULL);
bool executeCommand(aJsonObject* cmd, int8_t toggle = -1);
bool executeCommand(aJsonObject* cmd, int8_t toggle, itemCmd _itemCmd, aJsonObject* defaultItem=NULL, aJsonObject* defaultEmit=NULL);