def initialize(self, botengine): ''' Initializer :param botengine: BotEngine ''' Device.initialize(self, botengine) if not hasattr(self, "ee_offset_c"): self.ee_offset_c = None if not hasattr(self, "absolute_max_cooling_offset_c"): self.absolute_max_cooling_offset_c = 8.3 if not hasattr(self, 'absolute_max_heating_offset_c'): self.absolute_max_heating_offset_c = 8.3 if not hasattr(self, 'dr_stack'): self.dr_stack = {} if not hasattr(self, 'ee_active'): self.ee_active = False if not hasattr(self, 'ee_offset_c'): self.ee_offset_c = None if not hasattr(self, 'ee_stack'): self.ee_stack = {} if not hasattr(self, 'last_cooling_setpoint_command'): self.last_cooling_setpoint_command = None if not hasattr(self, 'last_heating_setpoint_command'): self.last_heating_setpoint_command = None if not hasattr(self, 'last_system_mode_colmmand'): self.last_system_mode_command = None if not hasattr(self, 'preferred_cooling_offset_away_c'): self.preferred_cooling_offset_away_c = 2.4 if not hasattr(self, 'preferred_heating_offset_away_c'): self.preferred_heating_offset_away_c = 2.4 if not hasattr(self, 'preferred_cooling_setpoint_home_c'): self.preferred_cooling_setpoint_home_c = 23.9 if not hasattr(self, 'preferred_heating_setpoint_home_c'): self.preferred_heating_setpoint_home_c = 20.0 if not hasattr(self, 'preferred_cooling_offset_sleep_c'): self.preferred_cooling_offset_sleep_c = 2.4 if not hasattr(self, 'preferred_heating_offset_sleep_c'): self.preferred_heating_offset_sleep_c = 2.4 if not hasattr(self, 'saved_system_mode'): self.saved_system_mode = None if not hasattr(self, 'user_adjusted_timestamp'): self.user_adjusted_timestamp = None
def __init__(self, botengine, device_id, device_type, device_description, precache_measurements=True): Device.__init__(self, botengine, device_id, device_type, device_description, precache_measurements=precache_measurements) # The boolean on/off state of this device that was saved self.saved_state = False # Saved brightess self.saved_brightness = None # Saved hue self.saved_hue = None # Saved saturation self.saved_saturation = None # Whether the saved_state is valid or not self.saved = False
def new_device(device: Device): logging.info(f"FOUND {device}") device.on_close().subscribe( on_next=lambda reason: print(f"CLOSE DEVICE {reason}"), on_error=logging.warning )
def __init__(self, botengine, device_id, device_type, device_description, precache_measurements=True): Device.__init__(self, botengine, device_id, device_type, device_description, precache_measurements=precache_measurements) if not hasattr(self, "saved_state"): self.saved_state = False if not hasattr(self, "saved"): self.saved = False
def __init__(self, device_manager: 'DeviceManager', device_name: str, state: State = State.unknown): Device.__init__(self, device_manager, device_name, state) self.adb_port = 5037 self.port = None
def reboot(self) -> None: Device.reboot(self) output, errors, result_code = adb.adb_command( self, "reboot", timeout=settings.ADB_REGULAR_COMMAND_TIMEOUT) if result_code != 0: logger.log_progress(f"\nUnable to reboot device: {self.name}") logger.log_progress("\nPlease, turn it off and on manually.") raise Exception(f"Unable to reboot device: {self.name}")
def initialize(self, botengine): Device.initialize(self, botengine) if not hasattr(self, 'saved_brightness'): self.saved_brightness = None if not hasattr(self, 'saved_hue'): self.saved_hue = None if not hasattr(self, 'saved_saturation'): self.saved_saturation = None
def __init__(self, botengine, device_id, device_type, device_description, precache_measurements=True): Device.__init__(self, botengine, device_id, device_type, device_description, precache_measurements=precache_measurements)
def _add_volume(self, udi, volume): device = Device() device.device_id = udi device.device_file = volume.GetProperty("block.device") device.label = volume.GetProperty("volume.label") device.fstype = volume.GetProperty("volume.fstype") device.mounted = volume.GetProperty("volume.is_mounted") device.mount_point = volume.GetProperty("volume.mount_point") try: device.size = volume.GetProperty("volume.size") except: device.size = 0 self._devices[udi] = device return device
def run(self, device: Device, package_name: str, script_name: str) -> None: assert device.api_level() >= self.minimum_api verbose_level = RequiredFeature('verbose_level').request() start_time = time.time() self.prepare_device_for_run(device) evolutiz_cmd = f"evolutiz -p {package_name} -v -v -v --throttle 200 --ignore-crashes " \ f"--ignore-security-exceptions --ignore-timeouts --bugreport -f /mnt/sdcard/{script_name} 1" output, errors, result_code = adb.shell_command( device, evolutiz_cmd, timeout=settings.TEST_CASE_EVAL_TIMEOUT) if verbose_level > 1: print(f"Test case running finished with output:\n{output}") if "Exception" in errors: device_stacktrace = errors.split("** Error: ")[1] raise Exception( f"An error occurred when running test case: {device_stacktrace}" ) # need to manually kill evolutiz when timeout adb.pkill(device, "evolutiz") self.clean_device_after_run(device) if verbose_level > 0: logger.log_progress( f'\nEvolutiz test run took: {time.time() - start_time:.2f} seconds' )
def generate(self, device: Device, package_name: str, destination_file_name: str) -> TestCase: assert device.api_level() >= self.minimum_api verbose_level = RequiredFeature('verbose_level').request() start_time = time.time() self.prepare_device_for_run(device) evolutiz_events = settings.SEQUENCE_LENGTH_MAX test_case = [] launch_result = self.evolutiz_connector.send_command( device, package_name, f"performview launch-app") for i in range(0, evolutiz_events): widget_action_result = WidgetAction.random(device, self.evolutiz_connector) test_event: TestEvent = widget_action_result test_case.append(test_event) if widget_action_result.is_outbound(): break if verbose_level > 0: logger.log_progress( f'\nEvolutiz test generation took: {time.time() - start_time:.2f} ' f'seconds for {len(test_case):d} events') self.clean_device_after_run(device) return test_case
def parse_adb_devices(self, adb_port: int, emulators_found: int, real_devices_found: int) -> Tuple[int, int]: devices_cmd = adb.adb_cmd_prefix + ' devices' try: output, errors, result_code = run_cmd( devices_cmd, env={"ANDROID_ADB_SERVER_PORT": str(adb_port)}) except TimeoutExpired as e: return emulators_found, real_devices_found error_lines = errors.split("\n") for line in error_lines: if "daemon not running" in line: continue if "daemon started successfully" in line: continue if line.strip() != "": raise Exception( f"There was an error running \'adb devices\' command: {errors}" ) lines = output.split("\n") for line in lines: if "List of devices attached" in line: continue if line.strip() == "": continue if "offline" not in line: device_name = line.split("\t")[0].strip() matching_devices = [ device for device in self.devices if device.name == device_name ] if len(matching_devices) > 0: device = matching_devices.pop(0) if device.state < State.reachable: device.state = State.reachable if type(device) is Emulator: emulators_found += 1 else: real_devices_found += 1 elif "emulator" in line and emulators_found < self.emulators_number: self.devices.append( Emulator(self, device_name, state=State.reachable)) emulators_found += 1 elif "device" in line and real_devices_found < self.real_devices_number: self.devices.append( Device(self, device_name, state=State.reachable)) real_devices_found += 1 return emulators_found, real_devices_found
def boot(self, port: Optional[int] = None, adb_port: Optional[int] = None) -> None: verbose_level = RequiredFeature('verbose_level').request() Device.boot(self) # ensure the emulator configuration is correct self.port = port if port is not None else self.device_manager.get_next_available_emulator_port( ) self.avd_name = self.avd_manager.get_avd_name_for_emulator_port( self.port) if not self.avd_manager.avd_name_exists(self.avd_name): raise Exception( f"AVD name {self.avd_name} doesn't exist. " f"Check that the provided AVD series ({self.avd_manager.avd_series}) is correct." ) # start custom abd server for this emulator self.adb_port = adb_port if adb_port is not None else self.device_manager.get_next_available_adb_server_port( ) adb.restart_server(self.adb_port) # start emulator self.name = f"emulator-{str(self.port)}" emulator_cmd = f"{self.get_adb_server_port_prefix()} QEMU_AUDIO_DRV=none $ANDROID_HOME/emulator/emulator" flags: str = f" -no-snapshot -wipe-data -no-boot-anim -writable-system -port {str(self.port)}" if verbose_level < 3: # -no-window flag can't be at the end flags = f" -no-window{flags}" logs = " >/dev/null 2>/dev/null" if verbose_level > 0: logs = f" >logs/{self.avd_name}.log 2>logs/{self.avd_name}.err" flags = f"{flags} -verbose -debug all" cmd: str = f'{emulator_cmd} -avd {self.avd_name}{flags}{logs}' if verbose_level > 1: logger.log_progress(f"\nFiring up emulator with command: {cmd}") sub.Popen(cmd, shell=True)
def evaluate( self, device: Device, individual: IndividualMultiObjective) -> IndividualMultiObjective: assert not individual.fitness.valid coverage_fetcher = RequiredFeature('coverage_fetcher').request() start_time = time.time() device.mark_work_start() script_path, suite_lengths = self.dump_individual_to_files(individual) coverage, unique_crashes, scripts_crash_status = coverage_fetcher.get_suite_coverage( script_path, device, individual.generation, individual.index_in_generation) # remove from suite lengths the scripts that did NOT cause a crash for script, had_crash in scripts_crash_status.items(): if not had_crash: suite_lengths.pop(script, None) # 1st obj: coverage, 2nd: average seq length of the suite, 3rd: #crashes if suite_lengths: length = numpy.mean(list(suite_lengths.values())) else: length = sys.maxsize crashes = len(unique_crashes) individual.fitness.values = (coverage, length, crashes) finish_time = time.time() individual.evaluation_finish_timestamp = finish_time individual.evaluation_elapsed_time = finish_time - start_time individual.length = length individual.crashes = crashes hall_of_fame = RequiredFeature('hall_of_fame').request() hall_of_fame.update([individual]) budget_manager = RequiredFeature('budget_manager').request() budget_manager.increase_evaluations_used() device.mark_work_stop() return individual
def get_csv(self, botengine, oldest_timestamp_ms=None, newest_timestamp_ms=None): """ Get a standardized .csv string of all the data :param botengine: BotEngine environment :param oldest_timestamp_ms: oldest timestamp in milliseconds :param newest_timestamp_ms: newest timestamp in milliseconds :return: .csv string, largely matching the .csv data you would receive from the "botengine --download_device [device_id]" command line interface. Or None if this device doesn't have data. """ return Device.get_csv(self, botengine, oldest_timestamp_ms=oldest_timestamp_ms, newest_timestamp_ms=newest_timestamp_ms, params=[EntryDevice.MEASUREMENT_NAME_STATUS])
def get_csv(self, botengine, oldest_timestamp_ms=None, newest_timestamp_ms=None): """ Get a standardized .csv string of all the data :param botengine: BotEngine environment :param oldest_timestamp_ms: oldest timestamp in milliseconds :param newest_timestamp_ms: newest timestamp in milliseconds :return: .csv string, largely matching the .csv data you would receive from the "botengine --download_device [device_id]" command line interface. Or None if this device doesn't have data. """ return Device.get_csv(self, botengine, oldest_timestamp_ms=oldest_timestamp_ms, newest_timestamp_ms=newest_timestamp_ms, params=[MotionDevice.MEASUREMENT_NAME_STATUS])
def gen_individual(self, device: Device, individual_index: int, generation: int) -> Individual: start_time = time.time() device.mark_work_start() suite = self.get_suite(device, generation, individual_index) device.mark_work_stop() individual: Individual = getattr(creator, Individual.get_name())(suite) finish_time = time.time() elapsed_time = finish_time - start_time individual.creation_finish_timestamp = finish_time individual.creation_elapsed_time = elapsed_time individual.index_in_generation = individual_index individual.generation = generation return individual
def evaluate( self, device: Device, individual: IndividualSingleObjective ) -> IndividualSingleObjective: assert not individual.fitness.valid coverage_fetcher = RequiredFeature('coverage_fetcher').request() start_time = time.time() device.mark_work_start() script_path, suite_lengths = self.dump_individual_to_files(individual) coverage, unique_crashes, scripts_crash_status = coverage_fetcher.get_suite_coverage( script_path, device, individual.generation, individual.index_in_generation) # remove from suite lengths the scripts that did NOT cause a crash for script, had_crash in scripts_crash_status.items(): if not had_crash: suite_lengths.pop(script, None) individual.fitness.values = (coverage, ) finish_time = time.time() individual.evaluation_finish_timestamp = finish_time individual.evaluation_elapsed_time = finish_time - start_time # Save crashes and length info outside fitness, to avoid messing up with the "wvalues" and "dominates" function # inside Deap's Fitness base class. if suite_lengths: individual.length = numpy.mean(list(suite_lengths.values())) else: individual.length = sys.maxsize individual.crashes = len(unique_crashes) hall_of_fame = RequiredFeature('hall_of_fame').request() hall_of_fame.update([individual]) budget_manager = RequiredFeature('budget_manager').request() budget_manager.increase_evaluations_used() device.mark_work_stop() return individual
def initialize(devices, chain=5): # Define devices to represent start and finish of simulation start = Device(0, 0) start.start = 0 start.finish = 0 finish = Device(0, 0) # Set length of simulation T = 23 # Find predecessor and successor for each device predSucc(devices, start, finish, chain) # Call getEarly and getLate for every five devices for i in range(0, len(devices), chain): getEarly(devices[i], chain) getLate(devices[i], T)
def generateDevice(n, step=60): # Calculate # of time steps in an hour factor = 60/step # List of possible duration times duration = {1: .35, 2: .25, 3: .2, 4:.1, 5:.1} #duration = {1: .5, 5:.5} # If steps are not an hour if step != 60: # Initialize new dictionary newDur = {} # Create new dictionary for possible task durations for key in duration.keys(): for i in range(factor): newDur[factor*key + i] = duration[key] # Define new dictionaries duration = newDur # Initialize list of devices devices = [] for i in range(n): # Choose duration dur = weighted_choice(duration) # Choose power power = np.random.uniform(1e3, 10e3) # 50% chance to reduce device power by one order of magnitude # if np.random.randint(0, 10) != 0: # power *= 0.1 # Create device device = Device(power, dur, step) devices.append(device) device.index = len(devices) - 1 return devices
def __init__(self, device_manager: 'DeviceManager', device_name: str = "", state: State = State.unknown) -> None: Device.__init__(self, device_manager, device_name, state) self.avd_manager = RequiredFeature('avd_manager').request() self.port: Optional[int] self.adb_port: Optional[int] if device_name != "": # we assume device_name has form "emulator-xxxx" self.port = int(device_name.split('-')[1]) self.adb_port = self.get_adb_server_port_for_emulator_port( self.port) else: self.port = None self.adb_port = None self.avd_name: str = ""
def start_ella(self, device: Device) -> None: self.prepare_ella_folder_for_device(device) run_cmd(f"find {self.get_device_ella_output_folder_path(device)} -type f -name \"coverage.dat.*\" | " f"xargs -I {{}} rm {{}}") # start ELLA server output, errors, result_code = run_cmd(f"./ella.sh s", cwd=self.get_device_ella_folder_path(device)) if result_code != 0: raise Exception("Unable to start ELLA server") # start the TCP relay service on the device output, errors, result_code = adb.shell_command( device, f"am startservice -n {self.tcp_relay_android_package_name}/.RelayService -a start " f"--ei pA {self.ella_original_port} --ei pB {self.get_device_tcp_relay_port(device)}") if result_code != 0: raise Exception("Unable to start the TCP relay service") # forward the TCP relay port from the emulator to the local PC device.forward_port(self.get_device_tcp_relay_port(device), self.get_device_tcp_relay_port(device)) # forward, locally, the TCP relay port to the ELLA server port run_cmd(f"socat TCP:localhost:{self.get_device_ella_port(device)} TCP:localhost:{self.get_device_tcp_relay_port(device)} &", discard_output=True)
def __init__(self): Device.__init__(self, ping_reverse_order, ping_parser_list) self.partitions = []
def initialize(self, botengine): Device.initialize(self, botengine)
def reboot(self) -> None: Device.reboot(self) self.shutdown() self.boot(port=self.port, adb_port=self.adb_port)
def shutdown(self) -> None: Device.shutdown(self) adb.adb_command(self, "emu kill") time.sleep(3)
def __init__(self, botengine, device_id, device_type, device_description, precache_measurements=True): # Whether we've saved the state of this thermostat self.ee_active = False # Saved system mode self.saved_system_mode = None # Stack of energy efficiency policies. # Each policy is a dictionary of the form: # # "name of the policy so it can be cancelled later": offset_c relative to the user's preferred temperature while home (always a positive number) # # The act of having a policy stored implies the policy is active. # The most aggressive energy efficiency policy is applied. # Energy efficiency policies are additive to any current demand response event. self.ee_stack = {} # Stack of demand response policies. # Each policy is a dictionary of the form: # # "name of the policy so it can be cancelled later": offset_c relative to the user's preferred temperature while home (always a positive number) # # The act of having a policy stored implies the policy is active. # The most aggressive demand response policy is applied. # Demand response policies are additive to any current energy efficiency policies. self.dr_stack = {} # Tuple (value, timestamp, verified): the last system mode command we sent the thermostat self.last_system_mode_command = None # Tuple (value, timestamp, verified): the last cooling setpoint command we sent the thermostat self.last_cooling_setpoint_command = None # Tuple (value, timestamp, verified): the last heating setpoint command we sent the thermostat self.last_heating_setpoint_command = None # Timestamp at which the user last adjusted their thermostat self.user_adjusted_timestamp = None # Preferred heating setpoint for this thermostat while you're home self.preferred_heating_setpoint_home_c = 20.0 # 68F # Preferred cooling setpoint for this thermostat while you're home self.preferred_cooling_setpoint_home_c = 23.9 # 75F # Absolute maximum heating offset (when you're on vacation or something) self.absolute_max_heating_offset_c = 8.3 # 15F # Absolute maximum cooling offset (when you're on vacation or something) self.absolute_max_cooling_offset_c = 8.3 # 15F # Preferred cooling offset when we're in away mode self.preferred_heating_offset_away_c = 2.4 # 5F # Preferred heating offset when we're in away mode self.preferred_cooling_offset_away_c = 2.4 # 5F # Preferred cooling offset when we're in away mode self.preferred_heating_offset_sleep_c = 2.4 # 5F # Preferred heating offset when we're in away mode self.preferred_cooling_offset_sleep_c = 2.4 # 5F Device.__init__(self, botengine, device_id, device_type, device_description, precache_measurements=precache_measurements)
def __init__(self, ping=ping_source_repeat_caller): Device.__init__(self, ping, ping_parser_dot)
def boot(self, port: Optional[int] = None) -> None: """There is no way to contact a real device if it's shutdown.""" Device.boot(self)
def shutdown(self) -> None: Device.shutdown(self) adb.shell_command(self, "reboot -p")
def __init__(self): Device.__init__(self, ping_a_caller, ping_parser_list)