1#!/usr/bin/env python
2#
3#   Copyright 2016 - The Android Open Source Project
4#
5#   Licensed under the Apache License, Version 2.0 (the "License");
6#   you may not use this file except in compliance with the License.
7#   You may obtain a copy of the License at
8#
9#       http://www.apache.org/licenses/LICENSE-2.0
10#
11#   Unless required by applicable law or agreed to in writing, software
12#   distributed under the License is distributed on an "AS IS" BASIS,
13#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14#   See the License for the specific language governing permissions and
15#   limitations under the License.
16from acts import base_test
17from acts.controllers.relay_lib.relay import SynchronizeRelays
18
19
20class RelayDeviceSampleTest(base_test.BaseTestClass):
21    """ Demonstrates example usage of a configurable access point."""
22
23    def setup_class(self):
24        # Take devices from relay_devices.
25        self.relay_device = self.relay_devices[0]
26
27        # You can use this workaround to get devices by name:
28
29        relay_rig = self.relay_devices[0].rig
30        self.other_relay_device = relay_rig.devices['UniqueDeviceName']
31        # Note: If the "devices" key from the config is missing
32        # a GenericRelayDevice that contains every switch in the config
33        # will be stored in relay_devices[0]. Its name will be
34        # "GenericRelayDevice".
35
36    def setup_test(self):
37        # setup() will set the relay device to the default state.
38        # Unless overridden, the default state is all switches set to off.
39        self.relay_device.setup()
40
41    def teardown_test(self):
42        # clean_up() will set the relay device back to a default state.
43        # Unless overridden, the default state is all switches set to off.
44        self.relay_device.clean_up()
45
46    # Typical use of a GenericRelayDevice looks like this:
47    def test_relay_device(self):
48
49        # This function call will sleep until .25 seconds are up.
50        # Blocking_nc_for will emulate a button press, which turns on the relay
51        # (or stays on if it already was on) for the given time, and then turns
52        # off.
53        self.relay_device.relays['BT_Power_Button'].set_nc_for(.25)
54
55        # do_something_after_turning_on_bt_power()
56
57        # Note that the relays are mechanical switches, and do take real time
58        # to go from one state to the next.
59
60        self.relay_device.relays['BT_Pair'].set_nc()
61
62        # do_something_while_holding_down_the_pair_button()
63
64        self.relay_device.relays['BT_Pair'].set_no()
65
66        # do_something_after_releasing_bt_pair()
67
68        # Note that although cleanup sets the relays to the 'NO' state after
69        # each test, they do not press things like the power button to turn
70        # off whatever hardware is attached. When using a GenericRelayDevice,
71        # you'll have to do this manually.
72        # Other RelayDevices may handle this for you in their clean_up() call.
73        self.relay_device.relays['BT_Power_Button'].set_nc_for(.25)
74
75    def test_toggling(self):
76        # This test just spams the toggle on each relay.
77        for _ in range(0, 2):
78            self.relay_device.relays['BT_Power_Button'].toggle()
79            self.relay_device.relays['BT_Pair'].toggle()
80            self.relay_device.relays['BT_Reset'].toggle()
81            self.relay_device.relays['BT_SomethingElse'].toggle()
82
83    def test_synchronize_relays(self):
84        """Toggles relays using SynchronizeRelays().
85
86        This makes each relay do it's action at the same time, without waiting
87        after each relay to swap. Instead, all relays swap at the same time, and
88        the wait is done after exiting the with statement.
89        """
90        for _ in range(0, 10):
91            with SynchronizeRelays():
92                self.relay_device.relays['BT_Power_Button'].toggle()
93                self.relay_device.relays['BT_Pair'].toggle()
94                self.relay_device.relays['BT_Reset'].toggle()
95                self.relay_device.relays['BT_SomethingElse'].toggle()
96
97        # For more fine control over the wait time of relays, you can set
98        # Relay.transition_wait_time. This is not recommended unless you are
99        # using solid state relays, or async calls.
100