Skip to content

Commit

Permalink
correct switch between modes raw versus message + protocol and add ra…
Browse files Browse the repository at this point in the history
…w error codes (1technophile#374)

has been discussed here:
puuu/ESPiLight#39
  • Loading branch information
1technophile committed Apr 22, 2019
1 parent cf68950 commit c631a0e
Showing 1 changed file with 54 additions and 43 deletions.
97 changes: 54 additions & 43 deletions ZgatewayPilight.ino
Original file line number Diff line number Diff line change
Expand Up @@ -63,58 +63,69 @@ void pilightCallback(const String &protocol, const String &message, int status,

void MQTTtoPilight(char * topicOri, JsonObject& Pilightdata) {

int result = 0;

if (strcmp(topicOri,subjectMQTTtoPilight) == 0){
int result = 0;
if (strcmp(topicOri,subjectMQTTtoPilight) == 0){
trc(F("MQTTtoPilight json data analysis"));
const char * message = Pilightdata["message"];
const char * protocol = Pilightdata["protocol"];
if (message && protocol) {
const char * raw = Pilightdata["raw"];
if(raw){
int msgLength = 0;
uint16_t codes[MAXPULSESTREAMLENGTH];
msgLength = rf.stringToPulseTrain(
raw,
codes, MAXPULSESTREAMLENGTH);
if (msgLength > 0) {
trc(F("MQTTtoPilight raw ok"));
rf.sendPulseTrain(codes, msgLength);
result = msgLength;
}else{
trc(F("MQTTtoPilight raw KO"));
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"));
}
int msgLength = 0;
uint16_t codes[MAXPULSESTREAMLENGTH];
msgLength = rf.stringToPulseTrain(
message,
codes, MAXPULSESTREAMLENGTH);
if (msgLength > 0) {
trc(F("MQTTtoPilight raw ok"));
rf.sendPulseTrain(codes, msgLength);
result = msgLength;
}else{
trc(F("MQTTtoPilight raw KO"));
result = -9999;
trc(F("MQTTtoPilight fail reading from json"));
}

String MQTTmessage;
if (result > 0) {

if (result > 0) {
trc(F("Adv data MQTTtoPilight push state via PilighttoMQTT"));
pub(subjectGTWPilighttoMQTT, Pilightdata);
} else {
switch (result) {
case ESPiLight::ERROR_UNAVAILABLE_PROTOCOL:
MQTTmessage = "protocol is not avaiable";
break;
case ESPiLight::ERROR_INVALID_PILIGHT_MSG:
MQTTmessage = "message is invalid";
break;
case ESPiLight::ERROR_INVALID_JSON:
MQTTmessage = "message is not a proper json object";
break;
case ESPiLight::ERROR_NO_OUTPUT_PIN:
MQTTmessage = "no transmitter pin";
break;
case -9999:
MQTTmessage = "invalid pulse train message";
break;
} else {
switch (result) {
case ESPiLight::ERROR_UNAVAILABLE_PROTOCOL:
trc(F("protocol is not available"));
break;
case ESPiLight::ERROR_INVALID_PILIGHT_MSG:
trc(F("message is invalid"));
break;
case ESPiLight::ERROR_INVALID_JSON:
trc(F("message is not a proper json object"));
break;
case ESPiLight::ERROR_NO_OUTPUT_PIN:
trc(F("no transmitter pin"));
break;
default:
trc(F("invalid json data, can't read raw or message/protocol"));
break;
}
}
trc(F("ESPiLight Error: "));
trc(String(MQTTmessage));
pub(subjectGTWPilighttoMQTT, MQTTmessage);
}
}
}
}
#endif

0 comments on commit c631a0e

Please sign in to comment.