@@ -516,7 +516,7 @@ def on_attributes_update(self, content):
516
516
def server_side_rpc_handler (self , content ):
517
517
try :
518
518
if content .get ('data' ) is None :
519
- content ['data' ] = {'params' : content ['params' ], 'method' : content ['method' ]}
519
+ content ['data' ] = {'params' : content ['params' ], 'method' : content ['method' ], 'id' : content [ 'id' ] }
520
520
521
521
rpc_method = content ["data" ].get ("method" )
522
522
@@ -528,76 +528,102 @@ def server_side_rpc_handler(self, content):
528
528
content ['device' ] = content ['params' ].split (' ' )[0 ].split ('=' )[- 1 ]
529
529
except (ValueError , IndexError ):
530
530
self .__log .error ('Invalid RPC method name: %s' , rpc_method )
531
+ except AttributeError :
532
+ pass
531
533
532
- # firstly check if a method is not service
533
- if rpc_method == 'set' or rpc_method == 'get' :
534
- full_path = ''
535
- args_list = []
536
- device = content .get ('device' )
534
+ if content .get ('device' ):
535
+ # firstly check if a method is not service
536
+ if rpc_method == 'set' or rpc_method == 'get' :
537
+ full_path = ''
538
+ args_list = []
539
+ device = content .get ('device' )
537
540
538
- try :
539
- args_list = content ['data' ]['params' ].split (';' )
541
+ try :
542
+ args_list = content ['data' ]['params' ].split (';' )
543
+
544
+ if 'ns' in content ['data' ]['params' ]:
545
+ full_path = ';' .join (
546
+ [item for item in (args_list [0 :- 1 ] if rpc_method == 'set' else args_list )])
547
+ else :
548
+ full_path = args_list [0 ].split ('=' )[- 1 ]
549
+ except IndexError :
550
+ self .__log .error ('Not enough arguments. Expected min 2.' )
551
+ self .__gateway .send_rpc_reply (device = device ,
552
+ req_id = content ['data' ].get ('id' ),
553
+ content = {content ['data' ][
554
+ 'method' ]: 'Not enough arguments. Expected min 2.' ,
555
+ 'code' : 400 })
556
+
557
+ result = {}
558
+ if rpc_method == 'get' :
559
+ task = self .__loop .create_task (self .__read_value (full_path , result ))
560
+
561
+ while not task .done ():
562
+ sleep (.2 )
563
+ elif rpc_method == 'set' :
564
+ value = args_list [2 ].split ('=' )[- 1 ]
565
+ task = self .__loop .create_task (self .__write_value (full_path , value , result ))
566
+
567
+ while not task .done ():
568
+ sleep (.2 )
540
569
541
- if 'ns' in content ['data' ]['params' ]:
542
- full_path = ';' .join ([item for item in (args_list [0 :- 1 ] if rpc_method == 'set' else args_list )])
543
- else :
544
- full_path = args_list [0 ].split ('=' )[- 1 ]
545
- except IndexError :
546
- self .__log .error ('Not enough arguments. Expected min 2.' )
547
570
self .__gateway .send_rpc_reply (device = device ,
548
571
req_id = content ['data' ].get ('id' ),
549
- content = {content ['data' ][
550
- 'method' ]: 'Not enough arguments. Expected min 2.' ,
551
- 'code' : 400 })
552
-
553
- result = {}
554
- if rpc_method == 'get' :
555
- task = self .__loop .create_task (self .__read_value (full_path , result ))
556
-
557
- while not task .done ():
558
- sleep (.2 )
559
- elif rpc_method == 'set' :
560
- value = args_list [2 ].split ('=' )[- 1 ]
561
- task = self .__loop .create_task (self .__write_value (full_path , value , result ))
562
-
563
- while not task .done ():
564
- sleep (.2 )
565
-
566
- self .__gateway .send_rpc_reply (device = device ,
567
- req_id = content ['data' ].get ('id' ),
568
- content = {content ['data' ]['method' ]: result })
569
- else :
570
- device = tuple (filter (lambda i : i .name == content ['device' ], self .__device_nodes ))[0 ]
571
-
572
- for rpc in device .config ['rpc_methods' ]:
573
- if rpc ['method' ] == content ["data" ]['method' ]:
574
- arguments_from_config = rpc ["arguments" ]
575
- arguments = content ["data" ].get ("params" ) if content ["data" ].get (
576
- "params" ) is not None else arguments_from_config
577
- method_name = content ['data' ]['method' ]
578
-
579
- try :
580
- result = {}
581
- task = self .__loop .create_task (
582
- self .__call_method (device .path , method_name , arguments , result ))
583
-
584
- while not task .done ():
585
- sleep (.2 )
586
-
587
- self .__gateway .send_rpc_reply (content ["device" ],
588
- content ["data" ]["id" ],
589
- {content ["data" ]["method" ]: result , "code" : 200 })
590
- self .__log .debug ("method %s result is: %s" , rpc ['method' ], result )
591
- except Exception as e :
592
- self .__log .exception (e )
572
+ content = {content ['data' ]['method' ]: result })
573
+ else :
574
+ device = tuple (filter (lambda i : i .name == content ['device' ], self .__device_nodes ))[0 ]
575
+
576
+ for rpc in device .config ['rpc_methods' ]:
577
+ if rpc ['method' ] == content ["data" ]['method' ]:
578
+ arguments_from_config = rpc ["arguments" ]
579
+ arguments = content ["data" ].get ("params" ) if content ["data" ].get (
580
+ "params" ) is not None else arguments_from_config
581
+ method_name = content ['data' ]['method' ]
582
+
583
+ try :
584
+ result = {}
585
+ task = self .__loop .create_task (
586
+ self .__call_method (device .path , method_name , arguments , result ))
587
+
588
+ while not task .done ():
589
+ sleep (.2 )
590
+
591
+ self .__gateway .send_rpc_reply (content ["device" ],
592
+ content ["data" ]["id" ],
593
+ {content ["data" ]["method" ]: result , "code" : 200 })
594
+ self .__log .debug ("method %s result is: %s" , rpc ['method' ], result )
595
+ except Exception as e :
596
+ self .__log .exception (e )
597
+ self .__gateway .send_rpc_reply (content ["device" ], content ["data" ]["id" ],
598
+ {"error" : str (e ), "code" : 500 })
599
+ else :
600
+ self .__log .error ("Method %s not found for device %s" , rpc_method , content ["device" ])
593
601
self .__gateway .send_rpc_reply (content ["device" ], content ["data" ]["id" ],
594
- {"error" : str (e ), "code" : 500 })
595
- else :
596
- self .__log .error ("Method %s not found for device %s" , rpc_method , content ["device" ])
602
+ {"error" : "%s - Method not found" % rpc_method ,
603
+ "code" : 404 })
604
+ else :
605
+ results = []
606
+ for device in self .__device_nodes :
607
+ content ['device' ] = device .name
608
+
609
+ arguments = content ['data' ]['params' ]["arguments" ]
610
+
611
+ try :
612
+ result = {}
613
+ task = self .__loop .create_task (
614
+ self .__call_method (device .path , rpc_method , arguments , result ))
615
+
616
+ while not task .done ():
617
+ sleep (.2 )
618
+
619
+ results .append (result )
620
+ self .__log .debug ("method %s result is: %s" , rpc_method , result )
621
+ except Exception as e :
622
+ self .__log .exception (e )
597
623
self .__gateway .send_rpc_reply (content ["device" ], content ["data" ]["id" ],
598
- {"error" : "%s - Method not found" % rpc_method ,
599
- "code" : 404 })
624
+ {"error" : str (e ), "code" : 500 })
600
625
626
+ return results
601
627
except Exception as e :
602
628
self .__log .exception (e )
603
629
0 commit comments