Skip to content

Commit 17203b3

Browse files
committed
trying to add protocol switching
1 parent 2360ef2 commit 17203b3

File tree

1 file changed

+16
-5
lines changed

1 file changed

+16
-5
lines changed

src/powersensor_local/rawplug.py

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
if 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
1616
from powersensor_local.abstract_event_handler import AbstractEventHandler
1717

1818
async def print_message_ignore_event(_, message):
@@ -22,8 +22,12 @@ async def print_event(event):
2222
print(event)
2323

2424
class 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

Comments
 (0)