mycroft-core/mycroft/messagebus/send.py

87 lines
2.9 KiB
Python
Raw Normal View History

SkillManager, msm messagebus notifications Significantly reworked the loading/updating of Skills. Unified all management under a single SkillManager class. This class runs as a thread that initially loads, upgrades (via MSM) and reloads skills. Removed the independent threads that were being run. The skill updating still happens once an hour, but works in conjunction with the scan to reload modified skills. Also added messagebus notifications from MSM so mycroft-core can pause reloading skills until the installation is complete. Added a new mycroft.messagebus.send module to allow command line interaction with the messagebus, e.g.: python -m mycroft.messagebus.send mycroft.wifi.start python -m mycroft.messagebus.send speak '{"utterance":"hello"}' ==== Fixed Issues ==== MSM installs that have PIP dependencies were failing, as the load would occur after code was retrieved but before PIP install completed. Restart was required to load new skills. ==== Tech Notes ==== TODO: Change the way we manage modules. The auto-load of the remote configuration for the module is silly, slow and wasteful. I made the WebsocketClient.build_url() method static in anticipation of being able to do this more efficiently when the submodule load doesn't hit the remove API automatically. ==== Localization Notes ==== Modified 'sorry I couldn't install default skills' message. ==== Protocol Notes ==== MSM now generates: msm.updating msm.installing msm.install.succeeded { "skill" : name } msm.install.failed { "skill" : name, "error" : code } msm.installed msm.updated msm.removing msm.remove.succeeded { "skill" : name } msm.remove.failed { "skill" : name, "error" : code } msm.removed An update can now be forced by posting 'skillmanager.update' to the messagebus.
2017-10-13 07:21:58 +00:00
# Copyright 2017 Mycroft AI Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
import sys
import json
from mycroft.messagebus.client.ws import WebsocketClient
from mycroft.messagebus.message import Message
from mycroft.configuration import ConfigurationManager
from websocket import create_connection
def main():
"""
Main function, will run if executed from command line.
Sends parameters from commandline.
Param 1: message string
Param 2: data (json string)
"""
# Parse the command line
if len(sys.argv) == 2:
messageToSend = sys.argv[1]
dataToSend = {}
elif len(sys.argv) == 3:
messageToSend = sys.argv[1]
try:
dataToSend = json.loads(sys.argv[2])
except BaseException:
print("Second argument must be a JSON string")
print("Ex: python -m mycroft.messagebus.send speak "
"'{\"utterance\" : \"hello\"}'")
exit()
else:
print("Command line interface to the mycroft-core messagebus.")
print("Usage: python -m mycroft.messagebus.send message")
print(" python -m mycroft.messagebus.send message JSON-string\n")
print("Examples: python -m mycroft.messagebus.send system.wifi.setup")
print("Ex: python -m mycroft.messagebus.send speak "
"'{\"utterance\" : \"hello\"}'")
SkillManager, msm messagebus notifications Significantly reworked the loading/updating of Skills. Unified all management under a single SkillManager class. This class runs as a thread that initially loads, upgrades (via MSM) and reloads skills. Removed the independent threads that were being run. The skill updating still happens once an hour, but works in conjunction with the scan to reload modified skills. Also added messagebus notifications from MSM so mycroft-core can pause reloading skills until the installation is complete. Added a new mycroft.messagebus.send module to allow command line interaction with the messagebus, e.g.: python -m mycroft.messagebus.send mycroft.wifi.start python -m mycroft.messagebus.send speak '{"utterance":"hello"}' ==== Fixed Issues ==== MSM installs that have PIP dependencies were failing, as the load would occur after code was retrieved but before PIP install completed. Restart was required to load new skills. ==== Tech Notes ==== TODO: Change the way we manage modules. The auto-load of the remote configuration for the module is silly, slow and wasteful. I made the WebsocketClient.build_url() method static in anticipation of being able to do this more efficiently when the submodule load doesn't hit the remove API automatically. ==== Localization Notes ==== Modified 'sorry I couldn't install default skills' message. ==== Protocol Notes ==== MSM now generates: msm.updating msm.installing msm.install.succeeded { "skill" : name } msm.install.failed { "skill" : name, "error" : code } msm.installed msm.updated msm.removing msm.remove.succeeded { "skill" : name } msm.remove.failed { "skill" : name, "error" : code } msm.removed An update can now be forced by posting 'skillmanager.update' to the messagebus.
2017-10-13 07:21:58 +00:00
exit()
send(messageToSend, dataToSend)
def send(messageToSend, dataToSend=None):
"""
Send a single message over the websocket.
Args:
messageToSend (str): Message to send
dataToSend (dict): data structure to go along with the
message, defaults to empty dict.
"""
dataToSend = dataToSend or {}
# Calculate the standard Mycroft messagebus websocket address
config = ConfigurationManager.get().get("websocket")
url = WebsocketClient.build_url(config.get("host"),
config.get("port"),
config.get("route"),
config.get("ssl"))
# Send the provided message/data
ws = create_connection(url)
packet = Message(messageToSend, dataToSend).serialize()
ws.send(packet)
ws.close()
if __name__ == '__main__':
try:
main()
except IOError:
print('Could not connect to websocket, no message sent')