changeset 495:e3a86bd3a01a

i don't think this works
author Louis Opter <kalessin@kalessin.fr>
date Thu, 06 Oct 2016 02:16:35 -0700
parents 8f70d5539e5c
children 08ad69e0a7a7
files add_monolight.patch
diffstat 1 files changed, 61 insertions(+), 22 deletions(-) [+]
line wrap: on
line diff
--- a/add_monolight.patch	Wed Oct 05 00:40:24 2016 -0700
+++ b/add_monolight.patch	Thu Oct 06 02:16:35 2016 -0700
@@ -1,5 +1,5 @@
 # HG changeset patch
-# Parent  355e10e803403054d9d8a453a072fdaa311ec115
+# Parent  c38eb50fef4516266f1231b1278af61bb43b64b8
 Start an experimental GUI for a Monome 128 Varibright
 
 Written in Python >= 3.5.
@@ -102,7 +102,7 @@
 new file mode 100644
 --- /dev/null
 +++ b/monolight/monolight/lightsc.py
-@@ -0,0 +1,173 @@
+@@ -0,0 +1,212 @@
 +# Copyright (c) 2016, Louis Opter <louis@opter.org>
 +#
 +# This file is part of lightsd.
@@ -122,6 +122,7 @@
 +
 +import asyncio
 +import collections
++import contextlib
 +import functools
 +import json
 +import logging
@@ -136,6 +137,38 @@
 +)
 +
 +
++class _JSONRPCCall:
++
++    def __init__(self, method, params, response_handler):
++        self.request = {
++            "id": str(uuid.uuid4()),
++            "jsonrpc": "2.0",
++            "method": method,
++            "params": params,
++        }
++        self.response_handler = response_handler
++
++
++class _LightsClientBatchProxy:
++
++    def __init__(self, client):
++        self._client = client
++        self._batch = []
++
++        def batch_jsonrpc_call(method):
++            @functools.wraps(method)
++            async def wrapper(client, method, params, response_handler=None):
++                self._batch.append(
++                    _JSONRPCCall(method, params, response_handler),
++                )
++            return wrapper
++
++        self._jsonrpc_call = batch_jsonrpc_call(client._jsonrpc_call)
++
++    def __getattr__(self, name):
++        return self._client.__getattribute__(name)
++
++
 +class LightsClient:
 +
 +    READ_SIZE = 8192
@@ -224,27 +257,38 @@
 +
 +            self._handle_response(response["id"], response["result"])
 +
-+    async def _jsonrpc_call(self, method, params, response_handler=None):
-+        id = str(uuid.uuid4())
-+        payload = {
-+            "method": method,
-+            "params": params,
-+            "jsonrpc": "2.0",
-+            "id": id,
-+        }
-+        payload = json.dumps(payload)
++    async def _jsonrpc_execute(self, pipeline):
++        calls = [call.request for call in pipeline]
++        payload = json.dumps(calls[0] if len(calls) == 1 else calls)
 +        payload = payload.encode(self.encoding, "surrogateescape")
++
 +        self._writer.write(payload)
 +
-+        logger.info("Request {}: {}({})".format(id, method, params))
++        for call in calls:
++            logger.info("Request {id}: {method}({params})".format(**call))
 +
 +        await self._writer.drain()
 +
-+        timeout_cb = functools.partial(self._handle_response, id, response=None)
-+        self._pending_requests[id] = PendingRequestEntry(
-+            handler_cb=response_handler,
-+            timeout_handle=self._loop.call_later(self.TIMEOUT, timeout_cb)
-+        )
++        for call in pipeline:
++            id = call.request["id"]
++            timeout_cb = functools.partial(
++                self._handle_response, id, response=None
++            )
++            self._pending_requests[id] = PendingRequestEntry(
++                handler_cb=call.response_handler,
++                timeout_handle=self._loop.call_later(self.TIMEOUT, timeout_cb)
++            )
++
++    async def _jsonrpc_call(self, method, params, response_handler=None):
++        await self._jsonrpc_execute([
++            _JSONRPCCall(method, params, response_handler),
++        ])
++
++    @contextlib.contextmanager
++    def batch(self):
++        proxy = _LightsClientBatchProxy(self)
++        yield proxy
++        self.loop.ensure_future(self._jsonrpc_execute(proxy._batch))
 +
 +    async def get_light_state(self, targets):
 +        await self._jsonrpc_call(
@@ -263,11 +307,6 @@
 +    def _handle_light_state(self, response):
 +        self._bulbs = {b["label"]: b for b in response}
 +
-+    async def poll(self):
-+        while True:
-+            await self.get_light_state(["*"])
-+            await asyncio.sleep(self.refresh_interval)
-+
 +
 +async def create_lightsd_connection(url, loop=None):
 +    if loop is None: