correct switch between modes raw versus message + protocol and add raw error codes (#374)

has been discussed here:
https://github.com/puuu/ESPiLight/issues/39
This commit is contained in:
Florian authored and GitHub committed 2019-03-19 22:32:08 +01:00
1 parent e6191915e3
commit e1c9e0fcef
1 file changed
+30 -19
+30 -19
View File
@@ -63,22 +63,18 @@ void pilightCallback(const String &protocol, const String &message, int status,
void MQTTtoPilight(char * topicOri, JsonObject& Pilightdata) { void MQTTtoPilight(char * topicOri, JsonObject& Pilightdata) {
int result = 0; int result = 0;
if (strcmp(topicOri,subjectMQTTtoPilight) == 0){ if (strcmp(topicOri,subjectMQTTtoPilight) == 0){
trc(F("MQTTtoPilight json data analysis")); trc(F("MQTTtoPilight json data analysis"));
const char * message = Pilightdata["message"]; const char * message = Pilightdata["message"];
const char * protocol = Pilightdata["protocol"]; const char * protocol = Pilightdata["protocol"];
if (message && protocol) { const char * raw = Pilightdata["raw"];
trc(F("MQTTtoPilight msg & protocol ok")); if(raw){
result = rf.send(protocol, message);
}else{
trc(F("MQTTtoPilight fail reading from json"));
}
int msgLength = 0; int msgLength = 0;
uint16_t codes[MAXPULSESTREAMLENGTH]; uint16_t codes[MAXPULSESTREAMLENGTH];
msgLength = rf.stringToPulseTrain( msgLength = rf.stringToPulseTrain(
message, raw,
codes, MAXPULSESTREAMLENGTH); codes, MAXPULSESTREAMLENGTH);
if (msgLength > 0) { if (msgLength > 0) {
trc(F("MQTTtoPilight raw ok")); trc(F("MQTTtoPilight raw ok"));
@@ -86,34 +82,49 @@ int result = 0;
result = msgLength; result = msgLength;
}else{ }else{
trc(F("MQTTtoPilight raw KO")); trc(F("MQTTtoPilight raw KO"));
result = -9999; switch (result) {
case ESPiLight::ERROR_INVALID_PULSETRAIN_MSG_C:
trc(F("'c' not found in string, or has no data"));
break;
case ESPiLight::ERROR_INVALID_PULSETRAIN_MSG_P:
trc(F("'p' not found in string, or has no data"));
break;
case ESPiLight::ERROR_INVALID_PULSETRAIN_MSG_END:
trc(F("';' or '@' not found in data string"));
break;
case ESPiLight::ERROR_INVALID_PULSETRAIN_MSG_TYPE:
trc(F("pulse type not defined"));
break;
}
}
}else if (message && protocol) {
trc(F("MQTTtoPilight msg & protocol ok"));
result = rf.send(protocol, message);
}else{
trc(F("MQTTtoPilight fail reading from json"));
} }
String MQTTmessage;
if (result > 0) { if (result > 0) {
trc(F("Adv data MQTTtoPilight push state via PilighttoMQTT")); trc(F("Adv data MQTTtoPilight push state via PilighttoMQTT"));
pub(subjectGTWPilighttoMQTT, Pilightdata); pub(subjectGTWPilighttoMQTT, Pilightdata);
} else { } else {
switch (result) { switch (result) {
case ESPiLight::ERROR_UNAVAILABLE_PROTOCOL: case ESPiLight::ERROR_UNAVAILABLE_PROTOCOL:
MQTTmessage = "protocol is not avaiable"; trc(F("protocol is not available"));
break; break;
case ESPiLight::ERROR_INVALID_PILIGHT_MSG: case ESPiLight::ERROR_INVALID_PILIGHT_MSG:
MQTTmessage = "message is invalid"; trc(F("message is invalid"));
break; break;
case ESPiLight::ERROR_INVALID_JSON: case ESPiLight::ERROR_INVALID_JSON:
MQTTmessage = "message is not a proper json object"; trc(F("message is not a proper json object"));
break; break;
case ESPiLight::ERROR_NO_OUTPUT_PIN: case ESPiLight::ERROR_NO_OUTPUT_PIN:
MQTTmessage = "no transmitter pin"; trc(F("no transmitter pin"));
break; break;
case -9999: default:
MQTTmessage = "invalid pulse train message"; trc(F("invalid json data, can't read raw or message/protocol"));
break; break;
} }
trc(F("ESPiLight Error: "));
trc(String(MQTTmessage));
pub(subjectGTWPilighttoMQTT, MQTTmessage);
} }
} }
} }