Exemplo n.º 1
0
LOCAL void ICACHE_FLASH_ATTR mod_tc_mk2_read(i2c_config *config, char *response, bool poll) {
	poll = true;
	
	tc_config_data *config_data = (tc_config_data *)config->data;
	
	char address_str[MAX_I2C_ADDRESS];
	json_i2c_address(address_str, config->address);
	
	i2c_status status = tc_read(config);
	if (status == I2C_OK) {
		char poll_str[WEBSERVER_MAX_VALUE];
		if (poll) {
			json_poll_str(poll_str, tc_refresh / 1000, tc_each, tc_threshold);
		} else {
			poll_str[0] = '\0';
		}
		
		char data_str[WEBSERVER_MAX_VALUE];
		json_data(
			response, MOD_TC_MK2, OK_STR,
			json_sprintf(
				data_str,
				"\"Temperature\" : %s %s",
				config_data->temperature_str,
				poll_str
			),
			address_str
		);
	} else {
		json_error(response, MOD_TC_MK2, i2c_status_str(status), address_str);
	}
}
Exemplo n.º 2
0
void ICACHE_FLASH_ATTR mod_irda_handler(
	struct espconn *pConnection, 
	request_method method, 
	char *url, 
	char *data, 
	uint16 data_len, 
	uint32 content_len, 
	char *response,
	uint16 response_len
) {
	i2c_status status = I2C_OK;
	i2c_config *config = i2c_init_handler(MOD_IRDA, MOD_IRDA_URL, irda_init, url, response);
	if (config == NULL) {
		return;
	}
	
	irda_config_data *config_data = (irda_config_data *)config->data;
	
	struct jsonparse_state parser;
	int type;
	
	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, "Mode") == 0) {
					jsonparse_next(&parser);
					jsonparse_next(&parser);
					if (jsonparse_strcmp_value(&parser, "SIRC") == 0) {
						config_data->mode = 1;
					} else {
						config_data->mode = 0;
					}
				} else if (jsonparse_strcmp_value(&parser, "Device") == 0) {
					jsonparse_next(&parser);
					jsonparse_next(&parser);
					config_data->device = jsonparse_get_value_as_int(&parser);
				} else if (jsonparse_strcmp_value(&parser, "Command") == 0) {
					jsonparse_next(&parser);
					jsonparse_next(&parser);
					config_data->command = jsonparse_get_value_as_int(&parser);
				}
			}
		}
		
		status = irda_set(config);
	}
	
	char address_str[MAX_I2C_ADDRESS];
	json_i2c_address(address_str, config->address);
	
	if (status == I2C_OK) {
		char data_str[WEBSERVER_MAX_VALUE];
		json_data(
			response, MOD_IRDA, OK_STR,
			json_sprintf(
				data_str,
				"\"Mode\" : \"%s\", "
				"\"Device\" : %d, "
				"\"Command\" : %d ",
				irda_mode_str(config_data->mode), 
				config_data->device,
				config_data->command
			),
			address_str
		);
	} else {
		json_error(response, MOD_IRDA, i2c_status_str(status), address_str);
	}
}
Exemplo n.º 3
0
void ICACHE_FLASH_ATTR mod_rgb_handler(
	struct espconn *pConnection, 
	request_method method, 
	char *url, 
	char *data, 
	uint16 data_len, 
	uint32 content_len, 
	char *response,
	uint16 response_len
) {
	i2c_status status;
	i2c_config *config = i2c_init_handler(MOD_RGB, MOD_RGB_URL, rgb_init, url, response);
	if (config == NULL) {
		return;
	}
	
	rgb_config_data *config_data = (rgb_config_data *)config->data;
	
	struct jsonparse_state parser;
	int type;
	
	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, "R") == 0) {
					jsonparse_next(&parser);
					jsonparse_next(&parser);
					config_data->red = jsonparse_get_value_as_int(&parser);
				} else if (jsonparse_strcmp_value(&parser, "G") == 0) {
					jsonparse_next(&parser);
					jsonparse_next(&parser);
					config_data->green = jsonparse_get_value_as_int(&parser);
				} else if (jsonparse_strcmp_value(&parser, "B") == 0) {
					jsonparse_next(&parser);
					jsonparse_next(&parser);
					config_data->blue = jsonparse_get_value_as_int(&parser);
				}
			}
		}
	}
	
	char address_str[MAX_I2C_ADDRESS];
	json_i2c_address(address_str, config->address);
	
	status = rgb_set(config);
	if (status == I2C_OK) {
		char data_str[WEBSERVER_MAX_VALUE];
		json_data(
			response, MOD_RGB, OK_STR,
			json_sprintf(
				data_str,
				"\"R\" : %d, \"G\" : %d, \"B\" : %d",
				config_data->red, 
				config_data->green, 
				config_data->blue
			),
			address_str
		);
	} else {
		json_error(response, MOD_RGB, i2c_status_str(status), address_str);
	}
}
Exemplo n.º 4
0
void ICACHE_FLASH_ATTR dimmer_handler(
	struct espconn *pConnection, 
	request_method method, 
	char *url, 
	char *data, 
	uint16 data_len, 
	uint32 content_len, 
	char *response,
	uint16 response_len
) {
	i2c_status status = I2C_OK;
	i2c_config *config = i2c_init_handler(DIMMER_STR, DIMMER_URL, dimmer_init, url, response);
	if (config == NULL) {
		return;
	}
	
	dimmer_config_data *config_data = (dimmer_config_data *)config->data;
	
	struct jsonparse_state parser;
	int type;

	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);
					config_data->relay = jsonparse_get_value_as_int(&parser);
				} else if (jsonparse_strcmp_value(&parser, "Brightness") == 0) {
					jsonparse_next(&parser);
					jsonparse_next(&parser);
					config_data->brightness = jsonparse_get_value_as_int(&parser);
				} else if (jsonparse_strcmp_value(&parser, "Refresh") == 0) {
					jsonparse_next(&parser);
					jsonparse_next(&parser);
					dimmer_refresh = jsonparse_get_value_as_int(&parser) * 1000;
				} else if (jsonparse_strcmp_value(&parser, "Each") == 0) {
					jsonparse_next(&parser);
					jsonparse_next(&parser);
					dimmer_each = jsonparse_get_value_as_int(&parser);
				} else if (jsonparse_strcmp_value(&parser, "Threshold") == 0) {
					jsonparse_next(&parser);
					jsonparse_next(&parser);
					dimmer_threshold = jsonparse_get_value_as_int(&parser);
				}
			}
		}
		
		dimmer_timer_init();
		status = dimmer_set(config);
	}
	
	char address_str[MAX_I2C_ADDRESS];
	json_i2c_address(address_str, config->address);
	
	if (status == I2C_OK) {
		dimmer_response(config, response, true);
	} else {
		json_error(response, DIMMER_STR, i2c_status_str(status), address_str);
	}
}