Ejemplo n.º 1
0
class MagicBlueShell:
    def __init__(self, bluetooth_adapter):
        self.available_cmds = [{
            'cmd': 'help',
            'func': self.list_commands,
            'params': '',
            'help': 'Show this help',
            'con_required': False
        }, {
            'cmd': 'list_devices',
            'func': self.cmd_list_devices,
            'params': '',
            'help': 'List Bluetooth LE devices in range',
            'con_required': False
        }, {
            'cmd': 'connect',
            'func': self.cmd_connect,
            'params': 'mac_address',
            'help': 'Connect to light bulb',
            'con_required': False
        }, {
            'cmd': 'disconnect',
            'func': self.cmd_disconnect,
            'params': '',
            'help': 'Disconnect from current light bulb',
            'con_required': True
        }, {
            'cmd': 'set_color',
            'func': self.cmd_set_color,
            'params': 'name|hexvalue',
            'help': "Change bulb's color",
            'con_required': True
        }, {
            'cmd': 'set_warm_light',
            'func': self.cmd_set_warm_light,
            'params': 'intensity[0.0-1.0]',
            'help': "Set warm light",
            'con_required': True
        }, {
            'cmd': 'turn',
            'func': self.cmd_turn,
            'params': 'on|off',
            'help': "Turn on / off the bulb",
            'con_required': True
        }, {
            'cmd': 'exit',
            'func': self.cmd_exit,
            'params': '',
            'help': 'Exit the script',
            'con_required': False
        }]
        self.bluetooth_adapter = bluetooth_adapter
        self._magic_blue = None

    def start_interactive_mode(self):
        print('Magic Blue interactive shell v{}'.format(__version__))
        print('Type "help" to see what you can do')

        try:
            str_cmd = ''
            while str_cmd != 'exit':
                str_cmd = input('> ')
                self.exec_cmd(str_cmd)
        except EOFError:  # Catch CTRL+D
            self.cmd_exit()

    def exec_cmd(self, str_cmd):
        cmd = self._get_command(str_cmd)
        if cmd is not None:
            if cmd['con_required'] and not (self._magic_blue and
                                            self._magic_blue.is_connected()):
                logger.error(
                    'You must be connected to magic blue bulb to run this command'
                )
            else:
                if self._check_args(str_cmd, cmd):
                    cmd['func'](str_cmd.split()[1:])
        else:
            logger.error(
                'Command "{}" is not available. Type "help" to see what you can do'
                .format(str_cmd.split()[0]))

    def print_usage(self, str_cmd):
        cmd = self._get_command(str_cmd)
        if cmd is not None:
            print('Usage: {} {}'.format(cmd['cmd'], cmd['params']))
        else:
            logger.error('Unknow command {}'.format(str_cmd))
        return False

    def cmd_list_devices(self, *args):
        try:
            service = DiscoveryService(self.bluetooth_adapter)
        except RuntimeError as e:
            logger.error('Problem with the Bluetooth adapter : {}'.format(e))
            return False

        print(
            'Listing Bluetooth LE devices in range. Press CTRL+C to stop searching.'
        )
        print('{: <12} {: <12}'.format('Name', 'Mac address'))
        print('{: <12} {: <12}'.format('----', '-----------'))

        try:
            while 42:
                for device_mac, device_name in service.discover(2).items():
                    print('{: <12} {: <12}'.format(device_name, device_mac))
                print('---------------')
                time.sleep(1)
        except KeyboardInterrupt:
            print('\n')
        except RuntimeError as e:
            logger.error(
                'Error while trying to list bluetooth devices : {}'.format(e))

    def cmd_connect(self, *args):
        self._magic_blue = MagicBlue(args[0][0])
        self._magic_blue.connect(self.bluetooth_adapter)
        logger.info('Connected : {}'.format(self._magic_blue.is_connected()))

    def cmd_disconnect(self, *args):
        self._magic_blue.disconnect()
        self._magic_blue = None

    def cmd_turn(self, *args):
        if args[0][0] == 'on':
            self._magic_blue.turn_on()
        else:
            self._magic_blue.turn_off()

    def cmd_set_color(self, *args):
        color = args[0][0]
        try:
            if color.startswith('#'):
                self._magic_blue.set_color(webcolors.hex_to_rgb(color))
            else:
                self._magic_blue.set_color(webcolors.name_to_rgb(color))
        except ValueError as e:
            logger.error('Invalid color value : {}'.format(str(e)))
            self.print_usage('set_color')

    def cmd_set_warm_light(self, *args):
        try:
            self._magic_blue.set_warm_light(float(args[0][0]))
        except ValueError as e:
            logger.error('Invalid intensity value : {}'.format(str(e)))
            self.print_usage('set_color')

    def list_commands(self, *args):
        print(' ----------------------------')
        print('| List of available commands |')
        print(' ----------------------------')
        print('{: <16}{: <25}{}'.format('COMMAND', 'PARAMETERS', 'DETAILS'))
        print('{: <16}{: <25}{}'.format('-------', '----------', '-------'))
        print('\n'.join([
            '{: <16}{: <25}{}'.format(command['cmd'], command['params'],
                                      command['help'])
            for command in self.available_cmds
        ]))

    def cmd_exit(self, *args):
        print('Bye !')

    def _check_args(self, str_cmd, cmd):
        expected_nb_args = len(cmd['params'].split())
        args = str_cmd.split()[1:]
        if len(args) != expected_nb_args:
            self.print_usage(str_cmd.split()[0])
            return False
        return True

    def _get_command(self, str_cmd):
        str_cmd = str_cmd.split()[0]
        return next(
            (item for item in self.available_cmds if item['cmd'] == str_cmd),
            None)
