Skip to content

Commit e50ca75

Browse files
committed
Plumb report handler through dbus service
1 parent ff97f0c commit e50ca75

File tree

2 files changed

+35
-19
lines changed

2 files changed

+35
-19
lines changed

crystalfontz/dbus/interface.py

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -93,45 +93,54 @@
9393
DBUS_NAME = "org.jfhbrook.crystalfontz"
9494

9595

96-
async def load_client(config_file: Optional[str]) -> Client:
97-
config: Config = Config.from_file(config_file)
98-
99-
client = await create_connection(config.port)
100-
101-
return client
102-
103-
10496
class DbusReportHandler(ReportHandler):
105-
def __init__(self: Self, iface: "DbusInterface") -> None:
106-
self.iface = iface
97+
def __init__(self: Self) -> None:
98+
self.iface: "Optional[DbusInterface]" = None
10799

108100
async def on_key_activity(self: Self, report: KeyActivityReport) -> None:
109-
"""
110-
This method is called on any new key activity report.
111-
"""
101+
if not self.iface:
102+
return
112103

113104
self.iface.key_activity_reports.emit(KeyActivityReportM.pack(report))
114105

115106
async def on_temperature(self: Self, report: TemperatureReport) -> None:
116-
"""
117-
This method is called on any new temperature report.
118-
"""
107+
if not self.iface:
108+
return
119109

120110
self.iface.temperature_reports.emit(TemperatureReportM.pack(report))
121111

122112

113+
async def load_client(
114+
report_handler: Optional[DbusReportHandler], config_file: Optional[str]
115+
) -> Client:
116+
config: Config = Config.from_file(config_file)
117+
118+
client = await create_connection(config.port, report_handler=report_handler)
119+
120+
return client
121+
122+
123123
class DbusInterface( # type: ignore
124124
DbusInterfaceCommonAsync, interface_name=DBUS_NAME # type: ignore
125125
):
126126
"""
127127
A DBus interface for controlling the Crystalfontz device.
128128
"""
129129

130-
def __init__(self: Self, client: Client, config_file: Optional[str] = None) -> None:
130+
def __init__(
131+
self: Self,
132+
client: Client,
133+
report_handler: Optional[DbusReportHandler] = None,
134+
config_file: Optional[str] = None,
135+
) -> None:
131136
super().__init__()
132137
self._config: Config = Config.from_file(config_file)
133138
self.client: Client = client
134139
self._client_lock: asyncio.Lock = asyncio.Lock()
140+
self._report_handler = report_handler
141+
142+
if self._report_handler:
143+
self._report_handler.iface = self
135144

136145
@dbus_property_async(ConfigM.t)
137146
def config(self: Self) -> ConfigT:

crystalfontz/dbus/service/__init__.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,12 @@
1515
select_system_bus,
1616
)
1717
from crystalfontz.dbus.error import handle_dbus_error
18-
from crystalfontz.dbus.interface import DBUS_NAME, DbusInterface, load_client
18+
from crystalfontz.dbus.interface import (
19+
DBUS_NAME,
20+
DbusInterface,
21+
DbusReportHandler,
22+
load_client,
23+
)
1924

2025
logger = logging.getLogger(__name__)
2126

@@ -25,7 +30,9 @@ async def service(config_file: Optional[str] = None) -> DbusInterface:
2530
Create a configure DBus service with a supplied config file.
2631
"""
2732

28-
client = await load_client(config_file)
33+
client = await load_client(
34+
report_handler=DbusReportHandler(), config_file=config_file
35+
)
2936
iface = DbusInterface(client, config_file=config_file)
3037

3138
logger.debug(f"Requesting bus name {DBUS_NAME}...")

0 commit comments

Comments
 (0)