2019-01-15 21:40:44 +01:00
|
|
|
import json
|
|
|
|
import logging
|
|
|
|
|
2019-03-23 18:18:52 +01:00
|
|
|
|
|
|
|
mainLogger = logging.getLogger("hugvey")
|
2019-01-15 21:40:44 +01:00
|
|
|
|
2019-02-11 21:28:48 +01:00
|
|
|
# hyper verbose log level. Have it here, becase it needs to be _somewhere_
|
|
|
|
LOG_BS = 5
|
|
|
|
|
2019-01-15 21:40:44 +01:00
|
|
|
def getTopic(hugvey_id):
|
2019-04-16 15:23:40 +02:00
|
|
|
if hugvey_id > 99:
|
|
|
|
raise Exception("Incompatible with hugvey id > 99")
|
|
|
|
|
|
|
|
# We will have to zero-fill the number (01 instead of 1), because of how zmq
|
|
|
|
# topic subscription works: subscribing to hv1 also brings in messages for
|
|
|
|
# hv10 hv11 and hv1whatever, hence only hv01, hv02 etc is unique
|
|
|
|
return "hv{:02}".format(hugvey_id)
|
2019-01-15 21:40:44 +01:00
|
|
|
|
|
|
|
|
2019-01-16 09:00:49 +01:00
|
|
|
def zmqSend(socket, hugvey_id, msg):
|
2019-03-25 17:45:07 +01:00
|
|
|
log = mainLogger.getChild("{}".format(hugvey_id)).getChild("communication")
|
2019-03-23 18:18:52 +01:00
|
|
|
log.debug("SEND: {}".format(msg))
|
2019-01-15 21:40:44 +01:00
|
|
|
msgData = json.dumps(msg)
|
|
|
|
topic = getTopic(hugvey_id)
|
2019-03-23 18:18:52 +01:00
|
|
|
log.debug("Send 0mq to {} containing {}".format(topic, msg))
|
2019-01-16 09:00:49 +01:00
|
|
|
socket.send_multipart([topic.encode(), msgData.encode()])
|
2019-01-15 21:40:44 +01:00
|
|
|
|
|
|
|
async def zmqReceive(socket):
|
|
|
|
topic, msg = await socket.recv_multipart()
|
2019-04-16 15:23:40 +02:00
|
|
|
hugvey_id = int(topic.decode()[2:])
|
2019-03-25 17:45:07 +01:00
|
|
|
mainLogger.getChild("{}".format(hugvey_id)).getChild("communication").debug(
|
2019-03-23 18:18:52 +01:00
|
|
|
"Received 0mq messages for Hugvey #{} containing {}".format(hugvey_id, msg.decode())
|
|
|
|
)
|
2019-04-16 15:23:40 +02:00
|
|
|
return hugvey_id, json.loads(msg.decode())
|