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
LOCAL void ICACHE_FLASH_ATTR dimmer_response(i2c_config *config, char *response, bool poll) {
	dimmer_config_data *config_data = (dimmer_config_data *)config->data;
	
	char address_str[MAX_I2C_ADDRESS];
	json_i2c_address(address_str, config->address);
	
	char poll_str[WEBSERVER_MAX_VALUE];
	if (poll) {
		json_poll_str(poll_str, dimmer_refresh / 1000, dimmer_each, dimmer_threshold);
	} else {
		poll_str[0] = '\0';
	}
		
	char data_str[WEBSERVER_MAX_VALUE];
	json_data(
		response, DIMMER_STR, OK_STR,
		json_sprintf(
			data_str,
			"\"Relay\" : %d, "
			"\"Brightness\" : %d,"
			"\"Current\" : %d"
			"%s",
			config_data->relay, 
			config_data->brightness,
			config_data->current,
			poll_str
		),
		address_str
	);
}
Exemplo n.º 3
0
LOCAL void ICACHE_FLASH_ATTR finger_params_ready(finger_packet *packet) {
	char response[WEBSERVER_MAX_RESPONSE_LEN];
	char data_str[WEBSERVER_MAX_VALUE];
	
	json_data(
		response, MOD_FINGER, OK_STR,
		json_sprintf(
			data_str,
			"\"Address\" : \"0x%08X\", "
			"\"DBSize\" : %d, "
			"\"DBStored\" : %d, "
			"\"FirstFree\" : %d, "
			"\"SecurityLevel\" : %d, "
			"\"Mode\" : \"%s\"",
			finger_address(), 
			finger_db_size(),
			finger_db_stored(),
			finger_first_free_id(),
			finger_security_level(),
			finger_mode_str(finger_current_mode)
		),
		NULL
	);
	
	user_event_raise(FINGER_URL, response);
}
Exemplo n.º 4
0
void DiveShareExportDialog::doUpload()
{
	//Store current settings
	QSettings settings;
	settings.setValue("diveshareExport/uid", ui->txtUID->text());
	settings.setValue("diveshareExport/private", ui->chkPrivate->isChecked());

	//Change UI into results mode
	ui->frameConfigure->setVisible(false);
	ui->frameResults->setVisible(true);
	ui->progressBar->setVisible(true);
	ui->progressBar->setRange(0, 0);

	//generate json
	struct membuffer buf = { 0 };
	export_list(&buf, NULL, exportSelected, false);
	QByteArray json_data(buf.buffer, buf.len);
	free_buffer(&buf);

	//Request to server
	QNetworkRequest request;

	if (ui->chkPrivate->isChecked())
		request.setUrl(QUrl(DIVESHARE_BASE_URI "/upload?private=true"));
	else
		request.setUrl(QUrl(DIVESHARE_BASE_URI "/upload"));

	request.setRawHeader("User-Agent", getUserAgent().toUtf8());
	if (ui->txtUID->text().length() != 0)
		request.setRawHeader("X-UID", ui->txtUID->text().toUtf8());

	reply = WebServices::manager()->put(request, json_data);

	QObject::connect(reply, SIGNAL(finished()), this, SLOT(finishedSlot()));
}
Exemplo n.º 5
0
void ICACHE_FLASH_ATTR user_event_connect() {
#if EVENTS_DEBUG
	debug("EVENTS: Station connected\n");
	memory_info();
#endif
	char status[WEBSERVER_MAX_RESPONSE_LEN];
	user_event_raise(
		USER_CONFIG_STATION_URL, 
		json_data(status, ESP8266, CONNECTED, (char *)config_wifi_station(), NULL)
	);
}
Exemplo n.º 6
0
LOCAL void ICACHE_FLASH_ATTR user_relay_state(char *response) {
	char data_str[WEBSERVER_MAX_VALUE];
	json_data(
		response, ESP8266, OK_STR,
		json_sprintf(
			data_str,
			"\"Relay\" : %d",
			relay_state
		),
		NULL
	);
}
Json::Value load_current_histogram(){
    Json::Value root;
    std::string filename_data = "nfb_histogram_data/nfb_histogram.json";
    {
        std::ifstream json_data(filename_data);
        try{
            json_data >> root;
        }catch(...){
             Json::Value root;
        }
    }
    return root;
}
Exemplo n.º 8
0
void* uploadMeasuresHandler(void *arg)
{
	Client* c = (Client*) arg;
	sleep(60);
	while (!toStop)
	{
		char temp_payload[1024];
		// Push values using full data path.
		json_data(temp_payload, "machine.temperature", temp_ts, temp_values, temp_size);
		char hum_payload[1024];
		json_data(hum_payload, "machine.humidity", hum_ts, hum_values, hum_size);
		sprintf(payload, "%s, %s", temp_payload, hum_payload);
		char* buffer = json_encapsulate(payload);
		printf("[SAMPLES] %s\n", buffer);

		MQTTMessage msg;
		msg.qos = QOS0;
		msg.retained = 0;
		msg.dup = 0;
		msg.id = 0;
		msg.payload = buffer;
		msg.payloadlen = strlen(buffer);

		char* topic = malloc(15+strlen(deviceId));
		sprintf(topic, "%s/messages/json", deviceId);
		printf("Publish on %s\n", topic);

		int rc = MQTTPublish(c, topic, &msg);
		if(rc != 0)
			printf("publish error: %d\n", rc);

		free(buffer);
		hum_size = 0;
		temp_size = 0;
		sleep(60);
	}
	return NULL;
}
Exemplo n.º 9
0
LOCAL void ICACHE_FLASH_ATTR user_switch1_state(char *response) {
    char data_str[WEBSERVER_MAX_VALUE];
    json_data(
        response, SWITCH1_STR, OK_STR,
        json_sprintf(
            data_str,
            "\"Relay\" : %d, "
            "\"Switch\" : %d, "
            "\"Preference\" : %d ",
            switch1_hardware[0].state,
            switch1_hardware[1].state,
            switch1_hardware[1].preference
        ),
        NULL
    );
}
Exemplo n.º 10
0
LOCAL void ICACHE_FLASH_ATTR button_set_response(char *state) {
	char response[WEBSERVER_MAX_VALUE];
	debug("BUTTON: %s\n", state);
	
	char data[WEBSERVER_MAX_VALUE];
	json_data(
		response, ESP8266, OK_STR,
		json_sprintf(
			data,
			"\"Button\" : \"%s\"",
			state
		),
		NULL
	);
	
	user_event_raise(BUTTON_URL, response);
}
Exemplo n.º 11
0
void ICACHE_FLASH_ATTR user_event_server_disconnected(const char *msg) {
	char data[WEBSERVER_MAX_VALUE];
	char buff[WEBSERVER_MAX_VALUE];
	user_websocket_event(
		USER_CONFIG_IOT_URL, 
		json_data(
			data, ESP8266, DISCONNECTED, 
			json_sprintf(
				buff, 
				"\"Message\": \"%s\"", 
				msg
			),
			NULL
		),
		NULL
	);
}
Exemplo n.º 12
0
void ICACHE_FLASH_ATTR wifi_scan_get_result(char *response) {
	if (wifi_scan_in_progress) {
		webserver_set_status(0);
		return;
	}
	
	if (wifi_scan_ap_count == 0 || wifi_scan_result == NULL) {
		if (wifi_scan_start()) {
			webserver_set_status(0);
			return;
		}
		
		webserver_set_status(200);
		json_error(response, ESP8266, "Can not start scan", NULL);
		return;
	}
	
	webserver_set_status(200);
	
	char result[WEBSERVER_MAX_VALUE*wifi_scan_ap_count];
	os_memset(result, '\0', sizeof(result));
	
	uint8 i=0;
	for (i=0; i<wifi_scan_ap_count; i++) {
		os_sprintf(
			result + os_strlen(result), 
			"%s{\"SSID\" : \"%s\", \"Strength\" : %d, \"Mode\" : \"%s\"}", 
			i > 0 ? ", " : "",
			wifi_scan_result[i]->ssid, 
			wifi_scan_result[i]->rssi, 
			wifi_auth_mode_str(wifi_scan_result[i]->authmode)
		);
	}
	
	char data_str[WEBSERVER_MAX_VALUE*wifi_scan_ap_count];
	json_data(
		response, ESP8266, OK_STR,
		json_sprintf(
			data_str,
			"\"WiFi\" : [%s]",
			result
		),
		NULL
	);
}
/* Make response */
LOCAL void ICACHE_FLASH_ATTR mb_pcd8544_set_response(char *response, bool is_fault, uint8 req_type) {
	char data_str[WEBSERVER_MAX_RESPONSE_LEN];
	char full_device_name[USER_CONFIG_USER_SIZE];
	
	mb_make_full_device_name(full_device_name, MB_PCD8544_DEVICE, USER_CONFIG_USER_SIZE);
	
	MB_PCD8544_DEBUG("PCD8544:Resp.prep:%d;isFault:%d:\n", req_type, is_fault)
	
	// Sensor fault
	if (is_fault) {
		json_error(response, full_device_name, DEVICE_STATUS_FAULT, NULL);
	}
	// POST request - status & config only
	else if (req_type == MB_REQTYPE_POST) {
		json_status(response, full_device_name, DEVICE_STATUS_OK, 
			json_sprintf(
				data_str, 
				"\"Config\" : {"
					"\"Reset_pin\": %d,"
					"\"Sce_pin\":%d,"
					"\"Dc_pin\": %d,"
					"\"Sdin_pin\": %d,"
					"\"Sclk_pin\": %d"
				"}",
				mb_p_pcd8544_config->resetPin,
				mb_p_pcd8544_config->scePin,
				mb_p_pcd8544_config->dcPin,
				mb_p_pcd8544_config->sdinPin,
				mb_p_pcd8544_config->sclkPin
			)
		);

	// normal event measurement
	} else {
		json_data(
			response, full_device_name, DEVICE_STATUS_OK, 
				json_sprintf(data_str,
					"\"pcd8544\": {"
					"}"
				),
				NULL
		);
	}
}
Exemplo n.º 14
0
LOCAL void ICACHE_FLASH_ATTR user_switch1_state(char *response) {
	char data_str[WEBSERVER_MAX_VALUE];
	
	uint8 i;
	for (i=0; i<SWITCH_COUNT; i++) {
		if (switch1_hardware[i].type == SWITCH1_SWITCH) {
			switch1_hardware[i].state = GPIO_INPUT_GET(GPIO_ID_PIN(switch1_hardware[i].gpio.gpio_id));
		}
	}
	
	json_data(
		response, SWITCH1_STR, OK_STR,
		json_sprintf(
			data_str,
			"\"Relay\" : %d, "
			"\"Switch\" : %d ",
			switch1_hardware[0].state,
			switch1_hardware[1].state
		),
		NULL
	);
}
Exemplo n.º 15
0
LOCAL void ICACHE_FLASH_ATTR adc_read(char *response, bool poll) {
	char data[WEBSERVER_MAX_VALUE];
	char poll_str[WEBSERVER_MAX_VALUE];
	
	poll = true;
	state = system_adc_read();
	
	if (poll) {
		json_poll_str(poll_str, adc_refresh / 1000, adc_each, adc_threshold);
	} else {
		poll_str[0] = '\0';
	}
	
	json_data(
		response, ESP8266, OK_STR, 
		json_sprintf(
			data,
			"\"ADC\" : {\"Value\" : %d %s}",
			state,
			poll_str
		),
		NULL
	);
}
Exemplo n.º 16
0
void ICACHE_FLASH_ATTR mod_led_8x8_rgb_handler(
	struct espconn *pConnection, 
	request_method method, 
	char *url, 
	char *data, 
	uint16 data_len, 
	uint32 content_len, 
	char *response,
	uint16 response_len
) {
	
	
	if (mod_led_8x8_text == NULL) {
		mod_led_8x8_text = (char *)os_zalloc(MOD_LED_8x8_RGB_MAX_TEXT);
	}
	
	if (method == POST && data != NULL && data_len != 0) {
		if (led_8x8_rgb_busy()) {
			json_error(response, MOD_LED8x8RGB, BUSY_STR, NULL);
			return;
		}
		
		if (mod_led_8x8_rgb_parse(data, data_len)) {
			if (!led_8x8_rgb_set_dimensions(mod_led_8x8_cols, mod_led_8x8_rows)) {
				json_error(response, MOD_LED8x8RGB, "Dimensions can not be set", NULL);
				return;
			}
			
			mod_led_8x8_rgb_preferences_set();
		}
		
		if (
			!led_8x8_rgb_scroll(
				mod_led_8x8_r, 
				mod_led_8x8_g, 
				mod_led_8x8_b, 
				mod_led_8x8_text, 
				MOD_LED_8x8_RGB_MAX_SPEED - mod_led_8x8_speed + 1, 
				mod_led_8x8_rgb_scroll_done
			)
		) {
			json_error(response, MOD_LED8x8RGB, BUSY_STR, NULL);
			return;
		}
	}
	
	char data_str[WEBSERVER_MAX_VALUE * 2];
	char *escaped = json_escape_str(mod_led_8x8_text, MOD_LED_8x8_RGB_MAX_TEXT);
	
	json_data(
		response, MOD_LED8x8RGB, OK_STR,
		json_sprintf(
			data_str,
			"\"cols\" : %d, "
			"\"rows\" : %d, "
			"\"Speed\" : %d, "
			"\"R\" : %d, "
			"\"G\" : %d, "
			"\"B\" : %d, "
			"\"Text\" : \"%s\"",
			led_8x8_rgb_get_cols(),
			led_8x8_rgb_get_rows(),
			mod_led_8x8_speed,
			mod_led_8x8_r,
			mod_led_8x8_g,
			mod_led_8x8_b,
			escaped
		),
		NULL
	);
	
	os_free(escaped);
}
Exemplo n.º 17
0
void ICACHE_FLASH_ATTR emtr_handler(
	struct espconn *pConnection, 
	request_method method, 
	char *url, 
	char *data, 
	uint16 data_len, 
	uint32 content_len, 
	char *response,
	uint16 response_len
) {
	if (device_get_uart() != UART_EMTR) {
		json_error(response, MOD_EMTR, DEVICE_NOT_FOUND, NULL);
		return;
	}
	
	if (emtr_registers.calibration == NULL) {
		emtr_registers.calibration = (emtr_calibration_registers *)os_zalloc(sizeof(emtr_calibration_registers));
	}
	
	if (emtr_registers.event == NULL) {
		emtr_registers.event = (emtr_event_registers *)os_zalloc(sizeof(emtr_event_registers));
	}
		
	struct jsonparse_state parser;
	int type;
	bool set_counter = false;
	emtr_mode mode = emtr_current_mode;
	_uint64_ counter_active = emtr_counter_active();
	_uint64_ counter_apparent = emtr_counter_apparent();
	
	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, "Log") == 0) {
						emtr_current_mode = EMTR_LOG;
					} else if (jsonparse_strcmp_value(&parser, "Configure") == 0) {
						emtr_current_mode = EMTR_CONFIGURE;
					} else if (jsonparse_strcmp_value(&parser, "Calibration") == 0) {
						emtr_current_mode = EMTR_CALIBRATION;
					}
				} else if (jsonparse_strcmp_value(&parser, "ReadInterval") == 0) {
					jsonparse_next(&parser);
					jsonparse_next(&parser);
					emtr_read_interval = jsonparse_get_value_as_int(&parser);
				} else if (jsonparse_strcmp_value(&parser, "CounterActive") == 0) {
					jsonparse_next(&parser);
					jsonparse_next(&parser);
					counter_active = jsonparse_get_value_as_int(&parser);
					set_counter = true;
				} else if (jsonparse_strcmp_value(&parser, "CounterApparent") == 0) {
					jsonparse_next(&parser);
					jsonparse_next(&parser);
					counter_apparent = jsonparse_get_value_as_int(&parser);
					set_counter = true;
				}
				
				if (mode == EMTR_CONFIGURE) {
					if (jsonparse_strcmp_value(&parser, "OverCurrentLimit") == 0) {
						jsonparse_next(&parser);
						jsonparse_next(&parser);
						emtr_registers.event->over_current_limit = jsonparse_get_value_as_int(&parser);
					} else if (jsonparse_strcmp_value(&parser, "OverPowerLimit") == 0) {
						jsonparse_next(&parser);
						jsonparse_next(&parser);
						emtr_registers.event->over_power_limit = jsonparse_get_value_as_int(&parser);
					} else if (jsonparse_strcmp_value(&parser, "OverFrequencyLimit") == 0) {
						jsonparse_next(&parser);
						jsonparse_next(&parser);
						emtr_registers.event->over_frequency_limit = jsonparse_get_value_as_int(&parser);
					} else if (jsonparse_strcmp_value(&parser, "UnderFrequencyLimit") == 0) {
						jsonparse_next(&parser);
						jsonparse_next(&parser);
						emtr_registers.event->under_frequency_limit = jsonparse_get_value_as_int(&parser);
					} else if (jsonparse_strcmp_value(&parser, "OverTemperatureLimit") == 0) {
						jsonparse_next(&parser);
						jsonparse_next(&parser);
						emtr_registers.event->over_temperature_limit = jsonparse_get_value_as_int(&parser);
					} else if (jsonparse_strcmp_value(&parser, "UnderTemperatureLimit") == 0) {
						jsonparse_next(&parser);
						jsonparse_next(&parser);
						emtr_registers.event->under_temperature_limit = jsonparse_get_value_as_int(&parser);
					} else if (jsonparse_strcmp_value(&parser, "VoltageSagLimit") == 0) {
						jsonparse_next(&parser);
						jsonparse_next(&parser);
						emtr_registers.event->voltage_sag_limit = jsonparse_get_value_as_int(&parser);
					} else if (jsonparse_strcmp_value(&parser, "VoltageSurgeLimit") == 0) {
						jsonparse_next(&parser);
						jsonparse_next(&parser);
						emtr_registers.event->voltage_surge_limit = jsonparse_get_value_as_int(&parser);
					} else if (jsonparse_strcmp_value(&parser, "OverCurrentHold") == 0) {
						jsonparse_next(&parser);
						jsonparse_next(&parser);
						emtr_registers.event->over_current_hold = jsonparse_get_value_as_int(&parser);
					} else if (jsonparse_strcmp_value(&parser, "OverPowerHold") == 0) {
						jsonparse_next(&parser);
						jsonparse_next(&parser);
						emtr_registers.event->over_power_hold = jsonparse_get_value_as_int(&parser);
					} else if (jsonparse_strcmp_value(&parser, "OverFrequencyHold") == 0) {
						jsonparse_next(&parser);
						jsonparse_next(&parser);
						emtr_registers.event->over_frequency_hold = jsonparse_get_value_as_int(&parser);
					} else if (jsonparse_strcmp_value(&parser, "UnderFrequencyHold") == 0) {
						jsonparse_next(&parser);
						jsonparse_next(&parser);
						emtr_registers.event->under_frequency_hold = jsonparse_get_value_as_int(&parser);
					} else if (jsonparse_strcmp_value(&parser, "OverTemperatureHold") == 0) {
						jsonparse_next(&parser);
						jsonparse_next(&parser);
						emtr_registers.event->over_temperature_hold = jsonparse_get_value_as_int(&parser);
					} else if (jsonparse_strcmp_value(&parser, "UnderTemperatureHold") == 0) {
						jsonparse_next(&parser);
						jsonparse_next(&parser);
						emtr_registers.event->under_temperature_hold = jsonparse_get_value_as_int(&parser);
					} else if (jsonparse_strcmp_value(&parser, "EventEnable") == 0) {
						jsonparse_next(&parser);
						jsonparse_next(&parser);
						emtr_registers.event->event_enable = jsonparse_get_value_as_int(&parser);
					} else if (jsonparse_strcmp_value(&parser, "EventMaskCritical") == 0) {
						jsonparse_next(&parser);
						jsonparse_next(&parser);
						emtr_registers.event->event_mask_critical = jsonparse_get_value_as_int(&parser);
					} else if (jsonparse_strcmp_value(&parser, "EventMaskStandard") == 0) {
						jsonparse_next(&parser);
						jsonparse_next(&parser);
						emtr_registers.event->event_mask_standard = jsonparse_get_value_as_int(&parser);
					} else if (jsonparse_strcmp_value(&parser, "EventTest") == 0) {
						jsonparse_next(&parser);
						jsonparse_next(&parser);
						emtr_registers.event->event_test = jsonparse_get_value_as_int(&parser);
					} else if (jsonparse_strcmp_value(&parser, "EventClear") == 0) {
						jsonparse_next(&parser);
						jsonparse_next(&parser);
						emtr_registers.event->event_clear = jsonparse_get_value_as_int(&parser);
					}
				}
			}
		}
		
		if (mode == EMTR_CONFIGURE) {
			emtr_set_event(emtr_registers.event, NULL);
		}
		
		if (set_counter) {
			emtr_set_counter(counter_active, counter_apparent, NULL);
		}
	}
	
	char data_str[WEBSERVER_MAX_RESPONSE_LEN];
	if (emtr_current_mode == EMTR_CALIBRATION) {
		json_data(
			response, MOD_EMTR, OK_STR,
			json_sprintf(
				data_str,
				"\"Address\" : \"0x%04X\", "
				"\"Mode\" : \"%s\", "
				"\"CounterActive\" : %d, "
				"\"CounterApparent\" : %d, "
				"\"ReadInterval\" : %d, "
				
				"\"GainCurrentRMS\" : %d, "
				"\"GainVoltageRMS\" : %d, "
				"\"GainActivePower\" : %d, "
				"\"GainReactivePower\" : %d, "
				"\"OffsetCurrentRMS\" : %d, "
				"\"OffsetActivePower\" : %d, "
				"\"OffsetReactivePower\" : %d, "
				"\"DCOffsetCurrent\" : %d, "
				"\"PhaseCompensation\" : %d, "
				"\"ApparentPowerDivisor\" : %d, "
				"\"SystemConfiguration\" : \"0x%08X\", "
				"\"DIOConfiguration\" : \"0x%04X\", "
				"\"Range\" : \"0x%08X\", "
				
				"\"CalibrationCurrent\" : %d, "
				"\"CalibrationVoltage\" : %d, "
				"\"CalibrationActivePower\" : %d, "
				"\"CalibrationReactivePower\" : %d, "
				"\"AccumulationInterval\" : %d",
				emtr_address(),
				emtr_mode_str(emtr_current_mode),
				emtr_counter_active(),
				emtr_counter_apparent(),
				emtr_read_interval,
				
				emtr_registers.calibration->gain_current_rms,
				emtr_registers.calibration->gain_voltage_rms,
				emtr_registers.calibration->gain_active_power,
				emtr_registers.calibration->gain_reactive_power,
				emtr_registers.calibration->offset_current_rms,
				emtr_registers.calibration->offset_active_power,
				emtr_registers.calibration->offset_reactive_power,
				emtr_registers.calibration->dc_offset_current,
				emtr_registers.calibration->phase_compensation,
				emtr_registers.calibration->apparent_power_divisor,
				emtr_registers.calibration->system_configuration,
				emtr_registers.calibration->dio_configuration,
				emtr_registers.calibration->range,
				
				emtr_registers.calibration->calibration_current,
				emtr_registers.calibration->calibration_voltage,
				emtr_registers.calibration->calibration_active_power,
				emtr_registers.calibration->calibration_reactive_power,
				emtr_registers.calibration->accumulation_interval
			),
			NULL
		);
		setTimeout(emtr_calibration_read, NULL, 1500);
	} else if (emtr_current_mode == EMTR_CONFIGURE) {
		json_data(
			response, MOD_EMTR, OK_STR,
			json_sprintf(
				data_str,
				"\"Address\" : \"0x%04X\", "
				"\"Mode\" : \"%s\", "
				"\"CounterActive\" : %d, "
				"\"CounterApparent\" : %d, "
				"\"ReadInterval\" : %d, "
				
				"\"OverCurrentLimit\" : %d, "
				"\"OverPowerLimit\" : %d, "
				"\"OverFrequencyLimit\" : %d, "
				"\"UnderFrequencyLimit\" : %d, "
				"\"OverTemperatureLimit\" : %d, "
				"\"UnderTemperatureLimit\" : %d, "
				"\"VoltageSagLimit\" : %d, "
				"\"VoltageSurgeLimit\" : %d, "
				"\"OverCurrentHold\" : %d, "
				"\"OverPowerHold\" : %d, "
				"\"OverFrequencyHold\" : %d, "
				"\"UnderFrequencyHold\" : %d, "
				"\"OverTemperatureHold\" : %d, "
				"\"UnderTemperatureHold\" : %d, "
				"\"EventEnable\" : %d, "
				"\"EventMaskCritical\" : %d, "
				"\"EventMaskStandard\" : %d, "
				"\"EventTest\" : %d, "
				"\"EventClear\" : %d",
				emtr_address(),
				emtr_mode_str(emtr_current_mode),
				emtr_counter_active(),
				emtr_counter_apparent(),
				emtr_read_interval,
				
				emtr_registers.event->over_current_limit,
				emtr_registers.event->over_power_limit,
				emtr_registers.event->over_frequency_limit,
				emtr_registers.event->under_frequency_limit,
				emtr_registers.event->over_temperature_limit,
				emtr_registers.event->under_temperature_limit,
				emtr_registers.event->voltage_sag_limit,
				emtr_registers.event->voltage_surge_limit,
				emtr_registers.event->over_current_hold,
				emtr_registers.event->over_power_hold,
				emtr_registers.event->over_frequency_hold,
				emtr_registers.event->under_frequency_hold,
				emtr_registers.event->over_temperature_hold,
				emtr_registers.event->under_temperature_hold,
				emtr_registers.event->event_enable,
				emtr_registers.event->event_mask_critical,
				emtr_registers.event->event_mask_standard,
				emtr_registers.event->event_test,
				emtr_registers.event->event_clear
			),
			NULL
		);
		setTimeout(emtr_events_read, NULL, 1500);
	} else {
		json_data(
			response, MOD_EMTR, OK_STR,
			json_sprintf(
				data_str,
				"\"Address\" : \"0x%04X\", "
				"\"Mode\" : \"%s\", "
				"\"CounterActive\" : %d, "
				"\"CounterApparent\" : %d, "
				"\"ReadInterval\" : %d",
				emtr_address(),
				emtr_mode_str(emtr_current_mode),
				emtr_counter_active(),
				emtr_counter_apparent(),
				emtr_read_interval
			),
			NULL
		);
	}
	
	emtr_start_read();
}
Exemplo n.º 18
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.º 19
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.º 20
0
LOCAL void ICACHE_FLASH_ATTR emtr_read_done(emtr_packet *packet) {
	LOCAL emtr_output_registers *registers = NULL;
	LOCAL uint32 time = 0;
	LOCAL uint32 interval = 0;
	LOCAL uint32 now = 0;
	
	now = system_get_time();
	interval = (time != 0) ? 
		(now - time) / 1000
		: 
		0
	;
	time = now;
	
	if (registers == NULL) {
		registers = (emtr_output_registers *)os_zalloc(sizeof(emtr_output_registers));
	}
	
	emtr_parse_output(packet, registers);
	
	char event_str[20];
	os_memset(event_str, 0, sizeof(event_str));
	
	char response[WEBSERVER_MAX_RESPONSE_LEN];
	char data_str[WEBSERVER_MAX_RESPONSE_LEN];
	json_data(
		response, MOD_EMTR, OK_STR,
		json_sprintf(
			data_str,
			"\"Address\" : \"0x%04X\", "
			"\"CounterActive\" : %d, "
			"\"CounterApparent\" : %d, "
			"\"Interval\" : %d, "
			
			"\"CurrentRMS\" : %d, "
			"\"VoltageRMS\" : %d, "
			"\"ActivePower\" : %d, "
			"\"ReactivePower\" : %d, "
			"\"ApparentPower\" : %d, "
			"\"PowerFactor\" : %d, "
			"\"LineFrequency\" : %d, "
			"\"ThermistorVoltage\" : %d, "
			"\"EventFlag\" : %d, "
			"\"SystemStatus\" : \"0x%04X\"",
			emtr_address(),
			emtr_counter_active(),
			emtr_counter_apparent(),
			interval,
			
			registers->current_rms,
			registers->voltage_rms,
			registers->active_power,
			registers->reactive_power,
			registers->apparent_power,
			registers->power_factor,
			registers->line_frequency,
			registers->thermistor_voltage,
			registers->event_flag,
			registers->system_status
		),
		NULL
	);
	
	emtr_counter_add(
		registers->active_power * interval / 1000, 
		registers->apparent_power * interval / 1000
	);
	
	user_event_raise(EMTR_URL, response);
	emtr_start_read();
	
	if (registers->event_flag != 0) {
		emtr_clear_event(registers->event_flag, NULL);
	}
}
Exemplo n.º 21
0
/*
 * json_top_level --
 *	Parse the top level JSON input.
 */
