Skip to content
This repository was archived by the owner on Apr 13, 2025. It is now read-only.

Commit d819121

Browse files
authored
Merge pull request #256 from noeppi-noeppi/dbus
DBus service
2 parents 8c5e324 + 0979c54 commit d819121

File tree

10 files changed

+1416
-12
lines changed

10 files changed

+1416
-12
lines changed
+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import { DBusConfig } from "./index";
2+
import * as dbus from "dbus-next";
3+
import { MessageBus, ProxyObject, ClientInterface, Variant } from "dbus-next";
4+
5+
export class DBusClient {
6+
public readonly session: MessageBus;
7+
public readonly system: MessageBus;
8+
9+
constructor(config: DBusConfig) {
10+
this.session = dbus.sessionBus(config);
11+
this.system = dbus.systemBus();
12+
}
13+
14+
public async proxy<T>(config: DBusProxyConfig<T>): Promise<T> {
15+
const proxy = await (config.system ? this.system : this.session).getProxyObject(config.iface, config.path);
16+
return config.create(this, proxy);
17+
}
18+
19+
static createClient(config: DBusConfig): DBusClient {
20+
return new DBusClient(config);
21+
}
22+
}
23+
24+
// Required, so we don't have circular imports.
25+
export interface DBusProxyConfig<T> {
26+
iface: string;
27+
path: string;
28+
system: boolean;
29+
create(client: DBusClient, proxy: ProxyObject): T;
30+
}
31+
32+
export class DBusObject {
33+
protected readonly client: DBusClient;
34+
protected readonly proxy: ProxyObject;
35+
private readonly properties: RatBagPropertyInterface;
36+
37+
protected constructor(client: DBusClient, proxy: ProxyObject) {
38+
this.client = client;
39+
this.proxy = proxy;
40+
this.properties = proxy.getInterface("org.freedesktop.DBus.Properties") as RatBagPropertyInterface;
41+
}
42+
43+
async getProperty(iface: string, name: string): Promise<Variant> {
44+
return await this.properties.Get(iface, name);
45+
}
46+
47+
async setProperty(iface: string, name: string, value: Variant): Promise<void> {
48+
return await this.properties.Set(iface, name, value);
49+
}
50+
}
51+
52+
type RatBagPropertyInterface = ClientInterface & {
53+
Get(iface: string, path: string): Promise<Variant>;
54+
Set(iface: string, path: string, value: Variant): Promise<void>;
55+
};

nodecg-io-dbus/extension/index.ts

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import { NodeCG } from "nodecg-types/types/server";
2+
import { Result, emptySuccess, success, ServiceBundle } from "nodecg-io-core";
3+
import { DBusClient } from "./dbusClient";
4+
import * as dbus from "dbus-next";
5+
6+
export interface DBusConfig {
7+
busAddress?: string;
8+
}
9+
10+
export * from "./dbusClient";
11+
12+
module.exports = (nodecg: NodeCG) => {
13+
new DBusService(nodecg, "dbus", __dirname, "../schema.json").register();
14+
};
15+
16+
class DBusService extends ServiceBundle<DBusConfig, DBusClient> {
17+
async validateConfig(config: DBusConfig): Promise<Result<void>> {
18+
dbus.sessionBus(config);
19+
return emptySuccess();
20+
}
21+
22+
async createClient(config: DBusConfig): Promise<Result<DBusClient>> {
23+
const client = DBusClient.createClient(config);
24+
this.nodecg.log.info("Successfully created dbus client.");
25+
return success(client);
26+
}
27+
28+
stopClient(client: DBusClient): void {
29+
client.session.disconnect();
30+
client.system.disconnect();
31+
this.nodecg.log.info("Successfully stopped dbus client.");
32+
}
33+
34+
removeHandlers(client: DBusClient): void {
35+
client.session.removeAllListeners();
36+
client.system.removeAllListeners();
37+
}
38+
}

0 commit comments

Comments
 (0)