def cycle_device_port(device_id, port, wait=2): """Cycle a port off then on""" 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 not validate_port(target, port): return jsonify({ "error": "{} is out of range for device ({} - {})".format( port, sispm.getminport(target), sispm.getmaxport(target)) }) sispm.switchoff(target, port) time.sleep(wait) sispm.switchon(target, port) return jsonify("OK")
def switch_on(self, socket): try: if sispm.getid(self.powerstrips_rooms[str( self.device_room)]) == self.device_id: sispm.switchon(self.powerstrips_rooms[str(self.device_room)], socket) except Exception as e: print('Powerstrip().switch_on()', e)
def switch_all_on(self): try: if sispm.getid(self.powerstrips_rooms[str( self.device_room)]) == self.device_id: for i in range(self.powerstrip_length): socket = self.first_socket + i sispm.switchon( self.powerstrips_rooms[str(self.device_room)], socket) except Exception as e: print('Powerstrip().switch_all_on()', e)
port = tty, \ baudrate = 115200, \ stopbits = serial.STOPBITS_ONE, \ bytesize = serial.EIGHTBITS, \ timeout = 5) # Switch device off print("== Switching off ==") sispm.switchoff(dev, port) # Wait for two seconds time.sleep(2) # Switch device on print("== Switching on ==") sispm.switchon(dev, port) # Wait for U-Boot message skip_output(ser, 'stop autoboot:') # Stop boot process ser.write(b'\r\r') skip_output(ser) # Print environment ser.write(b'printenv\n') skip_output(ser) # Switch device off print("== Switching off ==") sispm.switchoff(dev, port)
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
print("Device " + id + " not found") quit() # Open serial connection ser = serial.Serial(port=tty, baudrate=115200, stopbits=serial.STOPBITS_ONE, bytesize=serial.EIGHTBITS, timeout=5) # Switch device off print("== Switching off ==") sispm.switchoff(dev, port) # Wait for two seconds time.sleep(2) # Switch device on print("== Switching on ==") sispm.switchon(dev, port) # Wait for U-Boot message skip_output(ser, "stop autoboot:") # Stop boot process ser.write(b"\r\r") skip_output(ser) # Print environment ser.write(b"printenv\n") skip_output(ser) # Switch device off print("== Switching off ==") sispm.switchoff(dev, port)