diff --git a/ZgatewayPilight.ino b/ZgatewayPilight.ino index 46a4c82..2e06889 100644 --- a/ZgatewayPilight.ino +++ b/ZgatewayPilight.ino @@ -64,12 +64,10 @@ boolean PilighttoMQTT(){ trc(F("Rcv. Pilight")); String MQTTprotocol; String MQTTdeviceID; - String MQTTstatus; String MQTTmessage; MQTTprotocol = String(pilightrd.protocol); MQTTdeviceID = String(pilightrd.deviceID); - MQTTstatus = String(pilightrd.status); MQTTmessage = String(pilightrd.message); String MQTTPilightstring; MQTTPilightstring = subjectPilighttoMQTT+String("/")+MQTTprotocol+String("/")+MQTTdeviceID; @@ -84,16 +82,102 @@ boolean PilighttoMQTT(){ void pilightCallback(const String &protocol, const String &message, int status, size_t repeats, const String &deviceID) { - pilightrd.protocol=protocol; - pilightrd.deviceID=deviceID; - pilightrd.status=status; - pilightrd.message=message; - pilightrd.hasNewData=true; + if (status == VALID) { + pilightrd.protocol=protocol; + pilightrd.deviceID=deviceID; + pilightrd.status=status; + pilightrd.message=message; + pilightrd.hasNewData=true; + } } void MQTTtoPilight(char * topicOri, char * datacallback) { + String topic = topicOri; + + int pos = topic.lastIndexOf(subjectMQTTtoPilight); + if (pos == -1){ + return; + } + + trc(F("Adv data PilighttoMQTT")); + + char *last = strrchr(topicOri, '/'); + String protocol = ""; + + if (last+1 != NULL) { + protocol = last+1; + } + + int result = 0; + + if (protocol == PilightRAW){ + trc(F("RAW:")); + trc(String(datacallback)); + + uint16_t rawpulses[MAXPULSESTREAMLENGTH]; + int rawlen = + rf.stringToPulseTrain(datacallback, rawpulses, MAXPULSESTREAMLENGTH); + if (rawlen > 0) { + rf.sendPulseTrain(rawpulses, rawlen); + result = rawlen; + } else { + result = -9999; + } + } else { + trc(F("PROTO:")); + trc(protocol); + result = rf.send(protocol, datacallback); + } + + String MQTTmessage; + String MQTTprotocol = String(protocol); + String MQTTPilightstring = subjectPilighttoMQTT+String("/")+MQTTprotocol+String("/"); + + if (result > 0) { + trc(F("Adv data MQTTtoPilight push state via PilighttoMQTT")); + + MQTTmessage = String(datacallback); + + char *deviceID = "0"; + + DynamicJsonBuffer jb; + JsonObject& rjson = jb.parseObject(MQTTmessage); + if (rjson.success()) { + strcpy(deviceID, rjson["id"]); + } + + MQTTmessage = String(datacallback); + MQTTPilightstring += String(deviceID); + + } 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; + } + trc(F("ESPiLight Error: ")); + trc(String(MQTTmessage)); + MQTTPilightstring += String("error"); + + } + + client.publish((char *)MQTTPilightstring.c_str(),(char *)MQTTmessage.c_str()); + } #endif + diff --git a/config_RF.h b/config_RF.h index 41cd868..7e49cd8 100644 --- a/config_RF.h +++ b/config_RF.h @@ -64,6 +64,7 @@ RF supported protocols //433Mhz Pilight MQTT Subjects and keys #define subjectMQTTtoPilight Base_Topic Gateway_Name "/commands/MQTTtoPilight" #define subjectPilighttoMQTT Base_Topic Gateway_Name "/PilighttoMQTT" +#define PilightRAW "RAW" /*-------------------PIN DEFINITIONS----------------------*/ #ifdef ESP8266