Files @ ba2f00912e20
Branch filter:

Location: light9/src/light9/collector/collector_client_asyncio.py

drewp@bigasterisk.com
fix white output on RGBW devices
import asyncio
import json
import logging
import time
from light9 import networking
from light9.effect.settings import DeviceSettings
import zmq.asyncio
from prometheus_client import Summary

log = logging.getLogger('coll_client')

ZMQ_SEND = Summary('zmq_send', 'calls')


def toCollectorJson(client, session, settings: DeviceSettings) -> str:
    assert isinstance(settings, DeviceSettings)
    return json.dumps({
        'settings': settings.asList(),
        'client': client,
        'clientSession': session,
        'sendTime': time.time(),
    })


class _Sender:

    def __init__(self):
        self.context = zmq.asyncio.Context()
        self.socket = self.context.socket(zmq.PUB)
        self.socket.connect('tcp://127.0.0.1:9203')  #todo: tie to :collectorZmq in graph
        # old version used: 'tcp://%s:%s' % (service.host, service.port)

    @ZMQ_SEND.time()
    async def send(self, client: str, session: str, settings: DeviceSettings):
        msg = toCollectorJson(client, session, settings).encode('utf8')
        # log.info(f'zmq send {len(msg)}')
        await self.socket.send_multipart([b'setAttr', msg])


_sender = _Sender()

sendToCollector = _sender.send