int ICACHE_FLASH_ATTR stledOps(HttpdConnData *connData) { int i, r; jsmn_parser p; jsmntok_t t[16]; /* We expect no more than 16 tokens */ char *json = connData->post->buff; if (connData->requestType == HTTPD_METHOD_OPTIONS) { sendJSON(connData); HTTPD_SEND_STR("{\"status\":\"ok\"}\r\n\r\n"); return HTTPD_CGI_DONE; } else if (connData->requestType == HTTPD_METHOD_GET) { sendJSON(connData); HTTPD_PRINTF("{\"status_led\": \"%s\"}\r\n\r\n", opStr[opState]); return HTTPD_CGI_DONE; } jsmn_init(&p); r = jsmn_parse(&p, json, strlen(json), t, sizeof(t)/sizeof(t[0])); if (r < 0) { ERROR("[json] Failed to parse JSON: %d", r); goto err; } else DEBUG("[json]: OK. %d tokens\n", r); /* Assume the top-level element is an object */ if (r < 1 || t[0].type != JSMN_OBJECT) { ERROR("Object expected\n"); goto err; } /* Loop over all keys of the root object */ for (i = 1; i < r; ) { if (jsonEq(json, &t[i], "status_led") && i + 1 < r) { if (jsonEq(json, &t[i+1], "on")) stledSet(STLED_ON); else if (jsonEq(json, &t[i+1], "off")) stledSet(STLED_OFF); else if (jsonEq(json, &t[i+1], "blink")) stledSet(STLED_BLINK_SLOW); else if (jsonEq(json, &t[i+1], "blink_fast")) stledSet(STLED_BLINK_FAST); else if (jsonEq(json, &t[i+1], "blink_slow")) stledSet(STLED_BLINK_SLOW); else if (jsonEq(json, &t[i+1], "blink_hb")) stledSet(STLED_BLINK_HB); i += 2; } else { //os_printf("BAD token %d: %s [%d,%d] sz=%d\n", i, type[t[i].type], t[i].start, t[i].end, t[i].size); ERROR("BAD token %d: type=%d [%d,%d] sz=%d", i, t[i].type, t[i].start, t[i].end, t[i].size); i++; } } sendOK(connData, "OK"); return HTTPD_CGI_DONE; err: sendOK(connData, "Error"); // FIXME return HTTPD_CGI_DONE; }
void ICACHE_FLASH_ATTR parseNexaSendMessage(const char *data, uint32_t data_len){ uint32_t i; int32_t r; jsmn_parser p; jsmntok_t t[15]; bool versionAssigned = false; bool idAssigned = false; bool groupAssigned = false; bool onffAssigned = false; bool channelAssigned = false; bool dimAssigned = false; bool repeatAssigned = false; int32_t version = 1, id, group, onoff, channel, dim = 0, repeat = 4; jsmn_init(&p); r = jsmn_parse(&p, data, data_len, t, sizeof(t)/sizeof(t[0])); if (r < 0) { INFO("NEXA: JSON, Parser error: %d\n", r); return; } if (r < 1 || t[0].type != JSMN_OBJECT) { INFO("NEXA: JSON, Object expected\n"); return; } if((r != 9) && (r != 11) && (r != 13) && (r != 15)){ INFO("NEXA: JSON, Missing token(s): %d\n", r); return; } INFO("NEXA: JSON payload \n"); for (i = 1; i < r; i++) { if (jsonEq(data, &t[i], "version")) { version = strtol(data + t[i+1].start, NULL, 10); versionAssigned = true; INFO("\tVersion: %d\n", version); i++; } else if (jsonEq(data, &t[i], "id")) { id = strtol(data + t[i+1].start, NULL, 10); idAssigned = true; INFO("\tId: %d\n", id); i++; } else if (jsonEq(data, &t[i], "group")) { group = strtol(data + t[i+1].start, NULL, 10); groupAssigned = true; INFO("\tGroup: %d\n", group); i++; } else if (jsonEq(data, &t[i], "onoff")) { onoff = strtol(data + t[i+1].start, NULL, 10); onffAssigned = true; INFO("\tOnOff: %d\n", onoff); i++; } else if (jsonEq(data, &t[i], "channel")) { channel = strtol(data + t[i+1].start, NULL, 10); channelAssigned = true; INFO("\tChannel: %d\n", channel); i++; } else if (jsonEq(data, &t[i], "dim")) { dim = strtol(data + t[i+1].start, NULL, 10); dimAssigned = true; INFO("\tDim: %d\n", dim); i++; } else if (jsonEq(data, &t[i], "repeat")) { repeat = strtol(data + t[i+1].start, NULL, 10); repeatAssigned = true; INFO("\tRepeat: %d\n", repeat); i++; } else { INFO("\tUnexpected key\n"); } } if(idAssigned && groupAssigned && onffAssigned && channelAssigned){ INFO("NEXA: JSON, All parameters assigned for on/off breaker\n"); createNexaFrame(version, id, group, onoff, channel, -1, repeat); }else if(idAssigned && groupAssigned && channelAssigned && dimAssigned){ INFO("NEXA: JSON, All parameters assigned for dimmer \n"); createNexaFrame(version, id, group, 0, channel, dim, repeat); } }