node.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 # -*- coding: utf-8 -*-
00003 
00004 # python libraries
00005 import sys
00006 import logging
00007 
00008 # ros libraries
00009 import rospy
00010 from rospeex_msgs.msg import SpeechSynthesisRequest
00011 from rospeex_msgs.msg import SpeechRecognitionResponse
00012 
00013 # local libraries
00014 import logging_util
00015 from webaudiomonitor_server import WebAudioMonotorServer
00016 from spi_connector import SPIConnector
00017 
00018 
00019 logger = logging_util.get_logger(__file__)
00020 
00021 
00022 class WebAudioMonitorNode(object):
00023     """ WebAudioMonitorNode class """
00024     SS_REQUEST_TOPIC_NAME = 'ss_req'
00025     SR_RESPONSE_TOPIC_NAME = 'sr_res'
00026 
00027     def __init__(self):
00028         """ init class
00029 
00030         """
00031         self._ws_host = '127.0.0.1'
00032         self._ws_port = 9000
00033         self._vad_host = '127.0.0.1'
00034         self._vad_port = 5002
00035         self._epd_host = '127.0.0.1'
00036         self._epd_port = 16001
00037         self._ws_server = None
00038         self._spi_connector = None
00039 
00040     def _sr_response_received(self, request):
00041         logger.info('receive sr response.')
00042         self._ws_server.notify_sr_text(request.message)
00043 
00044     def _ss_request_received(self, request):
00045         logger.info('receive ss request.')
00046         self._ws_server.notify_ss_text(request.message)
00047 
00048     def _load_parameter(self):
00049         self._ws_host = rospy.get_param('~ip_addr', self._ws_host)
00050         self._ws_port = rospy.get_param('~websocket_port', self._ws_port)
00051         self._vad_host = rospy.get_param('~ip_addr', self._vad_host)
00052         self._vad_port = rospy.get_param('~send_port', self._vad_port)
00053         self._epd_host = rospy.get_param('~ip_addr', self._epd_host)
00054         self._epd_port = rospy.get_param('~recv_port', self._epd_port)
00055 
00056     def run(self):
00057         # init rospy
00058         rospy.init_node('webaudiomonitor', anonymous=True)
00059         self._load_parameter()
00060 
00061         # subscribe topics
00062         rospy.Subscriber(
00063             self.SS_REQUEST_TOPIC_NAME,
00064             SpeechSynthesisRequest,
00065             self._ss_request_received
00066         )
00067 
00068         rospy.Subscriber(
00069             self.SR_RESPONSE_TOPIC_NAME,
00070             SpeechRecognitionResponse,
00071             self._sr_response_received
00072         )
00073 
00074         # setup logger
00075         logging_util.setup_logging(
00076             default_level=logging.INFO
00077         )
00078 
00079         # create websocket
00080         logger.info('create websocket server')
00081         self._ws_server = WebAudioMonotorServer(
00082             host=self._ws_host,
00083             port=self._ws_port,
00084             debug=True
00085         )
00086 
00087         # create spi connector
00088         logger.info('create spi connector')
00089         self._spi_connector = SPIConnector(
00090             vad_host=self._vad_host,
00091             vad_port=self._vad_port,
00092             epd_host=self._epd_host,
00093             epd_port=self._epd_port
00094         )
00095 
00096         # setup callback
00097         logger.info('setup callback')
00098         self._spi_connector.set_vad_callback(
00099             vad_connected=self._ws_server.notify_vad_connected,
00100             vad_disconnected=self._ws_server.notify_vad_disconnected
00101         )
00102 
00103         self._spi_connector.set_recv_callback(
00104             vad_received=self._ws_server.send_packet,
00105             epd_received=self._ws_server.notify_epd_message
00106         )
00107 
00108         self._ws_server.set_websocket_callback(
00109             recv_audio_cb=self._spi_connector.send_vad_packet,
00110             recv_command_cb=self._spi_connector.send_epd_packet
00111         )
00112 
00113         # start servers
00114         try:
00115             logger.info('start spi connector')
00116             self._spi_connector.start()
00117 
00118             logger.info('start websocket server')
00119             self._ws_server.start()
00120 
00121             rospy.spin()
00122 
00123         except rospy.ROSInterruptException:
00124             pass
00125 
00126         except Exception as err:
00127             logger.exception(err)
00128 
00129         finally:
00130             # end spi connector
00131             logger.info('end spi connector')
00132             self._spi_connector.end()
00133             self._spi_connector.join(1.0)
00134 
00135             # end websocket
00136             logger.info('end spi websocket server')
00137             self._ws_server.stop()
00138 
00139 
00140 def main():
00141 
00142     # start web audio monitor
00143     node = WebAudioMonitorNode()
00144     node.run()
00145     return 1
00146 
00147 if __name__ == '__main__':
00148     sys.exit(main())


rospeex_webaudiomonitor
Author(s): Komei Sugiura
autogenerated on Thu Jun 6 2019 18:53:16