import time
from daemon import log
from daemon.models.gtfs_work import session as dbhot
from daemon.models.gtfs_static import session as dbstatic


if not os.environ.get('HEARTBEAT'):
	hb = 60 * 15 # 15 minutos
else:
	hb = int(os.environ.get('HEARTBEAT'))


def main_loop(shouldIrun):

    log.info("Iniciando el loop principal")
    doki=int(time.time()) + hb
    while shouldIrun:

        if check_for_work():
            ingest_waiting_gtfs()

        time.sleep(1)
		i = int(time.time())
		if i >= doki:
			doki = i + hb
			log.info('Heartbeat')


def check_for_work():
    result = dbstatic.query(sArchivosGTFS).filter(sArchivosGTFS.loaded==0).first()

    if result is not None:
        return True

    return False

def ingest_waiting_gtfs():

    pass



def load_dataset(nombrearchivo):
    import zipfile
    import tempfile

    with tempfile.TemporaryDirectory() as tmpdirname:
        with zipfile.ZipFile(nombrearchivo, 'r') as zip_ref:
            zip_ref.extractall(tmpdirname)
