Remove code that waits for websocket to open
This is unnecessary now that WebsocketClient does this automaticallypull/1509/head
parent
e6a211a0aa
commit
5c8cf053a0
|
@ -246,7 +246,6 @@ class Enclosure(object):
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.ws = WebsocketClient()
|
self.ws = WebsocketClient()
|
||||||
self.ws.on("open", self.on_ws_open)
|
|
||||||
|
|
||||||
Configuration.init(self.ws)
|
Configuration.init(self.ws)
|
||||||
|
|
||||||
|
@ -258,13 +257,6 @@ class Enclosure(object):
|
||||||
self.reader = EnclosureReader(self.serial, self.ws, self.lang)
|
self.reader = EnclosureReader(self.serial, self.ws, self.lang)
|
||||||
self.writer = EnclosureWriter(self.serial, self.ws)
|
self.writer = EnclosureWriter(self.serial, self.ws)
|
||||||
|
|
||||||
# initiates the web sockets on display manager
|
|
||||||
# NOTE: this is a temporary place to initiate display manager sockets
|
|
||||||
initiate_display_manager_ws()
|
|
||||||
|
|
||||||
def on_ws_open(self, event=None):
|
|
||||||
# Mark 1 auto-detection:
|
|
||||||
#
|
|
||||||
# Prepare to receive message when the Arduino responds to the
|
# Prepare to receive message when the Arduino responds to the
|
||||||
# following "system.version"
|
# following "system.version"
|
||||||
self.ws.on("enclosure.started", self.on_arduino_responded)
|
self.ws.on("enclosure.started", self.on_arduino_responded)
|
||||||
|
@ -281,6 +273,10 @@ class Enclosure(object):
|
||||||
# Notifications from mycroft-core
|
# Notifications from mycroft-core
|
||||||
self.ws.on("enclosure.notify.no_internet", self.on_no_internet)
|
self.ws.on("enclosure.notify.no_internet", self.on_no_internet)
|
||||||
|
|
||||||
|
# initiates the web sockets on display manager
|
||||||
|
# NOTE: this is a temporary place to initiate display manager sockets
|
||||||
|
initiate_display_manager_ws()
|
||||||
|
|
||||||
def on_arduino_responded(self, event=None):
|
def on_arduino_responded(self, event=None):
|
||||||
self.eyes = EnclosureEyes(self.ws, self.writer)
|
self.eyes = EnclosureEyes(self.ws, self.writer)
|
||||||
self.mouth = EnclosureMouth(self.ws, self.writer)
|
self.mouth = EnclosureMouth(self.ws, self.writer)
|
||||||
|
|
|
@ -45,6 +45,7 @@ class WebsocketClient(object):
|
||||||
self.pool = ThreadPool(10)
|
self.pool = ThreadPool(10)
|
||||||
self.retry = 5
|
self.retry = 5
|
||||||
self.connected_event = Event()
|
self.connected_event = Event()
|
||||||
|
self.started_running = False
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def build_url(host, port, route, ssl):
|
def build_url(host, port, route, ssl):
|
||||||
|
@ -85,10 +86,12 @@ class WebsocketClient(object):
|
||||||
self.emitter.emit, (parsed_message.type, parsed_message))
|
self.emitter.emit, (parsed_message.type, parsed_message))
|
||||||
|
|
||||||
def emit(self, message):
|
def emit(self, message):
|
||||||
self.connected_event.wait(10)
|
if not self.connected_event.wait(10):
|
||||||
if (not self.client or not self.client.sock or
|
if not self.started_running:
|
||||||
not self.client.sock.connected):
|
raise ValueError('You must execute run_forever() '
|
||||||
return
|
'before emitting messages')
|
||||||
|
self.connected_event.wait()
|
||||||
|
|
||||||
if hasattr(message, 'serialize'):
|
if hasattr(message, 'serialize'):
|
||||||
self.client.send(message.serialize())
|
self.client.send(message.serialize())
|
||||||
else:
|
else:
|
||||||
|
@ -145,6 +148,7 @@ class WebsocketClient(object):
|
||||||
self.emitter.remove_all_listeners(event_name)
|
self.emitter.remove_all_listeners(event_name)
|
||||||
|
|
||||||
def run_forever(self):
|
def run_forever(self):
|
||||||
|
self.started_running = True
|
||||||
self.client.run_forever()
|
self.client.run_forever()
|
||||||
|
|
||||||
def close(self):
|
def close(self):
|
||||||
|
|
|
@ -33,6 +33,7 @@ class TestMessagebusMethods(unittest.TestCase):
|
||||||
It currently only tests send and receive. The tests could include
|
It currently only tests send and receive. The tests could include
|
||||||
more.
|
more.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
"""
|
"""
|
||||||
This sets up for testing the message buss
|
This sets up for testing the message buss
|
||||||
|
@ -43,8 +44,6 @@ class TestMessagebusMethods(unittest.TestCase):
|
||||||
"""
|
"""
|
||||||
# start the mycroft service. and get the pid of the script.
|
# start the mycroft service. and get the pid of the script.
|
||||||
self.pid = Popen(["python", "mycroft/messagebus/service/main.py"]).pid
|
self.pid = Popen(["python", "mycroft/messagebus/service/main.py"]).pid
|
||||||
# delay to allow the service to start up.
|
|
||||||
time.sleep(10)
|
|
||||||
# Create the two web clients
|
# Create the two web clients
|
||||||
self.ws1 = WebsocketClient()
|
self.ws1 = WebsocketClient()
|
||||||
self.ws2 = WebsocketClient()
|
self.ws2 = WebsocketClient()
|
||||||
|
@ -54,8 +53,6 @@ class TestMessagebusMethods(unittest.TestCase):
|
||||||
# Start threads to handle websockets
|
# Start threads to handle websockets
|
||||||
Thread(target=self.ws1.run_forever).start()
|
Thread(target=self.ws1.run_forever).start()
|
||||||
Thread(target=self.ws2.run_forever).start()
|
Thread(target=self.ws2.run_forever).start()
|
||||||
# Sleep to give the websockets to startup before adding handlers
|
|
||||||
time.sleep(10)
|
|
||||||
# Setup handlers for each of the messages.
|
# Setup handlers for each of the messages.
|
||||||
self.ws1.on('ws1.message', self.onHandle1)
|
self.ws1.on('ws1.message', self.onHandle1)
|
||||||
self.ws2.on('ws2.message', self.onHandle2)
|
self.ws2.on('ws2.message', self.onHandle2)
|
||||||
|
@ -98,7 +95,7 @@ class TestMessagebusMethods(unittest.TestCase):
|
||||||
self.ws2.emit(Message('ws1.message'))
|
self.ws2.emit(Message('ws1.message'))
|
||||||
self.ws1.emit(Message('ws2.message'))
|
self.ws1.emit(Message('ws2.message'))
|
||||||
# allow time for messages to be processed
|
# allow time for messages to be processed
|
||||||
time.sleep(10)
|
time.sleep(0.2)
|
||||||
# Check that both of the handlers were called.
|
# Check that both of the handlers were called.
|
||||||
self.assertTrue(self.handle1)
|
self.assertTrue(self.handle1)
|
||||||
self.assertTrue(self.handle2)
|
self.assertTrue(self.handle2)
|
||||||
|
|
Loading…
Reference in New Issue