From c5d58a4ff584e7840cf6f75114597f29b5f596bf Mon Sep 17 00:00:00 2001 From: Sam Willcocks Date: Thu, 7 Jul 2022 00:17:14 +0000 Subject: [PATCH] Switch to mqtt adaptor --- .envrc | 1 + Cargo.lock | 628 +++++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 3 + flake.lock | 50 ++-- flake.nix | 56 ++-- src/bin/ddcmqtt.rs | 2 + src/bin/setmon.rs | 1 - src/lib.rs | 81 +++++- 8 files changed, 768 insertions(+), 54 deletions(-) diff --git a/.envrc b/.envrc index d482335..2235ed3 100644 --- a/.envrc +++ b/.envrc @@ -3,3 +3,4 @@ if ! has nix_direnv_version || ! nix_direnv_version 1.3.0; then fi use flake +export RUST_LOG=info diff --git a/Cargo.lock b/Cargo.lock index 4746e3d..189127a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,18 +2,63 @@ # It is not intended for manual editing. version = 3 +[[package]] +name = "ansi_term" +version = "0.12.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d52a9bb7ec0cf484c551830a7ce27bd20d67eac647e1befb56b0be4ee39a55d2" +dependencies = [ + "winapi", +] + +[[package]] +name = "autocfg" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa" + +[[package]] +name = "base64" +version = "0.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "904dfeac50f3cdaba28fc6f57fdcddb75f49ed61346676a78c4ffe55877802fd" + [[package]] name = "bitflags" version = "1.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" +[[package]] +name = "bumpalo" +version = "3.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "37ccbd214614c6783386c1af30caf03192f17891059cecc394b4fb119e363de3" + [[package]] name = "byteorder" version = "1.4.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "14c189c53d098945499cdfa7ecc63567cf3886b3332b312a5b4585d8d3a6a610" +[[package]] +name = "bytes" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c4872d67bab6358e59559027aa3b9157c53d9358c51423c17554809a8858e0f8" + +[[package]] +name = "cc" +version = "1.0.73" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2fff2a6927b3bb87f9595d67196a70493f627687a71d87a0d692242c33f58c11" + +[[package]] +name = "cfg-if" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" + [[package]] name = "ddc" version = "0.2.2" @@ -42,6 +87,64 @@ dependencies = [ "ddc", "ddc-i2c", "rosc", + "rumqttc", + "tracing", + "tracing-subscriber", +] + +[[package]] +name = "flume" +version = "0.10.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1ceeb589a3157cac0ab8cc585feb749bd2cea5cb55a6ee802ad72d9fd38303da" +dependencies = [ + "futures-core", + "futures-sink", + "nanorand", + "pin-project", + "spin 0.9.3", +] + +[[package]] +name = "fnv" +version = "1.0.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1" + +[[package]] +name = "futures-core" +version = "0.3.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0c09fd04b7e4073ac7156a9539b57a484a8ea920f79c7c675d05d289ab6110d3" + +[[package]] +name = "futures-sink" +version = "0.3.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "21163e139fa306126e6eedaf49ecdb4588f939600f0b1e770f4205ee4b7fa868" + +[[package]] +name = "getrandom" +version = "0.2.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4eb1a864a501629691edf6c15a593b7a51eebaa1e8468e9ddc623de7c9b58ec6" +dependencies = [ + "cfg-if", + "js-sys", + "libc", + "wasi", + "wasm-bindgen", +] + +[[package]] +name = "http" +version = "0.2.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "75f43d41e26995c17e71ee126451dd3941010b0514a81a9d11f3b341debc2399" +dependencies = [ + "bytes", + "fnv", + "itoa", ] [[package]] @@ -77,6 +180,27 @@ dependencies = [ "libc", ] +[[package]] +name = "itoa" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "112c678d4050afce233f4f2852bb2eb519230b3cf12f33585275537d7e41578d" + +[[package]] +name = "js-sys" +version = "0.3.58" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c3fac17f7123a73ca62df411b1bf727ccc805daa070338fda671c86dac1bdc27" +dependencies = [ + "wasm-bindgen", +] + +[[package]] +name = "lazy_static" +version = "1.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" + [[package]] name = "libc" version = "0.2.125" @@ -93,6 +217,25 @@ dependencies = [ "pkg-config", ] +[[package]] +name = "lock_api" +version = "0.4.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "327fa5b6a6940e4699ec49a9beae1ea4845c6bab9314e4f84ac68742139d8c53" +dependencies = [ + "autocfg", + "scopeguard", +] + +[[package]] +name = "log" +version = "0.4.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "abb12e687cfb44aa40f41fc3978ef76448f9b6038cad6aef4259d3c095a2382e" +dependencies = [ + "cfg-if", +] + [[package]] name = "mccs" version = "0.1.0" @@ -114,6 +257,27 @@ version = "0.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "68354c5c6bd36d73ff3feceb05efa59b6acb7626617f4962be322a825e61f79a" +[[package]] +name = "mio" +version = "0.8.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "57ee1c23c7c63b0c9250c339ffdc69255f110b298b901b9f6c82547b7b87caaf" +dependencies = [ + "libc", + "log", + "wasi", + "windows-sys", +] + +[[package]] +name = "nanorand" +version = "0.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6a51313c5820b0b02bd422f4b44776fbf47961755c74ce64afc73bfad10226c3" +dependencies = [ + "getrandom", +] + [[package]] name = "nom" version = "7.1.1" @@ -124,12 +288,68 @@ dependencies = [ "minimal-lexical", ] +[[package]] +name = "once_cell" +version = "1.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "18a6dbe30758c9f83eb00cbea4ac95966305f5a7772f3f42ebfc7fc7eddbd8e1" + +[[package]] +name = "pin-project" +version = "1.0.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "78203e83c48cffbe01e4a2d35d566ca4de445d79a85372fc64e378bfc812a260" +dependencies = [ + "pin-project-internal", +] + +[[package]] +name = "pin-project-internal" +version = "1.0.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "710faf75e1b33345361201d36d04e98ac1ed8909151a017ed384700836104c74" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "pin-project-lite" +version = "0.2.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e0a7ae3ac2f1173085d398531c705756c94a4c56843785df85a60c1a0afac116" + [[package]] name = "pkg-config" version = "0.3.25" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1df8c4ec4b0627e53bdf214615ad287367e482558cf84b109250b37464dc03ae" +[[package]] +name = "pollster" +version = "0.2.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5da3b0203fd7ee5720aa0b5e790b591aa5d3f41c3ed2c34a3a393382198af2f7" + +[[package]] +name = "proc-macro2" +version = "1.0.40" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dd96a1e8ed2596c337f8eae5f24924ec83f5ad5ab21ea8e455d3566c69fbcaf7" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quote" +version = "1.0.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3bcdf212e9776fbcb2d23ab029360416bb1706b1aea2d1a5ba002727cbcab804" +dependencies = [ + "proc-macro2", +] + [[package]] name = "resize-slice" version = "0.1.3" @@ -139,6 +359,21 @@ dependencies = [ "uninitialized", ] +[[package]] +name = "ring" +version = "0.16.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3053cf52e236a3ed746dfc745aa9cacf1b791d846bdaf412f60a8d7d6e17c8fc" +dependencies = [ + "cc", + "libc", + "once_cell", + "spin 0.5.2", + "untrusted", + "web-sys", + "winapi", +] + [[package]] name = "rosc" version = "0.7.0" @@ -149,6 +384,236 @@ dependencies = [ "nom", ] +[[package]] +name = "rumqttc" +version = "0.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "502d06d1030126f5ca1a3b64944af28c7475f3c2858ff20deec12c230f266a53" +dependencies = [ + "bytes", + "flume", + "http", + "log", + "pollster", + "rustls-pemfile", + "thiserror", + "tokio", + "tokio-rustls", +] + +[[package]] +name = "rustls" +version = "0.20.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5aab8ee6c7097ed6057f43c187a62418d0c05a4bd5f18b3571db50ee0f9ce033" +dependencies = [ + "log", + "ring", + "sct", + "webpki", +] + +[[package]] +name = "rustls-pemfile" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1ee86d63972a7c661d1536fefe8c3c8407321c3df668891286de28abcd087360" +dependencies = [ + "base64", +] + +[[package]] +name = "scopeguard" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d29ab0c6d3fc0ee92fe66e2d99f700eab17a8d57d1c1d3b748380fb20baa78cd" + +[[package]] +name = "sct" +version = "0.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d53dcdb7c9f8158937a7981b48accfd39a43af418591a5d008c7b22b5e1b7ca4" +dependencies = [ + "ring", + "untrusted", +] + +[[package]] +name = "sharded-slab" +version = "0.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "900fba806f70c630b0a382d0d825e17a0f19fcd059a2ade1ff237bcddf446b31" +dependencies = [ + "lazy_static", +] + +[[package]] +name = "smallvec" +version = "1.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2fd0db749597d91ff862fd1d55ea87f7855a744a8425a64695b6fca237d1dad1" + +[[package]] +name = "socket2" +version = "0.4.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "66d72b759436ae32898a2af0a14218dbf55efde3feeb170eb623637db85ee1e0" +dependencies = [ + "libc", + "winapi", +] + +[[package]] +name = "spin" +version = "0.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6e63cff320ae2c57904679ba7cb63280a3dc4613885beafb148ee7bf9aa9042d" + +[[package]] +name = "spin" +version = "0.9.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c530c2b0d0bf8b69304b39fe2001993e267461948b890cd037d8ad4293fa1a0d" +dependencies = [ + "lock_api", +] + +[[package]] +name = "syn" +version = "1.0.98" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c50aef8a904de4c23c788f104b7dddc7d6f79c647c7c8ce4cc8f73eb0ca773dd" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "thiserror" +version = "1.0.31" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bd829fe32373d27f76265620b5309d0340cb8550f523c1dda251d6298069069a" +dependencies = [ + "thiserror-impl", +] + +[[package]] +name = "thiserror-impl" +version = "1.0.31" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0396bc89e626244658bef819e22d0cc459e795a5ebe878e6ec336d1674a8d79a" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "thread_local" +version = "1.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5516c27b78311c50bf42c071425c560ac799b11c30b31f87e3081965fe5e0180" +dependencies = [ + "once_cell", +] + +[[package]] +name = "tokio" +version = "1.17.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2af73ac49756f3f7c01172e34a23e5d0216f6c32333757c2c61feb2bbff5a5ee" +dependencies = [ + "bytes", + "libc", + "memchr", + "mio", + "pin-project-lite", + "socket2", + "tokio-macros", + "winapi", +] + +[[package]] +name = "tokio-macros" +version = "1.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9724f9a975fb987ef7a3cd9be0350edcbe130698af5b8f7a631e23d42d052484" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "tokio-rustls" +version = "0.23.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4151fda0cf2798550ad0b34bcfc9b9dcc2a9d2471c895c68f3a8818e54f2389e" +dependencies = [ + "rustls", + "tokio", + "webpki", +] + +[[package]] +name = "tracing" +version = "0.1.35" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a400e31aa60b9d44a52a8ee0343b5b18566b03a8321e0d321f695cf56e940160" +dependencies = [ + "cfg-if", + "pin-project-lite", + "tracing-attributes", + "tracing-core", +] + +[[package]] +name = "tracing-attributes" +version = "0.1.22" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "11c75893af559bc8e10716548bdef5cb2b983f8e637db9d0e15126b61b484ee2" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "tracing-core" +version = "0.1.28" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7b7358be39f2f274f322d2aaed611acc57f382e8eb1e5b48cb9ae30933495ce7" +dependencies = [ + "once_cell", + "valuable", +] + +[[package]] +name = "tracing-log" +version = "0.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "78ddad33d2d10b1ed7eb9d1f518a5674713876e97e5bb9b7345a7984fbb4f922" +dependencies = [ + "lazy_static", + "log", + "tracing-core", +] + +[[package]] +name = "tracing-subscriber" +version = "0.3.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3a713421342a5a666b7577783721d3117f1b69a393df803ee17bb73b1e122a59" +dependencies = [ + "ansi_term", + "sharded-slab", + "smallvec", + "thread_local", + "tracing-core", + "tracing-log", +] + [[package]] name = "udev" version = "0.2.0" @@ -159,14 +624,177 @@ dependencies = [ "libudev-sys", ] +[[package]] +name = "unicode-ident" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5bd2fe26506023ed7b5e1e315add59d6f584c621d037f9368fea9cfb988f368c" + [[package]] name = "uninitialized" version = "0.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "74c1aa4511c38276c548406f0b1f5f8b793f000cfb51e18f278a102abd057e81" +[[package]] +name = "untrusted" +version = "0.7.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a156c684c91ea7d62626509bce3cb4e1d9ed5c4d978f7b4352658f96a4c26b4a" + +[[package]] +name = "valuable" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "830b7e5d4d90034032940e4ace0d9a9a057e7a45cd94e6c007832e39edb82f6d" + [[package]] name = "void" version = "1.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6a02e4885ed3bc0f2de90ea6dd45ebcbb66dacffe03547fadbb0eeae2770887d" + +[[package]] +name = "wasi" +version = "0.11.0+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" + +[[package]] +name = "wasm-bindgen" +version = "0.2.81" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7c53b543413a17a202f4be280a7e5c62a1c69345f5de525ee64f8cfdbc954994" +dependencies = [ + "cfg-if", + "wasm-bindgen-macro", +] + +[[package]] +name = "wasm-bindgen-backend" +version = "0.2.81" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5491a68ab4500fa6b4d726bd67408630c3dbe9c4fe7bda16d5c82a1fd8c7340a" +dependencies = [ + "bumpalo", + "lazy_static", + "log", + "proc-macro2", + "quote", + "syn", + "wasm-bindgen-shared", +] + +[[package]] +name = "wasm-bindgen-macro" +version = "0.2.81" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c441e177922bc58f1e12c022624b6216378e5febc2f0533e41ba443d505b80aa" +dependencies = [ + "quote", + "wasm-bindgen-macro-support", +] + +[[package]] +name = "wasm-bindgen-macro-support" +version = "0.2.81" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7d94ac45fcf608c1f45ef53e748d35660f168490c10b23704c7779ab8f5c3048" +dependencies = [ + "proc-macro2", + "quote", + "syn", + "wasm-bindgen-backend", + "wasm-bindgen-shared", +] + +[[package]] +name = "wasm-bindgen-shared" +version = "0.2.81" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6a89911bd99e5f3659ec4acf9c4d93b0a90fe4a2a11f15328472058edc5261be" + +[[package]] +name = "web-sys" +version = "0.3.58" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2fed94beee57daf8dd7d51f2b15dc2bcde92d7a72304cdf662a4371008b71b90" +dependencies = [ + "js-sys", + "wasm-bindgen", +] + +[[package]] +name = "webpki" +version = "0.22.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f095d78192e208183081cc07bc5515ef55216397af48b873e5edcd72637fa1bd" +dependencies = [ + "ring", + "untrusted", +] + +[[package]] +name = "winapi" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" +dependencies = [ + "winapi-i686-pc-windows-gnu", + "winapi-x86_64-pc-windows-gnu", +] + +[[package]] +name = "winapi-i686-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" + +[[package]] +name = "winapi-x86_64-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" + +[[package]] +name = "windows-sys" +version = "0.36.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ea04155a16a59f9eab786fe12a4a450e75cdb175f9e0d80da1e17db09f55b8d2" +dependencies = [ + "windows_aarch64_msvc", + "windows_i686_gnu", + "windows_i686_msvc", + "windows_x86_64_gnu", + "windows_x86_64_msvc", +] + +[[package]] +name = "windows_aarch64_msvc" +version = "0.36.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9bb8c3fd39ade2d67e9874ac4f3db21f0d710bee00fe7cab16949ec184eeaa47" + +[[package]] +name = "windows_i686_gnu" +version = "0.36.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "180e6ccf01daf4c426b846dfc66db1fc518f074baa793aa7d9b9aaeffad6a3b6" + +[[package]] +name = "windows_i686_msvc" +version = "0.36.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e2e7917148b2812d1eeafaeb22a97e4813dfa60a3f8f78ebe204bcc88f12f024" + +[[package]] +name = "windows_x86_64_gnu" +version = "0.36.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4dcd171b8776c41b97521e5da127a2d86ad280114807d0b2ab1e462bc764d9e1" + +[[package]] +name = "windows_x86_64_msvc" +version = "0.36.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c811ca4a8c853ef420abd8592ba53ddbbac90410fab6903b3e79972a631f7680" diff --git a/Cargo.toml b/Cargo.toml index 7973865..ebeb929 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,6 +7,9 @@ edition = "2021" ddc = "*" ddc-i2c = { version = "0.2.1", features = [ "with-linux-enumerate" ] } rosc = "*" +rumqttc = "0.13.0" +tracing = "0.1.35" +tracing-subscriber = "0.3.14" [[bin]] name = "ddcmqtt" diff --git a/flake.lock b/flake.lock index c6c2c5c..de21585 100644 --- a/flake.lock +++ b/flake.lock @@ -6,11 +6,11 @@ "nixpkgs": "nixpkgs" }, "locked": { - "lastModified": 1649691969, - "narHash": "sha256-nY1aUWIyh3TcGVo3sn+3vyCh+tOiEZL4JtMX3aOZSeY=", + "lastModified": 1655976588, + "narHash": "sha256-VreHyH6ITkf/1EX/8h15UqhddJnUleb0HgbC3gMkAEQ=", "owner": "numtide", "repo": "devshell", - "rev": "e22633b05fec2fe196888c593d4d9b3f4f648a25", + "rev": "899ca4629020592a13a46783587f6e674179d1db", "type": "github" }, "original": { @@ -36,11 +36,11 @@ }, "flake-utils_2": { "locked": { - "lastModified": 1649676176, - "narHash": "sha256-OWKJratjt2RW151VUlJPRALb7OU2S5s+f0vLj4o1bHM=", + "lastModified": 1656928814, + "narHash": "sha256-RIFfgBuKz6Hp89yRr7+NR5tzIAbn52h8vT6vXkYjZoM=", "owner": "numtide", "repo": "flake-utils", - "rev": "a4b154ebbdc88c8498a5c7b01589addc9e9cb678", + "rev": "7e2a3b3dfd9af950a856d66b0a7d01e3c18aa249", "type": "github" }, "original": { @@ -51,11 +51,11 @@ }, "flake-utils_3": { "locked": { - "lastModified": 1637014545, - "narHash": "sha256-26IZAc5yzlD9FlDT54io1oqG/bBoyka+FJk5guaX4x4=", + "lastModified": 1656065134, + "narHash": "sha256-oc6E6ByIw3oJaIyc67maaFcnjYOz1mMcOtHxbEf9NwQ=", "owner": "numtide", "repo": "flake-utils", - "rev": "bba5dcc8e0b20ab664967ad83d24d64cb64ec4f4", + "rev": "bee6a7250dd1b01844a2de7e02e4df7d8a0a206c", "type": "github" }, "original": { @@ -69,11 +69,11 @@ "nixpkgs": "nixpkgs_2" }, "locked": { - "lastModified": 1649096192, - "narHash": "sha256-7O8e+eZEYeU+ET98u/zW5epuoN/xYx9G+CIh4DjZVzY=", + "lastModified": 1655042882, + "narHash": "sha256-9BX8Fuez5YJlN7cdPO63InoyBy7dm3VlJkkmTt6fS1A=", "owner": "nix-community", "repo": "naersk", - "rev": "d626f73332a8f587b613b0afe7293dd0777be07d", + "rev": "cddffb5aa211f50c4b8750adbec0bbbdfb26bb9f", "type": "github" }, "original": { @@ -100,10 +100,9 @@ }, "nixpkgs_2": { "locked": { - "lastModified": 1641687203, - "narHash": "sha256-W6Xrb/l1x+E+WMVLw4q5HUnNjt3x4WQFSYJtjJtopbU=", - "path": "/nix/store/8hd2049cr4dz9myjcjjh6cjjapz1p107-source", - "rev": "00acdb2aa817048fbe1f91ece18fe7de09762531", + "lastModified": 0, + "narHash": "sha256-wYG86PUkPZ1P/oHsCpepTkb/U26poaEPPp1XFjRsgdA=", + "path": "/nix/store/0cj8vacqmw8wg4ahrg0jxhvag727s5rb-source", "type": "path" }, "original": { @@ -113,10 +112,9 @@ }, "nixpkgs_3": { "locked": { - "lastModified": 1641687203, - "narHash": "sha256-W6Xrb/l1x+E+WMVLw4q5HUnNjt3x4WQFSYJtjJtopbU=", - "path": "/nix/store/8hd2049cr4dz9myjcjjh6cjjapz1p107-source", - "rev": "00acdb2aa817048fbe1f91ece18fe7de09762531", + "lastModified": 0, + "narHash": "sha256-wYG86PUkPZ1P/oHsCpepTkb/U26poaEPPp1XFjRsgdA=", + "path": "/nix/store/0cj8vacqmw8wg4ahrg0jxhvag727s5rb-source", "type": "path" }, "original": { @@ -126,11 +124,11 @@ }, "nixpkgs_4": { "locked": { - "lastModified": 1637453606, - "narHash": "sha256-Gy6cwUswft9xqsjWxFYEnx/63/qzaFUwatcbV5GF/GQ=", + "lastModified": 1656401090, + "narHash": "sha256-bUS2nfQsvTQW2z8SK7oEFSElbmoBahOPtbXPm0AL3I4=", "owner": "NixOS", "repo": "nixpkgs", - "rev": "8afc4e543663ca0a6a4f496262cd05233737e732", + "rev": "16de63fcc54e88b9a106a603038dd5dd2feb21eb", "type": "github" }, "original": { @@ -155,11 +153,11 @@ "nixpkgs": "nixpkgs_4" }, "locked": { - "lastModified": 1649644592, - "narHash": "sha256-q4vQ54pmCC+b5+bdsH2/90jIeiydTAJhF2IvP13r3yA=", + "lastModified": 1657075953, + "narHash": "sha256-xVgbASGI/wODPpUiMexxZau+XDVOZMBbFmw2KsZyFKA=", "owner": "oxalica", "repo": "rust-overlay", - "rev": "4770c6c6cb518d2c5b660c5099816c3512d8bdad", + "rev": "339de634e6f19e420e81d5a6acaa39364a726999", "type": "github" }, "original": { diff --git a/flake.nix b/flake.nix index 6905237..fb3e9ac 100644 --- a/flake.nix +++ b/flake.nix @@ -6,35 +6,47 @@ inputs.naersk.url = "github:nix-community/naersk"; inputs.rust-overlay.url = "github:oxalica/rust-overlay"; - outputs = { self, flake-utils, devshell, nixpkgs, naersk, rust-overlay }: + outputs = { + self, + flake-utils, + devshell, + nixpkgs, + naersk, + rust-overlay, + }: flake-utils.lib.eachDefaultSystem (system: let pkgs = import nixpkgs { inherit system; - overlays = [ (import rust-overlay) ]; + overlays = [(import rust-overlay)]; }; naersk-lib = naersk.lib."${system}"; in { /* - defaultPackage = naersk-lib.buildPackage { - pname = "ddcmqtt"; - root = ./.; - nativeBuildInputs = with pkgs; [ pkgconfig libudev ]; - }; - /* devshell currently not really working for stuff that needs pkgconfig/libs - devShell = - let pkgs = import nixpkgs { - inherit system; - - overlays = [ devshell.overlay ]; - }; - in - pkgs.devshell.mkShell { - packages = with pkgs; [ rustc cargo gcc ]; - nativeBuildInputs = with pkgs; [ libudev pkgconfig ]; - }; - */ - devShell = pkgs.mkShell{ - nativeBuildInputs = with pkgs; [ rust-bin.stable.latest.default libudev pkgconfig ]; + defaultPackage = naersk-lib.buildPackage { + pname = "ddcmqtt"; + root = ./.; + nativeBuildInputs = with pkgs; [ pkgconfig libudev ]; + }; + /* devshell currently not really working for stuff that needs pkgconfig/libs + devShell = + let pkgs = import nixpkgs { + inherit system; + overlays = [ devshell.overlay ]; + }; + in + pkgs.devshell.mkShell { + packages = with pkgs; [ rustc cargo gcc ]; + nativeBuildInputs = with pkgs; [ libudev pkgconfig ]; + }; + */ + devShells.default = pkgs.mkShell { + nativeBuildInputs = with pkgs; [ + (rust-bin.stable.latest.default.override {extensions = ["rls"];}) + udev + pkgconfig + mosquitto # For testing mqtt with mosquitto_pub + ]; }; + formatter = pkgs.alejandra; }); } diff --git a/src/bin/ddcmqtt.rs b/src/bin/ddcmqtt.rs index 252fe98..988033b 100644 --- a/src/bin/ddcmqtt.rs +++ b/src/bin/ddcmqtt.rs @@ -1,5 +1,7 @@ use ::ddcmqtt::{main as main_, StdError}; +use tracing_subscriber; fn main() -> StdError<()> { + tracing_subscriber::fmt::init(); main_() } diff --git a/src/bin/setmon.rs b/src/bin/setmon.rs index 7d63edd..de93847 100644 --- a/src/bin/setmon.rs +++ b/src/bin/setmon.rs @@ -1,5 +1,4 @@ use ddc::Ddc; -use ddc_i2c::{I2cDeviceDdc, I2cDeviceEnumerator}; use ::ddcmqtt::StdError; diff --git a/src/lib.rs b/src/lib.rs index eab79cf..e6f82d9 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,12 +1,16 @@ use ddc::Ddc; use ddc_i2c::{I2cDeviceDdc, I2cDeviceEnumerator}; +use rosc; +use rumqttc::mqttbytes::v4::Packet; +use rumqttc::{Client, Event, MqttOptions, QoS}; use std::collections::HashMap; +use std::env; use std::error::Error; use std::net::UdpSocket; use std::sync::mpsc::{channel, Receiver, Sender}; use std::thread; use std::time::{Duration, Instant}; -use rosc; +use tracing::{event, Level}; pub type StdError = Result>; @@ -47,9 +51,9 @@ fn run_i2c(mut dev: I2cDeviceDdc, command_channel: Receiver) { } } match cmd { - MonitorCommand::Brightness(b) => dev.set_vcp_feature(0x10, b.into()).unwrap(), + MonitorCommand::Brightness(b) => dev.set_vcp_feature(cmd.vcp(), b.into()).unwrap(), // Hack - add 15 to align with DELL monitors - MonitorCommand::Input(i) => dev.set_vcp_feature(0x60, (i + 15).into()).unwrap(), + MonitorCommand::Input(i) => dev.set_vcp_feature(cmd.vcp(), (i + 15).into()).unwrap(), } last_sent_command.insert(cmd.cmd_str(), Some(Instant::now())); } @@ -66,7 +70,74 @@ pub fn main() -> StdError<()> { tx }) .collect(); - let sock = UdpSocket::bind("0.0.0.0:1234").unwrap(); + run_mqtt(&txes)?; + Ok(()) +} + +fn run_mqtt(txes: &Vec>) -> StdError<()> { + let client_id = env::var("MQTT_CLIENT_ID").unwrap_or("ddcmqtt".into()); + let mqtt_host = env::var("MQTT_HOST").unwrap_or("localhost".into()); + let mqtt_port = env::var("MQTT_PORT") + .unwrap_or("1883".into()) + .parse::()?; + let mqtt_user = env::var("MQTT_USER").ok(); + let mqtt_pass = env::var("MQTT_PASS").ok(); + let mut mqttoptions = MqttOptions::new(client_id, mqtt_host.clone(), mqtt_port); + // Set credentials if we have user and pass specified. + // TODO: warn if only one maybe? + if let (Some(u), Some(p)) = (mqtt_user, mqtt_pass) { + event!(Level::INFO, user = u, "Using MQTT user/pass from env"); + mqttoptions.set_credentials(u, p); + } + mqttoptions.set_keep_alive(Duration::from_secs(5)); + + let (mut client, mut connection) = Client::new(mqttoptions, 10); + client.subscribe("ddcmqtt/#", QoS::AtMostOnce)?; + + event!(Level::INFO, mqtt_host, mqtt_port, "Running MQTT client"); + + for notification in connection.iter() { + event!(Level::INFO, ?notification, "Got notification"); + if let Ok(Event::Incoming(Packet::Publish(p))) = notification { + let topic: Vec<_> = p.topic.split("/").collect(); + if topic.len() != 3 { + event!( + Level::WARN, + ?topic, + "Publish topic has wrong format, ignoring." + ); + continue; + } + if topic[0] != "ddcmqtt" { + event!( + Level::ERROR, + ?topic, + "Got publish that we didn't subscribe to!" + ); + continue; + } + let mon_idx = topic[1].parse::(); + match (mon_idx, topic[2]) { + (Ok(idx), "input") => { + // TODO: don't crash on these ?s - I'm feeling lazy + let input_id = std::str::from_utf8(&p.payload)?.parse::()?; + handle_cmd( + Command::Monitor((idx.into(), MonitorCommand::Input(input_id))), + &txes, + )?; + } + _ => { + event!(Level::ERROR, ?topic, "Unrecognised or invalid topic"); + continue; + } + } + } + } + Ok(()) +} + +fn run_osc(txes: &Vec>) -> StdError<()> { + let sock = UdpSocket::bind("0.0.0.0:1234")?; let mut buf = [0u8; rosc::decoder::MTU]; loop { @@ -89,7 +160,6 @@ pub fn main() -> StdError<()> { } } } - Ok(()) } fn handle_cmd(cmd: Command, txes: &Vec>) -> StdError<()> { @@ -98,6 +168,7 @@ fn handle_cmd(cmd: Command, txes: &Vec>) -> StdError<()> }; Ok(()) } + fn osc_message_to_command(msg: rosc::OscMessage) -> StdError { println!("OSC: {}, args: {:?}", msg.addr, msg.args); let splitaddr: Vec<_> = msg.addr.split("/").collect();