class EnoceanConnector:

    def __init__(self, port):
        self._port = port
        self._enocean = None
        self._cached_base_id = None

    def open(self):
        self._enocean = SerialCommunicator(self._port)
        self._enocean.start()
        _logger.debug("open")

    def close(self):
        if self._enocean is not None:  # and self._enocean.is_alive():
            self._enocean.stop()
            self._enocean = None

    def is_alive(self):
        if not self._enocean:
            return False
        return self._enocean.is_alive()

    def assure_connection(self):  # force option?
        if self._enocean is None:
            self.open()
        else:
            if not self._enocean.is_alive():
                _logger.warning("enocean is not alive - try to reopen! (may crash, restart via systemd)")
                self.close()
                self.open()

    def get_messages(self) -> [EnoceanMessage]:
        messages = []  # type[EnoceanMessage]
        loop = 0
        while self._enocean.is_alive() and loop < 50:
            loop += 1

            try:
                packet = self._enocean.receive.get(block=False)
            except queue.Empty:
                break  # loop untile the queue is empty...

            if hasattr(packet, "sender_int"):
                message = EnoceanMessage(payload=packet, enocean_id=packet.sender_int)
                messages.append(message)

        return messages

    @property
    def base_id(self):
        if self._cached_base_id is None and self._enocean is not None:
            self._cached_base_id = self._enocean.base_id
        return self._cached_base_id

    def send(self, packet):
        if self._enocean is not None:
            self._enocean.send(packet)
            pass
Ejemplo n.º 2
0
# Needs a bit of sleep in between, working too fast :S
time.sleep(0.1)
turn_on([0x01, 0x94, 0xE3, 0xB9])
time.sleep(1)

turn_off([0x01, 0x94, 0xB9, 0x46])
time.sleep(0.1)
turn_off([0x01, 0x94, 0xE3, 0xB9])

print(
    'Press and hold the teach-in button on the plug now, till it starts turning itself off and on (about 10 seconds or so...)'
)
devices_learned = []

# endless loop receiving radio packets
while communicator.is_alive():
    try:
        # Loop to empty the queue...
        packet = communicator.receive.get(block=True, timeout=1)
        if isinstance(packet, UTETeachInPacket):
            print('New device learned! The ID is %s.' % (packet.sender_hex))
            devices_learned.append(packet.sender)
    except queue.Empty:
        continue
    except KeyboardInterrupt:
        break
    except Exception:
        traceback.print_exc(file=sys.stdout)
        break

print('Devices learned during this session: %s' %
Ejemplo n.º 3
0
                           rorg_type=0x01,
                           destination=destination,
                           sender=communicator.base_id,
                           command=1,
                           IO=0x1E,
                           OV=output_value))


def turn(destination, value):
    send_command(destination, value)


value = 0
if (ans == "on"):
    value = 1
elif (ans == "off"):
    value = 0
else:
    sys.exit("Erreur rentrer l'argument on ou off s'il vous plait")
communicator = SerialCommunicator()
communicator.start()
print('The Base ID of your module is %s.' %
      enocean.utils.to_hex_string(communicator.base_id))

#01:98:ED:72->01:96:0C:91
turn([0x01, 0x98, 0xED, 0x72], value)

time.sleep(0.1)

if communicator.is_alive():
    communicator.stop()
Ejemplo n.º 4
0
turn_on([0x01, 0x94, 0xB9, 0x46])
# Needs a bit of sleep in between, working too fast :S
time.sleep(0.1)
turn_on([0x01, 0x94, 0xE3, 0xB9])
time.sleep(1)

turn_off([0x01, 0x94, 0xB9, 0x46])
time.sleep(0.1)
turn_off([0x01, 0x94, 0xE3, 0xB9])


print('Press and hold the teach-in button on the plug now, till it starts turning itself off and on (about 10 seconds or so...)')
devices_learned = []

# endless loop receiving radio packets
while communicator.is_alive():
    try:
        # Loop to empty the queue...
        packet = communicator.receive.get(block=True, timeout=1)
        if isinstance(packet, UTETeachIn):
            print('New device learned! The ID is %s.' % (packet.sender_hex))
            devices_learned.append(packet.sender)
    except queue.Empty:
        continue
    except KeyboardInterrupt:
        break
    except Exception:
        traceback.print_exc(file=sys.stdout)
        break

print('Devices learned during this session: %s' % (', '.join([enocean.utils.to_hex_string(x) for x in devices_learned])))