Ejemplo n.º 2
0
class MagicBlueShell:
    def __init__(self, bluetooth_adapter):
        self.available_cmds = [
            {'cmd': 'help', 'func': self.list_commands, 'params': '', 'help': 'Show this help', 'con_required': False},
            {'cmd': 'list_devices', 'func': self.cmd_list_devices, 'params': '', 'help': 'List Bluetooth LE devices in range', 'con_required': False},
            {'cmd': 'connect', 'func': self.cmd_connect, 'params': 'mac_address', 'help': 'Connect to light bulb', 'con_required': False},
            {'cmd': 'disconnect', 'func': self.cmd_disconnect, 'params': '', 'help': 'Disconnect from current light bulb', 'con_required': True},
            {'cmd': 'set_color', 'func': self.cmd_set_color, 'params': 'name|hexvalue', 'help': "Change bulb's color", 'con_required': True},
            {'cmd': 'set_warm_light', 'func': self.cmd_set_warm_light, 'params': 'intensity[0.0-1.0]', 'help': "Set warm light", 'con_required': True},
            {'cmd': 'turn', 'func': self.cmd_turn, 'params': 'on|off', 'help': "Turn on / off the bulb", 'con_required': True},
            {'cmd': 'exit', 'func': self.cmd_exit, 'params': '', 'help': 'Exit the script', 'con_required': False}
        ]
        self.bluetooth_adapter = bluetooth_adapter
        self._magic_blue = None

    def start_interactive_mode(self):
        print('Magic Blue interactive shell v{}'.format(__version__))
        print('Type "help" to see what you can do')

        try:
            str_cmd = ''
            while str_cmd != 'exit':
                str_cmd = input('> ')
                self.exec_cmd(str_cmd)
        except EOFError:  # Catch CTRL+D
            self.cmd_exit()

    def exec_cmd(self, str_cmd):
        cmd = self._get_command(str_cmd)
        if cmd is not None:
            if cmd['con_required'] and not (self._magic_blue and self._magic_blue.is_connected()):
                logger.error('You must be connected to magic blue bulb to run this command')
            else:
                if self._check_args(str_cmd, cmd):
                    cmd['func'](str_cmd.split()[1:])
        else:
            logger.error('Command "{}" is not available. Type "help" to see what you can do'.format(str_cmd.split()[0]))

    def print_usage(self, str_cmd):
        cmd = self._get_command(str_cmd)
        if cmd is not None:
            print('Usage: {} {}'.format(cmd['cmd'], cmd['params']))
        else:
            logger.error('Unknow command {}'.format(str_cmd))
        return False

    def cmd_list_devices(self, *args):
        try:
            service = DiscoveryService(self.bluetooth_adapter)
        except RuntimeError as e:
            logger.error('Problem with the Bluetooth adapter : {}'.format(e))
            return False

        print('Listing Bluetooth LE devices in range. Press CTRL+C to stop searching.')
        print('{: <12} {: <12}'.format('Name', 'Mac address'))
        print('{: <12} {: <12}'.format('----', '-----------'))

        try:
            while 42:
                for device_mac, device_name in service.discover(2).items():
                    print('{: <12} {: <12}'.format(device_name, device_mac))
                print('---------------')
                time.sleep(1)
        except KeyboardInterrupt:
            print('\n')
        except RuntimeError as e:
            logger.error('Error while trying to list bluetooth devices : {}'.format(e))

    def cmd_connect(self, *args):
        self._magic_blue = MagicBlue(args[0][0])
        self._magic_blue.connect(self.bluetooth_adapter)
        logger.info('Connected : {}'.format(self._magic_blue.is_connected()))

    def cmd_disconnect(self, *args):
        self._magic_blue.disconnect()
        self._magic_blue = None

    def cmd_turn(self, *args):
        if args[0][0] == 'on':
            self._magic_blue.turn_on()
        else:
            self._magic_blue.turn_off()

    def cmd_set_color(self, *args):
        color = args[0][0]
        try:
            if color.startswith('#'):
                self._magic_blue.set_color(webcolors.hex_to_rgb(color))
            else:
                self._magic_blue.set_color(webcolors.name_to_rgb(color))
        except ValueError as e:
            logger.error('Invalid color value : {}'.format(str(e)))
            self.print_usage('set_color')

    def cmd_set_warm_light(self, *args):
        try:
            self._magic_blue.set_warm_light(float(args[0][0]))
        except ValueError as e:
            logger.error('Invalid intensity value : {}'.format(str(e)))
            self.print_usage('set_color')

    def list_commands(self, *args):
        print(' ----------------------------')
        print('| List of available commands |')
        print(' ----------------------------')
        print('{: <16}{: <25}{}'.format('COMMAND', 'PARAMETERS', 'DETAILS'))
        print('{: <16}{: <25}{}'.format('-------', '----------', '-------'))
        print('\n'.join(['{: <16}{: <25}{}'.format(command['cmd'], command['params'], command['help']) for command in self.available_cmds]))

    def cmd_exit(self, *args):
        print('Bye !')

    def _check_args(self, str_cmd, cmd):
        expected_nb_args = len(cmd['params'].split())
        args = str_cmd.split()[1:]
        if len(args) != expected_nb_args:
            self.print_usage(str_cmd.split()[0])
            return False
        return True

    def _get_command(self, str_cmd):
        str_cmd = str_cmd.split()[0]
        return next((item for item in self.available_cmds if item['cmd'] == str_cmd), None)
