9
9
import contextlib
10
10
import sys
11
11
import time
12
+ import json
13
+ import logging
12
14
13
- from booster import BoosterApi , TelemetryReader
15
+ from booster import Booster
16
+ import miniconf
14
17
15
18
# The default bias current to tune to.
16
19
DEFAULT_BIAS_CURRENT = 0.05
@@ -33,66 +36,65 @@ async def channel_on(booster, channel, initial_state='Enabled'):
33
36
""" Context manager to ensure a channel is disabled upon exit.
34
37
35
38
Args:
36
- booster: The BoosterApi objct.
39
+ booster: The Booster connection
37
40
channel: The channel to configure.
38
41
initial_state: The state to configure the channel into.
39
42
"""
40
43
try :
41
44
print (f'Commanding channel { channel } into { initial_state } ' )
42
- await booster .settings_interface .set (f'/channel/{ channel } /state' , initial_state )
45
+ await booster .miniconf .set (f'/channel/{ channel } /state' , initial_state )
43
46
yield
44
47
finally :
45
48
print (f'Commanding channel { channel } off' )
46
- await booster .settings_interface .set (f'/channel/{ channel } /state' , 'Off' )
49
+ await booster .miniconf .set (f'/channel/{ channel } /state' , 'Off' )
47
50
48
51
49
- async def test_channel (booster , channel , prefix , broker ):
52
+ async def test_channel (booster , channel , tele_queue ):
50
53
""" Basic testing of a single RF channel.
51
54
52
55
Args:
53
- booster: The BoosterApi .
56
+ booster: The Booster connection .
54
57
channel: The channel index to test.
55
- prefix: Booster's miniconf prefix.
56
- broker: The broker IP address.
58
+ tele: The received telemetry
57
59
"""
58
60
print (f'-> Conducting self-test on channel { channel } ' )
59
61
60
- # Start receiving telemetry for the channel under test.
61
- telemetry = await TelemetryReader .create (prefix , broker , channel )
62
-
63
62
# Tune the bias current on the channel
64
63
async with channel_on (booster , channel , 'Powered' ):
65
64
vgs , ids = await booster .tune_bias (channel , DEFAULT_BIAS_CURRENT )
66
65
print (f'Channel { channel } bias tuning: Vgs = { vgs } , Ids = { ids } ' )
67
66
68
67
# Disable the channel.
69
- await booster .settings_interface .set (f'/channel/{ channel } /state' , 'Off' )
68
+ await booster .miniconf .set (f'/channel/{ channel } /state' , 'Off' )
70
69
71
70
# Check that telemetry indicates channel is powered off.
72
71
async def is_off () -> bool :
73
- _ , tlm = await telemetry .get_next_telemetry ()
72
+ msg = await tele_queue .__anext__ ()
73
+ tlm = json .loads (msg .payload )
74
74
return tlm ['state' ] == 'Off'
75
75
76
76
await periodic_check (is_off , timeout = 5 )
77
77
78
78
# Set the interlock threshold so that it won't trip.
79
79
print ('Setting output interlock threshold to 30 dB' )
80
- await booster .settings_interface .set (f'/channel/{ channel } /output_interlock_threshold' , 30 )
80
+ await booster .miniconf .set (f'/channel/{ channel } /output_interlock_threshold' , 30 )
81
81
82
82
# Enable the channel, verify telemetry indicates it is now enabled.
83
83
async with channel_on (booster , channel ):
84
84
async def is_enabled () -> bool :
85
- _ , tlm = await telemetry .get_next_telemetry ()
85
+ msg = await tele_queue .__anext__ ()
86
+ tlm = json .loads (msg .payload )
86
87
return tlm ['state' ] == 'Enabled'
87
88
88
89
await periodic_check (is_enabled , timeout = 5 )
89
90
90
91
# Lower the interlock threshold so it trips.
91
92
print ('Setting output interlock threshold to -5 dB, verifying interlock trips' )
92
- await booster .settings_interface .set (f'/channel/{ channel } /output_interlock_threshold' , - 5.7 )
93
+ await booster .miniconf .set (f'/channel/{ channel } /output_interlock_threshold' , - 5.7 )
93
94
94
95
async def is_tripped () -> bool :
95
- _ , tlm = await telemetry .get_next_telemetry ()
96
+ msg = await tele_queue .__anext__ ()
97
+ tlm = json .loads (msg .payload )
96
98
return tlm ['state' ] == 'Tripped(Output)'
97
99
98
100
# Verify the channel is now tripped.
@@ -104,8 +106,11 @@ async def is_tripped() -> bool:
104
106
105
107
def main ():
106
108
""" Main program entry point. """
107
- parser = argparse .ArgumentParser (description = 'Loopback tests for Stabilizer HITL testing' ,)
108
- parser .add_argument ('--prefix' , type = str , help = 'The MQTT prefix of the target' )
109
+ parser = argparse .ArgumentParser (description = 'Loopback tests for Booster HITL testing' )
110
+ parser .add_argument ('--prefix' , default = 'dt/sinara/booster/+' , type = str ,
111
+ help = 'The prefix of the booster to test' )
112
+ parser .add_argument ("--no-discover" , "-d" , action = "store_true" ,
113
+ help = "Do not discover device prefix." )
109
114
parser .add_argument ('--broker' , '-b' , default = 'mqtt' , type = str ,
110
115
help = 'The MQTT broker address' )
111
116
parser .add_argument ('--channels' , '-c' , nargs = '+' , help = 'Channels indices to test' ,
@@ -115,18 +120,43 @@ def main():
115
120
116
121
async def test ():
117
122
""" The actual testing being completed. """
118
- booster = await BoosterApi .create (args .prefix , args .broker )
119
-
120
- # Disable configure the telemetry rate.
121
- await booster .settings_interface .set ('/telemetry_period' , 1 , retain = False )
122
-
123
- # Test operation of an RF channel
124
- for channel in args .channels :
125
- await test_channel (booster , channel , booster .prefix , args .broker )
123
+ async with miniconf .Client (
124
+ args .broker ,
125
+ protocol = miniconf .MQTTv5 ,
126
+ logger = logging .getLogger ("aiomqtt-client" ),
127
+ queue_type = asyncio .LifoQueue ,
128
+ max_queued_incoming_messages = 1 ,
129
+ ) as client :
130
+ if not args .no_discover :
131
+ prefix , _alive = await miniconf .discover_one (client , args .prefix )
132
+ else :
133
+ prefix = args .prefix
134
+
135
+ booster = Booster (client , prefix )
136
+
137
+ # Disable configure the telemetry rate.
138
+ await booster .miniconf .set ('/telemetry_period' , 1 )
139
+
140
+ # Test operation of an RF channel
141
+ async with miniconf .Client (
142
+ args .broker , protocol = miniconf .MQTTv5 ,
143
+ logger = logging .getLogger ("aiomqtt-client" )
144
+ ) as tele :
145
+ for channel in args .channels :
146
+ await tele .subscribe (f"{ prefix } /telemetry/ch{ channel } " )
147
+ await test_channel (booster , channel , tele .messages )
148
+ await tele .unsubscribe (f"{ prefix } /telemetry/ch{ channel } " )
126
149
127
150
loop = asyncio .get_event_loop ()
128
151
sys .exit (loop .run_until_complete (test ()))
129
152
130
153
131
154
if __name__ == '__main__' :
155
+ import os
156
+ import sys
157
+ if sys .platform .lower () == "win32" or os .name .lower () == "nt" :
158
+ from asyncio import set_event_loop_policy , WindowsSelectorEventLoopPolicy
159
+
160
+ set_event_loop_policy (WindowsSelectorEventLoopPolicy ())
161
+
132
162
main ()
0 commit comments