Skip to content

Commit f56651c

Browse files
committed
Add configuration file
1 parent 594e9a0 commit f56651c

File tree

4 files changed

+109
-28
lines changed

4 files changed

+109
-28
lines changed

Cargo.lock

+50-13
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dbus-monitor/Cargo.toml

+4-1
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,12 @@ version = "0.0.1"
55

66
[dependencies]
77
dbus = { version = "0.9.7", features = ["vendored"] }
8-
clap = { version = "4.0", features = ["derive"] }
8+
clap = { version = "4.4.6", features = ["derive"] }
99
log = "0.4"
1010
env_logger = "0.10.0"
11+
serde = { version="1.0", features = ["derive"] }
12+
serde_json = "1.0"
13+
lazy_static = "1.4"
1114

1215
[dependencies.libdbus-sys]
1316
default-features = false

dbus-monitor/src/config.json

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"dbus_interface": "org.freedesktop.Notifications",
3+
"dbus_member": "Notify",
4+
"scan_args_for": "calendar.google.com",
5+
"run_inputmodule_commands": [
6+
"led-matrix --pattern all-on --blink-n-times 3",
7+
"led-matrix --brightness 0"
8+
]
9+
}
10+

dbus-monitor/src/dbus_monitor.rs

+45-14
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
// Mostly taken from https://github.com/diwic/dbus-rs/blob/366a6dca3d20745f5dcfa006b1b1311c376d420e/dbus/examples/monitor.rs
22

33
// This programs implements the equivalent of running the "dbus-monitor" tool
4-
// modified to only search for messages in the org.freedesktop.Notifications interface
4+
// modified to only search for messages in the interface specificied in config.json,
5+
// and then run arbitary inputmodule-rs commands to react to them
6+
57
use dbus::blocking::Connection;
68
use dbus::channel::MatchingReceiver;
79
use dbus::message::MatchRule;
@@ -17,6 +19,40 @@ use inputmodule_control::inputmodule::serial_commands;
1719

1820
use log::debug;
1921

22+
use serde::{Deserialize, Serialize};
23+
use serde_json;
24+
use std::fs::File;
25+
use std::io::Read;
26+
27+
use lazy_static::lazy_static;
28+
29+
#[derive(Debug, Deserialize, Serialize)]
30+
pub struct Config {
31+
dbus_interface: String,
32+
dbus_member: String,
33+
scan_args_for: String,
34+
run_inputmodule_commands: Vec<String>,
35+
}
36+
37+
fn read_config(file_path: &str) -> Result<Config, Box<dyn std::error::Error>> {
38+
let mut file = File::open(file_path)?;
39+
let mut config_data = String::new();
40+
file.read_to_string(&mut config_data)?;
41+
42+
let config: Config = serde_json::from_str(&config_data)?;
43+
44+
Ok(config)
45+
}
46+
47+
lazy_static! {
48+
pub static ref CONFIG: Config = {
49+
// Read and deserialize the JSON configuration
50+
let config_file = "dbus-monitor/src/config.json";
51+
let config = read_config(config_file).expect("Failed to read config");
52+
config
53+
};
54+
}
55+
2056
fn handle_message(msg: &Message) {
2157
debug!("Got message from DBus: {:?}", msg);
2258

@@ -26,15 +62,11 @@ fn handle_message(msg: &Message) {
2662
let string_value: String = string_ref.to_string();
2763
debug!("String value: {}", string_value);
2864

29-
if string_value.contains("calendar.google.com") {
30-
run_inputmodule_command(vec![
31-
"led-matrix",
32-
"--pattern",
33-
"all-on",
34-
"--blink-n-times",
35-
"3",
36-
]);
37-
run_inputmodule_command(vec!["led-matrix", "--brightness", "0"]);
65+
if string_value.contains(&CONFIG.scan_args_for) {
66+
for command in &CONFIG.run_inputmodule_commands {
67+
let command_vec: Vec<&str> = command.split_whitespace().collect();
68+
run_inputmodule_command(command_vec);
69+
}
3870
}
3971
}
4072
iter.next();
@@ -56,12 +88,11 @@ pub fn run_dbus_monitor() {
5688
let conn = Connection::new_session().expect("D-Bus connection failed");
5789
debug!("Connection to DBus session monitor opened");
5890

59-
// Second create a rule to match messages we want to receive; in this example we add no
60-
// further requirements, so all messages will match
91+
// Second create a rule to match messages we want to receive
6192
let rule = MatchRule::new()
6293
.with_type(MessageType::MethodCall)
63-
.with_interface("org.freedesktop.Notifications")
64-
.with_member("Notify");
94+
.with_interface(&CONFIG.dbus_interface)
95+
.with_member(&CONFIG.dbus_member);
6596

6697
// Try matching using new scheme
6798
let proxy = conn.with_proxy(

0 commit comments

Comments
 (0)