@@ -66,7 +66,6 @@ def __init__(
66
66
"""Create a protocol object."""
67
67
super ().__init__ (transport = transport )
68
68
self ._terminal_uuid : str = base64 .b64encode (md5 (uuid .uuid4 ().bytes )).decode ()
69
- self ._request_id_generator = SnowflakeId (1 , 1 )
70
69
self ._query_lock = asyncio .Lock ()
71
70
self ._multi_request_batch_size = (
72
71
self ._transport ._config .batch_size or self .DEFAULT_MULTI_REQUEST_BATCH_SIZE
@@ -77,11 +76,11 @@ def get_smart_request(self, method, params=None) -> str:
77
76
"""Get a request message as a string."""
78
77
request = {
79
78
"method" : method ,
80
- "params" : params ,
81
- "requestID" : self ._request_id_generator .generate_id (),
82
79
"request_time_milis" : round (time .time () * 1000 ),
83
80
"terminal_uuid" : self ._terminal_uuid ,
84
81
}
82
+ if params :
83
+ request ["params" ] = params
85
84
return json_dumps (request )
86
85
87
86
async def query (self , request : str | dict , retry_count : int = 3 ) -> dict :
@@ -157,8 +156,10 @@ async def _execute_multiple_query(self, requests: dict, retry_count: int) -> dic
157
156
debug_enabled = _LOGGER .isEnabledFor (logging .DEBUG )
158
157
multi_result : dict [str , Any ] = {}
159
158
smart_method = "multipleRequest"
159
+
160
160
multi_requests = [
161
- {"method" : method , "params" : params } for method , params in requests .items ()
161
+ {"method" : method , "params" : params } if params else {"method" : method }
162
+ for method , params in requests .items ()
162
163
]
163
164
164
165
end = len (multi_requests )
@@ -168,7 +169,7 @@ async def _execute_multiple_query(self, requests: dict, retry_count: int) -> dic
168
169
# If step is 1 do not send request batches
169
170
for request in multi_requests :
170
171
method = request ["method" ]
171
- req = self .get_smart_request (method , request [ "params" ] )
172
+ req = self .get_smart_request (method , request . get ( "params" ) )
172
173
resp = await self ._transport .send (req )
173
174
self ._handle_response_error_code (resp , method , raise_on_error = False )
174
175
multi_result [method ] = resp ["result" ]
@@ -347,86 +348,6 @@ async def close(self) -> None:
347
348
await self ._transport .close ()
348
349
349
350
350
- class SnowflakeId :
351
- """Class for generating snowflake ids."""
352
-
353
- EPOCH = 1420041600000 # Custom epoch (in milliseconds)
354
- WORKER_ID_BITS = 5
355
- DATA_CENTER_ID_BITS = 5
356
- SEQUENCE_BITS = 12
357
-
358
- MAX_WORKER_ID = (1 << WORKER_ID_BITS ) - 1
359
- MAX_DATA_CENTER_ID = (1 << DATA_CENTER_ID_BITS ) - 1
360
-
361
- SEQUENCE_MASK = (1 << SEQUENCE_BITS ) - 1
362
-
363
- def __init__ (self , worker_id , data_center_id ):
364
- if worker_id > SnowflakeId .MAX_WORKER_ID or worker_id < 0 :
365
- raise ValueError (
366
- "Worker ID can't be greater than "
367
- + str (SnowflakeId .MAX_WORKER_ID )
368
- + " or less than 0"
369
- )
370
- if data_center_id > SnowflakeId .MAX_DATA_CENTER_ID or data_center_id < 0 :
371
- raise ValueError (
372
- "Data center ID can't be greater than "
373
- + str (SnowflakeId .MAX_DATA_CENTER_ID )
374
- + " or less than 0"
375
- )
376
-
377
- self .worker_id = worker_id
378
- self .data_center_id = data_center_id
379
- self .sequence = 0
380
- self .last_timestamp = - 1
381
-
382
- def generate_id (self ):
383
- """Generate a snowflake id."""
384
- timestamp = self ._current_millis ()
385
-
386
- if timestamp < self .last_timestamp :
387
- raise ValueError ("Clock moved backwards. Refusing to generate ID." )
388
-
389
- if timestamp == self .last_timestamp :
390
- # Within the same millisecond, increment the sequence number
391
- self .sequence = (self .sequence + 1 ) & SnowflakeId .SEQUENCE_MASK
392
- if self .sequence == 0 :
393
- # Sequence exceeds its bit range, wait until the next millisecond
394
- timestamp = self ._wait_next_millis (self .last_timestamp )
395
- else :
396
- # New millisecond, reset the sequence number
397
- self .sequence = 0
398
-
399
- # Update the last timestamp
400
- self .last_timestamp = timestamp
401
-
402
- # Generate and return the final ID
403
- return (
404
- (
405
- (timestamp - SnowflakeId .EPOCH )
406
- << (
407
- SnowflakeId .WORKER_ID_BITS
408
- + SnowflakeId .SEQUENCE_BITS
409
- + SnowflakeId .DATA_CENTER_ID_BITS
410
- )
411
- )
412
- | (
413
- self .data_center_id
414
- << (SnowflakeId .SEQUENCE_BITS + SnowflakeId .WORKER_ID_BITS )
415
- )
416
- | (self .worker_id << SnowflakeId .SEQUENCE_BITS )
417
- | self .sequence
418
- )
419
-
420
- def _current_millis (self ):
421
- return round (time .monotonic () * 1000 )
422
-
423
- def _wait_next_millis (self , last_timestamp ):
424
- timestamp = self ._current_millis ()
425
- while timestamp <= last_timestamp :
426
- timestamp = self ._current_millis ()
427
- return timestamp
428
-
429
-
430
351
class _ChildProtocolWrapper (SmartProtocol ):
431
352
"""Protocol wrapper for controlling child devices.
432
353
@@ -456,6 +377,8 @@ def _get_method_and_params_for_request(self, request):
456
377
smart_method = "multipleRequest"
457
378
requests = [
458
379
{"method" : method , "params" : params }
380
+ if params
381
+ else {"method" : method }
459
382
for method , params in request .items ()
460
383
]
461
384
smart_params = {"requests" : requests }
0 commit comments