예제 #1
0
파일: network.py 프로젝트: ObsidianX/pyCEC
 def start(self):
     _LOGGER.info("HDMI network starting...")  # pragma: no cover
     self._running = True
     self._loop.create_task(self.async_init())
     self._loop.create_task(self.async_watch())
     if self._managed_loop:
         self._loop.run_in_executor(None, self._loop.run_forever)
예제 #2
0
파일: network.py 프로젝트: rajlaud/pyCEC
 async def async_scan(self):
     _LOGGER.info("Looking for new devices...")
     if not self.initialized:
         _LOGGER.error("Device not initialized!!!")  # pragma: no cover
         return
     for d in range(15):
         task = self._adapter.poll_device(d)
         task.add_done_callback(functools.partial(self._after_polled, d))
예제 #3
0
파일: network.py 프로젝트: ObsidianX/pyCEC
 def async_run(self):
     _LOGGER.debug("Starting device %d", self.logical_address)
     while not self._stop:
         for prop in UPDATEABLE:
             if not self._stop:
                 yield from self.async_request_update(prop[0])
         start_time = self._loop.time()
         while not self._stop and self._loop.time() <= (
                 start_time + self._update_period):
             yield from asyncio.sleep(.3, loop=self._loop)
     _LOGGER.info("HDMI device %s stopped.", self)  # pragma: no cover
예제 #4
0
파일: network.py 프로젝트: ObsidianX/pyCEC
 def stop(self):
     _LOGGER.debug("HDMI network shutdown.")  # pragma: no cover
     self._running = False
     for d in self._devices.values():
         d.stop()
     if self._managed_loop:
         self._loop.stop()
         while self._loop.is_running():
             time.sleep(.1)
         self._loop.close()
     self._adapter.shutdown()
     _LOGGER.info("HDMI network stopped.")  # pragma: no cover
예제 #5
0
 def stop(self):
     _LOGGER.debug("HDMI network shutdown.")  # pragma: no cover
     self._running = False
     for d in self.devices:
         d.stop()
     self._adapter.shutdown()
     if self._managed_loop:
         _LOGGER.debug("Stopping HDMI loop.")  # pragma: no cover
         self._loop.stop()
         _LOGGER.debug("Cleanup loop")  # pragma: no cover
         asyncio.sleep(3, loop=self._loop)
         self._loop.close()
     _LOGGER.info("HDMI network stopped.")  # pragma: no cover
예제 #6
0
파일: network.py 프로젝트: rajlaud/pyCEC
 async def async_run(self):
     _LOGGER.debug("Starting device %d", self.logical_address)
     while not self._stop:
         for prop in UPDATEABLE:
             if not self._stop and (prop != CMD_PHYSICAL_ADDRESS
                                    or self._physical_address is None):
                 # Skip update of physical address unless unset to prevent
                 # unwanted activity on Panasonic television display.
                 _LOGGER.debug("Updating property: %s", UPDATEABLE[prop])
                 await self.async_request_update(prop[0])
         start_time = self._loop.time()
         while not self._stop and self._loop.time() <= (
                 start_time + self._update_period):
             await asyncio.sleep(0.3, loop=self._loop)
     _LOGGER.info("HDMI device %s stopped.", self)  # pragma: no cover
예제 #7
0
파일: network.py 프로젝트: ObsidianX/pyCEC
 def async_watch(self, loop=None):
     _LOGGER.debug("Start watching...")  # pragma: no cover
     if loop is None:
         loop = self._loop
     _LOGGER.debug("loop: %s", loop)
     while self._running:
         if self.initialized:
             _LOGGER.debug("Scanning...")  # pragma: no cover
             yield from self.async_scan()
             _LOGGER.debug("Sleep...")  # pragma: no cover
             start_time = self._loop.time()
             while self._loop.time() <= (
                     start_time + self._scan_interval) and self._running:
                 yield from asyncio.sleep(.3, loop=loop)
         else:
             _LOGGER.warning("Not initialized. Waiting for init.")
             yield from asyncio.sleep(1, loop=loop)
     _LOGGER.info("No watching anymore")
