1
1
from __future__ import annotations
2
2
3
3
import asyncio
4
+ import contextlib
4
5
import functools
5
6
import json
6
7
import logging
@@ -126,9 +127,9 @@ def validate_message(
126
127
port : int ,
127
128
res : dict [str , Any ],
128
129
extrakey : str | None = None ,
129
- minfields : None | int = None ,
130
- maxfields : None | int = None ,
131
- ) -> dict [ str , Any ] :
130
+ minfields : None | int = 1 ,
131
+ maxfields : None | int = 1 ,
132
+ ) -> Any :
132
133
# all miner message comes with a STATUS
133
134
for key in ["STATUS" , "id" , * ([extrakey ] if extrakey else [])]:
134
135
if key in res :
@@ -142,17 +143,17 @@ def validate_message(
142
143
143
144
n = len (res [extrakey ])
144
145
msg = None
145
- if minfields and (n < minfields ):
146
+ if ( minfields is not None ) and (n < minfields ):
146
147
msg = f"found { n } fields for { extrakey } invalid: " f"{ n } <= { minfields } "
147
- elif maxfields and (n > maxfields ):
148
+ elif ( maxfields is not None ) and (n > maxfields ):
148
149
msg = f"found { n } fields for { extrakey } invalid: " f"{ n } >= { maxfields } "
149
150
if msg is None :
150
151
return res [extrakey ]
151
152
raise exceptions .MinerCommandMalformedMessageError (host , port , msg , res )
152
153
153
154
154
155
@wrapped
155
- async def logon (host : str , port : int , timeout : float | None = 3 ) -> str :
156
+ async def logon (host : str , port : int , timeout : float | None = None ) -> str :
156
157
timeout = TIMEOUT if timeout is None else timeout
157
158
res = await roundtrip (host , port , {"command" : "logon" }, timeout = timeout )
158
159
@@ -166,7 +167,7 @@ async def logon(host: str, port: int, timeout: float | None = 3) -> str:
166
167
)
167
168
sessions = validate_message (host , port , res , "SESSION" , 1 , 1 )
168
169
169
- session = sessions [0 ] # type: ignore
170
+ session = sessions [0 ]
170
171
171
172
if "SessionID" not in session :
172
173
raise exceptions .MinerCommandSessionAlreadyActive (
@@ -177,56 +178,15 @@ async def logon(host: str, port: int, timeout: float | None = 3) -> str:
177
178
178
179
@wrapped
179
180
async def logoff (
180
- host : str , port : int , sid : str , timeout : float | None = 3
181
+ host : str , port : int , sid : str , timeout : float | None = None
181
182
) -> dict [str , Any ]:
182
183
timeout = TIMEOUT if timeout is None else timeout
183
184
return await roundtrip (
184
185
host , port , {"command" : "logoff" , "parameter" : sid }, timeout = timeout
185
186
)
186
187
187
188
188
- @wrapped
189
- async def execute_command (
190
- host : str ,
191
- port : int ,
192
- timeout_sec : float | None ,
193
- cmd : str ,
194
- parameters : list [str ] | None = None ,
195
- verbose : bool = False ,
196
- asjson : bool | None = True ,
197
- add_address : bool = False ,
198
- ) -> tuple [tuple [str , int ], dict [str , Any ]] | dict [str , Any ]:
199
- timeout = TIMEOUT if timeout_sec is None else timeout_sec
200
- parameters = parameters or []
201
-
202
- sid = None
203
- if api .logon_required (cmd ):
204
- sid = await logon (host , port )
205
- parameters = [sid , * parameters ]
206
- log .debug ("session id requested & obtained for %s:%i (%s)" , host , port , sid )
207
- else :
208
- log .debug ("no logon required for command '%s' on %s:%i" , cmd , host , port )
209
-
210
- try :
211
- packet = {"command" : cmd }
212
- if parameters :
213
- packet ["parameter" ] = "," .join (parameters )
214
- log .debug (
215
- "executing command '%s' on '%s:%i' with parameters: %s" ,
216
- cmd ,
217
- host ,
218
- port ,
219
- packet .get ("parameter" , "" ),
220
- )
221
- ret = await roundtrip (host , port , packet , timeout = timeout , asjson = asjson )
222
- log .debug ("received from %s:%s: %s" , host , port , str (ret ))
223
- return ((host , port ), ret ) if add_address else ret
224
- finally :
225
- if sid :
226
- await logoff (host , port , sid )
227
-
228
-
229
- def _rexec_parameters (
189
+ def parameters_to_list (
230
190
parameters : str | list [Any ] | dict [str , Any ] | None = None ,
231
191
) -> list [str ]:
232
192
if isinstance (parameters , dict ):
@@ -256,9 +216,7 @@ async def rexec(
256
216
retry : int | None = None ,
257
217
retry_delay : float | None = None ,
258
218
) -> dict [str , Any ] | None :
259
- from . import api
260
-
261
- parameters = _rexec_parameters (parameters )
219
+ parameters = parameters_to_list (parameters )
262
220
263
221
timeout = TIMEOUT if timeout is None else timeout
264
222
retry = RETRIES if retry is None else retry
@@ -328,3 +286,14 @@ async def rexec(
328
286
await logoff (host , port , sid )
329
287
if isinstance (failure , Exception ):
330
288
raise failure
289
+
290
+
291
+ @contextlib .asynccontextmanager
292
+ async def with_atm (host , port , enabled : bool , timeout : float | None = None ):
293
+ res = await rexec (host , port , "atm" , timeout = timeout )
294
+ if not res :
295
+ raise exceptions .MinerConnectionError (host , port , "cannot check atm" )
296
+ current = validate_message (host , port , res , "ATM" )[0 ]["Enabled" ]
297
+ await rexec (host , port , "atmset" , {"enabled" : enabled }, timeout = timeout )
298
+ yield current
299
+ await rexec (host , port , "atmset" , {"enabled" : current }, timeout = timeout )
0 commit comments