Beispiel #1
0
    def execute_command(self, tag, value):

        execute = False
        for it in self._actions:
            if it.is_execute(tag):
                it.execute(value)
                execute = True
        if not execute:
            LOGGER.debug('unknowm command')
Beispiel #2
0
    def client(self):

        LOGGER.debug('connecting to server %s:%s', self._host, self._port)
        try:
            stream = yield tcpclient.TCPClient().connect(self._host, self._port)
        except socket.error:
            LOGGER.error('failed to connect')
            self.close()
            return

        while not stream.closed():
            try:
                res = yield stream.read_bytes(NUM_START_BYTES)
            except iostream.StreamClosedError:
                self.close()
                return

            tag, length = struct.unpack('>BB', res)

            if length > 0:
                try:
                    value = yield stream.read_bytes(length)
                    r, g, b = struct.unpack('BBB', value)
                except iostream.StreamClosedError:
                    LOGGER.warning('connection closed in the middle of command')
                    self.close()
                    return
                value = (r, g, b)
            else:
                value = None

            LOGGER.debug('got message %s %s %s', tag, length, repr(value))
            self.execute_command(tag, value)
            print(self.lamp.draw())
Beispiel #3
0
 def handle_stream(self, stream, address):
     LOGGER.info('torch %s connected', address)
     for _ in range(20):
         msg = random.choice([
             struct.pack('>BB', 18, 0),
             struct.pack('>BB', 19, 0),
             struct.pack('>BBBBB', 32, 3, random.randint(0, 250), random.randint(0, 250), random.randint(0, 250))
         ])
         try:
             stream.write(msg)
         except iostream.StreamClosedError:
             LOGGER.warning(
                 'connection with %s closed unexpectedly', address)
             return
         time.sleep(.5)
     LOGGER.info('closing connection with %s', address)
Beispiel #4
0
 def add_command(self):
     LOGGER.debug('add commands')
     self.lamp.actions = On(ref(self.lamp)())
     self.lamp.actions = Off(ref(self.lamp)())
     self.lamp.actions = ChangeColor(ref(self.lamp)())
Beispiel #5
0
 def __init__(self, host=DEFAULT_HOST, port=DEFAULT_PORT):
     LOGGER.debug('init client')
     self._host = host
     self._port = port
     self.lamp = Lamp()
Beispiel #6
0
from tornado import tcpserver

from lamp_ivideon.settings import LOGGER


class TorchTestServer(tcpserver.TCPServer):
    def handle_stream(self, stream, address):
        LOGGER.info('torch %s connected', address)
        for _ in range(20):
            msg = random.choice([
                struct.pack('>BB', 18, 0),
                struct.pack('>BB', 19, 0),
                struct.pack('>BBBBB', 32, 3, random.randint(0, 250), random.randint(0, 250), random.randint(0, 250))
            ])
            try:
                stream.write(msg)
            except iostream.StreamClosedError:
                LOGGER.warning(
                    'connection with %s closed unexpectedly', address)
                return
            time.sleep(.5)
        LOGGER.info('closing connection with %s', address)


if __name__ == '__main__':
    server = TorchTestServer()
    server.listen(9999)
    LOGGER.info('starting torch test server on 127.0.0.1:9999')
    instance = ioloop.IOLoop.instance()
    instance.start()