Example #1
0
    def run(self):
        url = utils.read_secret("switch_url")
        token = utils.get_token(CONTEXT)
        repo = dr.device_repo()

        switch_names = []
        switch_statuses = {}

        for name in utils.retry_if_none(lambda: repo.get_device_names()):
            switch_names.append(name)

        while is_streaming:
            for name in switch_names:
                value = utils.retry_if_none(lambda: repo.get_value(name))
                if not value is None and value != switch_statuses.get(name):
                    post_switch_status(url, token, name, value)
                    switch_statuses[name] = value

                switch_status = get_switch_status(url, token, name)
                if not switch_status is None and switch_status[
                        "value"] != switch_statuses.get(name):
                    utils.retry_if_none(
                        lambda: repo.set_value(name, switch_status["value"]))
                    switch_statuses[name] = value

            time.sleep(1)

        repo.close_connection()
Example #2
0
    def run(self):
        url = utils.read_secret("setting_url")
        token = utils.get_token(CONTEXT)
        repo = setr.setting_repo()

        setting_names = []
        setting_values = {}

        for name in utils.retry_if_none(lambda: repo.get_setting_names()):
            setting_names.append(name)
            value = utils.retry_if_none(lambda: repo.get_value(name))
            setting_values[name] = value
            post_setting(url, token, name, value)

        while is_streaming:
            for name in setting_names:
                setting = get_setting(url, token, name)
                if not setting is None and setting[
                        "value"] != setting_values.get(name):
                    utils.retry_if_none(
                        lambda: repo.set_value(name, setting["value"]))
                    setting_values[name] = value

            time.sleep(1)

        repo.close_connection()
    def run(self):
        global thread

        repo = sr.sensor_repo()

        token = utils.get_token(CONTEXT)
        url = utils.read_secret("measurement_url")

        while is_streaming:
            sensor_names = utils.retry_if_none(lambda: repo.get_sensor_names())

            for name in sensor_names:
                value = utils.retry_if_none(lambda: repo.get_value(name))

                if value:
                    try:
                        r = requests.post(
                            url,
                            json={
                                "measurement": name,
                                "value": value
                            },
                            headers={'Authorization': 'Bearer ' + token})

                        if r.status_code != 200:
                            logging.warning(
                                f"[{CONTEXT}] cannot post screenshot via http")
                    except requests.exceptions.RequestException:
                        logging.warning(
                            f"[{CONTEXT}] connection error while posting screenshot"
                        )
                else:
                    logging.warning(
                        f"[{CONTEXT}] measurement {name} could not be retrieved from repo"
                    )

            time.sleep(3)

        repo.close_connection()
        thread = None
def get_variable(variable):
    if variable.get('type') == 'constant':
        return float(variable.get('value'))

    if variable.get('type') == 'unknown':
        return None

    if variable.get('type') == 'current_time':
        now = dt.datetime.now()
        hour = str(now.hour)
        minute = str(now.minute)

        if len(minute) == 1:
            return float(hour + '0' + minute)
        else:
            return float(hour + minute)

    if variable.get('type') == 'sensor':
        value = utils.retry_if_none(
            lambda: sensor_repo.get_value(variable.get('value')))

        if value is None:
            return None

        addition = variable.get('addition')

        if addition is None:
            return float(value)
        else:
            return float(value) + float(addition)

    if variable.get('type') == 'setting':
        value = utils.retry_if_none(
            lambda: setting_repo.get_value(variable.get('value')))
        addition = variable.get('addition')

        if addition is None:
            return float(value)
        else:
            return float(value) + float(addition)
Example #5
0
                pulse_end = time.time()
                
            pulse_time = utils.time_diff(pulse_start, pulse_end)
            if pulse_time < 0.005:
                measurements.append(round(MAX_VOLUME - pulse_time * (SPEED_OF_SOUND / 2) * SURFACE_AREA, 2))
            else:
                logging.warning(f"[{CONTEXT}] invalid measurement")
                
            time.sleep(0.01)
        
        measurements.sort()
        volume = round(sum(measurements[75:225]) / 150, 2)
        
        if not volume is None:
            measurements_avg.append(volume)
            utils.retry_if_none(lambda : repo.set_value(CONTEXT, volume))
        else:
            logging.warning(f"[{CONTEXT}] invalid measurement")
        
        if len(measurements_avg) >= 100:
            utils.retry_if_none(lambda : repo.set_value(WATERVOLUME_AVG, round(sum(measurements_avg) / len(measurements_avg), 2)))
            measurements_avg = []
        
except KeyboardInterrupt:
    pass
except:
    logging.exception(f"[{CONTEXT}] general error")
finally:
    repo.close_connection()
    GPIO.cleanup(PIN_TRIGGER)
    GPIO.cleanup(PIN_ECHO)
Example #6
0
CONTEXT = "light"
LIGHT_CHANNEL = 7

utils.setup_logging(CONTEXT)
repo = sr.sensor_repo()
adc = MCP3008()

