Exemplo n.º 1
0
def relay_script():
    # Basic commandline interface for testing the relay module.
    def usage():
        print "Usage: %s [powercycle|status|turnon|turnoff] <hostname> <bank> <relay>" % sys.argv[
            0]
        sys.exit(1)

    if len(sys.argv) != 5:
        usage()
    cmd, hostname, bnk, rly = sys.argv[1:5]
    bnk, rly = int(bnk), int(rly)
    if cmd == 'powercycle':
        if relay.powercycle(hostname, bnk, rly, timeout=60):
            print "OK"
        else:
            print "FAILED"
            sys.exit(1)
    elif cmd == 'status':
        status = relay.get_status(hostname, bnk, rly, timeout=60)
        if status is None:
            print "FAILED"
            sys.exit(1)
        print "bank %d, relay %d status: %s" % (bnk, rly,
                                                'on' if status else 'off')
    elif cmd == 'turnon' or cmd == 'turnoff':
        status = (cmd == 'turnon')
        if relay.set_status(hostname, bnk, rly, status, timeout=60):
            print "OK"
        else:
            print "FAILED"
            sys.exit(1)
    else:
        usage()
    sys.exit(0)
 def poweroff(self, device_name):
     """
     Initiate a power-off operation for DEVICE_NAME.  Returns True on success
     and False on error.
     """
     hostname, bnk, rly = self.db.devices.get_relay_info(device_name)
     return relay.set_status(hostname, bnk, rly, False, 30)
Exemplo n.º 3
0
def relay_script():
    # Basic commandline interface for testing the relay module.
    def usage():
        print "Usage: %s [powercycle|status|turnon|turnoff] <hostname> <bank> <relay>" % sys.argv[0]
        sys.exit(2)
    if len(sys.argv) != 5:
        usage()
    cmd, hostname, bnk, rly = sys.argv[1:5]
    bnk, rly = int(bnk), int(rly)
    if cmd == 'powercycle':
        if relay.powercycle(hostname, bnk, rly, timeout=60):
            print "OK"
        else:
            print "FAILED"
            sys.exit(1)
    elif cmd == 'status':
        status = relay.get_status(hostname, bnk, rly, timeout=60)
        if status is None:
            print "FAILED"
            sys.exit(1)
        print "bank %d, relay %d status: %s" % (bnk, rly, 'on' if status else 'off')
    elif cmd == 'turnon' or cmd == 'turnoff':
        status = (cmd == 'turnon')
        if relay.set_status(hostname, bnk, rly, status, timeout=60):
            print "OK"
        else:
            print "FAILED"
            sys.exit(1)
    else:
        usage()
    sys.exit(0)
Exemplo n.º 4
0
def start_poweroff(device_name, callback, max_time=30):
    """
    Initiate a power-off operation for DEVICE_NAME.  This function returns
    immediately, and will invoke CALLBACK with a boolean success indication
    when the operation is complete.  CALLBACK will be invoked in a different
    thread from that where this function was called.

    Use `start_powercycle` to turn power back on.

    The function guarantees to callback before MAX_TIME seconds have elapsed,
    or not call back at all.
    """
    callback_before = time.time() + max_time

    hostname, bnk, rly = data.device_relay_info(device_name)

    logs.device_logs.add(device_name, "initiating power-off ", 'bmm')
    _run_async(callback_before, callback,
            lambda : relay.set_status(hostname, bnk, rly, False, max_time))
Exemplo n.º 5
0
def start_poweroff(device_name, callback, max_time=30):
    """
    Initiate a power-off operation for DEVICE_NAME.  This function returns
    immediately, and will invoke CALLBACK with a boolean success indication
    when the operation is complete.  CALLBACK will be invoked in a different
    thread from that where this function was called.

    Use `start_powercycle` to turn power back on.

    The function guarantees to callback before MAX_TIME seconds have elapsed,
    or not call back at all.
    """
    callback_before = time.time() + max_time

    hostname, bnk, rly = data.device_relay_info(device_name)

    logs.device_logs.add(device_name, "initiating power-off ", 'bmm')
    _run_async(callback_before, callback,
               lambda: relay.set_status(hostname, bnk, rly, False, max_time))
Exemplo n.º 6
0
 def test_set_status_timeout(self):
     self.relayboard.delay = 0.12
     self.relayboard.skip_final_1s = True
     self.assertEqual(relay.set_status(self.relay_host, 2, 2, True, 0.1),
                      False)
Exemplo n.º 7
0
 def test_set_status_off(self, sleep):
     self.assertEqual(relay.set_status(self.relay_host, 2, 2, False, 10),
                      True)
     self.assertEqual(self.relayboard.actions, [('set', 'panda-off', 2, 2)])
Exemplo n.º 8
0
 def test_set_status_timeout(self):
     self.server.delay = 1
     self.assertEqual(relay.set_status('127.0.0.1', 2, 2, True, 0.1), False)
Exemplo n.º 9
0
 def test_set_status_off(self, sleep):
     self.assertEqual(relay.set_status('127.0.0.1', 2, 2, False, 10), True)
     self.assertEqual(self.server.actions, [('set', 'panda-off', 2, 2)])
Exemplo n.º 10
0
 def test_set_status_timeout(self):
     self.relayboard.delay = 1
     self.assertEqual(relay.set_status(self.relay_host, 2, 2, True, 0.1), False)
Exemplo n.º 11
0
 def test_set_status_off(self, sleep):
     self.assertEqual(relay.set_status(self.relay_host, 2, 2, False, 10), True)
     self.assertEqual(self.relayboard.actions, [('set', 'panda-off', 2, 2)])
Exemplo n.º 12
0
 def test_set_status_timeout(self):
     self.relayboard.delay = 0.12
     self.relayboard.skip_final_1s = True
     self.assertEqual(relay.set_status(self.relay_host, 2, 2, True, 0.1), False)