q='select mean("temperature_C") as temperature_C from temperature where "name" = \'{}\' and time >= {}s and time < {}s group by time({}) fill(linear)'.format(name,start,end,window)
q='select mean("temperature_C") as temperature_C, mean("humidity") as humidity from temperature where "name" = \'{}\' and time >= {}s and time < {}s group by time({}) fill(linear)'.format(name,start,end,window)
elifmeasurement=='ertscm':
client=sensors_client
q='select derivative(max("consumption_data"))*{} as delta, max("consumption_data")*{} as max from ertscm where "name" = \'{}\' and time >= {}s and time < {}s group by time({}) fill(previous)'.format(scale,scale,name,start,end,window)
elifmeasurement=='thermostat':
client=sensors_client
q='select first("spacetemp") as spacetemp, first("heattemp") as heattemp, first("state") as state from thermostat where "name" = \'{}\' and time >= {}s and time < {}s group by time({}) fill(previous)'.format(name,start,end,window)
q='select first("spacetemp") as spacetemp, first("heattemp") as heattemp, mode("state") as state from thermostat where "name" = \'{}\' and time >= {}s and time < {}s group by time({}) fill(previous)'.format(name,start,end,window)
elifmeasurement=='dust':
client=sensors_client
q='select max("avg_p10") as max_p10, max("avg_p25") as max_p25 from dust where "name" = \'{}\' and time >= {}s and time < {}s group by time({}) fill(linear)'.format(name,start,end,window)
elifmeasurement=='air':
client=sensors_client
q='select max("pm10") as max_p10, max("pm25") as max_p25, max("co2") as max_co2, max("voc_idx") as max_voc from air where "name" = \'{}\' and time >= {}s and time < {}s group by time({}) fill(linear)'.format(name,start,end,window)
elifmeasurement=='lux':
client=sensors_client
q='select mean("lux") as lux from air where "name" = \'{}\' and time >= {}s and time < {}s group by time({}) fill(linear)'.format(name,start,end,window)
elifmeasurement=='sleep':
client=sensors_client
q='select max("max_mag") as max_mag from sleep where "name" = \'{}\' and time >= {}s and time < {}s group by time({}) fill(linear)'.format(name,start,end,window)
elifmeasurement=='solar':
client=solar_client
q='select max("actual_total") as actual_total from ecu where time >= {}s and time < {}s group by time({}) fill(linear)'.format(start,end,window)
q='select max("actual_total") as actual_total, last("lifetime_energy")-first("lifetime_energy") as lifetime_energy from ecu where time >= {}s and time < {}s group by time({}) fill(linear)'.format(start,end,window)
else:
raise
q+=' tz(\'America/Edmonton\')'
#if window and moving_average:
# q = 'select moving_average(mean("value"),{}) as value from {} where "name" = \'{}\' and time >= {}s and time < {}s group by time({}m) fill(none)'.format(moving_average, measurement, name, start, end, window)
#elif window:
# q = 'select mean("value") as value from {} where "name" = \'{}\' and time >= {}s and time < {}s group by time({}m) fill(none)'.format(measurement, name, start, end, window)
#elif moving_average:
# q = 'select moving_average("value", {}) as value from {} where "name" = \'{}\' and time >= {}s and time < {}s'.format(moving_average, name, start, end)
#else:
# q = 'select value from {} where "name" = \'{}\' and time >= {}s and time < {}s'.format(measurement, name, start, end)
result=list(client.query(q).get_points())
returnweb.json_response(result)
asyncdeflatest(request):
result=dict()
forsensorinsensors:
ifsensor.type_=='solar':
continue
q='select * from {} where "name" = \'{}\' order by desc limit 1'.format(sensor.type_,sensor.name)