static int
json_top_level(WT_SESSION *session, JSON_INPUT_STATE *ins, uint32_t flags)
{
	CONFIG_LIST cl;
	WT_DECL_RET;
	char *config, *tableuri;
	int toktype;
	static const char *json_markers[] = {
	    "\"config\"", "\"colgroups\"", "\"indices\"", "\"data\"", NULL };

	memset(&cl, 0, sizeof(cl));
	tableuri = NULL;
	JSON_EXPECT(session, ins, '{');
	while (json_peek(session, ins) == 's') {
		JSON_EXPECT(session, ins, 's');
		tableuri = realloc(tableuri, ins->toklen);
		snprintf(tableuri, ins->toklen, "%.*s",
		    (int)(ins->toklen - 2), ins->tokstart + 1);
		JSON_EXPECT(session, ins, ':');

		/*
		 * Allow any ordering of 'config', 'colgroups',
		 * 'indices' before 'data', which must appear last.
		 * The non-'data' items build up a list of entries
		 * that created in our session before the data is
		 * inserted.
		 */
		for (;;) {
			if (json_skip(session, ins, json_markers) != 0)
				goto err;
			JSON_EXPECT(session, ins, 's');
			if (JSON_STRING_MATCH(ins, "config")) {
				JSON_EXPECT(session, ins, ':');
				JSON_EXPECT(session, ins, 's');
				if ((ret = json_strdup(ins, &config)) != 0) {
					ret = util_err(ret, NULL);
					goto err;
				}
				if ((ret = config_list_add(&cl, tableuri)) != 0)
					goto err;
				if ((ret = config_list_add(&cl, config)) != 0)
					goto err;
				tableuri = NULL;
			} else if (JSON_STRING_MATCH(ins, "colgroups")) {
				JSON_EXPECT(session, ins, ':');
				JSON_EXPECT(session, ins, '[');
				if ((ret = json_column_group_index(
				    session, ins, &cl, 0)) != 0)
					goto err;
				JSON_EXPECT(session, ins, ']');
			} else if (JSON_STRING_MATCH(ins, "indices")) {
				JSON_EXPECT(session, ins, ':');
				JSON_EXPECT(session, ins, '[');
				if ((ret = json_column_group_index(
				    session, ins, &cl, 1)) != 0)
					goto err;
				JSON_EXPECT(session, ins, ']');
			} else if (JSON_STRING_MATCH(ins, "data")) {
				JSON_EXPECT(session, ins, ':');
				JSON_EXPECT(session, ins, '[');
				if ((ret = json_data(session, ins, &cl,
				    flags)) != 0)
					goto err;
				config_list_free(&cl);
				break;
			}
			else
				goto err;
		}

		while ((toktype = json_peek(session, ins)) == '}' ||
		    toktype == ']')
			JSON_EXPECT(session, ins, toktype);
		if (toktype == 0) /* Check EOF. */
			break;
		if (toktype == ',') {
			JSON_EXPECT(session, ins, ',');
			if (json_peek(session, ins) != 's')
				goto err;
			continue;
		}
	}
	JSON_EXPECT(session, ins, 0);

	if (0) {
err:		if (ret == 0)
			ret = EINVAL;
	}
	config_list_free(&cl);
	if (tableuri != NULL)
		free(tableuri);
	return (ret);
}