diff options
Diffstat (limited to 'interfaces/endpoint.py')
-rwxr-xr-x | interfaces/endpoint.py | 20 |
1 files changed, 13 insertions, 7 deletions
diff --git a/interfaces/endpoint.py b/interfaces/endpoint.py index 15356c2..2f3af95 100755 --- a/interfaces/endpoint.py +++ b/interfaces/endpoint.py @@ -5,7 +5,6 @@ from zope.interface import Attribute class IEndpoint(interface.Interface):
- queue = Attribute("""The queue to send the message""")
state = Attribute("""The connection status""")
def isConnected(self) -> bool:
@@ -47,6 +46,7 @@ from zope import component import json
from interfaces.message import IMessage, Debug
+from consumer import Mapping
@component.adapter(IConnection)
@interface.implementer(IEndpoint)
@@ -58,17 +58,23 @@ class EndPoint(object): def __init__(self, connection):
self.connection = connection
- self.queue = None
self.state = self.STATE_DISCONNECTED
def isConnected(self) -> bool:
return self.state != self.STATE_DISCONNECTED
def connect(self):
+ state = self.state
try:
self.connection.connect()
self.state = self.STATE_CONNECTED
component.handle(Debug("Connected"))
+ if state == self.STATE_DISCONNECTED:
+ # This is the first connection
+ # Otherwise the state should be STATE_CONNECTING
+ # Initialize with the default layer
+ component.handle(Mapping(("default", None)))
+
except Exception as e:
print(e)
self.state = self.STATE_DISCONNECTED
@@ -90,23 +96,23 @@ class EndPoint(object): # If we do not have any entry from the macropad, just return
return
- print("recv", received)
layout = str(received, "utf-8").strip()
desktop = component.queryUtility(desktopEvent.IDesktop)
if desktop is not None:
title = desktop.getForegroundWindowTitle()
else:
title = None
- self.queue.put((layout, title))
+ component.handle(Mapping((layout, title)))
def send(self, data: list[dict[str, str]]):
- """ Send the data to the macropad
+ """ Send the data to the endpoint. The data must be the representation
+ of a json element.
"""
if self.state != self.STATE_CONNECTED:
return
try:
- j = json.dumps( data )
- self.connection.write(bytes(j, "utf-8"))
+ j = json.JSONEncoder().encode( data ) + "\n"
+ self.connection.write(str.encode(j))
except Exception as e:
print("send error", e)
self.state = self.STATE_DISCONNECTED
|