Esempio n. 1
0
def all_metrics():

    logger.info("Collecting Metrics for phase: %s",
                config.CURRENT_PHASE['name'])

    dht11_values()
    #mcp3008_values()
    fan_relay_value()
    light_relay_value()
    water_relay_value()

    logger.info("Collected Metrics for phase: %s",
                config.CURRENT_PHASE['name'])
Esempio n. 2
0
def mcp3008_values():
    #   Get MCP3008 moisture sensor values:
    #       Values  Condition
    #       --------------------------
    #       0-17    sensor in open air
    #       18-424  sensor in dry soil
    #       425-689 sensor in humid soil
    #       690+    sensor in water
    logger.info("Get Soil Moisture from MCP3008 sensor...")
    # for i in range(8):
    #     sensor = MCP3008(channel=i)
    #     logger.debug("Soil Moisture CHANNEL %s: %s", i, sensor.value)

    try:
        # Sensor is OFF by default in order
        # to minimize corrosion affect
        hardware.MOISTURE_SENSOR_POWER.on()
        time.sleep(2)
        sensor_values = []
        # 10 readings 10 milliseconds apart
        for i in range(10):
            time.sleep(.01)
            sensor_values.append(hardware.MOISTURE_SENSOR.value)
        logger.debug("Soil Moisture values is: %s", sensor_values)
        moisture = utils.moisture_calibrate(sensor_values)

        hardware.MOISTURE_SENSOR_POWER.off()

        if moisture:
            MOISTURE.set(moisture)
            logger.debug("Soil Moisture is: %s", moisture)
    except Exception as e:
        logger.error("Moisture Sensor error: %s", e)
Esempio n. 3
0
def water_relay_value():
    logger.info("Water Relay get status...")
    try:
        status = hardware.WATER_RELAY.value
        WATER.set(status)
        logger.debug("Water Relay status is: %s", config.RELAY_MODES[status])
    except Exception as e:
        WATER.set(-1)
        logger.error("Get Water Relay error: %s", e)
Esempio n. 4
0
def light_relay_value():
    logger.info("Light Relay get status...")
    try:
        status = hardware.LIGHT_RELAY.value
        LIGHT.set(status)
        logger.debug("Light Relay status is: %s", config.RELAY_MODES[status])
    except Exception as e:
        LIGHT.set(-1)
        logger.error("Get Light Relay error: %s", e)
Esempio n. 5
0
def fan_relay_value():
    logger.info("Fan Relay get status...")
    try:
        status = hardware.FAN_RELAY.value
        FAN.set(status)
        logger.debug("Fan Relay status is: %s", config.RELAY_MODES[status])
    except Exception as e:
        FAN.set(-1)
        logger.error("Get Fan Relay error: %s", e)
Esempio n. 6
0
def current_phase_metrics(timedelta, index, uptime, duration):
    PHASE_DURATION.set(parse_time(duration).seconds)
    logger.debug("Current Phase Duration: %s",
                 int(PHASE_DURATION._value.get()))
    if index == 0:
        PHASE_FROM_START.set(uptime.seconds)
    else:
        PHASE_FROM_START.set((uptime - timedelta[index - 1]).seconds)
    logger.debug("Time from Phase start: %s", PHASE_FROM_START._value.get())
    PHASE_BEFORE_FINISH.set((timedelta[index] - uptime).seconds)
    logger.debug("Time before Phase finish: %s",
                 PHASE_BEFORE_FINISH._value.get())
Esempio n. 7
0
def current_phase_index(phases_timedelta, uptime):
    for index, timedelta in enumerate(phases_timedelta):
        if index == 0 and uptime <= timedelta:
            logger.debug("Current uptime %s <= %s", uptime, timedelta)
            return 0
        elif uptime > phases_timedelta[index - 1] and uptime <= timedelta:
            logger.debug("Current uptime %s > %s AND <= %s", uptime,
                         phases_timedelta[index - 1], timedelta)
            return index
            break
        elif uptime > phases_timedelta[-1]:
            raise RuntimeError("we are out of lifecycle!!!")
        else:
            logger.debug("Its not phase %s. Check next", index)
