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,8 +78,8 @@ def flow(client):
78 78
     set_state(client, (False, False)) # turn off heater and pump
79 79
 
80 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 84
         try:
85 85
             if get_unit_is_fahrenheit(client):
@@ -99,7 +99,11 @@ if __name__ == "__main__":
99 99
         print("Disconnecting")
100 100
         client.disconnect()
101 101
 
102
-    arg = None
102
+    adapter = None
103
+    mac = None
103 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,11 +7,12 @@ import time
7 7
 serviceUuidVolcano3 = "10100000-5354-4f52-5a26-4249434b454c"
8 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 13
     if dev != None:
14
-        print("Connecting to {}...".format(address))
14
+        address = dev.address()
15
+        print("Connecting to '{}'...".format(address))
15 16
         dev.connect()
16 17
 
17 18
     return dev
@@ -78,8 +79,8 @@ if __name__ == "__main__":
78 79
         else:
79 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 84
         if device == None:
84 85
             return
85 86
 
@@ -103,8 +104,11 @@ if __name__ == "__main__":
103 104
 
104 105
     import sys
105 106
 
106
-    arg = None
107
+    adapter = None
108
+    mac = None
107 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,19 +1,18 @@
1 1
 import simplepyble
2 2
 
3
-def ble_scan(addr):
3
+def ble_scan(addr = None, adapterIndex = 0, timeout = 1):
4 4
     adapters = simplepyble.Adapter.get_adapters()
5 5
 
6 6
     if len(adapters) == 0:
7 7
         print("No adapters found")
8 8
         return None
9 9
 
10
-    # TODO allow selection of bluetooth adapter
11
-    adapter = adapters[0]
10
+    adapter = adapters[adapterIndex]
12 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 17
     peripherals = adapter.scan_get_results()
19 18
     for peripheral in peripherals:
@@ -30,10 +29,13 @@ def ble_scan(addr):
30 29
 if __name__ == "__main__":
31 30
     import sys
32 31
 
33
-    arg = None
32
+    adapter = None
33
+    mac = None
34 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 40
     if dev != None:
39 41
         print("{} {}".format(dev.identifier(), dev.address()))

Loading…
Cancel
Save