Publish refactoring (#298)

* factoring of the publishing function
* integration of json publishing to MQTT
* receiving and transcode json messages from mqtt
* replace the TRACE runtime condition by a buildtime one so as to decrease program size
* remove retainFlag parameter not necessary for the moment
* Increase the scope of tests for arduino mega and esp32
* Update arduinojson library to 5.13.3
* add macros to choose between simple or json publishing or both
* differentiate build test for ATmega2560
* correct wrong sprintf definition
* change IDE version for continuous integration
* let default warning level in arduino ide
* correct pins for ESP32
* reorganize user_config
This commit is contained in:
Florian authored and GitHub committed 2018-11-05 22:02:27 +01:00
1 parent f77df7347f
commit d8dba15a81
182 files changed
+2701 -20768

No files matched your search

+55 -95
View File
@@ -17,23 +17,18 @@
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
OpenMQTTGateway is distributed in the hope that it will be useful,
OpenMQTTGateway is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifdef ZgatewayPilight
#include <ESPiLight.h>
ESPiLight rf(RF_EMITTER_PIN); // use -1 to disable transmitter
#ifdef ZgatewayPilight
#include <ESPiLight.h>
ESPiLight rf(RF_EMITTER_PIN); // use -1 to disable transmitter
void setupPilight(){
#ifndef ZgatewayRF && ZgatewayRF2 //receiving with Pilight is not compatible with ZgatewayRF or RF2 as far as I can tell
#ifndef ZgatewayRF && ZgatewayRF2 && ZgatewayRF315 //receiving with Pilight is not compatible with ZgatewayRF or RF2 or RF315 as far as I can tell
rf.setCallback(pilightCallback);
rf.initReceiver(RF_RECEIVER_PIN);
trc(F("RF_EMITTER_PIN "));
@@ -41,94 +36,64 @@ void setupPilight(){
trc(F("RF_RECEIVER_PIN "));
trc(String(RF_RECEIVER_PIN));
trc(F("ZgatewayPilight setup done "));
#endif
#else
trc(F("ZgatewayPilight setup cannot be done, comment first ZgatewayRF && ZgatewayRF2 && ZgatewayRF315"));
#endif
}
boolean PilighttoMQTT(){
void PilighttoMQTT(){
rf.loop();
}
void pilightCallback(const String &protocol, const String &message, int status,
size_t repeats, const String &deviceID) {
if (status == VALID) {
if (status == VALID) {
trc(F("Creating RF PiLight buffer"));
StaticJsonBuffer<JSON_MSG_BUFFER> jsonBuffer;
JsonObject& RFPiLightdata = jsonBuffer.createObject();
trc(F("Rcv. Pilight"));
String MQTTprotocol;
String MQTTdeviceID;
String MQTTmessage;
MQTTprotocol = String(protocol);
MQTTdeviceID = String(deviceID);
MQTTmessage = String(message);
String MQTTPilightstring;
MQTTPilightstring = subjectPilighttoMQTT+String("/")+MQTTprotocol+String("/")+MQTTdeviceID;
trc(F("Adv data PilighttoMQTT"));
client.publish((char *)MQTTPilightstring.c_str(),(char *)MQTTmessage.c_str());
}
RFPiLightdata.set("message", (char *)message.c_str());
RFPiLightdata.set("protocol",(char *)protocol.c_str());
RFPiLightdata.set("length", (char *)deviceID.c_str());
RFPiLightdata.set("repeats", (int)repeats);
RFPiLightdata.set("status", (int)status);
pub(subjectPilighttoMQTT,RFPiLightdata);
}
}
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 {
void MQTTtoPilight(char * topicOri, JsonObject& Pilightdata) {
String topic = topicOri;
int result = 0;
if (topic == subjectMQTTtoPilight) {
trc(F("MQTTtoPilight json data analysis"));
const char * message = Pilightdata["message"];
const char * protocol = Pilightdata["protocol"];
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;
}
} 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) {
String MQTTmessage;
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 {
pub(subjectGTWPilighttoMQTT, Pilightdata);
} else {
switch (result) {
case ESPiLight::ERROR_UNAVAILABLE_PROTOCOL:
MQTTmessage = "protocol is not avaiable";
@@ -148,13 +113,8 @@ void MQTTtoPilight(char * topicOri, char * datacallback) {
}
trc(F("ESPiLight Error: "));
trc(String(MQTTmessage));
MQTTPilightstring += String("error");
}
client.publish((char *)MQTTPilightstring.c_str(),(char *)MQTTmessage.c_str());
pub(subjectGTWPilighttoMQTT, MQTTmessage);
}
}
}
#endif
#endif