homie topic strucrure correction

This commit is contained in:
2019-03-04 15:47:12 +03:00
parent ed0330307e
commit bdac63fa8a
4 changed files with 39 additions and 40 deletions

View File

@@ -198,7 +198,7 @@ boolean Item::getEnableCMD(int delta) {
#define MAXCTRLPAR 3 #define MAXCTRLPAR 3
int Item::Ctrl(char * payload, boolean send){ int Item::Ctrl(char * payload, boolean send, char * subItem){
if (!payload) return 0; if (!payload) return 0;
// debugSerial<<F("'")<<payload<<F("'")<<endl; // debugSerial<<F("'")<<payload<<F("'")<<endl;
int cmd = txt2cmd(payload); int cmd = txt2cmd(payload);
@@ -210,7 +210,7 @@ int Item::Ctrl(char * payload, boolean send){
while (payload && i < 3) while (payload && i < 3)
Par[i++] = getInt((char **) &payload); Par[i++] = getInt((char **) &payload);
return Ctrl(0, i, Par, send); return Ctrl(0, i, Par, send, subItem);
} }
break; break;
@@ -227,7 +227,7 @@ int Item::Ctrl(char * payload, boolean send){
Par[0] = map(hsv.h, 0, 255, 0, 365); Par[0] = map(hsv.h, 0, 255, 0, 365);
Par[1] = map(hsv.s, 0, 255, 0, 100); Par[1] = map(hsv.s, 0, 255, 0, 100);
Par[2] = map(hsv.v, 0, 255, 0, 100); Par[2] = map(hsv.v, 0, 255, 0, 100);
return Ctrl(0, 3, Par, send); return Ctrl(0, 3, Par, send, subItem);
} }
break; break;
} }
@@ -236,26 +236,26 @@ int Item::Ctrl(char * payload, boolean send){
// if (item.getEnableCMD(500) || lanStatus == 4) // if (item.getEnableCMD(500) || lanStatus == 4)
return Ctrl(cmd, 0, NULL, return Ctrl(cmd, 0, NULL,
send); //Accept ON command not earlier then 500 ms after set settings (Homekit hack) send, subItem); //Accept ON command not earlier then 500 ms after set settings (Homekit hack)
// else debugSerial<<F("on Skipped")); // else debugSerial<<F("on Skipped"));
break; break;
default: //some known command default: //some known command
return Ctrl(cmd, 0, NULL, send); return Ctrl(cmd, 0, NULL, send, subItem);
} //ctrl } //ctrl
} }
int Item::Ctrl(short cmd, short n, int *Parameters, boolean send) { int Item::Ctrl(short cmd, short n, int *Parameters, boolean send, char * subItem) {
debugSerial<<F(" MEM=")<<freeRam()<<F(" Cmd=")<<cmd<<F(" Par: "); debugSerial<<F(" MEM=")<<freeRam()<<F(" Item=")<<itemArr->name<<F(" Sub=")<<subItem<<F(" Cmd=")<<cmd<<F(" Par= ");
if (!itemArr) return -1; if (!itemArr) return -1;
int Par[MAXCTRLPAR] = {0, 0, 0}; int Par[MAXCTRLPAR] = {0, 0, 0};
if (Parameters) if (Parameters)
for (short i=0;i<n && i<MAXCTRLPAR;i++){ for (short i=0;i<n && i<MAXCTRLPAR;i++){
Par[i] = Parameters[i]; Par[i] = Parameters[i];
debugSerial<<Par[i]<<F(","); debugSerial<<F("<")<<Par[i]<<F("> ");
} }
debugSerial<<endl; debugSerial<<endl;
int iaddr = getArg(); int iaddr = getArg();

View File

@@ -105,8 +105,8 @@ class Item
Item(char * name); Item(char * name);
Item(aJsonObject * obj); Item(aJsonObject * obj);
boolean isValid (); boolean isValid ();
virtual int Ctrl(short cmd, short n=0, int * Parameters=NULL, boolean send=true); virtual int Ctrl(short cmd, short n=0, int * Parameters=NULL, boolean send=true, char * subItem=NULL);
virtual int Ctrl(char * payload, boolean send=true); virtual int Ctrl(char * payload, boolean send=true, char * subItem=NULL);
int getArg(short n=0); int getArg(short n=0);
boolean getEnableCMD(int delta); boolean getEnableCMD(int delta);

View File

@@ -176,19 +176,18 @@ void mqttCallback(char *topic, byte *payload, unsigned int length) {
debugSerial<<F("OOM!"); debugSerial<<F("OOM!");
return; return;
} }
for (int i = 0; i < length; i++) for (int i = 0; i < length; i++)
debugSerial<<((char) payload[i]); debugSerial<<((char) payload[i]);
debugSerial<<endl; debugSerial<<endl;
short intopic = 0; short intopic = 0;
short pfxlen = 0; short pfxlen = 0;
char * itemName; char * itemName = NULL;
char * subItem = NULL;
{ {
char buf[MQTT_TOPIC_LENGTH + 1]; char buf[MQTT_TOPIC_LENGTH + 1];
// strncpy_P(buf, inprefix, sizeof(buf));
if (lanStatus == RETAINING_COLLECTING) if (lanStatus == RETAINING_COLLECTING)
{ {
@@ -210,11 +209,7 @@ else
intopic = strncmp(topic, buf, pfxlen); intopic = strncmp(topic, buf, pfxlen);
} }
} }
} }
// in Retaining status - trying to restore previous state from retained output topic. Retained input topics are not relevant. // in Retaining status - trying to restore previous state from retained output topic. Retained input topics are not relevant.
if (intopic) { if (intopic) {
debugSerial<<F("Skipping.."); debugSerial<<F("Skipping..");
@@ -227,21 +222,22 @@ else
cmd_parse((char *)payload); cmd_parse((char *)payload);
return; return;
} }
//char subtopic[MQTT_SUBJECT_LENGTH] = "";
char *t; if (subItem = strchr(itemName, '/'))
if (t = strchr(itemName, '/'))
{ {
*t = 0; *subItem = 0;
t++; subItem++;
debugSerial<<F("Subtopic:")<<t<<endl; // debugSerial<<F("Subitem:")<<subItem<<endl;
//strncpy(subtopic, t + 1, MQTT_SUBJECT_LENGTH - 1);
} }
debugSerial<<F("Item:")<<itemName<<endl; // debugSerial<<F("Item:")<<itemName<<endl;
if (itemName[0]=='$' || subItem[0]=='$') return; //Skipping homie stuff
Item item(itemName); Item item(itemName);
if (item.isValid()) { if (item.isValid()) {
if (item.itemType == CH_GROUP && (lanStatus == RETAINING_COLLECTING)) if (item.itemType == CH_GROUP && (lanStatus == RETAINING_COLLECTING))
return; //Do not restore group channels - they consist not relevant data return; //Do not restore group channels - they consist not relevant data
item.Ctrl((char *)payload, !(lanStatus == RETAINING_COLLECTING)); item.Ctrl((char *)payload, !(lanStatus == RETAINING_COLLECTING),subItem);
} //valid item } //valid item
} }
@@ -378,19 +374,19 @@ void onMQTTConnect(){
// High level homie topics publishing // High level homie topics publishing
//strncpy_P(topic, outprefix, sizeof(topic)); //strncpy_P(topic, outprefix, sizeof(topic));
setTopic(topic,sizeof(topic),T_OUT); setTopic(topic,sizeof(topic),T_DEV);
strncat_P(topic, state_P, sizeof(topic)); strncat_P(topic, state_P, sizeof(topic));
strncpy_P(buf, ready_P, sizeof(buf)); strncpy_P(buf, ready_P, sizeof(buf));
mqttClient.publish(topic,buf,true); mqttClient.publish(topic,buf,true);
//strncpy_P(topic, outprefix, sizeof(topic)); //strncpy_P(topic, outprefix, sizeof(topic));
setTopic(topic,sizeof(topic),T_OUT); setTopic(topic,sizeof(topic),T_DEV);
strncat_P(topic, name_P, sizeof(topic)); strncat_P(topic, name_P, sizeof(topic));
strncpy_P(buf, nameval_P, sizeof(buf)); strncpy_P(buf, nameval_P, sizeof(buf));
mqttClient.publish(topic,buf,true); mqttClient.publish(topic,buf,true);
//strncpy_P(topic, outprefix, sizeof(topic)); //strncpy_P(topic, outprefix, sizeof(topic));
setTopic(topic,sizeof(topic),T_OUT); setTopic(topic,sizeof(topic),T_DEV);
strncat_P(topic, stats_P, sizeof(topic)); strncat_P(topic, stats_P, sizeof(topic));
strncpy_P(buf, statsval_P, sizeof(buf)); strncpy_P(buf, statsval_P, sizeof(buf));
mqttClient.publish(topic,buf,true); mqttClient.publish(topic,buf,true);
@@ -398,7 +394,7 @@ void onMQTTConnect(){
#ifndef NO_HOMIE #ifndef NO_HOMIE
// strncpy_P(topic, outprefix, sizeof(topic)); // strncpy_P(topic, outprefix, sizeof(topic));
setTopic(topic,sizeof(topic),T_OUT); setTopic(topic,sizeof(topic),T_DEV);
strncat_P(topic, homie_P, sizeof(topic)); strncat_P(topic, homie_P, sizeof(topic));
strncpy_P(buf, homiever_P, sizeof(buf)); strncpy_P(buf, homiever_P, sizeof(buf));
mqttClient.publish(topic,buf,true); mqttClient.publish(topic,buf,true);
@@ -440,7 +436,7 @@ void onMQTTConnect(){
} //switch } //switch
//strncpy_P(topic, outprefix, sizeof(topic)); //strncpy_P(topic, outprefix, sizeof(topic));
setTopic(topic,sizeof(topic),T_OUT); setTopic(topic,sizeof(topic),T_DEV);
strncat(topic,item->name,sizeof(topic)); strncat(topic,item->name,sizeof(topic));
strncat(topic,"/",sizeof(topic)); strncat(topic,"/",sizeof(topic));
@@ -450,7 +446,7 @@ void onMQTTConnect(){
if (format[0]) if (format[0])
{ {
//strncpy_P(topic, outprefix, sizeof(topic)); //strncpy_P(topic, outprefix, sizeof(topic));
setTopic(topic,sizeof(topic),T_OUT); setTopic(topic,sizeof(topic),T_DEV);
strncat(topic,item->name,sizeof(topic)); strncat(topic,item->name,sizeof(topic));
strncat(topic,"/",sizeof(topic)); strncat(topic,"/",sizeof(topic));
@@ -460,7 +456,7 @@ void onMQTTConnect(){
item = item->next; item = item->next;
} //if } //if
//strncpy_P(topic, outprefix, sizeof(topic)); //strncpy_P(topic, outprefix, sizeof(topic));
setTopic(topic,sizeof(topic),T_OUT); setTopic(topic,sizeof(topic),T_DEV);
strncat_P(topic, nodes_P, sizeof(topic)); strncat_P(topic, nodes_P, sizeof(topic));
mqttClient.publish(topic,buf,true); mqttClient.publish(topic,buf,true);
} }
@@ -519,7 +515,7 @@ void ip_ready_config_loaded_connecting_to_broker() {
strncpy_P(willMessage,disconnected_P,sizeof(willMessage)); strncpy_P(willMessage,disconnected_P,sizeof(willMessage));
// strncpy_P(willTopic, outprefix, sizeof(willTopic)); // strncpy_P(willTopic, outprefix, sizeof(willTopic));
setTopic(willTopic,sizeof(willTopic),T_OUT); setTopic(willTopic,sizeof(willTopic),T_DEV);
strncat_P(willTopic, state_P, sizeof(willTopic)); strncat_P(willTopic, state_P, sizeof(willTopic));

View File

@@ -82,14 +82,17 @@
#endif #endif
*/ */
//Default output topic
#ifndef OUTTOPIC #ifndef OUTTOPIC
#define OUTTOPIC "s_out" #define OUTTOPIC "s_out"
#endif #endif
//Topic to receive CLI commands
#ifndef CMDTOPIC #ifndef CMDTOPIC
#define CMDTOPIC "command" #define CMDTOPIC "$command"
#endif #endif
//Default broadcast topic
#ifndef INTOPIC #ifndef INTOPIC
#define INTOPIC "in" #define INTOPIC "in"
#endif #endif