Exemple #1
0
    def __init__(self, socket):
        """Create a new client.

    Args:
      socket: the socket to use for communications.
    """
        self._socket = socket
        self._channel = StreamRpcChannel(socket, self)
        self._stub = Ola_pb2.OlaServerService_Stub(self._channel)
        self._universe_callbacks = {}
Exemple #2
0
  def __init__(self, our_socket=None, close_callback=None):
    """Create a new client.

    Args:
      socket: the socket to use for communications, if not provided one is
        created.
      close_callback: A callable to run if the socket is closed
    """
    self._socket = our_socket

    if self._socket is None:
      self._socket = socket.socket()
      try:
        self._socket.connect(('localhost', 9010))
      except socket.error:
        raise OLADNotRunningException('Failed to connect to olad')

    self._close_callback = close_callback
    self._channel = StreamRpcChannel(self._socket, self, self._SocketClosed)
    self._stub = Ola_pb2.OlaServerService_Stub(self._channel)
    self._universe_callbacks = {}
Exemple #3
0
class OlaClient(Ola_pb2.OlaClientService):
    """The client used to communicate with olad."""
    def __init__(self, our_socket=None, close_callback=None):
        """Create a new client.

    Args:
      socket: the socket to use for communications, if not provided one is
        created.
      close_callback: A callable to run if the socket is closed
    """
        self._socket = our_socket

        if self._socket is None:
            self._socket = socket.socket()
            try:
                self._socket.connect(('localhost', 9010))
            except socket.error:
                raise OLADNotRunningException('Failed to connect to olad')

        self._close_callback = close_callback
        self._channel = StreamRpcChannel(self._socket, self,
                                         self._SocketClosed)
        self._stub = Ola_pb2.OlaServerService_Stub(self._channel)
        self._universe_callbacks = {}

    def GetSocket(self):
        """Returns the socket used to communicate with the server."""
        return self._socket

    def SocketReady(self):
        """Called when the socket has new data."""
        self._channel.SocketReady()

    def _SocketClosed(self):
        """Called by the RPCChannel if the socket is closed."""
        try:
            self._socket.shutdown(socket.SHUT_RDWR)
        except socket.error:
            pass
        self._socket.close()
        self._socket = None

        if self._close_callback:
            self._close_callback()

    def FetchPlugins(self, callback):
        """Fetch the list of plugins.

    Args:
      callback: the function to call once complete, takes two arguments, a
        RequestStatus object and a list of Plugin objects

    Returns:
      True if the request was sent, False otherwise.
    """
        if self._socket is None:
            return False

        controller = SimpleRpcController()
        request = Ola_pb2.PluginListRequest()
        done = lambda x, y: self._GetPluginsComplete(callback, x, y)
        try:
            self._stub.GetPlugins(controller, request, done)
        except socket.error:
            raise OLADNotRunningException()
        return True

    def PluginDescription(self, callback, plugin_id):
        """Fetch the description of a plugin.

    Args:
      callback: the function to call once complete, takes two arguments, a
        RequestStatus object and the plugin description text.
      plugin_id: the id of the plugin

    Returns:
      True if the request was sent, False otherwise.
    """
        if self._socket is None:
            return False

        controller = SimpleRpcController()
        request = Ola_pb2.PluginDescriptionRequest()
        request.plugin_id = plugin_id
        done = lambda x, y: self._PluginDescriptionComplete(callback, x, y)
        try:
            self._stub.GetPluginDescription(controller, request, done)
        except socket.error:
            raise OLADNotRunningException()
        return True

    def FetchDevices(self, callback, plugin_filter=Plugin.OLA_PLUGIN_ALL):
        """Fetch a list of devices from the server.

    Args:
      callback: The function to call once complete, takes two arguments, a
        RequestStatus object and a list of Device objects.
      filter: a plugin id to filter by

    Returns:
      True if the request was sent, False otherwise.
    """
        if self._socket is None:
            return False

        controller = SimpleRpcController()
        request = Ola_pb2.DeviceInfoRequest()
        request.plugin_id = plugin_filter
        done = lambda x, y: self._DeviceInfoComplete(callback, x, y)
        try:
            self._stub.GetDeviceInfo(controller, request, done)
        except socket.error:
            raise OLADNotRunningException()
        return True

    def FetchUniverses(self, callback):
        """Fetch a list of universes from the server

    Args:
      callback: The function to call once complete, takes two arguments, a
        RequestStatus object and a list of Universe objects.

    Returns:
      True if the request was sent, False otherwise.
    """
        if self._socket is None:
            return False

        controller = SimpleRpcController()
        request = Ola_pb2.OptionalUniverseRequest()
        done = lambda x, y: self._UniverseInfoComplete(callback, x, y)
        try:
            self._stub.GetUniverseInfo(controller, request, done)
        except socket.error:
            raise OLADNotRunningException()
        return True

    def FetchDmx(self, universe, callback):
        """Fetch DMX data from the server

    Args:
      universe: the universe to fetch the data for
      callback: The function to call once complete, takes three arguments, a
        RequestStatus object, a universe number and a list of dmx data.

    Returns:
      True if the request was sent, False otherwise.
    """
        if self._socket is None:
            return False

        controller = SimpleRpcController()
        request = Ola_pb2.UniverseRequest()
        request.universe = universe
        done = lambda x, y: self._GetDmxComplete(callback, x, y)
        try:
            self._stub.GetDmx(controller, request, done)
        except socket.error:
            raise OLADNotRunningException()
        return True

    def SendDmx(self, universe, data, callback=None):
        """Send DMX data to the server

    Args:
      universe: the universe to send the data for
      data: An array object with the DMX data
      callback: The function to call once complete, takes one argument, a
        RequestStatus object.

    Returns:
      True if the request was sent, False otherwise.
    """
        if self._socket is None:
            return False

        controller = SimpleRpcController()
        request = Ola_pb2.DmxData()
        request.universe = universe
        request.data = data.tostring()
        done = lambda x, y: self._AckMessageComplete(callback, x, y)
        try:
            self._stub.UpdateDmxData(controller, request, done)
        except socket.error:
            raise OLADNotRunningException()
        return True

    def SetUniverseName(self, universe, name, callback=None):
        """Set the name of a universe.

    Args:
      universe: the universe to set the name of
      name: the new name for the universe
      callback: The function to call once complete, takes one argument, a
        RequestStatus object.

    Returns:
      True if the request was sent, False otherwise.
    """
        if self._socket is None:
            return False

        controller = SimpleRpcController()
        request = Ola_pb2.UniverseNameRequest()
        request.universe = universe
        request.name = name
        done = lambda x, y: self._AckMessageComplete(callback, x, y)
        try:
            self._stub.SetUniverseName(controller, request, done)
        except socket.error:
            raise OLADNotRunningException()
        return True

    def SetUniverseMergeMode(self, universe, merge_mode, callback=None):
        """Set the merge mode of a universe.

    Args:
      universe: the universe to set the merge mode of
      merge_mode: either Universe.HTP or Universe.LTP
      callback: The function to call once complete, takes one argument, a
        RequestStatus object.

    Returns:
      True if the request was sent, False otherwise.
    """
        if self._socket is None:
            return False

        controller = SimpleRpcController()
        request = Ola_pb2.MergeModeRequest()
        request.universe = universe
        request.merge_mode = merge_mode
        done = lambda x, y: self._AckMessageComplete(callback, x, y)
        try:
            self._stub.SetMergeMode(controller, request, done)
        except socket.error:
            raise OLADNotRunningException()
        return True

    def RegisterUniverse(self, universe, action, data_callback, callback=None):
        """Register to receive dmx updates for a universe.

    Args:
      universe: the universe to register to
      action: OlaClient.REGISTER or OlaClient.UNREGISTER
      data_callback: the function to be called when there is new data, passed
        a single argument of type array.
      callback: The function to call once complete, takes one argument, a
        RequestStatus object.

    Returns:
      True if the request was sent, False otherwise.
    """
        if self._socket is None:
            return False

        controller = SimpleRpcController()
        request = Ola_pb2.RegisterDmxRequest()
        request.universe = universe
        request.action = action
        done = lambda x, y: self._AckMessageComplete(callback, x, y)
        try:
            self._stub.RegisterForDmx(controller, request, done)
        except socket.error:
            raise OLADNotRunningException()
        if action == self.PATCH:
            self._universe_callbacks[universe] = data_callback
        elif universe in self._universe_callbacks:
            del self._universe_callbacks[universe]
        return True

    def PatchPort(self,
                  device_alias,
                  port,
                  is_output,
                  action,
                  universe,
                  callback=None):
        """Patch a port to a universe.

    Args:
      device_alias: the alias of the device of which to patch a port
      port: the id of the port
      is_output: select the input or output port
      action: OlaClient.PATCH or OlaClient.UNPATCH
      universe: the universe to set the name of
      callback: The function to call once complete, takes one argument, a
        RequestStatus object.

    Returns:
      True if the request was sent, False otherwise.
    """
        if self._socket is None:
            return False

        controller = SimpleRpcController()
        request = Ola_pb2.PatchPortRequest()
        request.device_alias = device_alias
        request.port_id = port
        request.action = action
        request.is_output = is_output
        request.universe = universe
        done = lambda x, y: self._AckMessageComplete(callback, x, y)
        try:
            self._stub.PatchPort(controller, request, done)
        except socket.error:
            raise OLADNotRunningException()
        return True

    def ConfigureDevice(self, device_alias, request_data, callback):
        """Send a device config request.

    Args:
      device_alias: the alias of the device to configure
      request_data: the request to send to the device
      callback: The function to call once complete, takes two arguments, a
        RequestStatus object and a response.

    Returns:
      True if the request was sent, False otherwise.
    """
        if self._socket is None:
            return False

        controller = SimpleRpcController()
        request = Ola_pb2.DeviceConfigRequest()
        request.device_alias = device_alias
        request.data = request_data
        done = lambda x, y: self._ConfigureDeviceComplete(callback, x, y)
        try:
            self._stub.ConfigureDevice(controller, request, done)
        except socket.error:
            raise OLADNotRunningException()
        return True

    def SendTimeCode(self,
                     time_code_type,
                     hours,
                     minutes,
                     seconds,
                     frames,
                     callback=None):
        """Send Time Code Data.

    Args:
      time_code_type: One of OlaClient.TIMECODE_FILM, OlaClient.TIMECODE_EBU,
        OlaClient.TIMECODE_DF or OlaClient.TIMECODE_SMPTE
      hours: the hours
      minutes: the minutes
      seconds: the seconds
      frames: the frame count
      callback: The function to call once complete, takes one argument, a
        RequestStatus object.

    Returns:
      True if the request was sent, False otherwise.
    """
        if self._socket is None:
            return False

        controller = SimpleRpcController()
        request = Ola_pb2.TimeCode()
        request.type = time_code_type
        request.hours = hours
        request.minutes = minutes
        request.seconds = seconds
        request.frames = frames
        done = lambda x, y: self._AckMessageComplete(callback, x, y)
        try:
            self._stub.SendTimeCode(controller, request, done)
        except socket.error:
            raise OLADNotRunningException()
        return True

    def UpdateDmxData(self, controller, request, callback):
        """Called when we receive new DMX data.

    Args:
      controller: An RpcController object
      reqeust: A DmxData message
      callback: The callback to run once complete

    Returns:
      True if the request was sent, False otherwise.
    """
        if self._socket is None:
            return False

        if request.universe in self._universe_callbacks:
            data = array.array('B')
            data.fromstring(request.data)
            self._universe_callbacks[request.universe](data)
        response = Ola_pb2.Ack()
        callback(response)
        return True

    def FetchUIDList(self, universe, callback):
        """Used to get a list of UIDs for a particular universe.

    Args:
      universe: The universe to get the UID list for.
      callback: The function to call once complete, takes two arguments, a
        RequestStatus object and a iterable of UIDs.

    Returns:
      True if the request was sent, False otherwise.
    """
        if self._socket is None:
            return False

        controller = SimpleRpcController()
        request = Ola_pb2.UniverseRequest()
        request.universe = universe
        done = lambda x, y: self._FetchUIDsComplete(callback, x, y)
        try:
            self._stub.GetUIDs(controller, request, done)
        except socket.error:
            raise OLADNotRunningException()
        return True

    def RunRDMDiscovery(self, universe, full, callback):
        """Triggers RDM discovery for a universe.

    Args:
      universe: The universe to run discovery for.
      full: true to use full discovery, false for incremental (if supported)
      callback: The function to call once complete, takes one argument, a
        RequestStatus object.

    Returns:
      True if the request was sent, False otherwise.
    """
        if self._socket is None:
            return False

        controller = SimpleRpcController()
        request = Ola_pb2.DiscoveryRequest()
        request.universe = universe
        request.full = full
        done = lambda x, y: self._FetchUIDsComplete(callback, x, y)
        try:
            self._stub.ForceDiscovery(controller, request, done)
        except socket.error:
            raise OLADNotRunningException()
        return True

    def RDMGet(self,
               universe,
               uid,
               sub_device,
               param_id,
               callback,
               data='',
               include_frames=False):
        """Send an RDM get command.

    Args:
      universe: The universe to get the UID list for.
      uid: A UID object
      sub_device: The sub device index
      param_id: the param ID
      callback: The function to call once complete, takes a RDMResponse object
      data: the data to send
      include_frames: True if the response should include the raw frame data.

    Returns:
      True if the request was sent, False otherwise.
    """
        if self._socket is None:
            return False

        return self._RDMMessage(universe, uid, sub_device, param_id, callback,
                                data, include_frames)

    def RDMSet(self,
               universe,
               uid,
               sub_device,
               param_id,
               callback,
               data='',
               include_frames=False):
        """Send an RDM set command.

    Args:
      universe: The universe to get the UID list for.
      uid: A UID object
      sub_device: The sub device index
      param_id: the param ID
      callback: The function to call once complete, takes a RDMResponse object
      data: the data to send
      include_frames: True if the response should include the raw frame data.

    Returns:
      True if the request was sent, False otherwise.
    """
        if self._socket is None:
            return False

        return self._RDMMessage(universe,
                                uid,
                                sub_device,
                                param_id,
                                callback,
                                data,
                                include_frames,
                                set=True)

    def SendRawRDMDiscovery(self,
                            universe,
                            uid,
                            sub_device,
                            param_id,
                            callback,
                            data='',
                            include_frames=False):
        """Send an RDM Discovery command. Unless you're writing RDM tests you
      shouldn't need to use this.

    Args:
      universe: The universe to get the UID list for.
      uid: A UID object
      sub_device: The sub device index
      param_id: the param ID
      callback: The function to call once complete, takes a RDMResponse object
      data: the data to send
      include_frames: True if the response should include the raw frame data.

    Returns:
      True if the request was sent, False otherwise.
    """
        if self._socket is None:
            return False

        controller = SimpleRpcController()
        request = Ola_pb2.RDMDiscoveryRequest()
        request.universe = universe
        request.uid.esta_id = uid.manufacturer_id
        request.uid.device_id = uid.device_id
        request.sub_device = sub_device
        request.param_id = param_id
        request.data = data
        request.include_raw_response = True
        request.include_raw_response = include_frames
        done = lambda x, y: self._RDMCommandComplete(callback, x, y)
        try:
            self._stub.RDMDiscoveryCommand(controller, request, done)
        except socket.error:
            raise OLADNotRunningException()
        return True

    def GetCandidatePorts(self, callback, universe=None):
        """Send a GetCandidatePorts request. The result is similar to FetchDevices
    (GetDeviceInfo), except that returned devices will only contain ports
    available for patching to the given universe. If universe is None, then the
    devices will list their ports available for patching to a potential new
    universe.

    Args:
      callback: The function to call once complete, takes a RequestStatus
        object and a list of Device objects.
      universe: The universe to get the candidate ports for. If unspecified,
        return the candidate ports for a new universe.

    Returns:
      True if the request was sent, False otherwise.
    """
        if self._socket is None:
            return False

        controller = SimpleRpcController()
        request = Ola_pb2.OptionalUniverseRequest()

        if universe is not None:
            request.universe = universe

        # GetCandidatePorts works very much like GetDeviceInfo, so we can re-use
        # its complete method.
        done = lambda x, y: self._DeviceInfoComplete(callback, x, y)
        try:
            self._stub.GetCandidatePorts(controller, request, done)
        except socket.error:
            raise OLADNotRunningException()

        return True

    def _RDMMessage(self,
                    universe,
                    uid,
                    sub_device,
                    param_id,
                    callback,
                    data,
                    include_frames,
                    set=False):
        controller = SimpleRpcController()
        request = Ola_pb2.RDMRequest()
        request.universe = universe
        request.uid.esta_id = uid.manufacturer_id
        request.uid.device_id = uid.device_id
        request.sub_device = sub_device
        request.param_id = param_id
        request.data = data
        request.is_set = set
        request.include_raw_response = include_frames
        done = lambda x, y: self._RDMCommandComplete(callback, x, y)
        try:
            self._stub.RDMCommand(controller, request, done)
        except socket.error:
            raise OLADNotRunningException()
        return True

    def _GetPluginsComplete(self, callback, controller, response):
        """Called when the list of plugins is returned.

    Args:
      callback: the callback to run
      controller: an RpcController
      response: a PluginInfoReply message.
    """
        if not callback:
            return

        status = RequestStatus(controller)
        plugins = None

        if status.Succeeded():
            plugins = sorted([Plugin.FromProtobuf(p) for p in response.plugin])

        callback(status, plugins)

    def _PluginDescriptionComplete(self, callback, controller, response):
        """Called when the plugin description is returned.

    Args:
      callback: the callback to run
      controller: an RpcController
      response: a PluginInfoReply message.
    """
        if not callback:
            return

        status = RequestStatus(controller)
        description = None

        if status.Succeeded():
            description = response.description

        callback(status, description)

    def _DeviceInfoComplete(self, callback, controller, response):
        """Called when the Device info request returns.

    Args:
      callback: the callback to run
      controller: an RpcController
      response: a DeviceInfoReply message.
    """
        if not callback:
            return
        status = RequestStatus(controller)
        devices = None

        if status.Succeeded():
            devices = []
            for device in response.device:
                input_ports = []
                output_ports = []
                for port in device.input_port:
                    input_ports.append(Port.FromProtobuf(port))

                for port in device.output_port:
                    output_ports.append(Port.FromProtobuf(port))

                devices.append(
                    Device(device.device_id, device.device_alias,
                           device.device_name, device.plugin_id, input_ports,
                           output_ports))
        callback(status, devices)

    def _UniverseInfoComplete(self, callback, controller, response):
        """Called when the Universe info request returns.

    Args:
      callback: the callback to run
      controller: an RpcController
      response: a UniverseInfoReply message.
    """
        if not callback:
            return
        status = RequestStatus(controller)
        universes = None

        if status.Succeeded():
            universes = [Universe.FromProtobuf(u) for u in response.universe]

        callback(status, universes)

    def _GetDmxComplete(self, callback, controller, response):
        """Called when the Universe info request returns.

    Args:
      callback: the callback to run
      controller: an RpcController
      response: a UniverseInfoReply message.
    """
        if not callback:
            return
        status = RequestStatus(controller)
        data = None
        universe = None

        if status.Succeeded():
            data = array.array('B')
            data.fromstring(response.data)
            universe = response.universe

        callback(status, universe, data)

    def _AckMessageComplete(self, callback, controller, response):
        """Called when an rpc that returns an Ack completes.

    Args:
      callback: the callback to run
      controller: an RpcController
      response: an Ack message.
    """
        if not callback:
            return
        status = RequestStatus(controller)
        callback(status)

    def _ConfigureDeviceComplete(self, callback, controller, response):
        """Called when a ConfigureDevice request completes.

    Args:
      callback: the callback to run
      controller: an RpcController
      response: an DeviceConfigReply message.
    """
        if not callback:
            return

        status = RequestStatus(controller)
        data = None

        if status.Succeeded():
            data = response.data

        callback(status, data)

    def _FetchUIDsComplete(self, callback, controller, response):
        """Called when a FetchUIDList request completes.

    Args:
      callback: the callback to run
      controller: an RpcController
      response: an UIDListReply message.
    """
        if not callback:
            return

        status = RequestStatus(controller)
        uids = None

        if status.Succeeded():
            uids = []
            for uid in response.uid:
                uids.append(UID(uid.esta_id, uid.device_id))
            uids.sort()

        callback(status, uids)

    def _RDMCommandComplete(self, callback, controller, response):
        """Called when a RDM request completes.

    Args:
      callback: the callback to run
      controller: an RpcController
      response: an RDMResponse message.
    """
        if not callback:
            return
        callback(RDMResponse(controller, response))
