Exemplo n.º 1
0
def runLightSchedule(api, lightOnTime, lightCyclePeriod):
    while True:
        try:
            print "Running light schedule in background"
            results, code = getLightStatuses()
            results = json.loads(results.response[0])
            results = dict([(float(el[0]), el[1]) for el in results])
            if results[0] == False:
                sc.turnOn(api, light_0, "d")
                cur, con = dbc.connectToDB('localhost', 5432, 'postgres',
                                           'postgres', 'homegrowplus')
                cur.execute(
                    """UPDATE lights SET status = '{}' WHERE id = {}""".format(
                        'true', 0))
                con.commit()
                dbc.disconnectFromDB(con)
                time.sleep(lightOnTime)
            results, code = getLightStatuses()
            results = json.loads(results.response[0])
            results = dict([(float(el[0]), el[1]) for el in results])
            if results[0] == True:
                sc.turnOff(api, light_0, "d")
                cur, con = dbc.connectToDB('localhost', 5432, 'postgres',
                                           'postgres', 'homegrowplus')
                cur.execute(
                    """UPDATE lights SET status = '{}' WHERE id = {}""".format(
                        'false', 0))
                con.commit()
                dbc.disconnectFromDB(con)
                time.sleep(lightCyclePeriod)
        except Exception as e:
            print "Failed to run the light schedule"
            print e
Exemplo n.º 2
0
def startMixer():
    try:
        results, code = getPumpSensors()
        results = json.loads(results.response[0])
        results = dict([((el[0]), float(el[1])) for el in results])
        if results['mixer_empty'] < 2.5:
            print "Mixer it too low. Failed to start the mixer motor"
            return jsonify(), 503
        sc.turnOn(api, mixer_0, "d")
        cur, con = dbc.connectToDB('localhost', 5432, 'postgres', 'postgres',
                                   'homegrowplus')
        cur.execute(
            """UPDATE mixers SET status = {} WHERE name = '{}'""".format(
                "true", "mixer_0"))
        con.commit()
        dbc.disconnectFromDB(con)
        time.sleep(5)
        sc.turnOff(api, mixer_0, "d")
        cur, con = dbc.connectToDB('localhost', 5432, 'postgres', 'postgres',
                                   'homegrowplus')
        cur.execute(
            """UPDATE mixers SET status = {} WHERE name = '{}'""".format(
                "false", "mixer_0"))
        con.commit()
    except Exception as e:
        print "Failed to set mixer status"
        print e
    finally:
        dbc.disconnectFromDB(con)
        return jsonify(), 200
Exemplo n.º 3
0
def setPumpStatus():
    try:
        results, code = getPumpSensors()
        results = json.loads(results.response[0])
        results = dict([((el[0]), float(el[1])) for el in results])
        t = "d"
        if request.json['on'] == True:
            if (request.json['pump'] == "clean_water"
                    or request.json['pump'] == "nutrients"
                    or request.json['pump'] == "acid" or request.json['pump']
                    == "base") and results['mixer_full'] > 2.5:
                print "Tried to pump with mixer too full"
                return jsonify(), 503
            elif request.json['pump'] == "drain_water":
                if results['drain_water'] > 0:
                    print "Tried to pump with drain too full"
                    return jsonify(), 503
            elif request.json['pump'] == "mixer":
                if results['mixer_empty'] < 2.5:
                    print "Tried to pump with mixer empty"
                    return jsonify(), 503
            elif request.json['pump'] == "mister":
                if results['clean_water'] == 0:
                    print "Tries to use mister while clean water is low"
                    return jsonify(), 503
            elif request.json['pump'] == "drain_out":
                pass
            elif (request.json['pump'] != "drain_water"
                  or request.json['pump'] != "mixer"
                  or request.json['pump'] != "mister" or request.json['pump']
                  != "drain_out") and results[request.json['pump']] == 0:
                print "Tried to pump with bad low fluid level on {}".format(
                    request.json['pump'])
                return jsonify(), 503
            sc.turnOn(api, pins["{}_pump".format(request.json['pump'])], t)
        else:
            sc.turnOff(api, pins["{}_pump".format(request.json['pump'])], t)
        cur, con = dbc.connectToDB('localhost', 5432, 'postgres', 'postgres',
                                   'homegrowplus')
        cur.execute(
            """UPDATE pumps SET status = {} WHERE name = '{}'""".format(
                request.json['on'], request.json['pump']))
    except Exception as e:
        print "Failed to set pump status"
        print e
    finally:
        con.commit()
        dbc.disconnectFromDB(con)
        return jsonify(), 200