Esempio n. 8
0
def dht11_values():
    # Get DHT11 Temperature & Humidity
    # The parameter is the pin
    logger.info("Get Temperature in C & Humidity from the DHT11 sensor...")
    try:
        result = hardware.TEMP_HUMD_SENSOR.read()

        temp_c = result['temp_c']
        humidity = result['humidity']

        if result['valid']:
            TEMPERATURE.set(temp_c)
            HUMIDITY.set(humidity)
            logger.debug("Temp/Humidity is: %s, %s", temp_c, humidity)

    except Exception as e:
        TEMPERATURE.set(0)
        HUMIDITY.set(0)
        logger.error("Temp/Humid Sensor error: %s", e)
Esempio n. 9
0
def current_light_dark_mode(phase):
    current_mode = ''
    try:
        logger.info("Phase Light/Dark mode is: %s", phase['light_dark_mode'])
        light_period, dark_period = map(parse_time,
                                        phase['light_dark_mode'].split('/'))

        # Start checking from 0 second in every phase
        timedelta = datetime.timedelta(seconds=0)
        current_phase_duration = datetime.timedelta(
            seconds=int(PHASE_DURATION._value.get()))
        time_from_phase_start = datetime.timedelta(
            seconds=int(PHASE_FROM_START._value.get()))

        while timedelta <= current_phase_duration:
            # logger.debug("Light/Dark mode timedelta: %s", timedelta)
            if time_from_phase_start <= timedelta + light_period:
                current_mode = LIGHT_DARK_MODES[0]
                current_mode_duration = light_period
                time_from_mode_start = time_from_phase_start - timedelta
                time_before_mode_end = timedelta + light_period - time_from_phase_start
                break
            if time_from_phase_start >= timedelta + light_period and time_from_phase_start <= timedelta + light_period + dark_period:
                current_mode = LIGHT_DARK_MODES[1]
                current_mode_duration = dark_period
                time_from_mode_start = time_from_phase_start - timedelta - light_period
                time_before_mode_end = timedelta + light_period + dark_period - time_from_phase_start
                break
            timedelta = timedelta + light_period + dark_period

        LIGHT_DARK_MODE.state(current_mode)
        LIGHT_DARK_MODE_DURATION.set(current_mode_duration.seconds)
        LIGHT_DARK_MODE_FROM_START.set(time_from_mode_start.seconds)
        LIGHT_DARK_MODE_BEFORE_FINISH.set(time_before_mode_end.seconds)
        # current_light_dark_mode_metrics()
        logger.info("Current Light/Dark Mode: %s", current_mode)
        logger.debug("Current %s Mode Duration: %s", current_mode,
                     int(LIGHT_DARK_MODE_DURATION._value.get()))
        logger.debug("Time from %s start: %s", current_mode,
                     LIGHT_DARK_MODE_FROM_START._value.get())
        logger.debug("Time before %s finish: %s", current_mode,
                     LIGHT_DARK_MODE_BEFORE_FINISH._value.get())
    except Exception as e:
        logger.debug("Error defining current Light/Dark mode: %s", e)
Esempio n. 10
0
def current_phase(lifecycle, startup_date_time):
    try:
        # Get current date & times
        current_date_time = datetime.datetime.now()
        logger.info("Current Date/Time is %s",
                    current_date_time.strftime("%y-%m-%d %H:%M:%S"))
        current_uptime = current_date_time - startup_date_time
        logger.debug("Current Uptime: %s", current_uptime)

        # Calculate Phases timedelta
        phases_timedelta = calculate_phases_timedelta(lifecycle)

        logger.debug("Phases Timedeltas: %s", phases_timedelta)

        # Check which phase we are currently in
        current_index = current_phase_index(phases_timedelta, current_uptime)
        current_phase = lifecycle[current_index]

        logger.debug("Current Phase config:")
        for key, value in current_phase.items():
            logger.debug("%s: %s", key, value)

        PHASE_NAME.state(current_phase['name'])
        current_phase_metrics(phases_timedelta, current_index, current_uptime,
                              current_phase['duration'])
        current_light_dark_mode(current_phase)
        return current_phase

    except Exception as e:
        logger.debug("Error defining current lifecycle phase: %s", e)