Compare commits
No commits in common. "30650401891db0a6e624ec360dcd39f784ceab61" and "b0c1c7d966a68e545aa5a01f038915efe14a26a6" have entirely different histories.
3065040189
...
b0c1c7d966
|
@ -160,19 +160,6 @@ version = "1.0.0"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
|
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "chrono"
|
|
||||||
version = "0.4.19"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "670ad68c9088c2a963aaa298cb369688cf3f9465ce5e2d4ca10e6e0098a1ce73"
|
|
||||||
dependencies = [
|
|
||||||
"libc",
|
|
||||||
"num-integer",
|
|
||||||
"num-traits",
|
|
||||||
"time",
|
|
||||||
"winapi",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "concurrent-queue"
|
name = "concurrent-queue"
|
||||||
version = "1.2.2"
|
version = "1.2.2"
|
||||||
|
@ -680,7 +667,7 @@ checksum = "713d550d9b44d89174e066b7a6217ae06234c10cb47819a88290d2b353c31799"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"libc",
|
"libc",
|
||||||
"log",
|
"log",
|
||||||
"wasi 0.11.0+wasi-snapshot-preview1",
|
"wasi",
|
||||||
"windows-sys",
|
"windows-sys",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
@ -713,25 +700,6 @@ dependencies = [
|
||||||
"version_check",
|
"version_check",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "num-integer"
|
|
||||||
version = "0.1.45"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "225d3389fb3509a24c93f5c29eb6bde2586b98d9f016636dff58d7c6f7569cd9"
|
|
||||||
dependencies = [
|
|
||||||
"autocfg",
|
|
||||||
"num-traits",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "num-traits"
|
|
||||||
version = "0.2.15"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "578ede34cf02f8924ab9447f50c28075b4d3e5b269972345e7e0372b38c6cdcd"
|
|
||||||
dependencies = [
|
|
||||||
"autocfg",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "num_cpus"
|
name = "num_cpus"
|
||||||
version = "1.13.1"
|
version = "1.13.1"
|
||||||
|
@ -1051,7 +1019,6 @@ name = "sampad"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"bitvec",
|
"bitvec",
|
||||||
"chrono",
|
|
||||||
"hidapi",
|
"hidapi",
|
||||||
"rumqttc",
|
"rumqttc",
|
||||||
"serde",
|
"serde",
|
||||||
|
@ -1303,17 +1270,6 @@ dependencies = [
|
||||||
"once_cell",
|
"once_cell",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "time"
|
|
||||||
version = "0.1.44"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "6db9e6914ab8b1ae1c260a4ae7a49b6c5611b40328a735b21862567685e73255"
|
|
||||||
dependencies = [
|
|
||||||
"libc",
|
|
||||||
"wasi 0.10.0+wasi-snapshot-preview1",
|
|
||||||
"winapi",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "tinyvec"
|
name = "tinyvec"
|
||||||
version = "1.6.0"
|
version = "1.6.0"
|
||||||
|
@ -1554,12 +1510,6 @@ dependencies = [
|
||||||
"try-lock",
|
"try-lock",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "wasi"
|
|
||||||
version = "0.10.0+wasi-snapshot-preview1"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "1a143597ca7c7793eff794def352d41792a93c481eb1042423ff7ff72ba2c31f"
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "wasi"
|
name = "wasi"
|
||||||
version = "0.11.0+wasi-snapshot-preview1"
|
version = "0.11.0+wasi-snapshot-preview1"
|
||||||
|
|
|
@ -11,4 +11,3 @@ serde = "1"
|
||||||
serde_dhall = "0.11"
|
serde_dhall = "0.11"
|
||||||
tracing = "0.1.35"
|
tracing = "0.1.35"
|
||||||
tracing-subscriber = "0.3.14"
|
tracing-subscriber = "0.3.14"
|
||||||
chrono = "0.4.19"
|
|
||||||
|
|
13
build.rs
13
build.rs
|
@ -1,13 +0,0 @@
|
||||||
use std::error::Error;
|
|
||||||
use std::process::Command;
|
|
||||||
|
|
||||||
fn main() -> Result<(), Box<dyn Error>> {
|
|
||||||
let hash = Command::new("git")
|
|
||||||
.arg("rev-parse")
|
|
||||||
.arg("--short")
|
|
||||||
.arg("HEAD")
|
|
||||||
.output()?
|
|
||||||
.stdout;
|
|
||||||
println!("cargo:rustc-env=VERSION={}", std::str::from_utf8(&hash)?);
|
|
||||||
Ok(())
|
|
||||||
}
|
|
|
@ -1,26 +1,16 @@
|
||||||
let T = ./types.dhall
|
{ mqtt_servers.default = { host = "1.2.3.4", user = Some "username", pass = Some
|
||||||
|
"password" }
|
||||||
in { mqtt_servers.default
|
|
||||||
=
|
|
||||||
{ host = "1.2.3.4", user = Some "username", pass = Some "password" }
|
|
||||||
, layers.default
|
, layers.default
|
||||||
=
|
=
|
||||||
{ default_mapping = T.Action.Print "no mapping!"
|
{ default_mapping = Mapping.Print "no mapping!"
|
||||||
, mappings =
|
, mappings =
|
||||||
let sometopic = "homeassistant/switch/something/set"
|
let sometopic = "homeassistant/switch/something/set"
|
||||||
|
|
||||||
in { key_0 =
|
in { key_0 =
|
||||||
T.Mapping.Trigger
|
Mapping.Trigger (Action.MQTTPub { topic = sometopic, payload = "ON" })
|
||||||
(T.Action.MQTTPub { topic = sometopic, payload = "ON" })
|
|
||||||
, key_1 =
|
, key_1 =
|
||||||
T.Mapping.Trigger
|
Mapping.Trigger
|
||||||
(T.Action.MQTTPub { topic = sometopic, payload = "OFF" })
|
(Action.MQTTPub { topic = sometopic, payload = "OFF" })
|
||||||
, key_2 =
|
|
||||||
T.Mapping.TriggerMulti
|
|
||||||
[ T.Action.Print "Doing a thing"
|
|
||||||
, T.Action.MQTTPub
|
|
||||||
{ topic = "thing/foo/bar", payload = "boop" }
|
|
||||||
]
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -34,7 +34,6 @@
|
||||||
buildInputs = with pkgs; [
|
buildInputs = with pkgs; [
|
||||||
# Dhall (config) stuff
|
# Dhall (config) stuff
|
||||||
dhall
|
dhall
|
||||||
dhall-json
|
|
||||||
dhall-lsp-server
|
dhall-lsp-server
|
||||||
# Rust!
|
# Rust!
|
||||||
(rust-bin.stable.latest.default.override {
|
(rust-bin.stable.latest.default.override {
|
||||||
|
|
|
@ -1,7 +1,5 @@
|
||||||
use crate::StdError;
|
|
||||||
use chrono::Utc;
|
|
||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
use serde_dhall::{SimpleType, StaticType};
|
use serde_dhall::StaticType;
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::error::Error;
|
use std::error::Error;
|
||||||
use std::fmt;
|
use std::fmt;
|
||||||
|
@ -26,11 +24,7 @@ impl fmt::Display for Action {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Newtype wrapper around a Vec of actions.
|
#[derive(Deserialize, StaticType, Debug)]
|
||||||
// serde(transparent) and the imp StaticType below ensures we ignore the outer struct and
|
|
||||||
// transparently deserialize into the inner vec.
|
|
||||||
#[derive(Deserialize, Debug)]
|
|
||||||
#[serde(transparent)]
|
|
||||||
pub struct Actions(Vec<Action>);
|
pub struct Actions(Vec<Action>);
|
||||||
|
|
||||||
impl Actions {
|
impl Actions {
|
||||||
|
@ -46,12 +40,6 @@ impl fmt::Display for Actions {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl StaticType for Actions {
|
|
||||||
fn static_type() -> SimpleType {
|
|
||||||
SimpleType::List(Box::new(Action::static_type()))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Deserialize, StaticType, Debug)]
|
#[derive(Deserialize, StaticType, Debug)]
|
||||||
pub enum Mapping {
|
pub enum Mapping {
|
||||||
NOP, // Do nothing.
|
NOP, // Do nothing.
|
||||||
|
@ -99,25 +87,16 @@ pub struct Config {
|
||||||
|
|
||||||
impl Config {
|
impl Config {
|
||||||
pub fn from_file(p: &Path) -> Result<Self, Box<dyn Error>> {
|
pub fn from_file(p: &Path) -> Result<Self, Box<dyn Error>> {
|
||||||
serde_dhall::from_file(p)
|
match serde_dhall::from_file(p)
|
||||||
|
.with_builtin_type("Mapping".to_string(), Mapping::static_type())
|
||||||
|
.with_builtin_type("Action".to_string(), Action::static_type())
|
||||||
.parse::<Self>()
|
.parse::<Self>()
|
||||||
.map_err(|e| e.into())
|
{
|
||||||
}
|
Ok(c) => Ok(c),
|
||||||
|
Err(e) => {
|
||||||
pub fn generate_prelude() -> StdError<()> {
|
println!("{}", e);
|
||||||
let mut m = HashMap::new();
|
Err(Box::new(e))
|
||||||
m.insert("Action".to_string(), Action::static_type());
|
}
|
||||||
m.insert("Mapping".to_string(), Mapping::static_type());
|
}
|
||||||
println!(
|
|
||||||
"-- Sampad Dhall types, autogenerated by git#{} on {}",
|
|
||||||
option_env!("VERSION").unwrap_or("unknown"),
|
|
||||||
Utc::now(),
|
|
||||||
);
|
|
||||||
println!(
|
|
||||||
"{{ Action = {}, Mapping = {} }}",
|
|
||||||
Action::static_type(),
|
|
||||||
Mapping::static_type()
|
|
||||||
);
|
|
||||||
Ok(())
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
17
src/main.rs
17
src/main.rs
|
@ -5,14 +5,11 @@ use config::{Action, Config, Mapping};
|
||||||
use hidapi::HidApi;
|
use hidapi::HidApi;
|
||||||
use rumqttc::{Client, MqttOptions, QoS};
|
use rumqttc::{Client, MqttOptions, QoS};
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::env;
|
|
||||||
use std::error::Error;
|
use std::error::Error;
|
||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
use std::thread;
|
use std::thread;
|
||||||
use tracing::{event, Level};
|
use tracing::{event, Level};
|
||||||
|
|
||||||
type StdError<T> = Result<T, Box<dyn Error>>;
|
|
||||||
|
|
||||||
struct State<'a> {
|
struct State<'a> {
|
||||||
mqtt_servers: HashMap<String, Client>,
|
mqtt_servers: HashMap<String, Client>,
|
||||||
conf: &'a Config,
|
conf: &'a Config,
|
||||||
|
@ -110,19 +107,7 @@ impl<'a> State<'a> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() -> StdError<()> {
|
fn main() -> Result<(), Box<dyn Error>> {
|
||||||
let args: Vec<_> = env::args().collect();
|
|
||||||
if let [prog, args @ ..] = &args[..] {
|
|
||||||
if args == ["generate-dhall"] {
|
|
||||||
return Config::generate_prelude();
|
|
||||||
} else if args.len() > 0 {
|
|
||||||
eprintln!("Usage: {} [generate-dhall]", prog);
|
|
||||||
return Err("Invalid usage".into());
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
unreachable!();
|
|
||||||
}
|
|
||||||
|
|
||||||
tracing_subscriber::fmt::init();
|
tracing_subscriber::fmt::init();
|
||||||
event!(Level::INFO, "Starting...");
|
event!(Level::INFO, "Starting...");
|
||||||
let conf = Config::from_file(Path::new("./config.dhall"))?;
|
let conf = Config::from_file(Path::new("./config.dhall"))?;
|
||||||
|
|
22
types.dhall
22
types.dhall
|
@ -1,22 +0,0 @@
|
||||||
-- Sampad Dhall types, autogenerated by git#96d9ca3 on 2022-07-11 13:54:53.099283885 UTC
|
|
||||||
{ Action =
|
|
||||||
< ActivateLayer : Text
|
|
||||||
| MQTTPub : { payload : Text, topic : Text }
|
|
||||||
| Print : Text
|
|
||||||
>
|
|
||||||
, Mapping =
|
|
||||||
< NOP
|
|
||||||
| Passthrough
|
|
||||||
| Trigger :
|
|
||||||
< ActivateLayer : Text
|
|
||||||
| MQTTPub : { payload : Text, topic : Text }
|
|
||||||
| Print : Text
|
|
||||||
>
|
|
||||||
| TriggerMulti :
|
|
||||||
List
|
|
||||||
< ActivateLayer : Text
|
|
||||||
| MQTTPub : { payload : Text, topic : Text }
|
|
||||||
| Print : Text
|
|
||||||
>
|
|
||||||
>
|
|
||||||
}
|
|
Loading…
Reference in New Issue