diff options
Diffstat (limited to 'interfaces')
-rw-r--r-- | interfaces/configuration.py | 14 | ||||
-rwxr-xr-x | interfaces/desktopEvent.py | 3 | ||||
-rwxr-xr-x | interfaces/endpoint.py | 20 | ||||
-rwxr-xr-x | interfaces/message.py | 19 |
4 files changed, 46 insertions, 10 deletions
diff --git a/interfaces/configuration.py b/interfaces/configuration.py new file mode 100644 index 0000000..9449f41 --- /dev/null +++ b/interfaces/configuration.py @@ -0,0 +1,14 @@ +from zope import interface +from zope.interface import Attribute + +from typing import Dict + +class IConfiguration(interface.Interface): + + def get(self, key : str, default) -> Dict: + """ Load the mapping for a given key + """ + + def __setitem__(self, key : str, value : Dict) -> None: + """ Set a value + """ diff --git a/interfaces/desktopEvent.py b/interfaces/desktopEvent.py index e8a30a4..9030ccd 100755 --- a/interfaces/desktopEvent.py +++ b/interfaces/desktopEvent.py @@ -4,10 +4,9 @@ from zope.interface import Attribute class IDesktop(interface.Interface):
- queue = Attribute("""The queue to send the message""")
mapping = Attribute("""Correspondance between application and layer""")
- def getForegroundWindowTitle(sel) -> Optional[str]:
+ def getForegroundWindowTitle(self) -> Optional[str]:
""" Return the name of the selected window
"""
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
diff --git a/interfaces/message.py b/interfaces/message.py index 23ce2ec..bf05bef 100755 --- a/interfaces/message.py +++ b/interfaces/message.py @@ -1,14 +1,31 @@ +"""Messages sent to the user.
+"""
+
from zope import interface
from zope.interface import Attribute
class IMessage(interface.Interface):
+ """Interface for all the user messages.
+ """
content = Attribute("""The text message to log""")
+ level = Attribute("""Level of the message, 0 is high level and 10 is the lower""")
class ISocketMessage(interface.Interface):
+ """Message to sent to the endpoint."""
content = Attribute("""Content to send""")
@interface.implementer(IMessage)
-class Debug(object):
+class Debug():
+ """Send a message with a low level"""
+
+ def __init__(self, message):
+ self.content = message
+ self.level = 10
+
+@interface.implementer(IMessage)
+class Error():
+ """Send a message with a high level"""
def __init__(self, message):
self.content = message
+ self.level = 0
|