示例#1
0
static void JSONSchedule(const KVPairs & key_value_pairs, FILE * stream_file)
{
	int sched_num = -1;
	freeMemory();

	// Iterate through the kv pairs and search for the id.
	for (int i = 0; i < key_value_pairs.num_pairs; i++)
	{
		const char * key = key_value_pairs.keys[i];
		const char * value = key_value_pairs.values[i];
		if (strcmp(key, "id") == 0)
		{
			sched_num = atoi(value);
		}
	}

	// Now check to see if the id is in range.
	const uint8_t numSched = GetNumSchedules();
	if ((sched_num < 0) || (sched_num >= numSched))
	{
		ServeError(stream_file);
		return;
	}

	// Now construct the response and send it
	ServeHeader(stream_file, 200, "OK", false, "text/plain");
	Schedule sched;
	LoadSchedule(sched_num, &sched);
	fprintf_P(stream_file,
			PSTR("{\n\t\"name\" : \"%s\",\n\t\"enabled\" : \"%s\",\n\t\"wadj\" : \"%s\",\n\t\"type\" : \"%s\",\n\t\"d1\" : \"%s\",\n\t\"d2\" : \"%s\""),
			sched.name, sched.IsEnabled() ? "on" : "off", sched.IsWAdj() ? "on" : "off", sched.IsInterval() ? "off" : "on", sched.day & 0x01 ? "on" : "off",
			sched.day & 0x02 ? "on" : "off");
	fprintf_P(stream_file,
			PSTR(",\n\t\"d3\" : \"%s\",\n\t\"d4\" : \"%s\",\n\t\"d5\" : \"%s\",\n\t\"d6\" : \"%s\",\n\t\"d7\" : \"%s\",\n\t\"interval\" : \"%d\",\n\t\"times\" : [\n"),
			sched.day & 0x04 ? "on" : "off", sched.day & 0x08 ? "on" : "off", sched.day & 0x10 ? "on" : "off", sched.day & 0x20 ? "on" : "off",
			sched.day & 0x40 ? "on" : "off", sched.interval);
	for (int i = 0; i < 4; i++)
	{
		if (sched.time[i] == -1)
		{
			fprintf_P(stream_file, PSTR("%s\t\t{\"t\" : \"00:00\", \"e\" : \"off\" }"), (i == 0) ? "" : ",\n");
		}
		else
		{
			fprintf_P(stream_file, PSTR("%s\t\t{\"t\" : \"%02d:%02d\", \"e\" : \"on\" }"), (i == 0) ? "" : ",\n", sched.time[i] / 60, sched.time[i] % 60);
		}
	}
	fprintf_P(stream_file, PSTR("\n\t],\n\t\"zones\" : [\n"));
	for (int i = 0; i < NUM_ZONES; i++)
	{
		FullZone zone;
		LoadZone(i, &zone);
		fprintf(stream_file, "%s\t\t{\"name\" : \"%s\", \"e\":\"%s\", \"duration\" : %d}", (i == 0) ? "" : ",\n", zone.name, zone.bEnabled ? "on" : "off",
				sched.zone_duration[i]);
	}
	fprintf(stream_file, " ]\n}");
}
示例#2
0
static void ServeSchedPage(FILE * stream_file)
{
	Schedule sched;
	freeMemory();
	ServeHeader(stream_file, 200, "OK", false);
	const uint8_t numSched = GetNumSchedules();
	for (uint8_t iSchedNum = 0; iSchedNum < numSched; iSchedNum++)
	{
		LoadSchedule(iSchedNum, &sched);
		fprintf_P(stream_file, PSTR("<hr/>Schedule #%d<br/>"), iSchedNum);
		if (sched.IsEnabled())
			fprintf_P(stream_file, PSTR("Enabled"));
		else
			fprintf_P(stream_file, PSTR("Not Enabled"));
		fprintf_P(stream_file, PSTR("<br/>Name:%s<br/>"), sched.name);
		if (sched.IsInterval())
			fprintf_P(stream_file, PSTR("Interval : %d"), sched.interval);
		else
		{
			fprintf_P(stream_file, PSTR("Day :"));
			if (sched.day & 0x01)
				fprintf(stream_file, "Su");
			if (sched.day & 0x02)
				fprintf(stream_file, "M");
			if (sched.day & 0x04)
				fprintf(stream_file, "T");
			if (sched.day & 0x08)
				fprintf(stream_file, "W");
			if (sched.day & 0x10)
				fprintf(stream_file, "R");
			if (sched.day & 0x20)
				fprintf(stream_file, "F");
			if (sched.day & 0x40)
				fprintf(stream_file, "Sa");
			fprintf_P(stream_file, PSTR("(%d)"), sched.day);
		}
		for (uint8_t i = 0; i < 4; i++)
			fprintf_P(stream_file, PSTR("<br/>Time %d:%02d:%02d(%d)"), i + 1, sched.time[i] / 60, sched.time[i] % 60, sched.time[i]);
		for (uint8_t i = 0; i < NUM_ZONES; i++)
			fprintf_P(stream_file, PSTR("<br/>Zone %d Duration:%d"), i + 1, sched.zone_duration[i]);
	}
}