Exemple #4
0
class OlaClient(Ola_pb2.OlaClientService):
    """The client used to communicate with olad."""
    def __init__(self, socket):
        """Create a new client.

    Args:
      socket: the socket to use for communications.
    """
        self._socket = socket
        self._channel = StreamRpcChannel(socket, self)
        self._stub = Ola_pb2.OlaServerService_Stub(self._channel)
        self._universe_callbacks = {}

    def SocketReady(self):
        """Called when the socket has new data."""
        self._channel.SocketReady()

    def FetchPlugins(self,
                     callback,
                     plugin_filter=Plugin.OLA_PLUGIN_ALL,
                     include_description=False):
        """Fetch the list of plugins.

    Args:
      callback: the function to call once complete, takes two arguments, a
        RequestStatus object and a list of Plugin objects
      filter: the id of the plugin if you want to filter the results
      include_description: whether to include the plugin description or not
    """
        controller = SimpleRpcController()
        request = Ola_pb2.PluginInfoRequest()
        request.plugin_id = plugin_filter
        request.include_description = include_description
        done = lambda x, y: self._PluginInfoComplete(callback, x, y)
        self._stub.GetPluginInfo(controller, request, done)

    def FetchDevices(self, callback, plugin_filter=Plugin.OLA_PLUGIN_ALL):
        """Fetch a list of devices from the server.

    Args:
      callback: The function to call once complete, takes two arguments, a
        RequestStatus object and a list of Device objects.
      filter: a plugin id to filter by
    """
        controller = SimpleRpcController()
        request = Ola_pb2.DeviceInfoRequest()
        request.plugin_id = plugin_filter
        done = lambda x, y: self._DeviceInfoComplete(callback, x, y)
        self._stub.GetDeviceInfo(controller, request, done)

    def FetchUniverses(self, callback):
        """Fetch a list of universes from the server

    Args:
      callback: The function to call once complete, takes two arguments, a
        RequestStatus object and a list of Universe objects.
    """
        controller = SimpleRpcController()
        request = Ola_pb2.UniverseInfoRequest()
        done = lambda x, y: self._UniverseInfoComplete(callback, x, y)
        self._stub.GetUniverseInfo(controller, request, done)

    def FetchDmx(self, universe, callback):
        """Fetch a list of universes from the server

    Args:
      universe: the universe to fetch the data for
      callback: The function to call once complete, takes three arguments, a
        RequestStatus object, a universe number and a list of dmx data.
    """
        controller = SimpleRpcController()
        request = Ola_pb2.UniverseInfoRequest()
        request.universe = universe
        done = lambda x, y: self._GetDmxComplete(callback, x, y)
        self._stub.GetDmx(controller, request, done)

    def SendDmx(self, universe, data, callback=None):
        """Send DMX data to the server

    Args:
      universe: the universe to fetch the data for
      data: An array object with the DMX data
      callback: The function to call once complete, takes one argument, a
        RequestStatus object.
    """
        controller = SimpleRpcController()
        request = Ola_pb2.DmxData()
        request.universe = universe
        request.data = data.tostring()
        done = lambda x, y: self._AckMessageComplete(callback, x, y)
        self._stub.UpdateDmxData(controller, request, done)

    def SetUniverseName(self, universe, name, callback=None):
        """Set the name of a universe.

    Args:
      universe: the universe to set the name of
      name: the new name for the universe
      callback: The function to call once complete, takes one argument, a
        RequestStatus object.
    """
        controller = SimpleRpcController()
        request = Ola_pb2.UniverseNameRequest()
        request.universe = universe
        request.name = name
        done = lambda x, y: self._AckMessageComplete(callback, x, y)
        self._stub.SetUniverseName(controller, request, done)

    def SetUniverseMergeMode(self, universe, merge_mode, callback=None):
        """Set the merge_mode of a universe.

    Args:
      universe: the universe to set the name of
      merge_mode: either Universe.HTP or Universe.LTP
      callback: The function to call once complete, takes one argument, a
        RequestStatus object.
    """
        controller = SimpleRpcController()
        request = Ola_pb2.MergeModeRequest()
        request.universe = universe
        request.merge_mode = merge_mode
        done = lambda x, y: self._AckMessageComplete(callback, x, y)
        self._stub.SetMergeMode(controller, request, done)

    def RegisterUniverse(self, universe, action, data_callback, callback=None):
        """Register to receive dmx updates for a universe.

    Args:
      universe: the universe to set the name of
      action: OlaClient.REGISTER or OlaClient.UNREGISTER
      data_callback: the function to be called when there is new data, passed
        a single argument of type array.
      callback: The function to call once complete, takes one argument, a
        RequestStatus object.
    """
        controller = SimpleRpcController()
        request = Ola_pb2.RegisterDmxRequest()
        request.universe = universe
        request.action = action
        done = lambda x, y: self._AckMessageComplete(callback, x, y)
        self._stub.RegisterForDmx(controller, request, done)
        if action == self.PATCH:
            self._universe_callbacks[universe] = data_callback
        elif universe in self._universe_callbacks:
            del self._universe_callbacks[universe]

    def PatchPort(self, device_alias, port, action, universe, callback=None):
        """Patch a port to a universe.

    Args:
      device_alias: the alias of the device to configure
      port: the id of the port
      action: OlaClient.PATCH or OlcClient.UNPATCH
      universe: the universe to set the name of
      callback: The function to call once complete, takes one argument, a
        RequestStatus object.
    """
        controller = SimpleRpcController()
        request = Ola_pb2.PatchPortRequest()
        request.device_alias = device_alias
        request.port_id = port
        request.action = action
        request.universe = universe
        done = lambda x, y: self._AckMessageComplete(callback, x, y)
        self._stub.PatchPort(controller, request, done)

    def ConfigureDevice(self, device_alias, request_data, callback):
        """Send a device config request.

    Args:
      device_alias: the alias of the device to configure
      request_data: the request to send to the device
      callback: The function to call once complete, takes two arguments, a
        RequestStatus object and a response.
    """
        controller = SimpleRpcController()
        request = Ola_pb2.DeviceConfigRequest()
        request.device_alias = device_alias
        request.data = request_data
        done = lambda x, y: self._ConfigureDeviceComplete(callback, x, y)
        self._stub.ConfigureDevice(controller, request, done)

    def UpdateDmxData(self, controller, request, callback):
        """Called when we receive new DMX data.

    Args:
      controller: An RpcController object
      reqeust: A DmxData message
      callback: The callback to run once complete
    """
        if request.universe in self._universe_callbacks:
            data = array.array('B')
            data.fromstring(request.data)
            self._universe_callbacks[request.universe](data)
        response = Ola_pb2.Ack()
        callback(response)

    def _CreateStateFromController(self, controller):
        """Return a Status object given a RpcController object.

    Args:
      controller: An RpcController object.

    Returns:
      A RequestStatus object.
    """
        if controller.Failed():
            return RequestStatus(RequestStatus.FAILED, controller.ErrorText())
        elif controller.IsCanceled():
            return RequestStatus(RequestStatus.CANCELLED,
                                 controller.ErrorText())
        else:
            return RequestStatus()

    def _PluginInfoComplete(self, callback, controller, response):
        """Called when the list of plugins is returned.

    Args:
      callback: the callback to run
      controller: an RpcController
      response: a PluginInfoReply message.
    """
        if not callback:
            return
        status = self._CreateStateFromController(controller)
        if not status.Succeeded():
            return

        plugins = [
            Plugin(p.plugin_id, p.name, p.description) for p in response.plugin
        ]
        callback(status, plugins)

    def _DeviceInfoComplete(self, callback, controller, response):
        """Called when the Device info request returns.

    Args:
      callback: the callback to run
      controller: an RpcController
      response: a DeviceInfoReply message.
    """
        if not callback:
            return
        status = self._CreateStateFromController(controller)
        if not status.Succeeded():
            return

        devices = []
        for device in response.device:
            input_ports = []
            output_ports = []
            for port in device.input_port:
                input_ports.append(
                    Port(port.port_id, port.universe, port.active,
                         port.description))

            for port in device.output_port:
                output_ports.append(
                    Port(port.port_id, port.universe, port.active,
                         port.description))

            devices.append(
                Device(device.device_id, device.device_alias,
                       device.device_name, device.plugin_id, input_ports,
                       output_ports))
        callback(status, devices)

    def _UniverseInfoComplete(self, callback, controller, response):
        """Called when the Universe info request returns.

    Args:
      callback: the callback to run
      controller: an RpcController
      response: a UniverseInfoReply message.
    """
        if not callback:
            return
        status = self._CreateStateFromController(controller)
        if not status.Succeeded():
            return

        universes = [
            Universe(u.universe, u.name, u.merge_mode)
            for u in response.universe
        ]
        callback(status, universes)

    def _GetDmxComplete(self, callback, controller, response):
        """Called when the Universe info request returns.

    Args:
      callback: the callback to run
      controller: an RpcController
      response: a UniverseInfoReply message.
    """
        if not callback:
            return
        status = self._CreateStateFromController(controller)
        if not status.Succeeded():
            return

        data = array.array('B')
        data.fromstring(response.data)
        callback(status, response.universe, data)

    def _AckMessageComplete(self, callback, controller, response):
        """Called when an rpc that returns an Ack completes.

    Args:
      callback: the callback to run
      controller: an RpcController
      response: an Ack message.
    """
        if not callback:
            return
        status = self._CreateStateFromController(controller)
        if not status.Succeeded():
            return
        callback(status)

    def _ConfigureDeviceComplete(self, callback, controller, response):
        """Called when a ConfigureDevice request completes.

    Args:
      callback: the callback to run
      controller: an RpcController
      response: an DeviceConfigReply message.
    """
        if not callback:
            return
        status = self._CreateStateFromController(controller)
        if not status.Succeeded():
            return
        callback(status, response.data)