Example #1
0
def database_init():

    status = Status.query.first()
    if not status:
        timestamp = time()
        new_stamp = timestamp - 20
        status = Status(id=1,
                        timestamp=new_stamp,
                        battery_voltage=0.0,
                        battery_level=0,
                        status_charging=0,
                        cube_battery_level=0,
                        cube_id='None',
                        cube_battery_volts=0.0)
        db.session.add(status)
        db.session.commit()

    # Create the settings table if it doesn't exist
    settings = Settings.query.first()
    if not settings:
        settings = Settings(id=1)
        db.session.add(settings)
        db.session.commit()

    # Initialize app store table
    db.session.query(ApplicationStore).delete()
    db.session.commit()

    store_app_list = os.listdir(list_folder)

    for app_name in store_app_list:
        app_path = os.path.join(list_folder, app_name)
        f = open(app_path)
        name = f.readline()
        name = name.replace(']', '')
        name = name.replace('[', '')
        name = name.replace('\n', '')
        f.close()
        config.read(app_path)
        icon = config.get(name, 'icon')
        description = config.get(name, 'description')
        author = config.get(name, 'author')
        website = config.get(name, 'website')
        zip_file = config.get(name, 'zip_file')

        store_app = ApplicationStore(script_name=name,
                                     author=author,
                                     website=website,
                                     description=description,
                                     icon=icon,
                                     installed=False,
                                     zip_file=zip_file)

        db.session.add(store_app)
        db.session.commit()
Example #2
0
def get_stats(force=False):
    status = Status.query.first()
    timestamp = time()

    if status is None:
        new_stamp = timestamp - 20
        status = Status(timestamp=new_stamp)
        db.session.add(status)
        db.session.commit()

    elif timestamp - status.timestamp > 15 or force is True:

        # get robot name and ip from config file
        f = open(sdk_config_file)
        serial = f.readline()
        serial = serial.replace(']', '')
        serial = serial.replace('[', '')
        serial = serial.replace('\n', '')
        f.close()
        config.read(sdk_config_file)
        ip = config.get(serial, 'ip')
        name = config.get(serial, 'name')

        # get results from battery state and version state,
        # save to database
        try:
            args = anki_vector.util.parse_command_args()
            with anki_vector.Robot(args.serial,
                                   requires_behavior_control=False,
                                   cache_animation_list=False,
                                   behavior_activation_timeout=3) as robot:

                version_state = robot.get_version_state()
                battery_state = robot.get_battery_state()

                db.session.query(Status).delete()
                status = Status(
                    version=version_state.os_version,
                    battery_voltage=battery_state.battery_volts,
                    battery_level=battery_state.battery_level,
                    status_charging=battery_state.is_on_charger_platform,
                    cube_battery_level=battery_state.cube_battery.level,
                    cube_id=battery_state.cube_battery.factory_id,
                    cube_battery_volts=battery_state.
                    cube_battery.battery_volts,
                    ip=ip,
                    name=name,
                    serial=serial
                )
                db.session.add(status)
                db.session.commit()

        except _Rendezvous:
            sleep(3)
            get_stats(force=True)

        except anki_vector.exceptions.VectorNotFoundException:
            status = Status.query.first()
            status.timestamp = time()
            db.session.merge(status)
            db.session.commit()
            return 'vector_not_found'

        except anki_vector.exceptions.VectorControlTimeoutException:
            status = Status.query.first()
            status.timestamp = time()
            db.session.merge(status)
            db.session.commit()
            return 'vector_stuck'

        except anki_vector.exceptions.VectorInvalidVersionException:
            status = Status.query.first()
            status.timestamp = time()
            db.session.merge(status)
            db.session.commit()
            return 'invalid_sdk_version'

        except anki_vector.exceptions.VectorNotReadyException:
            status = Status.query.first()
            status.timestamp = time()
            db.session.merge(status)
            db.session.commit()
            return 'vector_not_ready'

        except anki_vector.exceptions.VectorTimeoutException:
            status = Status.query.first()
            status.timestamp = time()
            db.session.merge(status)
            db.session.commit()
            return 'vector_timed_out'

        except anki_vector.exceptions.VectorUnavailableException:
            status = Status.query.first()
            status.timestamp = time()
            db.session.merge(status)
            db.session.commit()
            return 'vector_unavailable'

        except anki_vector.exceptions.VectorUnimplementedException:
            status = Status.query.first()
            status.timestamp = time()
            db.session.merge(status)
            db.session.commit()
            return 'vector_unimplemented'

        except Exception:
            status = Status.query.first()
            status.timestamp = time()
            db.session.merge(status)
            db.session.commit()
            return 'general_error'
            return 'general_error'

    status = Status.query.first()
    status.timestamp = time()
    db.session.merge(status)
    db.session.commit()