Example #1
0
def _parse_room(session, room_model):
    room = dict(name=room_model.name,
                id=room_model.id,
                active=room_model.active)

    if room_model.sensors:
        sensors = []
        measurement_agg = 0
        measurement_cnt = 0
        current_temp_recorded_date = None

        for sensor_model in room_model.sensors:
            sensor = dict(id=sensor_model.id,
                          name=sensor_model.name,
                          manufacturer_id=sensor_model.manufacturer_id,
                          model=sensor_model.sensor_type.model,
                          manufacturer=sensor_model.sensor_type.manufacturer)

            measurement = db.get_most_recent_sensor_temperature(
                session,
                sensor_id=sensor_model.id,
                order_desc=True,
                order_by=models.Measurement.recorded_date)

            if measurement and measurement.recorded_date > datetime.now(
            ) - timedelta(minutes=12):
                measurement_agg = measurement_agg + (measurement.measurement *
                                                     room_model.weight)
                measurement_cnt = measurement_cnt + room_model.weight
                sensor['current_temp'] = measurement.measurement
                sensor['last_temp_recorded_date'] = measurement.recorded_date

                if not current_temp_recorded_date or measurement.recorded_date > current_temp_recorded_date:
                    current_temp_recorded_date = measurement.recorded_date

            sensors.append(sensor)

        if measurement_cnt > 0 and measurement_agg > 0:
            room['current_temp'] = measurement_agg / measurement_cnt

        room['sensors'] = sensors
        if current_temp_recorded_date:
            room[
                'current_temp_recorded_date'] = current_temp_recorded_date.strftime(
                    '%A, %m/%d/%y')
            room[
                'current_temp_recorded_time'] = current_temp_recorded_date.strftime(
                    '%I:%M:%S %p')
    return room
Example #2
0
def _parse_room(session, room_model):
    room = dict(name=room_model.name, id=room_model.id, active=room_model.active)

    if room_model.sensors:
        sensors = []
        measurement_agg = 0
        measurement_cnt = 0
        current_temp_recorded_date = None

        for sensor_model in room_model.sensors:
            sensor = dict(
                id=sensor_model.id,
                name=sensor_model.name,
                manufacturer_id=sensor_model.manufacturer_id,
                model=sensor_model.sensor_type.model,
                manufacturer=sensor_model.sensor_type.manufacturer,
            )

            measurement = db.get_most_recent_sensor_temperature(
                session, sensor_id=sensor_model.id, order_desc=True, order_by=models.Measurement.recorded_date
            )

            if measurement and measurement.recorded_date > datetime.now() - timedelta(minutes=12):
                measurement_agg = measurement_agg + (measurement.measurement * room_model.weight)
                measurement_cnt = measurement_cnt + room_model.weight
                sensor["current_temp"] = measurement.measurement
                sensor["last_temp_recorded_date"] = measurement.recorded_date

                if not current_temp_recorded_date or measurement.recorded_date > current_temp_recorded_date:
                    current_temp_recorded_date = measurement.recorded_date

            sensors.append(sensor)

        if measurement_cnt > 0 and measurement_agg > 0:
            room["current_temp"] = measurement_agg / measurement_cnt

        room["sensors"] = sensors
        if current_temp_recorded_date:
            room["current_temp_recorded_date"] = current_temp_recorded_date.strftime("%A, %m/%d/%y")
            room["current_temp_recorded_time"] = current_temp_recorded_date.strftime("%I:%M:%S %p")
    return room
Example #3
0
    def do(session):
        temp_agg = 0
        temp_cnt = 0

        room_models = db.get_rooms_dashboard(session)

        for room_model in room_models:
            if not room_model.active:
                continue

            room_temp = None
            measurement_agg = 0
            measurement_cnt = 0

            if room_model.sensors:
                for sensor_model in room_model.sensors:
                    measurement = db.get_most_recent_sensor_temperature(
                        session,
                        sensor_id=sensor_model.id,
                        order_desc=True,
                        order_by=models.Measurement.recorded_date)

                    if measurement and measurement.recorded_date > datetime.now(
                    ) - timedelta(minutes=12):
                        measurement_agg = measurement.measurement
                        measurement_cnt = measurement_cnt + 1

            if measurement_cnt > 0 and measurement_agg > 0:
                room_temp = measurement_agg / measurement_cnt

            if room_temp:
                temp_agg = temp_agg + (room_temp * room_model.weight)
                temp_cnt = temp_cnt + room_model.weight

        if not temp_cnt and not temp_agg:
            raise Exception('No current temperature data found...')

        return temp_agg / temp_cnt
Example #4
0
    def do(session):
        temp_agg = 0
        temp_cnt = 0

        room_models = db.get_rooms_dashboard(session)

        for room_model in room_models:
            if not room_model.active:
                continue

            room_temp = None
            measurement_agg = 0
            measurement_cnt = 0

            if room_model.sensors:
                for sensor_model in room_model.sensors:
                    measurement = db.get_most_recent_sensor_temperature(session,
                                                                        sensor_id=sensor_model.id,
                                                                        order_desc=True,
                                                                        order_by=models.Measurement.recorded_date)

                    if measurement and measurement.recorded_date > datetime.now() - timedelta(minutes=12):
                        measurement_agg = measurement.measurement
                        measurement_cnt = measurement_cnt + 1

            if measurement_cnt > 0 and measurement_agg > 0:
                room_temp = measurement_agg / measurement_cnt

            if room_temp:
                temp_agg = temp_agg + (room_temp * room_model.weight)
                temp_cnt = temp_cnt + room_model.weight

        if not temp_cnt and not temp_agg:
            raise Exception('No current temperature data found...')

        return temp_agg / temp_cnt