Exemplo n.º 4
0
def setLightStatus():
    try:
        if request.json['on'] is True:
            sc.turnOn(api, light_0, "d")
        else:
            sc.turnOff(api, light_0, "d")
        cur, con = dbc.connectToDB('localhost', 5432, 'postgres', 'postgres',
                                   'homegrowplus')
        cur.execute("""UPDATE lights SET status = {} WHERE id = {}""".format(
            request.json['on'], request.json['light']))
    except Exception as e:
        print "Failed to set light status"
        print e
        return jsonify, 503
    finally:
        con.commit()
        dbc.disconnectFromDB(con)
        return jsonify(), 200
Exemplo n.º 5
0
def fillMixer():
    while True:
        try:
            print "Filling mixer"
            cur, con = dbc.connectToDB('localhost', 5432, 'postgres',
                                       'postgres', 'homegrowplus')
            cur.execute("""SELECT * FROM conductivity_sensors""")
            results = cur.fetchall()
            dbc.disconnectFromDB(con)
            results = dict([((el[0]), float(el[1])) for el in results])
            if results['mixer_full'] > 2.5:
                print "Mixer is full"
                startMixer()
                break
            else:
                print "Mixer still not full"
                cur, con = dbc.connectToDB('localhost', 5432, 'postgres',
                                           'postgres', 'homegrowplus')
                cur.execute("""SELECT * FROM pumps""")
                pump_results = cur.fetchall()
                dbc.disconnectFromDB(con)
                pump_results = dict([((el[0]), el[1]) for el in pump_results])
                cur, con = dbc.connectToDB('localhost', 5432, 'postgres',
                                           'postgres', 'homegrowplus')
                cur.execute("""SELECT * FROM conductivity_sensors""")
                results = cur.fetchall()
                dbc.disconnectFromDB(con)
                results = dict([((el[0]), float(el[1])) for el in results])
                if pump_results['clean_water'] == False and results[
                        'mixer_full'] < 2.5:
                    print "Filling mixer with clean water"
                    sc.turnOn(api, clean_water_pump, "d")
                    cur, con = dbc.connectToDB('localhost', 5432, 'postgres',
                                               'postgres', 'homegrowplus')
                    cur.execute(
                        """UPDATE pumps SET status = {} WHERE name = '{}'""".
                        format('true', 'clean_water'))
                    con.commit()
                    dbc.disconnectFromDB(con)
                    time.sleep(3)
                cur, con = dbc.connectToDB('localhost', 5432, 'postgres',
                                           'postgres', 'homegrowplus')
                cur.execute("""SELECT * FROM pumps""")
                pump_results = cur.fetchall()
                dbc.disconnectFromDB(con)
                pump_results = dict([((el[0]), el[1]) for el in pump_results])
                if pump_results['clean_water'] == True:
                    sc.turnOff(api, clean_water_pump, "d")
                    cur, con = dbc.connectToDB('localhost', 5432, 'postgres',
                                               'postgres', 'homegrowplus')
                    cur.execute(
                        """UPDATE pumps SET status = {} WHERE name = '{}'""".
                        format('false', 'clean_water'))
                    con.commit()
                    dbc.disconnectFromDB(con)
                cur, con = dbc.connectToDB('localhost', 5432, 'postgres',
                                           'postgres', 'homegrowplus')
                cur.execute("""SELECT * FROM pumps""")
                pump_results = cur.fetchall()
                dbc.disconnectFromDB(con)
                pump_results = dict([((el[0]), el[1]) for el in pump_results])
                cur, con = dbc.connectToDB('localhost', 5432, 'postgres',
                                           'postgres', 'homegrowplus')
                cur.execute("""SELECT * FROM conductivity_sensors""")
                results = cur.fetchall()
                dbc.disconnectFromDB(con)
                results = dict([((el[0]), float(el[1])) for el in results])
                if pump_results[
                        'nutrients'] == False and results['mixer_full'] < 2.5:
                    print "Filling mixer with nutrients"
                    sc.turnOn(api, nutrients_pump, "d")
                    cur, con = dbc.connectToDB('localhost', 5432, 'postgres',
                                               'postgres', 'homegrowplus')
                    cur.execute(
                        """UPDATE pumps SET status = {} WHERE name = '{}'""".
                        format('true', 'nutrients'))
                    con.commit()
                    dbc.disconnectFromDB(con)
                    time.sleep(1)
                cur, con = dbc.connectToDB('localhost', 5432, 'postgres',
                                           'postgres', 'homegrowplus')
                cur.execute("""SELECT * FROM pumps""")
                pump_results = cur.fetchall()
                dbc.disconnectFromDB(con)
                pump_results = dict([((el[0]), el[1]) for el in pump_results])
                if pump_results['nutrients'] == True:
                    sc.turnOff(api, nutrients_pump, "d")
                    cur, con = dbc.connectToDB('localhost', 5432, 'postgres',
                                               'postgres', 'homegrowplus')
                    cur.execute(
                        """UPDATE pumps SET status = {} WHERE name = '{}'""".
                        format('false', 'nutrients'))
                    con.commit()
                    dbc.disconnectFromDB(con)
                cur, con = dbc.connectToDB('localhost', 5432, 'postgres',
                                           'postgres', 'homegrowplus')
                cur.execute("""SELECT * FROM pumps""")
                pump_results = cur.fetchall()
                dbc.disconnectFromDB(con)
                pump_results = dict([((el[0]), el[1]) for el in pump_results])
                cur, con = dbc.connectToDB('localhost', 5432, 'postgres',
                                           'postgres', 'homegrowplus')
                cur.execute("""SELECT * FROM conductivity_sensors""")
                results = cur.fetchall()
                dbc.disconnectFromDB(con)
                results = dict([((el[0]), float(el[1])) for el in results])
                if pump_results[
                        'acid'] == False and results['mixer_full'] < 2.5:
                    print "Filling mixer with acid"
                    sc.turnOn(api, acid_pump, "d")
                    cur, con = dbc.connectToDB('localhost', 5432, 'postgres',
                                               'postgres', 'homegrowplus')
                    cur.execute(
                        """UPDATE pumps SET status = {} WHERE name = '{}'""".
                        format('true', 'acid'))
                    con.commit()
                    dbc.disconnectFromDB(con)
                    time.sleep(1)
                cur, con = dbc.connectToDB('localhost', 5432, 'postgres',
                                           'postgres', 'homegrowplus')
                cur.execute("""SELECT * FROM pumps""")
                pump_results = cur.fetchall()
                dbc.disconnectFromDB(con)
                pump_results = dict([((el[0]), el[1]) for el in pump_results])
                if pump_results['acid'] == True:
                    sc.turnOff(api, acid_pump, "d")
                    cur, con = dbc.connectToDB('localhost', 5432, 'postgres',
                                               'postgres', 'homegrowplus')
                    cur.execute(
                        """UPDATE pumps SET status = {} WHERE name = '{}'""".
                        format('false', 'acid'))
                    con.commit()
                    dbc.disconnectFromDB(con)
                cur, con = dbc.connectToDB('localhost', 5432, 'postgres',
                                           'postgres', 'homegrowplus')
                cur.execute("""SELECT * FROM pumps""")
                pump_results = cur.fetchall()
                dbc.disconnectFromDB(con)
                pump_results = dict([((el[0]), el[1]) for el in pump_results])
                cur, con = dbc.connectToDB('localhost', 5432, 'postgres',
                                           'postgres', 'homegrowplus')
                cur.execute("""SELECT * FROM conductivity_sensors""")
                results = cur.fetchall()
                dbc.disconnectFromDB(con)
                results = dict([((el[0]), float(el[1])) for el in results])
                if pump_results[
                        'base'] == False and results['mixer_full'] < 2.5:
                    print "Filling mixer with base"
                    sc.turnOn(api, base_pump, "d")
                    cur, con = dbc.connectToDB('localhost', 5432, 'postgres',
                                               'postgres', 'homegrowplus')
                    cur.execute(
                        """UPDATE pumps SET status = {} WHERE name = '{}'""".
                        format('true', 'base'))
                    con.commit()
                    dbc.disconnectFromDB(con)
                    time.sleep(1)
                cur, con = dbc.connectToDB('localhost', 5432, 'postgres',
                                           'postgres', 'homegrowplus')
                cur.execute("""SELECT * FROM pumps""")
                pump_results = cur.fetchall()
                dbc.disconnectFromDB(con)
                pump_results = dict([((el[0]), el[1]) for el in pump_results])
                if pump_results['base'] == True:
                    sc.turnOff(api, base_pump, "d")
                    cur, con = dbc.connectToDB('localhost', 5432, 'postgres',
                                               'postgres', 'homegrowplus')
                    cur.execute(
                        """UPDATE pumps SET status = {} WHERE name = '{}'""".
                        format('false', 'base'))
                    con.commit()
                    dbc.disconnectFromDB(con)
        except Exception as e:
            print "Failed to auto fill mixer"
            print e
