void ICACHE_FLASH_ATTR switch1_handler( struct espconn *pConnection, request_method method, char *url, char *data, uint16 data_len, uint32 content_len, char *response, uint16 response_len ) { struct jsonparse_state parser; int type; uint16 state = 0; if (method == POST && data != NULL && data_len != 0) { jsonparse_setup(&parser, data, data_len); while ((type = jsonparse_next(&parser)) != 0) { if (type == JSON_TYPE_PAIR_NAME) { if (jsonparse_strcmp_value(&parser, "Relay") == 0) { jsonparse_next(&parser); jsonparse_next(&parser); state = jsonparse_get_value_as_int(&parser); user_switch1_set(0, state); } } } } user_switch1_state(response); }
LOCAL bool ICACHE_FLASH_ATTR switch1_parse(char *data, uint16 data_len) { struct jsonparse_state parser; int type; int preference = switch1_hardware[1].preference; jsonparse_setup(&parser, data, data_len); while ((type = jsonparse_next(&parser)) != 0) { if (type == JSON_TYPE_PAIR_NAME) { if (jsonparse_strcmp_value(&parser, "Relay") == 0) { jsonparse_next(&parser); jsonparse_next(&parser); user_switch1_set(0, jsonparse_get_value_as_sint(&parser)); } else if (jsonparse_strcmp_value(&parser, "Preference") == 0) { jsonparse_next(&parser); jsonparse_next(&parser); switch1_hardware[1].preference = jsonparse_get_value_as_sint(&parser); } } } return ( preference != switch1_hardware[1].preference ); }
LOCAL void user_switch1_off(void *arg) { switch1_config *config = arg; config->timer = 0; user_switch1_set(config->id, 0); user_switch1_event(); }
LOCAL void ICACHE_FLASH_ATTR switch1_toggle(void *arg) { LOCAL event_timer = 0; switch1_config *config = arg; bool event = false; if (1 == GPIO_INPUT_GET(GPIO_ID_PIN(config->gpio.gpio_id))) { if (config->state_buf < SWITCH_STATE_FILTER) { config->state_buf++; event = (config->state_buf == SWITCH_STATE_FILTER); } } else { if (config->state_buf > 0) { config->state_buf--; event = (config->state_buf == 0); } } if (event) { uint8 state = (config->state_buf == SWITCH_STATE_FILTER); if (config->state != state) { config->state = state; if (config->preference == 0) { return; } int relay_state = 0; if (config->preference < 0 || config->preference >= 2) { // Relay Off-On || On-Off || Toggle relay_state = config->preference; } else if (config->preference == 1) { // Relay Set relay_state = state; } user_switch1_set(config->id - 1, relay_state); clearTimeout(event_timer); event_timer = setTimeout(user_switch1_event, NULL, 50); } } }
LOCAL void ICACHE_FLASH_ATTR switch1_toggle(void *arg) { switch1_config *config = arg; bool event = false; if (1 == GPIO_INPUT_GET(GPIO_ID_PIN(config->gpio.gpio_id))) { if (config->state_buf < SWITCH_STATE_FILTER) { config->state_buf++; event = (config->state_buf == SWITCH_STATE_FILTER); } } else { if (config->state_buf > 0) { config->state_buf--; event = (config->state_buf == 0); } } if (event) { config->state = (config->state_buf == SWITCH_STATE_FILTER); user_switch1_set(config->id - 1, 2); user_switch1_event(); } }