1212if project_root not in sys .path :
1313 sys .path .append (project_root )
1414
15- from powersensor_local . plug_listener import PlugListener
15+ from powersensor_local import PlugListenerTcp , PlugListenerUdp
1616from powersensor_local .abstract_event_handler import AbstractEventHandler
1717
1818async def print_message_ignore_event (_ , message ):
@@ -22,8 +22,12 @@ async def print_event(event):
2222 print (event )
2323
2424class RawPlug (AbstractEventHandler ):
25- def __init__ (self ):
26- self .plug : Union [PlugListener , None ] = None
25+ def __init__ (self , protocol = None ):
26+ self .plug : Union [PlugListenerTcp , PlugListenerUdp , None ] = None
27+ if protocol is None :
28+ self ._protocol = 'udp'
29+ else :
30+ self ._protocol = 'tcp'
2731
2832 async def on_exit (self ):
2933 if self .plug is not None :
@@ -37,8 +41,15 @@ async def main(self):
3741
3842 # Signal handler for Ctrl+C
3943 self .register_sigint_handler ()
40-
41- plug = PlugListener (sys .argv [1 ], * sys .argv [2 :2 ])
44+ if len (sys .argv ) >= 4 :
45+ self ._protocol = sys .argv [3 ]
46+ plug = None
47+ if self ._protocol == 'udp' :
48+ plug = PlugListenerUdp (sys .argv [1 ], * sys .argv [2 :3 ])
49+ elif self ._protocol == 'tcp' :
50+ plug = PlugListenerTcp (sys .argv [1 ], * sys .argv [2 :3 ])
51+ else :
52+ print ('Unsupported protocol:' , self ._protocol )
4253 plug .subscribe ('exception' , print_message_ignore_event )
4354 plug .subscribe ('message' , print_message_ignore_event )
4455 plug .subscribe ('connecting' , print_event )
0 commit comments