コード例 #1
0
def get_target_from_id(device_id):
    """Return target (If available) based on ID from list of devices"""
    devices = sispm.connect()
    for device in devices:
        if sispm.getid(device) == device_id:
            return device
    return None
コード例 #2
0
ファイル: powerstrip.py プロジェクト: PDA-UR/roomware
 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)
コード例 #3
0
ファイル: sispmctl.py プロジェクト: xypron/pysispm
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)))
コード例 #4
0
ファイル: sispmctl.py プロジェクト: xypron/pysispm
def checkport(dev, p):
    """
	Checks if port p exists on the device.

	@param dev device
	@param p port
	@return port exists
	"""
    pmin = sispm.getminport(dev)
    pmax = sispm.getmaxport(dev)
    if p < pmin or p > pmax:
        print("Device {} only has ports {}..{}".format(sispm.getid(dev), pmin, pmax))
        return False
    return True
コード例 #5
0
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)
コード例 #6
0
                break
    print(line)


# Find Enermax devices.
devices = sispm.connect()

# Were they found?
if len(devices) == 0:
    print('No device found')
    quit()

# Find the device with the correct id
dev = None
for d in devices:
    if sispm.getid(d) == id:
        dev = d
        break

if dev is 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)
コード例 #7
0
ファイル: sispmctl.py プロジェクト: xypron/pysispm
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
コード例 #8
0
ファイル: printenv.py プロジェクト: xypron/pysispm
                break
    print(line)


# Find Enermax devices.
devices = sispm.connect()

# Were they found?
if len(devices) == 0:
    print("No device found")
    quit()

# Find the device with the correct id
dev = None
for d in devices:
    if sispm.getid(d) == id:
        dev = d
        break

if dev is 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
コード例 #9
0
def get_devices():
    """Return list of connected devices"""
    devices = sispm.connect()
    return jsonify({"devices": [sispm.getid(d) for d in devices]})