Esempio n. 1
0
def do_water_schedule():

    #If Already running, do nothing
    if (get_solenoid_status() == "on"):
        return 0

    remaining_this_hour = fishPI.services.water.get_water_ticks_remaining_hour(
    )

    if (remaining_this_hour > 0):

        ticks_now = get_water_ticks().value
        ticks_finish = ticks_now + remaining_this_hour

        logging.logInfo("Changing Water {} from {} ticks to {} ticks".format(
            str(wc_this_hour), str(get_water_ticks().value),
            str(ticks_finish)))

        while (int(get_water_total_ticks().value) < ticks_finish):
            fishPI.services.water.set_solenoid_on()

        fishPI.services.water.set_solenoid_off()

    else:

        fishPI.services.water.set_solenoid_off()
        return 0
Esempio n. 2
0
def do_water_schedule():

    if (get_solenoid_status() == "on"):
        return 0

    wc_this_hour = fishPI.services.water.get_water_remaining_this_hour()

    if (wc_this_hour > 0):  #Water Change is due

        ticks_now = get_water_ticks().value
        ticks_finish = int(ticks_now) + int(litres_to_ticks(wc_this_hour))

        logging.logInfo("Changing Water {} from {} ticks to {} ticks".format(
            str(wc_this_hour), str(get_water_ticks().value),
            str(ticks_finish)))

        while (int(get_water_ticks().value) < ticks_finish):
            fishPI.services.water.set_solenoid_on()

        fishPI.services.water.set_solenoid_off()

    else:

        fishPI.services.water.set_solenoid_off()
        return 0
Esempio n. 3
0
def service_loaders():

    from fishPI import services

    services.database.load()

    modules = glob.glob(os.path.join(fishPI.config.app_dir,"services","*.py"))
    for module in modules:
        fullModule = "fishPI.services." + ntpath.basename(module).replace(".py","")
        i = __import__(fullModule, globals(), locals()) # returns module object
        if hasattr(i, 'load'):
            try:
                eval(fullModule + '.load()')
            except Exception as e:
                logging.logInfo(" * Loading Service onLoad for " + fullModule + " FAILED : " + str(e))
Esempio n. 4
0
def create_schema():
    if (database_exists()):
        return True

    logging.logInfo(" * Creating Database")

    sql_create_meta_table = """ CREATE TABLE IF NOT EXISTS meta (
                                    id INTEGER PRIMARY KEY,
                                    key TEXT NOT NULL,
                                    value TEXT,
                                    added DATETIME DEFAULT CURRENT_TIMESTAMP
                                ); """
    conn = create_conn()
    create_table(sql_create_meta_table)
    close_conn(conn)
Esempio n. 5
0
def main():
    #Pre-Set Config 
    fishPI.config.working_dir = os.path.dirname(os.path.abspath(sys.argv[0]))
    fishPI.config.app_dir = os.path.join(fishPI.config.working_dir,"fishPI")
    fishPI.config.template_dir = os.path.join(fishPI.config.app_dir,"templates")
    fishPI.config.static_dir = os.path.join(fishPI.config.app_dir,"static")
    fishPI.config.version = fishPI.config.get_version()
    fishPI.config.host_name = str(platform.uname()[1])
    fishPI.config.host = environ.get('SERVER_HOST', '0.0.0.0')
    fishPI.config.database = os.path.join(fishPI.config.working_dir,fishPI.config.load_from_config("instance","database_file_name"))
    fishPI.config.log_dir = os.path.join(fishPI.config.working_dir,"logs")

    #INI File Config
    fishPI.config.title = fishPI.config.load_from_config("instance","title")
    fishPI.config.ui_version = fishPI.config.load_from_config("instance","swagger_ui_version")
    fishPI.config.port = fishPI.config.load_from_config("instance","port");
    fishPI.config.light_pins = fishPI.config.load_from_config("light_pins")

    fishPI.config.solenoid_pin = fishPI.config.load_from_config("plug_pins","solenoid")
    fishPI.config.rain_pin = fishPI.config.load_from_config("plug_pins","rain")
    fishPI.config.flow_pin = fishPI.config.load_from_config("flow_pins","flow")

    ssl._create_default_https_context = ssl._create_unverified_context

    fishPI.app = Flask(__name__,template_folder=fishPI.config.template_dir,static_folder = fishPI.config.static_dir)

    #Used for Loading YAMLS
    fishPI.load = lambda controller, definition: swag_from(os.path.join(fishPI.config.app_dir, "yaml", controller, definition + ".yaml"))

    fishPI.app.config['SWAGGER'] = {
        'uiversion': fishPI.config.ui_version,
        'title': fishPI.config.title,
        "headers": [
            ('Access-Control-Allow-Origin', '*'),
            ('Access-Control-Allow-Methods', "GET, POST, PUT, DELETE, OPTIONS"),
            ('Access-Control-Allow-Credentials', "true"),
        ],
        "specs": [
            {
                "version":fishPI.config.get_full_version(),
                "title": fishPI.config.host_name,
                "endpoint": 'v1_spec',
                "description": "FishPI API",
                "route": '/spec.json',
            }
        ]
    }

    Swagger(fishPI.app)
    before_launch()

    user = os.getenv("SUDO_USER")

    #Import all views, controllers, models and services 
    from fishPI import views, controllers, models, services

    #Runs Service Loaders
    logging.logInfo(" * Running Service Loaders")
    service_loaders()
    
    #Runs AP Schedulers 
    logging.logInfo(" * Starting Schedulers") 
    schedulers()

    #Run FishPI / Flask
    logging.logInfo(" * Starting " + fishPI.config.title + " [" + fishPI.config.host + ":" + str(fishPI.config.port) + "]")
    fishPI.app.run(host=fishPI.config.host, port=fishPI.config.port, debug=False)