def get_device_status(device_id=None, port=None): """Return dictionary of all device ports and status of each""" if device_id is not None: if len(device_id) == 1: target = get_target_from_index(device_id) if target is None: return jsonify( {"error": f"Index {device_id} not found in devices list"}) else: target = get_target_from_id(device_id) if target is None: return jsonify( {"error": f"Device with ID {device_id} not found"}) if port is None: retval = {"status": {}} for socket in range(sispm.getminport(target), sispm.getmaxport(target) + 1): retval["status"][socket] = sispm.getstatus(target, socket) elif validate_port(target, port): retval = {"status": sispm.getstatus(target, port)} else: return jsonify({ "error": "{} is out of range for device ({} - {})".format( port, sispm.getminport(target), sispm.getmaxport(target)) }) else: retval = {"status": {}} for device in sispm.connect(): device_id = sispm.getid(device) retval["status"][device_id] = {} for socket in range(sispm.getminport(device), sispm.getmaxport(device) + 1): retval["status"][device_id][socket] = sispm.getstatus( device, socket) return jsonify(retval)
def status_all(self): status = [] try: if sispm.getid(self.powerstrips_rooms[str( self.device_room)]) == self.device_id: for i in range(self.powerstrip_length): status.append( sispm.getstatus( self.powerstrips_rooms[str(self.device_room)], i)) return status except Exception as e: print('Powerstrip().status_all()', e) return None
def status(devices): """ Outputs the status of all devices. @param devices list of devices """ for dev in devices: print("device {}".format(devices.index(dev)), end=", ") # Print device id. print(sispm.getid(dev)) # Print status of all outlets. for i in range(sispm.getminport(dev), 1 + sispm.getmaxport(dev)): print("\tstatus[{}] = {}".format(i, sispm.getstatus(dev, i)))
def status(self, socket): status = sispm.getstatus(self.powerstrips_rooms[str(self.device_room)], socket) return status
def main(): # Find our devices. devices = sispm.connect() # Were they found? if len(devices) == 0: print("No device found") sys.exit(1) # If there is only one device, use it as default. if len(devices) == 1: dev = devices[0] else: dev = None # Define command line options. try: opts, args = getopt.getopt(sys.argv[1:], "D:d:f:ho:t:") except getopt.GetoptError as err: print(str(err)) usage() sys.exit(2) # Handle command line. for o, a in opts: if o == "-D": dev = None for d in devices: if sispm.getid(d) == a: dev = d break if dev == None: print("device with id {} not found".format(a)) break elif o == "-d": d = int(a) if d < 0 or d >= len(devices): print("unknown device {}".format(d)) break dev = devices[d] elif o == "-f": p = int(a) if not checkport(dev, p): break sispm.switchoff(dev, p) elif o == "-h": usage() print() elif o == "-o": p = int(a) if not checkport(dev, p): break sispm.switchon(dev, p) elif o == "-t": p = int(a) if not checkport(dev, p): break if sispm.getstatus(dev, p) == 0: sispm.switchon(dev, p) else: sispm.switchoff(dev, p) else: break # Always output the device status. status(devices) # Workaround for bug in old version of usb library. devices = None