try:    
    while True:
        measurements = []
        while len(measurements) < 300:
            value = adc.read( channel = LIGHT_CHANNEL )
            measurements.append(value)
            time.sleep(0.01)
    
        measurements.sort()
        light = round(sum(measurements[75:225]) / (150 * 10.23), 2)
        
        if not light is None:
            utils.retry_if_none(lambda : repo.set_value(CONTEXT, light))
        else:
            logging.warning(f"[{CONTEXT}] invalid measurement")
except KeyboardInterrupt:
    pass
except:
    logging.exception(f"[{CONTEXT}] general error")
finally:
    adc.close()
    repo.close_connection()
    
CONTEXT = "flow_control"
PUMP_RF = "pomp_rechts_voor"
FLOW_LOW = "stroming_laag_tijdstip"
FLOW_HIGH = "stroming_hoog_tijdstip"

utils.setup_logging(CONTEXT)
device_repo = dr.device_repo()
setting_repo = setr.setting_repo()

old = dt.datetime.now().time()

try:
    while True:
        flow_low_at = utils.parse_string_to_time(
            CONTEXT,
            utils.retry_if_none(lambda: setting_repo.get_value(FLOW_LOW)))
        flow_high_at = utils.parse_string_to_time(
            CONTEXT,
            utils.retry_if_none(lambda: setting_repo.get_value(FLOW_HIGH)))

        if flow_low_at is None or flow_high_at is None:
            logging.warning(
                f"[{CONTEXT}] flow high/low time could be retrieved")
            continue

        now = dt.datetime.now().time()

        if (now >= flow_high_at and old < flow_high_at):
            utils.retry_if_none(lambda: device_repo.set_value(PUMP_RF, True))

        if (now >= flow_low_at and old < flow_low_at):
import device_repo as dr
import sensor_repo as sr
import utils

CONTEXT = "relay_sensors"
THRESHOLD = 20

utils.setup_logging(CONTEXT)
device_repo = dr.device_repo()
sensor_repo = sr.sensor_repo()
ads = ADS1115()

try:
    while True:
        relay_sensors = utils.retry_if_none(
            lambda: device_repo.get_relay_sensors())
        for relay_sensor in relay_sensors:
            device = utils.retry_if_none(
                lambda: device_repo.get_device(relay_sensor['relay']))
            device_name = utils.retry_if_none(
                lambda: device_repo.get_device_name(device))

            if (device_name is None):
                continue

            measurements = []
            tries = 0
            while len(measurements) < 100 and tries < 200:
                try:
                    tries = tries + 1
                    measurements.append(
CONTEXT = "light_control"
LIGHT = "verlichting"
LIGHTS_ON = "verlichting_aan_tijdstip"
LIGHTS_OFF = "verlichting_uit_tijdstip"

utils.setup_logging(CONTEXT)
device_repo = dr.device_repo()
setting_repo = setr.setting_repo()

old = dt.datetime.now().time()

try:
    while True:
        lights_on_at = utils.parse_string_to_time(
            CONTEXT,
            utils.retry_if_none(lambda: setting_repo.get_value(LIGHTS_ON)))
        lights_off_at = utils.parse_string_to_time(
            CONTEXT,
            utils.retry_if_none(lambda: setting_repo.get_value(LIGHTS_OFF)))

        if lights_on_at is None or lights_off_at is None:
            logging.warning(
                f"[{CONTEXT}] lights on/off time could be retrieved")
            continue

        now = dt.datetime.now().time()

        if (now >= lights_on_at and old < lights_on_at):
            utils.retry_if_none(lambda: device_repo.set_value(LIGHT, True))

        if (now >= lights_off_at and old < lights_off_at):
Example #10
0
CONTEXT = "leakage"
LEAKAGE_CHANNEL = 6

utils.setup_logging(CONTEXT)
repo = sr.sensor_repo()
adc = MCP3008()

try:
    while True:
        measurements = []
        while len(measurements) < 50:
            value = adc.read(channel=LEAKAGE_CHANNEL)
            measurements.append(value)
            time.sleep(0.01)

        measurements.sort()
        leakage = 100 - round(sum(measurements[10:40]) / (30 * 10.23), 2)

        if not leakage is None:
            utils.retry_if_none(lambda: repo.set_value(CONTEXT, leakage))
        else:
            logging.warning(f"[{CONTEXT}] invalid measurement")
except KeyboardInterrupt:
    pass
except:
    logging.exception(f"[{CONTEXT}] general error")
finally:
    adc.close()
    repo.close_connection()
Example #11
0
    "name": "doseerpomp_2",
    "volume": "doseerpomp_2_volume"
}]

VOLUME_PM = 87

utils.setup_logging(CONTEXT)
device_repo = dr.device_repo()
setting_repo = setr.setting_repo()

old = dt.datetime.now().time()

try:
    while True:
        for pump in DOSING_PUMPS:
            utils.retry_if_none(
                lambda: device_repo.set_value(pump["name"], False))

        dosing_at = utils.parse_string_to_time(
            CONTEXT,
            utils.retry_if_none(lambda: setting_repo.get_value(DOSING_AT)))

        if dosing_at is None:
            logging.warning(f"[{CONTEXT}] dosing time could not be retrieved")
            continue

        now = dt.datetime.now().time()

        if now >= dosing_at and old < dosing_at:
            for pump in DOSING_PUMPS:
                pump_name = pump["name"]
                try:
