def read_all_data(trackbits, batch):
    from db_setup import init_database
    import threading
    threading.Thread(target=start_server).start()
    state = ""
    if first_time():
        init_database()
        if not batch:
            old_days = get_semantic_file(trackbits)
            old_days.to_file("semantics/location_semantics.txt")
        else:
            old_days = None

        if wait_until_save(100000):
            days = semantic_places.read_days("semantics/location_semantics.txt")
            #days = semantic_places.read_days("partial.txt")
            locs, last_date = read_locations()
            #locs = []
            #if not locs:
            locs = gather_locations(days)
            state = "first_time"
            #print "Info for %s days, up to %s" % (len(locs), last_date)
            return locs, state, old_days, days
    else:
        if not batch:
            old_days = get_semantic_file(trackbits)
            old_days.to_file("semantics/location_semantics.txt")
        else:
            old_days = None
        locs, last_date = read_locations()
        locations = compute_locations(locs)
        file = get_semantic_file_with_learning(trackbits, locations)
        file.to_file("semantics/location_semantics.txt")
        if wait_until_save(100000):
            days = semantic_places.read_days("semantics/location_semantics.txt")
            #days = semantic_places.read_days("partial.txt")
            locs, last_date = read_locations()
            #locs = []
            #if not locs:
            locs = update_locations(days, locs, last_date)
            state = "not_first_time"
            #print "Info for %s days, up to %s" % (len(locs), last_date)

            return locs, state, old_days, days
def read_all_data_batch(trackbits):
    from db_setup import init_database
    state = ""
    if first_time():
        init_database()
        #old_days = get_semantic_file(trackbits)
    #old_days.to_file("location_semantics.txt")
    #if wait_until_save(100000):
    days = semantic_places.read_days("location_semantics.txt")
    #days = semantic_places.read_days("partial.txt")
    #locs, last_date = read_locations()
    #locs = []
    #if not locs:
    locs = gather_locations(days)
    state = "first_time"
    #print "Info for %s days, up to %s" % (len(locs), last_date)
    return locs, state, days