Exemplo n.º 6
0
def runWaterSchedule(api, waterOnTime, waterCyclePeriod):
    while True:
        try:
            print "Running water schedule in background"
            results, code = getPumpSensors()
            results = json.loads(results.response[0])
            results = dict([((el[0]), float(el[1])) for el in results])
            pump_results, code = getPumpStatuses()
            pump_results = json.loads(pump_results.response[0])
            pump_results = dict([((el[0]), el[1]) for el in pump_results])
            if results['mixer_empty'] < 2.5:
                print "Mixer is empty"
                sc.turnOff(api, mixer_pump, "d")
                cur, con = dbc.connectToDB('localhost', 5432, 'postgres',
                                           'postgres', 'homegrowplus')
                cur.execute(
                    """UPDATE pumps SET status = {} WHERE name = '{}'""".
                    format('false', 'mixer'))
                con.commit()
                dbc.disconnectFromDB(con)
                fillMixer()
            else:
                print "Mixer not empty"
                if pump_results['mixer'] == False:
                    sc.turnOn(api, mixer_pump, "d")
                    cur, con = dbc.connectToDB('localhost', 5432, 'postgres',
                                               'postgres', 'homegrowplus')
                    cur.execute(
                        """UPDATE pumps SET status = {} WHERE name = '{}'""".
                        format('true', 'mixer'))
                    con.commit()
                    dbc.disconnectFromDB(con)
                    time.sleep(waterOnTime)
                pump_results, code = getPumpStatuses()
                pump_results = json.loads(pump_results.response[0])
                pump_results = dict([((el[0]), el[1]) for el in pump_results])
                if pump_results['mixer'] == True:
                    sc.turnOff(api, mixer_pump, "d")
                    cur, con = dbc.connectToDB('localhost', 5432, 'postgres',
                                               'postgres', 'homegrowplus')
                    cur.execute(
                        """UPDATE pumps SET status = {} WHERE name = '{}'""".
                        format('false', 'mixer'))
                    con.commit()
                    dbc.disconnectFromDB(con)
                    time.sleep(waterCyclePeriod)
                pump_results, code = getPumpStatuses()
                pump_results = json.loads(pump_results.response[0])
                pump_results = dict([((el[0]), el[1]) for el in pump_results])
                if pump_results['drain_out'] == False:
                    sc.turnOn(api, drain_out_pump, "d")
                    cur, con = dbc.connectToDB('localhost', 5432, 'postgres',
                                               'postgres', 'homegrowplus')
                    cur.execute(
                        """UPDATE pumps SET status = {} WHERE name = '{}'""".
                        format('true', 'drain_out'))
                    con.commit()
                    dbc.disconnectFromDB(con)
                    time.sleep(int(0.5 * waterOnTime))
                pump_results, code = getPumpStatuses()
                pump_results = json.loads(pump_results.response[0])
                pump_results = dict([((el[0]), el[1]) for el in pump_results])
                if pump_results['drain_out'] == True:
                    sc.turnOff(api, drain_out_pump, "d")
                    cur, con = dbc.connectToDB('localhost', 5432, 'postgres',
                                               'postgres', 'homegrowplus')
                    cur.execute(
                        """UPDATE pumps SET status = {} WHERE name = '{}'""".
                        format('false', 'drain_out'))
                    con.commit()
                    dbc.disconnectFromDB(con)
        except Exception as e:
            print "Failed to run the water schedule"
            print e