예제 #8
0
 def data_received(self, data):
     self.buffer += bytes.decode(data)
     for line in self.buffer.splitlines(keepends=True):
         if line.endswith('\n'):
             line = line.rstrip()
             if len(line) == 2:
                 _LOGGER.info("Received poll %s from %s", line,
                              self.transport.get_extra_info('peername'))
                 d = CecCommand(line).dst
                 t = network._adapter.poll_device(d)
                 t.add_done_callback(
                     functools.partial(_after_poll, d))
             else:
                 _LOGGER.info("Received command %s from %s", line,
                              self.transport.get_extra_info('peername'))
                 network.send_command(CecCommand(line))
             self.buffer = ''
         else:
             self.buffer = line
예제 #9
0
 def _send_command_to_tcp(command):
     for t in transports:
         _LOGGER.info("Sending %s to %s", command,
                      t.get_extra_info('peername'))
         t.write(str.encode("%s\n" % command.raw))
예제 #10
0
 def connection_lost(self, exc):
     _LOGGER.info("Connection with %s lost",
                  self.transport.get_extra_info('peername'))
     transports.remove(self.transport)
예제 #11
0
 def connection_made(self, transport):
     _LOGGER.info("Connection opened by %s",
                  transport.get_extra_info('peername'))
     self.transport = transport
     transports.add(transport)
예제 #12
0
def main():
    config = configure()

    # Configure logging
    setup_logger(config)

    transports = set()
    loop = asyncio.get_event_loop()
    network = HDMINetwork(CecAdapter("pyCEC", activate_source=False), loop=loop)

    class CECServerProtocol(asyncio.Protocol):
        transport = None
        buffer = ''

        def connection_made(self, transport):
            _LOGGER.info("Connection opened by %s",
                         transport.get_extra_info('peername'))
            self.transport = transport
            transports.add(transport)

        def data_received(self, data):
            self.buffer += bytes.decode(data)
            for line in self.buffer.splitlines(keepends=True):
                if line.endswith('\n'):
                    line = line.rstrip()
                    if len(line) == 2:
                        _LOGGER.info("Received poll %s from %s", line,
                                     self.transport.get_extra_info('peername'))
                        d = CecCommand(line).dst
                        t = network._adapter.poll_device(d)
                        t.add_done_callback(
                            functools.partial(_after_poll, d))
                    else:
                        _LOGGER.info("Received command %s from %s", line,
                                     self.transport.get_extra_info('peername'))
                        network.send_command(CecCommand(line))
                    self.buffer = ''
                else:
                    self.buffer = line

        def connection_lost(self, exc):
            _LOGGER.info("Connection with %s lost",
                         self.transport.get_extra_info('peername'))
            transports.remove(self.transport)

    def _after_poll(d, f):
        if f.result():
            cmd = PollCommand(network._adapter.get_logical_address(), src=d)
            _send_command_to_tcp(cmd)

    def _send_command_to_tcp(command):
        for t in transports:
            _LOGGER.info("Sending %s to %s", command,
                         t.get_extra_info('peername'))
            t.write(str.encode("%s\n" % command.raw))

    network.set_command_callback(_send_command_to_tcp)
    loop.run_until_complete(network.async_init())

    _LOGGER.info("CEC initialized... Starting server.")
    # Each client connection will create a new protocol instance
    coro = loop.create_server(CECServerProtocol, config['DEFAULT']['host'],
                              int(config['DEFAULT']['port']))
    server = loop.run_until_complete(coro)
    # Serve requests until Ctrl+C is pressed
    _LOGGER.info('Serving on {}'.format(server.sockets[0].getsockname()))
    if _LOGGER.level >= logging.DEBUG:
        loop.create_task(async_show_devices(network, loop))
    try:
        loop.run_forever()
    except KeyboardInterrupt:
        pass

    # Close the server
    server.close()
    loop.run_until_complete(server.wait_closed())
    loop.close()