Client now publishes itself
This commit is contained in:
parent
ad9a4b8f13
commit
7bce8fd318
4 changed files with 37 additions and 8 deletions
|
@ -1,6 +1,6 @@
|
||||||
events:
|
events:
|
||||||
cmd_address: "tcp://127.0.0.1:5555"
|
cmd_address: "tcp://127.0.0.1:5555"
|
||||||
publish_address: "tcp://0.0.0.0:5556"
|
publish_address: "tcp://127.0.0.1:5556"
|
||||||
voice:
|
voice:
|
||||||
input_rate: 44100
|
input_rate: 44100
|
||||||
target_rate: 16000
|
target_rate: 16000
|
||||||
|
|
|
@ -113,6 +113,7 @@ class CommandHandler(object):
|
||||||
self.hugvey_id = hugvey_id
|
self.hugvey_id = hugvey_id
|
||||||
self.cmd_address = cmd_address
|
self.cmd_address = cmd_address
|
||||||
self.publish_address = publish_address
|
self.publish_address = publish_address
|
||||||
|
# self.showMyself() # queue message for connection request
|
||||||
|
|
||||||
def handle(self, cmd):
|
def handle(self, cmd):
|
||||||
# self.sendMessage({'reply':'test'})
|
# self.sendMessage({'reply':'test'})
|
||||||
|
@ -121,9 +122,12 @@ class CommandHandler(object):
|
||||||
return
|
return
|
||||||
|
|
||||||
logger.info("Received {}".format(cmd))
|
logger.info("Received {}".format(cmd))
|
||||||
|
if cmd['action'] == 'show_yourself':
|
||||||
|
self.showMyself()
|
||||||
if cmd['action'] == 'play':
|
if cmd['action'] == 'play':
|
||||||
self.cmdPlay(cmd['id'], cmd['msg'])
|
self.cmdPlay(cmd['id'], cmd['msg'])
|
||||||
|
|
||||||
|
|
||||||
def cmdPlay(self, msgId, msgText):
|
def cmdPlay(self, msgId, msgText):
|
||||||
# espeak(msgText)
|
# espeak(msgText)
|
||||||
# TODO kill if playing & play wave file
|
# TODO kill if playing & play wave file
|
||||||
|
@ -135,6 +139,23 @@ class CommandHandler(object):
|
||||||
'msgId': msgId
|
'msgId': msgId
|
||||||
})
|
})
|
||||||
|
|
||||||
|
def showMyself(self):
|
||||||
|
"""Publish about this hugvey to central command
|
||||||
|
"""
|
||||||
|
self.sendMessage({
|
||||||
|
'event': 'connection',
|
||||||
|
'id': self.hugvey_id,
|
||||||
|
'host': socket.gethostname(),
|
||||||
|
'ip': self.getIp(),
|
||||||
|
})
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def getIp():
|
||||||
|
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
||||||
|
s.connect(("185.66.250.60", 80))
|
||||||
|
return s.getsockname()[0]
|
||||||
|
|
||||||
|
|
||||||
def sendMessage(self, msg):
|
def sendMessage(self, msg):
|
||||||
self.eventQueue.append(msg)
|
self.eventQueue.append(msg)
|
||||||
|
|
||||||
|
@ -153,12 +174,14 @@ class CommandHandler(object):
|
||||||
|
|
||||||
async def event_sender(self):
|
async def event_sender(self):
|
||||||
s = self.ctx.socket(zmq.PUB)
|
s = self.ctx.socket(zmq.PUB)
|
||||||
# TODO: see if we can connect() here. So we can PUSH(??) the ip
|
s.connect(self.publish_address)
|
||||||
s.bind(self.publish_address)
|
|
||||||
logger.info("Publish on: {}".format(self.publish_address))
|
logger.info("Publish on: {}".format(self.publish_address))
|
||||||
|
# For some reason, sending only one message is lost, perhaps due
|
||||||
|
# to connect() rather than bind() ??
|
||||||
|
self.showMyself()
|
||||||
while True:
|
while True:
|
||||||
for i in range(len(self.eventQueue)):
|
for i in range(len(self.eventQueue)):
|
||||||
await zmqSend(s, self.hugvey_id, self.eventQueue.pop(0))
|
zmqSend(s, self.hugvey_id, self.eventQueue.pop(0))
|
||||||
if len(self.eventQueue) == 0:
|
if len(self.eventQueue) == 0:
|
||||||
await asyncio.sleep(0.05)
|
await asyncio.sleep(0.05)
|
||||||
|
|
||||||
|
@ -203,6 +226,6 @@ class Hugvey(object):
|
||||||
asyncio.ensure_future(self.voice_server.asyncStart(loop))
|
asyncio.ensure_future(self.voice_server.asyncStart(loop))
|
||||||
asyncio.ensure_future(self.cmd_server.command_listener())
|
asyncio.ensure_future(self.cmd_server.command_listener())
|
||||||
asyncio.ensure_future(self.cmd_server.event_sender())
|
asyncio.ensure_future(self.cmd_server.event_sender())
|
||||||
|
self.cmd_server.showMyself()
|
||||||
loop.run_forever()
|
loop.run_forever()
|
||||||
logger.info('done')
|
logger.info('done')
|
||||||
|
|
|
@ -7,11 +7,11 @@ def getTopic(hugvey_id):
|
||||||
return "hv{}".format(hugvey_id)
|
return "hv{}".format(hugvey_id)
|
||||||
|
|
||||||
|
|
||||||
async def zmqSend(socket, hugvey_id, msg):
|
def zmqSend(socket, hugvey_id, msg):
|
||||||
msgData = json.dumps(msg)
|
msgData = json.dumps(msg)
|
||||||
topic = getTopic(hugvey_id)
|
topic = getTopic(hugvey_id)
|
||||||
logger.info("Send 0mq to {} containing {}".format(topic, msg))
|
logger.info("Send 0mq to {} containing {}".format(topic, msg))
|
||||||
await socket.send_multipart([topic.encode(), msgData.encode()])
|
socket.send_multipart([topic.encode(), msgData.encode()])
|
||||||
|
|
||||||
async def zmqReceive(socket):
|
async def zmqReceive(socket):
|
||||||
topic, msg = await socket.recv_multipart()
|
topic, msg = await socket.recv_multipart()
|
||||||
|
|
|
@ -3,6 +3,12 @@ import random
|
||||||
import sys
|
import sys
|
||||||
import time
|
import time
|
||||||
import hugvey.communication
|
import hugvey.communication
|
||||||
|
import coloredlogs, logging
|
||||||
|
|
||||||
|
coloredlogs.install(
|
||||||
|
level=logging.DEBUG
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
port = "5555"
|
port = "5555"
|
||||||
if len(sys.argv) > 1:
|
if len(sys.argv) > 1:
|
||||||
|
@ -11,7 +17,7 @@ if len(sys.argv) > 1:
|
||||||
|
|
||||||
context = zmq.Context()
|
context = zmq.Context()
|
||||||
socket = context.socket(zmq.PUB)
|
socket = context.socket(zmq.PUB)
|
||||||
socket.bind("tcp://*:%s" % port)
|
socket.connect("tcp://127.0.0.1:%s" % port)
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
# topic = random.randrange(9999,10005)
|
# topic = random.randrange(9999,10005)
|
||||||
|
|
Loading…
Reference in a new issue