def get_websocket(host, port, route='/', ssl=False): """ Returns a connection to a websocket """ client = MessageBusClient(host, port, route, ssl) client.run_in_thread() return client
class MessageHandler(): messages = [] client = MessageBusClient(host=os.environ['MYCROFT_HOST']) @staticmethod def initialize(): MessageHandler.client.on('speak', MessageHandler._receive_message) MessageHandler.client.run_in_thread() @staticmethod def _append_message(incoming, message): MessageHandler.messages.append({'incoming': incoming, 'message': message}) @staticmethod def _receive_message(message): MessageHandler._append_message(True, json.loads(message.serialize())['data']['utterance']) @staticmethod def send_message(message): MessageHandler._append_message(False, message) MessageHandler.client.emit(Message('recognizer_loop:utterance', {"utterances": [message], "lang": "en"})) @staticmethod def get_messages(): return MessageHandler.messages
def handle_radio_talanthus(self, message): print('Setting up client to connect to a local mycroft instance') client = MessageBusClient() client.run_in_thread() print('Sending speak message...') client.emit(Message('speak', data={'utterance': 'Currently at the top of the bounty board is the calamo five at ten million credits a head'}))
def __init__(self): # Load full config config = get_mycroft_compatible_config() self.lang = config['lang'] self.config = config.get("enclosure") LOG.info(config) config["gui_websocket"] = config.get("gui_websocket", { "host": "0.0.0.0", "base_port": 18181, "route": "/gui", "ssl": False }) config['gui_websocket']["base_port"] = config["gui_websocket"].get( "base_port", config["gui_websocket"].get("port", 18181)) self.global_config = config # Create Message Bus Client self.bus = MessageBusClient() self.gui = create_gui_service(self, config['gui_websocket']) # This datastore holds the data associated with the GUI provider. Data # is stored in Namespaces, so you can have: # self.datastore["namespace"]["name"] = value # Typically the namespace is a meaningless identifier, but there is a # special "SYSTEM" namespace. self.datastore = {} # self.loaded is a list, each element consists of a namespace named # tuple. # The namespace namedtuple has the properties "name" and "pages" # The name contains the namespace name as a string and pages is a # mutable list of loaded pages. # # [Namespace name, [List of loaded qml pages]] # [ # ["SKILL_NAME", ["page1.qml, "page2.qml", ... , "pageN.qml"] # [...] # ] self.loaded = [] # list of lists in order. self.explicit_move = True # Set to true to send reorder commands # Listen for new GUI clients to announce themselves on the main bus self.active_namespaces = [] self.bus.on("mycroft.gui.connected", self.on_gui_client_connected) self.register_gui_handlers() # First send any data: self.bus.on("gui.value.set", self.on_gui_set_value) self.bus.on("gui.page.show", self.on_gui_show_page) self.bus.on("gui.page.delete", self.on_gui_delete_page) self.bus.on("gui.clear.namespace", self.on_gui_delete_namespace) self.bus.on("gui.event.send", self.on_gui_send_event) self.bus.on("gui.status.request", self.handle_gui_status_request)
def connect_to_messagebus(): """ Connect to the mycroft messagebus and launch a thread handling the connection. Returns: WebsocketClient """ bus = MessageBusClient() # Mycroft messagebus connection event_thread = Thread(target=connect, args=[bus]) event_thread.setDaemon(True) event_thread.start() return bus
def __new__(cls, clazz, *args, **kwargs): # read config config_core = {'stt': kwargs.get("config")} or get_neon_speech_config() metric_upload = config_core.get("metric_upload", False) # build STT for k in list(kwargs.keys()): if k not in signature(clazz).parameters: kwargs.pop(k) stt = clazz(*args, **kwargs) # add missing properties if metric_upload: server_addr = config_core.get("remote_server", "64.34.186.120") stt.server_bus = MessageBusClient(host=server_addr) stt.server_bus.run_in_thread() else: stt.server_bus = None stt.keys = config_core.get("keys", {}) return stt
def setUpClass(cls) -> None: cls.messagebus = NeonBusService(debug=True, daemonic=True) cls.messagebus.start() cls.speech_thread = Process(target=neon_speech_main, kwargs={"speech_config": TEST_CONFIG}, daemon=False) cls.speech_thread.start() cls.bus = MessageBusClient() cls.bus.run_in_thread() if not cls.bus.connected_event.wait(60): raise TimeoutError("Bus not connected after 60 seconds") alive = False timeout = time() + 120 while not alive and time() < timeout: message = cls.bus.wait_for_response( Message("mycroft.speech.is_ready")) if message: alive = message.data.get("status") if not alive: raise TimeoutError("Speech module not ready after 120 seconds")
def init_display_manager_bus_connection(): """ Connects the display manager to the messagebus """ LOG.info("Connecting display manager to messagebus") # Should remove needs to be an object so it can be referenced in functions # [https://stackoverflow.com/questions/986006/how-do-i-pass-a-variable-by-reference] display_manager = DisplayManager() should_remove = [True] def check_flag(flag): if flag[0] is True: display_manager.remove_active() def set_delay(event=None): should_remove[0] = True Timer(2, check_flag, [should_remove]).start() def set_remove_flag(event=None): should_remove[0] = False def connect(): bus.run_forever() def remove_wake_word(): data = _read_data() if "active_skill" in data and data["active_skill"] == "wakeword": display_manager.remove_active() def set_wakeword_skill(event=None): display_manager.set_active("wakeword") Timer(10, remove_wake_word).start() bus = MessageBusClient() bus.on('recognizer_loop:audio_output_end', set_delay) bus.on('recognizer_loop:audio_output_start', set_remove_flag) bus.on('recognizer_loop:record_begin', set_wakeword_skill) event_thread = Thread(target=connect) event_thread.setDaemon(True) event_thread.start()
def main(): global client print('Setting up client to connect to a local mycroft instance') client = MessageBusClient() client.run_in_thread() client.emit(Message('speak', data={'utterance': 'Ping'})) print('looking for microbit') ser_micro = find_comport(PID_MICROBIT, VID_MICROBIT, 115200) if not ser_micro: print('microbit not found') return print('opening and monitoring microbit port') ser_micro.open() while True: line = ser_micro.readline() if line: # If it isn't a blank line line = line.strip().decode('UTF-8') parse_message(line) ser_micro.close()
#import modules import time import serial #import serial to talk to the arduino from mycroft_bus_client import MessageBusClient, Message #import messageBus to talk to Mycroft #set up the serial port ser = serial.Serial('/dev/ttyACM0', baudrate=9600, timeout=1) print('Setting up client to connect to a local mycroft instance') client = MessageBusClient() def print_utterance(message): victor_response = (format(message.data.get('utterance')) ) #get the latest output from Mycroft print(victor_response) #print the output to the terminal ser.write(victor_response.encode()) #send the output over serial print('Registering handler for speak message...') client.on('speak', print_utterance) client.run_forever()
def test_create_client(self): mc = MessageBusClient() assert mc.client.url == 'ws://0.0.0.0:8181/core'
def setup_ui(self): self.setWindowTitle(self.title) self.setGeometry(self.top, self.left, self.width, self.height) self.center_on_screen() self.centralwidget = QtWidgets.QWidget(self) self.setCentralWidget(self.centralwidget) self.gridLayout = QtWidgets.QGridLayout(self.centralwidget) spacer_item = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum) self.gridLayout.addItem(spacer_item, 1, 2, 1, 3) self.verticalLayout_intents = QtWidgets.QVBoxLayout() self.gridLayout.addLayout(self.verticalLayout_intents, 2, 1, 1, 1) self.label_intent1 = self.create_intent_label() self.label_intent2 = self.create_intent_label() self.label_intent3 = self.create_intent_label() self.label_intent4 = self.create_intent_label() self.label_intent5 = self.create_intent_label() self.label_intent6 = self.create_intent_label() self.label_intent7 = self.create_intent_label() self.line = QtWidgets.QFrame(self.centralwidget) size_policy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Maximum) size_policy.setHorizontalStretch(0) size_policy.setVerticalStretch(0) size_policy.setHeightForWidth( self.line.sizePolicy().hasHeightForWidth()) self.line.setSizePolicy(size_policy) self.line.setLineWidth(1) self.line.setMidLineWidth(0) self.line.setFrameShape(QtWidgets.QFrame.HLine) self.line.setFrameShadow(QtWidgets.QFrame.Sunken) self.gridLayout.addWidget(self.line, 3, 1, 1, 5) self.lineEdit_chat_message = QtWidgets.QLineEdit(self.centralwidget) size_policy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Fixed) size_policy.setHorizontalStretch(0) size_policy.setVerticalStretch(0) size_policy.setHeightForWidth( self.lineEdit_chat_message.sizePolicy().hasHeightForWidth()) self.lineEdit_chat_message.setSizePolicy(size_policy) self.gridLayout.addWidget(self.lineEdit_chat_message, 8, 1, 1, 4) self.label_intents_title = QtWidgets.QLabel(self.centralwidget) size_policy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Maximum, QtWidgets.QSizePolicy.Maximum) size_policy.setHorizontalStretch(0) size_policy.setVerticalStretch(0) size_policy.setHeightForWidth( self.label_intents_title.sizePolicy().hasHeightForWidth()) self.label_intents_title.setSizePolicy(size_policy) font_questions_title = QtGui.QFont() font_questions_title.setPointSize(16) self.label_intents_title.setFont(font_questions_title) self.gridLayout.addWidget(self.label_intents_title, 1, 1, 1, 1) self.scrollArea = QtWidgets.QScrollArea(self.centralwidget) size_policy = QtWidgets.QSizePolicy( QtWidgets.QSizePolicy.MinimumExpanding, QtWidgets.QSizePolicy.MinimumExpanding) size_policy.setHorizontalStretch(0) size_policy.setVerticalStretch(0) size_policy.setHeightForWidth( self.scrollArea.sizePolicy().hasHeightForWidth()) self.scrollArea.setSizePolicy(size_policy) self.scrollArea.setWidgetResizable(True) self.scrollAreaWidgetContents = QtWidgets.QWidget() self.scrollAreaWidgetContents.setGeometry(QtCore.QRect(0, 0, 768, 410)) self.verticalLayout = QtWidgets.QVBoxLayout( self.scrollAreaWidgetContents) self.gridLayout_conversation = QtWidgets.QGridLayout() self.verticalLayout.addLayout(self.gridLayout_conversation) self.scrollArea.setWidget(self.scrollAreaWidgetContents) self.gridLayout.addWidget(self.scrollArea, 7, 1, 1, 5) self.label_chat_title = QtWidgets.QLabel(self.centralwidget) size_policy = QtWidgets.QSizePolicy( QtWidgets.QSizePolicy.MinimumExpanding, QtWidgets.QSizePolicy.Maximum) size_policy.setHorizontalStretch(0) size_policy.setVerticalStretch(0) size_policy.setHeightForWidth( self.label_chat_title.sizePolicy().hasHeightForWidth()) font_chat_title = QtGui.QFont() font_chat_title.setPointSize(14) self.label_chat_title.setSizePolicy(size_policy) self.label_chat_title.setFont(font_chat_title) self.gridLayout.addWidget(self.label_chat_title, 5, 1) self.pushButton_mic = QtWidgets.QPushButton(self.centralwidget) size_policy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Maximum, QtWidgets.QSizePolicy.Maximum) size_policy.setHorizontalStretch(0) size_policy.setVerticalStretch(0) size_policy.setHeightForWidth( self.pushButton_mic.sizePolicy().hasHeightForWidth()) self.pushButton_mic.setSizePolicy(size_policy) self.mic_icon = QtGui.QIcon() self.mic_icon.addPixmap(QtGui.QPixmap("imgs/mic.svg")) self.mic_muted_icon = QtGui.QIcon() self.mic_muted_icon.addPixmap(QtGui.QPixmap("imgs/mic_muted.svg")) self.pushButton_mic.setIcon(self.mic_icon) palette = QtGui.QPalette() brush = QtGui.QBrush(QtGui.QColor(115, 210, 22)) brush.setStyle(QtCore.Qt.SolidPattern) palette.setBrush(QtGui.QPalette.Active, QtGui.QPalette.Button, brush) self.pushButton_mic.setPalette(palette) self.pushButton_mic.clicked.connect(self.on_mic_pressed) self.gridLayout.addWidget(self.pushButton_mic, 5, 5) self.pushButton_send = QtWidgets.QPushButton(self.centralwidget) self.pushButton_send.setGeometry(QtCore.QRect(399, 550, 50, 30)) self.pushButton_send.setPalette(palette) self.pushButton_send.clicked.connect(self.on_send_pressed) self.gridLayout.addWidget(self.pushButton_send, 8, 5, 1, 1) self.pushButton_logs = QtWidgets.QPushButton(self.centralwidget) self.pushButton_logs.setGeometry(QtCore.QRect(370, 10, 120, 40)) self.logs_file_icon = QtGui.QIcon() self.logs_file_icon.addPixmap(QtGui.QPixmap("imgs/file.svg")) self.pushButton_logs.setIcon(self.logs_file_icon) self.pushButton_logs.clicked.connect(self.on_logs_pressed) self.gridLayout.addWidget(self.pushButton_logs, 1, 5, 1, 1) self.skills_dialog = QtWidgets.QDialog(self) self.skills_dialog.setWindowTitle('Mycroft Skills') self.skills_dialog.resize(600, 600) self.pushButton_skills = QtWidgets.QPushButton(self.centralwidget) self.pushButton_skills.setGeometry(QtCore.QRect(370, 60, 120, 40)) self.skills_list_icon = QtGui.QIcon() self.skills_list_icon.addPixmap(QtGui.QPixmap("imgs/list.svg")) self.pushButton_skills.setIcon(self.skills_list_icon) self.pushButton_skills.clicked.connect(self.on_skills_pressed) self.gridLayout.addWidget(self.pushButton_skills, 2, 5, 1, 1) self.pushButton_manage_skills = QtWidgets.QPushButton( self.skills_dialog) self.pushButton_manage_skills.setGeometry( QtCore.QRect(470, 10, 120, 40)) self.pushButton_manage_skills.clicked.connect( self.on_manage_skills_pressed) # List of the skills that the user should not interact with dangerous_skills = [ 'mycroft-volume.mycroftai', 'mycroft-stop.mycroftai', 'fallback-unknown.mycroftai', 'fallback-query.mycroftai', 'mycroft-configuration.mycroftai' ] # List of the skills in the /opt/mycroft/skills folder [self.active_skills.append(name) for name in listdir('/opt/mycroft/skills/') \ if path.isdir('/opt/mycroft/skills/' + name) and name not in dangerous_skills] # Check if the chat needs to be updated every second self.timer = QtCore.QTimer(self) self.timer.setInterval(1000) self.timer.timeout.connect(self.check_for_chat_update) self.timer.start() self.retranslate_ui() # Send the webservice class to Mycroft server_socket = Thread(target=util.create_server_socket, args=[self.ws]) server_socket.setDaemon(True) server_socket.start() # Start Mycroft services subprocess.run([ 'bash', path.expanduser('~') + '/mycroft-core/start-mycroft.sh', 'all', 'restart' ]) # Wait until Mycroft services are started, there might be a better solution time.sleep(15) # Thread connected to Mycroft MessageBusClient self.bus = MessageBusClient() self.bus.run_in_thread() self.bus.on('speak', self.handle_speak) self.bus.on('recognizer_loop:utterance', self.handle_utterance) # Deactivate mycroft-volume.mycroftai skill, mic works weird when it's active self.bus.emit( Message('skillmanager.deactivate', {'skill': 'mycroft-volume.mycroftai'}))