Browse Source

Added API entry points for RFBridge module (#1407)

refactor-terminal
Xose Pérez 5 years ago
parent
commit
64c8cf313d
2 changed files with 102 additions and 41 deletions
  1. +95
    -38
      code/espurna/rfbridge.ino
  2. +7
    -3
      code/espurna/utils.ino

+ 95
- 38
code/espurna/rfbridge.ino View File

@ -453,7 +453,50 @@ bool _rfbSameOnOff(unsigned char id) {
return _rfbCompare(rfbRetrieve(id, true).c_str(), rfbRetrieve(id, false).c_str());
}
void _rfbParseCode(char * code) {
// The payload may be a code in HEX format ([0-9A-Z]{18}) or
// the code comma the number of times to transmit it.
char * tok = strtok(code, ",");
// Check if a switch is linked to that message
unsigned char id;
unsigned char status = 0;
if (_rfbMatch(tok, id, status)) {
if (status == 2) {
relayToggle(id);
} else {
relayStatus(id, status == 1);
}
return;
}
#if RF_RAW_SUPPORT
byte message[RF_MAX_MESSAGE_SIZE];
int len = _rfbToArray(tok, message, 0);
if ((len > 0) && (isRFRaw || len != RF_MESSAGE_SIZE)) {
_rfbSendRawOnce(message, len);
} else {
tok = strtok(NULL, ",");
byte times = (tok != NULL) ? atoi(tok) : 1;
_rfbSend(message, times);
}
#else // RF_RAW_SUPPORT
byte message[RF_MESSAGE_SIZE];
if (_rfbToArray(tok, message)) {
tok = strtok(NULL, ",");
byte times = (tok != NULL) ? atoi(tok) : 1;
_rfbSend(message, times);
}
#endif // RF_RAW_SUPPORT
}
#if MQTT_SUPPORT
void _rfbMqttCallback(unsigned int type, const char * topic, const char * payload) {
if (type == MQTT_CONNECT_EVENT) {
@ -498,55 +541,65 @@ void _rfbMqttCallback(unsigned int type, const char * topic, const char * payloa
#endif
#if not RF_SUPPORT
if (isRFOut || isRFRaw) {
// The payload may be a code in HEX format ([0-9A-Z]{18}) or
// the code comma the number of times to transmit it.
char * tok = strtok((char *) payload, ",");
// Check if a switch is linked to that message
unsigned char id;
unsigned char status = 0;
if (_rfbMatch(tok, id, status)) {
if (status == 2) {
relayToggle(id);
} else {
relayStatus(id, status == 1);
}
return;
if (isRFOut || isRFRaw) {
_rfbParseCode((char *) payload);
}
#endif // not RF_SUPPORT
#if RF_RAW_SUPPORT
}
byte message[RF_MAX_MESSAGE_SIZE];
int len = _rfbToArray(tok, message, 0);
if ((len > 0) && (isRFRaw || len != RF_MESSAGE_SIZE)) {
_rfbSendRawOnce(message, len);
} else {
tok = strtok(NULL, ",");
byte times = (tok != NULL) ? atoi(tok) : 1;
_rfbSend(message, times);
}
}
#else // RF_RAW_SUPPORT
#endif // MQTT_SUPPORT
byte message[RF_MESSAGE_SIZE];
if (_rfbToArray(tok, message)) {
tok = strtok(NULL, ",");
byte times = (tok != NULL) ? atoi(tok) : 1;
_rfbSend(message, times);
}
#if API_SUPPORT
#endif // RF_RAW_SUPPORT
void _rfbAPISetup() {
apiRegister(MQTT_TOPIC_RFOUT,
[](char * buffer, size_t len) {
snprintf_P(buffer, len, PSTR("OK"));
},
[](const char * payload) {
_rfbParseCode((char *) payload);
}
);
apiRegister(MQTT_TOPIC_RFLEARN,
[](char * buffer, size_t len) {
snprintf_P(buffer, len, PSTR("OK"));
},
[](const char * payload) {
// The payload must be the relayID plus the mode (0 or 1)
char * tok = strtok((char *) payload, ",");
if (NULL == tok) return;
if (!isNumber(tok)) return;
_learnId = atoi(tok);
if (_learnId >= relayCount()) {
DEBUG_MSG_P(PSTR("[RF] Wrong learnID (%d)\n"), _learnId);
return;
}
tok = strtok(NULL, ",");
if (NULL == tok) return;
_learnStatus = (char) tok[0] != '0';
_rfbLearn();
}
);
#endif // not RF_SUPPORT
}
#if RF_RAW_SUPPORT
apiRegister(MQTT_TOPIC_RFRAW,
[](char * buffer, size_t len) {
snprintf_P(buffer, len, PSTR("OK"));
},
[](const char * payload) {
_rfbParseCode(payload);
}
);
#endif // RF_RAW_SUPPORT
}
#endif
#endif // API_SUPPORT
#if TERMINAL_SUPPORT
@ -693,6 +746,10 @@ void rfbSetup() {
mqttRegister(_rfbMqttCallback);
#endif
#if API_SUPPORT
_rfbAPISetup();
#endif
#if WEB_SUPPORT
wsOnSendRegister(_rfbWebSocketOnSend);
wsOnActionRegister(_rfbWebSocketOnAction);


+ 7
- 3
code/espurna/utils.ino View File

@ -573,15 +573,19 @@ bool isNumber(const char * s) {
unsigned char len = strlen(s);
if (0 == len) return false;
bool decimal = false;
bool digit = false;
for (unsigned char i=0; i<len; i++) {
if (s[i] == '-') {
if (('-' == s[i]) || ('+' == s[i])) {
if (i>0) return false;
} else if (s[i] == '.') {
if (!digit) return false;
if (decimal) return false;
decimal = true;
} else if (!isdigit(s[i])) {
return false;
} else {
digit = true;
}
}
return true;
}
return digit;
}

Loading…
Cancel
Save