Browse Source

allow adapter selection

Thomas Buck 1 year ago
parent
commit
11ff08e82e
3 changed files with 32 additions and 22 deletions
  1. 9
    5
      python-test/flow.py
  2. 12
    8
      python-test/poll.py
  3. 11
    9
      python-test/scan.py

+ 9
- 5
python-test/flow.py View File

78
     set_state(client, (False, False)) # turn off heater and pump
78
     set_state(client, (False, False)) # turn off heater and pump
79
 
79
 
80
 if __name__ == "__main__":
80
 if __name__ == "__main__":
81
-    def main(address):
82
-        client = ble_conn(address)
81
+    def main(address, adapter):
82
+        client = ble_conn(address, adapter)
83
 
83
 
84
         try:
84
         try:
85
             if get_unit_is_fahrenheit(client):
85
             if get_unit_is_fahrenheit(client):
99
         print("Disconnecting")
99
         print("Disconnecting")
100
         client.disconnect()
100
         client.disconnect()
101
 
101
 
102
-    arg = None
102
+    adapter = None
103
+    mac = None
103
     if len(sys.argv) > 1:
104
     if len(sys.argv) > 1:
104
-        arg = sys.argv[1]
105
-    main(arg)
105
+        adapter = int(sys.argv[1])
106
+    if len(sys.argv) > 2:
107
+        mac = sys.argv[2]
108
+
109
+    main(mac, adapter)

+ 12
- 8
python-test/poll.py View File

7
 serviceUuidVolcano3 = "10100000-5354-4f52-5a26-4249434b454c"
7
 serviceUuidVolcano3 = "10100000-5354-4f52-5a26-4249434b454c"
8
 serviceUuidVolcano4 = "10110000-5354-4f52-5a26-4249434b454c"
8
 serviceUuidVolcano4 = "10110000-5354-4f52-5a26-4249434b454c"
9
 
9
 
10
-def ble_conn(address):
11
-    dev = ble_scan(address)
10
+def ble_conn(address, adapter):
11
+    dev = ble_scan(address, adapter)
12
 
12
 
13
     if dev != None:
13
     if dev != None:
14
-        print("Connecting to {}...".format(address))
14
+        address = dev.address()
15
+        print("Connecting to '{}'...".format(address))
15
         dev.connect()
16
         dev.connect()
16
 
17
 
17
     return dev
18
     return dev
78
         else:
79
         else:
79
             print("Pump is Off")
80
             print("Pump is Off")
80
 
81
 
81
-    def test(address):
82
-        device = ble_conn(address)
82
+    def test(address, adapter):
83
+        device = ble_conn(address, adapter)
83
         if device == None:
84
         if device == None:
84
             return
85
             return
85
 
86
 
103
 
104
 
104
     import sys
105
     import sys
105
 
106
 
106
-    arg = None
107
+    adapter = None
108
+    mac = None
107
     if len(sys.argv) > 1:
109
     if len(sys.argv) > 1:
108
-        arg = sys.argv[1]
110
+        adapter = int(sys.argv[1])
111
+    if len(sys.argv) > 2:
112
+        mac = sys.argv[2]
109
 
113
 
110
-    test(arg)
114
+    test(mac, adapter)

+ 11
- 9
python-test/scan.py View File

1
 import simplepyble
1
 import simplepyble
2
 
2
 
3
-def ble_scan(addr):
3
+def ble_scan(addr = None, adapterIndex = 0, timeout = 1):
4
     adapters = simplepyble.Adapter.get_adapters()
4
     adapters = simplepyble.Adapter.get_adapters()
5
 
5
 
6
     if len(adapters) == 0:
6
     if len(adapters) == 0:
7
         print("No adapters found")
7
         print("No adapters found")
8
         return None
8
         return None
9
 
9
 
10
-    # TODO allow selection of bluetooth adapter
11
-    adapter = adapters[0]
10
+    adapter = adapters[adapterIndex]
12
     print("Selected adapter: {} [{}]".format(adapter.identifier(), adapter.address()))
11
     print("Selected adapter: {} [{}]".format(adapter.identifier(), adapter.address()))
13
 
12
 
14
-    # TODO support longer scans?
15
-    print("Scanning for '{}' for 1s...".format(addr))
16
-    adapter.scan_for(1000)
13
+    # TODO abort scan when found?
14
+    print("Scanning for '{}' for {}s...".format(addr, timeout))
15
+    adapter.scan_for(timeout * 1000)
17
 
16
 
18
     peripherals = adapter.scan_get_results()
17
     peripherals = adapter.scan_get_results()
19
     for peripheral in peripherals:
18
     for peripheral in peripherals:
30
 if __name__ == "__main__":
29
 if __name__ == "__main__":
31
     import sys
30
     import sys
32
 
31
 
33
-    arg = None
32
+    adapter = None
33
+    mac = None
34
     if len(sys.argv) > 1:
34
     if len(sys.argv) > 1:
35
-        arg = sys.argv[1]
35
+        adapter = int(sys.argv[1])
36
+    if len(sys.argv) > 2:
37
+        mac = sys.argv[2]
36
 
38
 
37
-    dev = ble_scan(arg)
39
+    dev = ble_scan(mac, adapter)
38
     if dev != None:
40
     if dev != None:
39
         print("{} {}".format(dev.identifier(), dev.address()))
41
         print("{} {}".format(dev.identifier(), dev.address()))

Loading…
Cancel
Save