Ejemplo n.º 3
0
class MagicBlueShell:
    def __init__(self):
        self.available_cmds = [
            {"cmd": "help", "func": self.list_commands, "params": "", "help": "Show this help", "con_required": False},
            {
                "cmd": "list_devices",
                "func": self.cmd_list_devices,
                "params": "",
                "help": "List Bluetooth LE devices in range",
                "con_required": False,
            },
            {
                "cmd": "connect",
                "func": self.cmd_connect,
                "params": "mac_address",
                "help": "Connect to light bulb",
                "con_required": False,
            },
            {
                "cmd": "disconnect",
                "func": self.cmd_disconnect,
                "params": "",
                "help": "Disconnect from current light bulb",
                "con_required": True,
            },
            {
                "cmd": "set_color",
                "func": self.cmd_set_color,
                "params": "name|hexvalue",
                "help": "Change bulb's color",
                "con_required": True,
            },
            {
                "cmd": "turn",
                "func": self.cmd_turn,
                "params": "on|off",
                "help": "Turn on / off the bulb",
                "con_required": True,
            },
            {"cmd": "exit", "func": self.cmd_exit, "params": "", "help": "Exit the script", "con_required": False},
        ]
        self._magic_blue = None

    def start_interactive_mode(self):
        print("Magic Blue interactive shell v{}".format(__version__))
        print('Type "help" to see what you can do')

        try:
            str_cmd = ""
            while str_cmd != "exit":
                str_cmd = input("> ")
                self.exec_cmd(str_cmd)
        except EOFError:  # Catch CTRL+D
            self.cmd_exit()

    def exec_cmd(self, str_cmd):
        cmd = self._get_command(str_cmd)
        if cmd is not None:
            if cmd["con_required"] and not (self._magic_blue and self._magic_blue.is_connected()):
                logger.error("You must be connected to magic blue bulb to run this command")
            else:
                if self._check_args(str_cmd, cmd):
                    cmd["func"](str_cmd.split()[1:])
        else:
            logger.error('Command "{}" is not available. Type "help" to see what you can do'.format(str_cmd.split()[0]))

    def print_usage(self, str_cmd):
        cmd = self._get_command(str_cmd)
        if cmd is not None:
            print("Usage: {} {}".format(cmd["cmd"], cmd["params"]))
        else:
            logger.error("Unknow command {}".format(str_cmd))
        return False

    def cmd_list_devices(self, *args):
        print("Listing Bluetooth LE devices in range. Press CTRL+C to stop searching.")
        service = DiscoveryService()

        print("{: <12} {: <12}".format("Name", "Mac address"))
        print("{: <12} {: <12}".format("----", "-----------"))

        try:
            while 42:
                for device_mac, device_name in service.discover(2).items():
                    print("{: <12} {: <12}".format(device_name, device_mac))
                print("---------------")
                time.sleep(1)
        except KeyboardInterrupt:
            print("\n")

    def cmd_connect(self, *args):
        self._magic_blue = MagicBlue(args[0][0])
        self._magic_blue.connect()
        logger.info("Connected : {}".format(self._magic_blue.is_connected()))

    def cmd_disconnect(self, *args):
        self._magic_blue.disconnect()
        self._magic_blue = None

    def cmd_turn(self, *args):
        if args[0][0] == "on":
            self._magic_blue.turn_on()
        else:
            self._magic_blue.turn_off()

    def cmd_set_color(self, *args):
        color = args[0][0]
        try:
            if color.startswith("#"):
                self._magic_blue.set_color(webcolors.hex_to_rgb(color))
            else:
                self._magic_blue.set_color(webcolors.name_to_rgb(color))
        except ValueError as e:
            logger.error("Invalid color value : {}".format(str(e)))
            self.print_usage("set_color")

    def list_commands(self, *args):
        print(" ----------------------------")
        print("| List of available commands |")
        print(" ----------------------------")
        print("{: <16}{: <25}{}".format("COMMAND", "PARAMETERS", "DETAILS"))
        print("{: <16}{: <25}{}".format("-------", "----------", "-------"))
        print(
            "\n".join(
                [
                    "{: <16}{: <25}{}".format(command["cmd"], command["params"], command["help"])
                    for command in self.available_cmds
                ]
            )
        )

    def cmd_exit(self, *args):
        print("Bye !")

    def _check_args(self, str_cmd, cmd):
        expected_nb_args = len(cmd["params"].split())
        args = str_cmd.split()[1:]
        if len(args) != expected_nb_args:
            self.print_usage(str_cmd.split()[0])
            return False
        return True

    def _get_command(self, str_cmd):
        str_cmd = str_cmd.split()[0]
        return next((item for item in self.available_cmds if item["cmd"] == str_cmd), None)