def get_system_info(device_identity, device_address: str, **kwargs) -> {}: """Gets the 'version', 'type' and 'connection' and formats into dict. :param device_identity: Class of device being connected to. :param device_address: Connection string to the device. :param kwargs: Additional arguments that can be supplied to the UOS device. :return: Dictionary containing system data. """ sys_data = {} try: device = UOSDevice(identity=device_identity, address=device_address, **kwargs) result = device.get_system_info() getLogger(__name__).debug("Shim queried device info %s", str(result)) device.close() if result.status: sys_data["version"] = ( f"V{result.rx_packets[0][4]}.{result.rx_packets[0][5]}." f"{result.rx_packets[0][6]}") sys_data["address"] = device.address if f"hwid{result.rx_packets[0][7]}" in DEVICES: sys_data[ "type"] = f"{DEVICES[f'hwid{result.rx_packets[0][7]}'].name}" else: sys_data["type"] = "Unknown" except (AttributeError, ValueError, NotImplementedError, RuntimeError) as exception: message = ( f"Cannot open connection to '{device_address}', info: {exception.__str__()}" ) flash(message, "error") getLogger(__name__).error(message) return sys_data
def get_system_config(device_identity: str, device_address): """Gets the mode and level of all gpio configured on the device. :param device_identity: Class of device being connected to. :param device_address: Connection string to the device. :return: Dictionary containing 'gpioX' with 'mode'/'level' for each pin index X. """ uos_data = {} try: device = UOSDevice( identity=device_identity, address=device_address, ) for digital_pin in device.device.digital_pins: pin_config = device.get_gpio_config(digital_pin) getLogger(__name__).debug("Shim queried device info %s", str(pin_config)) if pin_config.status: uos_data[digital_pin] = { "current_mode": pin_config.rx_packets[0][4], "current_level": pin_config.rx_packets[0][5], "ram_mode": pin_config.rx_packets[0][6], "ram_level": pin_config.rx_packets[0][7], "eeprom_mode": pin_config.rx_packets[0][8], "eeprom_level": pin_config.rx_packets[0][9], } except (AttributeError, ValueError, NotImplementedError, RuntimeError) as exception: message = ( f"Cannot open connection to '{device_address}', info: {exception.__str__()}" ) flash(message, "error") getLogger(__name__).error(message) return uos_data
def uos_device(request): """Creates a fixture for testing through the abstraction layer.""" device = UOSDevice( DEVICES[request.param]["identity"], DEVICES[request.param]["address"], DEVICES[request.param]["interface"], loading=DEVICES[request.param]["loading"], ) yield device device.close()
def test_bad_connection(uos_identities: {}, interface: Interface): """Checks that bad connections fail sensibly.""" with pytest.raises(UOSCommunicationError): device = UOSDevice( uos_identities["identity"], "", interface=interface, loading=uos_identities["loading"], ) if device.is_lazy(): # lazy connection so manually open device.open()
def uos_errored_device(request): """Creates a fixture for testing through the abstraction layer.""" return UOSDevice( DEVICES[request.param]["identity"], DEVICES[request.param]["address"], DEVICES[request.param]["interface"], loading=DEVICES[request.param]["loading"], errored=1, )
def test_implemented_devices(uos_identities: {}): """Checks devices in config can init without error.""" assert ( UOSDevice( identity=uos_identities["identity"], address=uos_identities["address"], interface=uos_identities["interface"], ) is not None )
def execute_digital_instruction(device_identity: str, device_address: str, set_output: bool, set_level: bool) -> {}: """Configs the pin from form data and formats response into dict.""" uos_data = {} try: device = UOSDevice( identity=device_identity, address=device_address, ) # result = device.set_gpio_output() device.close() except (AttributeError, ValueError, NotImplementedError, RuntimeError) as exception: message = ( f"Cannot open connection to '{device_address}', info: {exception.__str__()}" ) flash(message, "error") getLogger(__name__).error(message) return uos_data
def route_hardware_function(api_version: str, function: str): """Can be used to execute standard UOS IO functions.""" if api_version not in API_VERSIONS: return jsonify( ComResult( False, exception= f"'{function}' not supported in api version {api_version}.", )) try: arguments = inspect.signature(getattr(UOSDevice, function)) except AttributeError as exception: return jsonify( ComResult( False, exception= f"API call on '{function}' threw error {exception.__str__()}.", )) possible_args = { argument.name: util.APIargument(argument.default == inspect.Parameter.empty, argument.annotation, None) for argument in arguments.parameters.values() if argument.name != "self" and argument.name != "kwargs" } response, required_args = util.check_required_args(possible_args, request.args, add_device=True) if response.status: device = UOSDevice( identity=required_args["identity"].arg_value, address=required_args["address"].arg_value, ) if function in dir(UOSDevice): instr_response = getattr(device, function)(*[ required_args[parameter.name].arg_value for parameter in inspect.signature(getattr( device, function)).parameters.values() if parameter.name in required_args and required_args[parameter.name].arg_value is not None ]) response.status = instr_response.status response.com_data = instr_response else: # dunno how to handle that function response.exception = f"function '{function}' has not been implemented." response.status = False return jsonify(response)
def test_unimplemented_devices(): """Checks an un-implemented device throws the correct error.""" with pytest.raises(UOSUnsupportedError): UOSDevice(identity="Not Implemented", address="", interface=Interface.STUB)