WATER_TOPOFF_AT = "water_bijvul_tijdstip"
WATERVOLUME_TARGET = "watervolume_streefwaarde"
WATERVOLUME_AVG = "watervolume_avg"
MAX_TOPOFF_VOLUME = 2
VOLUME_PM = 0.5

utils.setup_logging(CONTEXT)
device_repo = dr.device_repo()
sensor_repo = sr.sensor_repo()
setting_repo = setr.setting_repo()

old = dt.datetime.now().time()

try:
    while True:
        utils.retry_if_none(lambda: device_repo.set_value(ATO, False))

        if utils.retry_if_none(
                lambda: sensor_repo.get_value(FLOATSENSOR, 0)) == "1":
            logging.warning(f"[{CONTEXT}] water level too high")

        water_topoff_at = utils.parse_string_to_time(
            CONTEXT,
            utils.retry_if_none(
                lambda: setting_repo.get_value(WATER_TOPOFF_AT)))

        if water_topoff_at is None:
            logging.warning(
                f"[{CONTEXT}] water topoff time could not be retrieved")
            continue
Example #13
0
PIN = 36

utils.setup_logging(CONTEXT)
repo = sr.sensor_repo()

GPIO.setmode(GPIO.BOARD)
GPIO.setup(PIN, GPIO.IN)

try:
    repo.set_value(CONTEXT, "0")
    high_level_cnt = 0
    while True:
        if GPIO.input(PIN) == 0:
            high_level_cnt += 1
        else:
            time.sleep(5)
            utils.retry_if_none(lambda: repo.set_value(CONTEXT, "0"))

            high_level_cnt = 0

        if high_level_cnt > 100:
            utils.retry_if_none(lambda: repo.set_value(CONTEXT, "1"))

        time.sleep(0.1)
except KeyboardInterrupt:
    pass
except:
    logging.exception(f"[{CONTEXT}] general error")
finally:
    GPIO.cleanup(PIN)
import sensor_repo as sr
import utils

AMBIENT = "ambient"
CONTEXT = "ambienttemperature"

os.system("modprobe w1_gpio")
os.system("modprobe w1_therm")

utils.setup_logging(CONTEXT)
repo = sr.sensor_repo()

try:
    while True:
        temp_sensors = utils.retry_if_none(lambda: repo.get_temp_sensors())
        if temp_sensors:
            temp_ids = [key for (key, value) in temp_sensors.items() if value == AMBIENT]
            
            if len(temp_ids) == 1:
                temp = utils.read_temp(temp_ids[0], "[" + CONTEXT + "]")

                if temp and temp > 10 and temp < 40 :
                    utils.retry_if_none(lambda : repo.set_value(CONTEXT, temp))
                else:
                    logging.warning(f"[{CONTEXT}] invalid measurement")
            else:
                logging.warning(f"[{CONTEXT}] could not find ambient temperature sensor")
        else:
            logging.warning(f"[{CONTEXT}] temp sensors could not be retrieved from repo")
        
            pulse_end = time.time()
            while GPIO.input(PIN_ECHO) == 1 and utils.time_diff(
                    ref, pulse_end) < 0.01:
                pulse_end = time.time()

            pulse_time = utils.time_diff(pulse_start, pulse_end)
            if pulse_time < 0.005:
                measurements.append(
                    round(
                        MAX_VOLUME - pulse_time *
                        (SPEED_OF_SOUND / 2) * SURFACE_AREA, 2))
            else:
                logging.warning(f"[{CONTEXT}] invalid measurement")

            time.sleep(0.005)

        measurements.sort()
        volume = round(sum(measurements[75:225]) / 150, 2)

        if not volume is None:
            utils.retry_if_none(lambda: repo.set_value(CONTEXT, volume))
        else:
            logging.warning(f"[{CONTEXT}] invalid measurement")

except KeyboardInterrupt:
    pass
except:
    logging.exception(f"[{CONTEXT}] general error")
finally:
    repo.close_connection()
COOLING = "ventilatoren"
HEATING_THRESHOLD = "verwarming_drempel"
COOLING_THRESHOLD = "ventilator_drempel"
TEMP = "watertemperature"
MAX_QUERIES = 100

utils.setup_logging(CONTEXT)

sensor_repo = sr.sensor_repo()
setting_repo = setr.setting_repo()
device_repo = dr.device_repo()

try:
    while True:
        temp_heating_threshold = float(
            utils.retry_if_none(
                lambda: setting_repo.get_value(HEATING_THRESHOLD)))
        temp_cooling_threshold = float(
            utils.retry_if_none(
                lambda: setting_repo.get_value(COOLING_THRESHOLD)))

        temps = []
        queries = 0
        while len(temps) < 60 and queries < MAX_QUERIES:
            queries += 1
            temp = utils.retry_if_none(lambda: sensor_repo.get_value(TEMP))
            if temp:
                try:
                    temps.append(float(temp))
                except ValueError:
                    logging.warning(f"[{CONTEXT}] measurement is not a float")
            else: