def __init__(self): self.__ipcon = IPConnection() # Create IP connection self.__rs = BrickletRemoteSwitch(CONFIG.uid, self.__ipcon) # Create device object self.__ipcon.connect(CONFIG.host, CONFIG.port) # Connect to brickd self.__operations = deque() self.__operations_worker = Thread(target=self.__work_operations) self.__keep_worker_running = True self.__operations_worker.start() self.__mqtt = None LOG.info("RSBController running")
class RSBController(object): def __init__(self): self.__ipcon = IPConnection() # Create IP connection self.__rs = BrickletRemoteSwitch(CONFIG.uid, self.__ipcon) # Create device object self.__ipcon.connect(CONFIG.host, CONFIG.port) # Connect to brickd self.__operations = deque() self.__operations_worker = Thread(target=self.__work_operations) self.__keep_worker_running = True self.__operations_worker.start() self.__mqtt = None LOG.info("RSBController running") # noinspection PyAttributeOutsideInit def inject_mqtt_processor(self, mqtt): """ Add an mqtt processor that may be used for message publishing. :param mqtt: the mqtt processor """ self.__mqtt = mqtt def add_socket_switch_operation_to_queue(self, address, unit, state): """ Add a switch operation for the given socket to the RSBController. :param address: the address of the socket :param unit: the unit of the socket :param state: 0 (off) or 1 (on) """ self.__operations.append([int(address), int(unit), int(state)]) def shutdown(self): LOG.info("RSBController stopping") self.__keep_worker_running = False while self.__operations_worker.isAlive(): LOG.debug("Waiting for operations worker to stop") time.sleep(1) self.__ipcon.disconnect() LOG.info("RSBController shutdown") def __work_operations(self): LOG.info("Operations worker running") while self.__keep_worker_running: if self.__operation_open(): operation = self.__dequeueOperation() # valid assumptions, as operations can only be added through 'RSBController.add_socket_switch_operation_to_queue' self.__switch_socket_B(operation[0], operation[1], operation[2]) time.sleep(0.01) def __operation_open(self): if self.__operations: return True return False def __dequeueOperation(self): return self.__operations.popleft() def __switch_socket_B(self, address, unit, state): """ Switch socket of type B :param address: the address of the socket :param unit: the unit of the socket :param state: 0 (off) or 1 (on) """ try: if state == 0: LOG.debug("Switching {0}-{1} to off".format(address, unit)) elif state == 1: LOG.debug("Switching {0}-{1} to on".format(address, unit)) else: LOG.warning( "State {0} is no valid switching value".format(state)) tries = 0 while self.__rs.get_switching_state() == 1 and tries < 100: # wait until ready to switch time.sleep(0.01) tries += 1 if tries == 100: LOG.warning( "Sender was not ready to switch again after 100 tries") return self.__rs.switch_socket_b(address, unit, state) self.__mqtt.publish(address, unit, state) except Error as err: LOG.error(err.description)
def dim_socket(self, uid, address, unit, value): rs = BrickletRemoteSwitch(uid, self.ipcon) rs.dim_socket_b(address, unit, value)
def switch_socket(self, uid, address, unit, state): rs = BrickletRemoteSwitch(uid, self.ipcon) rs.switch_socket_b(address, unit, state)
#!/usr/bin/env python # -*- coding: utf-8 -*- HOST = "localhost" PORT = 4223 UID = "XYZ" # Change XYZ to the UID of your Remote Switch Bricklet from tinkerforge.ip_connection import IPConnection from tinkerforge.bricklet_remote_switch import BrickletRemoteSwitch if __name__ == "__main__": ipcon = IPConnection() # Create IP connection rs = BrickletRemoteSwitch(UID, ipcon) # Create device object ipcon.connect(HOST, PORT) # Connect to brickd # Don't use device before ipcon is connected # Switch on a type A socket with house code 17 and receiver code 1. # House code 17 is 10001 in binary (least-significant bit first) # and means that the DIP switches 1 and 5 are on and 2-4 are off. # Receiver code 1 is 10000 in binary (least-significant bit first) # and means that the DIP switch A is on and B-E are off. rs.switch_socket_a(17, 1, rs.SWITCH_TO_ON) raw_input("Press key to exit\n") # Use input() in Python 3 ipcon.disconnect()