def __init__(self, address=("192.168.2.22", 50000), logger=None): self.logger = logger or logging.getLogger(__name__) self.logger.info("Initializing light control server") super(LightServer, self).__init__(name="LightServer") self.msg_queue = Queue.Queue() # System modes self.standard_mode = 0 self.experiment_mode = 1 self.demo_mode = 2 # API Setup api.set_network('192.168.2') # Select the base network address self.logger.info("Performing API IP address discovery") self.ip_list = api.discover() self.logger.info("Discovered connections: %s" % str(self.ip_list)) if not self.ip_list: self.logger.warning("No luminaires detected") # Scan through fixtures to classify each self.experiment_fixtures = [] self.switch_fixtures = [] self.task_fixtures = [] for ip in self.ip_list: name = api.get_custom_device_name(ip) if name.lower().startswith("experiment"): self.experiment_fixtures.append(ip) elif name.lower().startswith("switch"): self.switch_fixtures.append(ip) elif name.lower().startswith("task"): self.task_fixtures.append(ip) else: self.logger.warning( "Unrecognized fixture name: %s, " "fixture is assumed to be an experiment light" % name) self.experiment_fixtures.append(ip) if len(self.experiment_fixtures) == 0: self.logger.warning("No experiment fixtures found") else: self.logger.info("Experiment fixture ip's found: %s" % (self.experiment_fixtures)) if len(self.switch_fixtures) == 0: self.logger.warning("No switch-controlled fixtures found") else: self.logger.info("Switch fixture ip's found: %s" % (self.switch_fixtures)) if len(self.task_fixtures) == 0: self.logger.warning("No task fixtures found") else: self.logger.info("Task fixture ip's found: %s" % (self.task_fixtures)) # Create socket thread to receive packets self.packet_server = PacketServer(self.msg_queue, address) self.logger.info("Light control server initialized")
def main(): print("Running Light Controll Server") # Discovery is mandatory. It will return a list of all the IP addresses # associated with luminaires connected to your network Raritanset(1, 1) Raritanset(2, 1) Raritanset(3, 1) Raritanset(4, 1) Raritanset(5, 1) Raritanset(6, 1) Raritanset(7, 1) Raritanset(8, 1) time.sleep(10) print("Starting api ip discovery") ip_list = api.discover() print("API discovery finished") ip_list = ['192.168.1.46', '192.168.1.47', '192.168.1.48', '192.168.1.49'] while 1: s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) try: s.bind((TCP_IP, TCP_SERVER_PORT)) except socket.error as msg: print("Bind failed. Error conde: " + str(msg[0]) + " Message: " + msg[1]) sys.exit() print("Socket bind complete") s.listen(1) while 1: print("Waiting for client") (client, address) = s.accept() print("Accepted client") while 1: # If the client breake connection, star accepting more clients try: data = client.recv(BUFFER_SIZE) # if the client terminates the connection, goback and wait for another client except: break if not data: break dataS = data.strip().split() print(dataS) if dataS[0] == 'SetRawAll': SendToLight = ConvertRaw(data) print SendToLight if SendToLight > 0: print api.sendMessageParallel(ip_list, SendToLight, tries=5, timeout=1.0) client.send('0\n') else: client.send('%s\n' % str(SendToLight)) if dataS[0] == 'SetRawIp': SendToLight = ConvertRawIp(data) print SendToLight if SendToLight > 0: Fix_number = int(SendToLight[0]) if Fix_number < len(ip_list): del SendToLight[0] print ip_list[Fix_number] api.set_all_drive_levels(ip_list[Fix_number], map(float, SendToLight)) client.send('0\n') client.send('testas\n') else: client.send('%s\n' % str(SendToLight))
import api import time api.set_network('192.168.2') ip_list = api.discover() print("API Discovery found the following fixture addresses: %s" % str(ip_list)) print ip_list #api.sendMessageParallel(ip_list, "PS00000000000000000000000000000000") for ip in ip_list: print("Turning on luminaire at IP %s" % ip) api.sendMessage(ip, "PS0FF000FF00FF00FF00FF00FF00FF00FF") print("S/N: %s" % api.get_sn(ip).rstrip("\n")) old_name = api.get_custom_device_name(ip).rstrip("\n") print("Current Name: %s" % old_name) option_selected = False select = raw_input("Would you like to rename this luminaire? (yes/no):") while not option_selected: if select.lower().startswith('y'): new_name = raw_input("Enter new luminaire name (experiment/switch/task):") api.set_custom_device_name(ip, new_name) time.sleep(.5) print("Luminaire name changed to %s" % new_name) option_selected = True elif select.lower().startswith('n'): option_selected = True else: select = raw_input("Invalid input. Please select an option (yes/no):") print("Turning off luminaire\n") api.sendMessage(ip, "PS00000000000000000000000000000000")
def initialize_lights(self): # Redirect error printing, api.discover() will otherwise print many warnings/errors that do not affect the program sys.stderr = open(os.devnull, "w") ip_list = api.discover() sys.stderr = sys.__stderr__ return ip_list