From cc34b02d0f061c0f5386a07a8f2b1cc993f4c499 Mon Sep 17 00:00:00 2001 From: Nareshkumar Rao Date: Tue, 30 Apr 2024 22:13:54 +0200 Subject: [PATCH] wip --- Cargo.lock | 609 ++++++++++++++++++++++++++++++++++++++++++++--- Cargo.toml | 11 +- src/actuators.rs | 46 ++-- src/cli_mode.rs | 83 +++---- src/control.rs | 3 + src/error.rs | 4 + src/io.rs | 21 +- src/main.rs | 39 ++- src/server.rs | 15 ++ src/state.rs | 14 ++ 10 files changed, 711 insertions(+), 134 deletions(-) create mode 100644 src/server.rs create mode 100644 src/state.rs diff --git a/Cargo.lock b/Cargo.lock index eb6b16d..f0f7753 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,6 +2,21 @@ # It is not intended for manual editing. version = 3 +[[package]] +name = "addr2line" +version = "0.21.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8a30b2e23b9e17a9f90641c7ab1549cd9b44f296d3ccbf309d2863cfe398a0cb" +dependencies = [ + "gimli", +] + +[[package]] +name = "adler" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe" + [[package]] name = "ads1x1x" version = "0.2.2" @@ -12,12 +27,105 @@ dependencies = [ "nb 1.1.0", ] +[[package]] +name = "async-trait" +version = "0.1.80" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c6fa2087f2753a7da8cc1c0dbfcf89579dd57458e36769de5ac750b4671737ca" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "axum" +version = "0.7.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3a6c9af12842a67734c9a2e355436e5d03b22383ed60cf13cd0c18fbfe3dcbcf" +dependencies = [ + "async-trait", + "axum-core", + "bytes", + "futures-util", + "http", + "http-body", + "http-body-util", + "hyper", + "hyper-util", + "itoa", + "matchit", + "memchr", + "mime", + "percent-encoding", + "pin-project-lite", + "rustversion", + "serde", + "serde_json", + "serde_path_to_error", + "serde_urlencoded", + "sync_wrapper 1.0.1", + "tokio", + "tower", + "tower-layer", + "tower-service", + "tracing", +] + +[[package]] +name = "axum-core" +version = "0.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a15c63fd72d41492dc4f497196f5da1fb04fb7529e631d73630d1b491e47a2e3" +dependencies = [ + "async-trait", + "bytes", + "futures-util", + "http", + "http-body", + "http-body-util", + "mime", + "pin-project-lite", + "rustversion", + "sync_wrapper 0.1.2", + "tower-layer", + "tower-service", + "tracing", +] + +[[package]] +name = "backtrace" +version = "0.3.71" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "26b05800d2e817c8b3b4b54abd461726265fa9789ae34330622f2db9ee696f9d" +dependencies = [ + "addr2line", + "cc", + "cfg-if", + "libc", + "miniz_oxide", + "object", + "rustc-demangle", +] + [[package]] name = "bitflags" version = "2.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cf4b9d6a944f767f8e5e0db018570623c85f3d925ac718db4e06d0187adb21c1" +[[package]] +name = "bytes" +version = "1.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "514de17de45fdb8dc022b1a7975556c53c86f9f0aa5f534b98977b171857c2c9" + +[[package]] +name = "cc" +version = "1.0.95" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d32a725bc159af97c3e629873bb9f88fb8cf8a4867175f76dc987815ea07c83b" + [[package]] name = "cfg-if" version = "1.0.0" @@ -84,7 +192,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a258e46cdc063eb8519c00b9fc845fc47bcfca4130e2f08e88665ceda8474245" dependencies = [ "libc", - "windows-sys", + "windows-sys 0.52.0", ] [[package]] @@ -101,7 +209,76 @@ checksum = "7e5768da2206272c81ef0b5e951a41862938a6070da63bcea197899942d3b947" dependencies = [ "cfg-if", "rustix", - "windows-sys", + "windows-sys 0.52.0", +] + +[[package]] +name = "fnv" +version = "1.0.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1" + +[[package]] +name = "form_urlencoded" +version = "1.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e13624c2627564efccf4934284bdd98cbaa14e79b0b5a141218e507b3a823456" +dependencies = [ + "percent-encoding", +] + +[[package]] +name = "futures-channel" +version = "0.3.30" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "eac8f7d7865dcb88bd4373ab671c8cf4508703796caa2b1985a9ca867b3fcb78" +dependencies = [ + "futures-core", +] + +[[package]] +name = "futures-core" +version = "0.3.30" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dfc6580bb841c5a68e9ef15c77ccc837b40a7504914d52e47b8b0e9bbda25a1d" + +[[package]] +name = "futures-task" +version = "0.3.30" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "38d84fa142264698cdce1a9f9172cf383a0c82de1bddcf3092901442c4097004" + +[[package]] +name = "futures-util" +version = "0.3.30" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3d6401deb83407ab3da39eba7e33987a73c3df0c82b4bb5813ee871c19c41d48" +dependencies = [ + "futures-core", + "futures-task", + "pin-project-lite", + "pin-utils", +] + +[[package]] +name = "gimli" +version = "0.28.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4271d37baee1b8c7e4b708028c57d816cf9d2434acb33a549475f78c181f6253" + +[[package]] +name = "growpi" +version = "0.1.0" +dependencies = [ + "ads1x1x", + "axum", + "libc", + "nb 1.1.0", + "rppal", + "rustyline", + "serde", + "tokio", + "toml", ] [[package]] @@ -116,7 +293,88 @@ version = "0.5.9" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e3d1354bf6b7235cb4a0576c2619fd4ed18183f689b12b006a0ee7329eeff9a5" dependencies = [ - "windows-sys", + "windows-sys 0.52.0", +] + +[[package]] +name = "http" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "21b9ddb458710bc376481b842f5da65cdf31522de232c1ca8146abce2a358258" +dependencies = [ + "bytes", + "fnv", + "itoa", +] + +[[package]] +name = "http-body" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1cac85db508abc24a2e48553ba12a996e87244a0395ce011e62b37158745d643" +dependencies = [ + "bytes", + "http", +] + +[[package]] +name = "http-body-util" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0475f8b2ac86659c21b64320d5d653f9efe42acd2a4e560073ec61a155a34f1d" +dependencies = [ + "bytes", + "futures-core", + "http", + "http-body", + "pin-project-lite", +] + +[[package]] +name = "httparse" +version = "1.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d897f394bad6a705d5f4104762e116a75639e470d80901eed05a860a95cb1904" + +[[package]] +name = "httpdate" +version = "1.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "df3b46402a9d5adb4c86a0cf463f42e19994e3ee891101b1841f30a545cb49a9" + +[[package]] +name = "hyper" +version = "1.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fe575dd17d0862a9a33781c8c4696a55c320909004a67a00fb286ba8b1bc496d" +dependencies = [ + "bytes", + "futures-channel", + "futures-util", + "http", + "http-body", + "httparse", + "httpdate", + "itoa", + "pin-project-lite", + "smallvec", + "tokio", +] + +[[package]] +name = "hyper-util" +version = "0.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ca38ef113da30126bbff9cd1705f9273e15d45498615d138b0c20279ac7a76aa" +dependencies = [ + "bytes", + "futures-util", + "http", + "http-body", + "hyper", + "pin-project-lite", + "socket2", + "tokio", ] [[package]] @@ -129,6 +387,12 @@ dependencies = [ "hashbrown", ] +[[package]] +name = "itoa" +version = "1.0.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "49f1f14873335454500d59611f1cf4a4b0f786f9ac11f4312a78e4cf2566695b" + [[package]] name = "libc" version = "0.2.153" @@ -147,12 +411,44 @@ version = "0.4.21" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "90ed8c1e510134f979dbc4f070f87d4313098b704861a105fe34231c70a3901c" +[[package]] +name = "matchit" +version = "0.7.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0e7465ac9959cc2b1404e8e2367b43684a6d13790fe23056cc8c6c5a6b7bcb94" + [[package]] name = "memchr" version = "2.7.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6c8640c5d730cb13ebd907d8d04b52f55ac9a2eec55b440c8892f40d56c76c1d" +[[package]] +name = "mime" +version = "0.3.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6877bb514081ee2a7ff5ef9de3281f14a4dd4bceac4c09388074a6b5df8a139a" + +[[package]] +name = "miniz_oxide" +version = "0.7.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9d811f3e15f28568be3407c8e7fdb6514c1cda3cb30683f15b6a1a1dc4ea14a7" +dependencies = [ + "adler", +] + +[[package]] +name = "mio" +version = "0.8.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a4a650543ca06a924e8b371db273b2756685faae30f8487da1b56505a8f78b0c" +dependencies = [ + "libc", + "wasi", + "windows-sys 0.48.0", +] + [[package]] name = "nb" version = "0.1.3" @@ -189,6 +485,59 @@ dependencies = [ "libc", ] +[[package]] +name = "object" +version = "0.32.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a6a622008b6e321afc04970976f62ee297fdbaa6f95318ca343e3eebb9648441" +dependencies = [ + "memchr", +] + +[[package]] +name = "once_cell" +version = "1.19.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3fdb12b2476b595f9358c5161aa467c2438859caa136dec86c26fdd2efe17b92" + +[[package]] +name = "percent-encoding" +version = "2.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e3148f5046208a5d56bcfc03053e3ca6334e51da8dfb19b6cdc8b306fae3283e" + +[[package]] +name = "pin-project" +version = "1.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b6bf43b791c5b9e34c3d182969b4abb522f9343702850a2e57f460d00d09b4b3" +dependencies = [ + "pin-project-internal", +] + +[[package]] +name = "pin-project-internal" +version = "1.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2f38a4412a78282e09a2cf38d195ea5420d15ba0602cb375210efbc877243965" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "pin-project-lite" +version = "0.2.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bda66fc9667c18cb2758a2ac84d1167245054bcf85d5d1aaa6923f45801bdd02" + +[[package]] +name = "pin-utils" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184" + [[package]] name = "proc-macro2" version = "1.0.81" @@ -217,19 +566,6 @@ dependencies = [ "nibble_vec", ] -[[package]] -name = "rpitest" -version = "0.1.0" -dependencies = [ - "ads1x1x", - "libc", - "nb 1.1.0", - "rppal", - "rustyline", - "serde", - "toml", -] - [[package]] name = "rppal" version = "0.17.1" @@ -245,6 +581,12 @@ dependencies = [ "void", ] +[[package]] +name = "rustc-demangle" +version = "0.1.23" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d626bb9dae77e28219937af045c257c28bfd3f69333c512553507f5f9798cb76" + [[package]] name = "rustix" version = "0.38.34" @@ -255,9 +597,15 @@ dependencies = [ "errno", "libc", "linux-raw-sys", - "windows-sys", + "windows-sys 0.52.0", ] +[[package]] +name = "rustversion" +version = "1.0.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "80af6f9131f277a45a3fba6ce8e2258037bb0477a67e610d3c1fe046ab31de47" + [[package]] name = "rustyline" version = "14.0.0" @@ -277,9 +625,15 @@ dependencies = [ "unicode-segmentation", "unicode-width", "utf8parse", - "windows-sys", + "windows-sys 0.52.0", ] +[[package]] +name = "ryu" +version = "1.0.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e86697c916019a8588c99b5fac3cead74ec0b4b819707a682fd4d23fa0ce1ba1" + [[package]] name = "serde" version = "1.0.198" @@ -300,6 +654,27 @@ dependencies = [ "syn", ] +[[package]] +name = "serde_json" +version = "1.0.116" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3e17db7126d17feb94eb3fad46bf1a96b034e8aacbc2e775fe81505f8b0b2813" +dependencies = [ + "itoa", + "ryu", + "serde", +] + +[[package]] +name = "serde_path_to_error" +version = "0.1.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "af99884400da37c88f5e9146b7f1fd0fbcae8f6eec4e9da38b67d05486f814a6" +dependencies = [ + "itoa", + "serde", +] + [[package]] name = "serde_spanned" version = "0.6.5" @@ -309,19 +684,41 @@ dependencies = [ "serde", ] +[[package]] +name = "serde_urlencoded" +version = "0.7.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d3491c14715ca2294c4d6a88f15e84739788c1d030eed8c110436aafdaa2f3fd" +dependencies = [ + "form_urlencoded", + "itoa", + "ryu", + "serde", +] + [[package]] name = "smallvec" version = "1.13.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3c5e1a9a646d36c3599cd173a41282daf47c44583ad367b8e6837255952e5c67" +[[package]] +name = "socket2" +version = "0.5.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "05ffd9c0a93b7543e062e759284fcf5f5e3b098501104bfbdde4d404db792871" +dependencies = [ + "libc", + "windows-sys 0.52.0", +] + [[package]] name = "spin_sleep" version = "1.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "368a978649eaf70006b082e79c832bd72556ac1393eaf564d686e919dca2347f" dependencies = [ - "windows-sys", + "windows-sys 0.52.0", ] [[package]] @@ -335,6 +732,44 @@ dependencies = [ "unicode-ident", ] +[[package]] +name = "sync_wrapper" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2047c6ded9c721764247e62cd3b03c09ffc529b2ba5b10ec482ae507a4a70160" + +[[package]] +name = "sync_wrapper" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a7065abeca94b6a8a577f9bd45aa0867a2238b74e8eb67cf10d492bc39351394" + +[[package]] +name = "tokio" +version = "1.37.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1adbebffeca75fcfd058afa480fb6c0b81e165a0323f9c9d39c9697e37c46787" +dependencies = [ + "backtrace", + "libc", + "mio", + "pin-project-lite", + "socket2", + "tokio-macros", + "windows-sys 0.48.0", +] + +[[package]] +name = "tokio-macros" +version = "2.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5b8a1e28f2deaa14e508979454cb3a223b10b938b45af148bc0986de36f1923b" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "toml" version = "0.8.12" @@ -369,6 +804,54 @@ dependencies = [ "winnow", ] +[[package]] +name = "tower" +version = "0.4.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b8fa9be0de6cf49e536ce1851f987bd21a43b771b09473c3549a6c853db37c1c" +dependencies = [ + "futures-core", + "futures-util", + "pin-project", + "pin-project-lite", + "tokio", + "tower-layer", + "tower-service", + "tracing", +] + +[[package]] +name = "tower-layer" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c20c8dbed6283a09604c3e69b4b7eeb54e298b8a600d4d5ecb5ad39de609f1d0" + +[[package]] +name = "tower-service" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b6bc1c9ce2b5135ac7f93c72918fc37feb872bdc6a5533a8b85eb4b86bfdae52" + +[[package]] +name = "tracing" +version = "0.1.40" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c3523ab5a71916ccf420eebdf5521fcef02141234bbc0b8a49f2fdc4544364ef" +dependencies = [ + "log", + "pin-project-lite", + "tracing-core", +] + +[[package]] +name = "tracing-core" +version = "0.1.32" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c06d3da6113f116aaee68e4d601191614c9053067f9ab7f6edbcb161237daa54" +dependencies = [ + "once_cell", +] + [[package]] name = "unicode-ident" version = "1.0.12" @@ -399,13 +882,43 @@ 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 = "windows-sys" +version = "0.48.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "677d2418bec65e3338edb076e806bc1ec15693c5d0104683f2efe857f61056a9" +dependencies = [ + "windows-targets 0.48.5", +] + [[package]] name = "windows-sys" version = "0.52.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "282be5f36a8ce781fad8c8ae18fa3f9beff57ec1b52cb3de0789201425d9a33d" dependencies = [ - "windows-targets", + "windows-targets 0.52.5", +] + +[[package]] +name = "windows-targets" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9a2fa6e2155d7247be68c096456083145c183cbbbc2764150dda45a87197940c" +dependencies = [ + "windows_aarch64_gnullvm 0.48.5", + "windows_aarch64_msvc 0.48.5", + "windows_i686_gnu 0.48.5", + "windows_i686_msvc 0.48.5", + "windows_x86_64_gnu 0.48.5", + "windows_x86_64_gnullvm 0.48.5", + "windows_x86_64_msvc 0.48.5", ] [[package]] @@ -414,28 +927,46 @@ version = "0.52.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6f0713a46559409d202e70e28227288446bf7841d3211583a4b53e3f6d96e7eb" dependencies = [ - "windows_aarch64_gnullvm", - "windows_aarch64_msvc", - "windows_i686_gnu", + "windows_aarch64_gnullvm 0.52.5", + "windows_aarch64_msvc 0.52.5", + "windows_i686_gnu 0.52.5", "windows_i686_gnullvm", - "windows_i686_msvc", - "windows_x86_64_gnu", - "windows_x86_64_gnullvm", - "windows_x86_64_msvc", + "windows_i686_msvc 0.52.5", + "windows_x86_64_gnu 0.52.5", + "windows_x86_64_gnullvm 0.52.5", + "windows_x86_64_msvc 0.52.5", ] +[[package]] +name = "windows_aarch64_gnullvm" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2b38e32f0abccf9987a4e3079dfb67dcd799fb61361e53e2882c3cbaf0d905d8" + [[package]] name = "windows_aarch64_gnullvm" version = "0.52.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7088eed71e8b8dda258ecc8bac5fb1153c5cffaf2578fc8ff5d61e23578d3263" +[[package]] +name = "windows_aarch64_msvc" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dc35310971f3b2dbbf3f0690a219f40e2d9afcf64f9ab7cc1be722937c26b4bc" + [[package]] name = "windows_aarch64_msvc" version = "0.52.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9985fd1504e250c615ca5f281c3f7a6da76213ebd5ccc9561496568a2752afb6" +[[package]] +name = "windows_i686_gnu" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a75915e7def60c94dcef72200b9a8e58e5091744960da64ec734a6c6e9b3743e" + [[package]] name = "windows_i686_gnu" version = "0.52.5" @@ -448,24 +979,48 @@ version = "0.52.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "87f4261229030a858f36b459e748ae97545d6f1ec60e5e0d6a3d32e0dc232ee9" +[[package]] +name = "windows_i686_msvc" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8f55c233f70c4b27f66c523580f78f1004e8b5a8b659e05a4eb49d4166cca406" + [[package]] name = "windows_i686_msvc" version = "0.52.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "db3c2bf3d13d5b658be73463284eaf12830ac9a26a90c717b7f771dfe97487bf" +[[package]] +name = "windows_x86_64_gnu" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "53d40abd2583d23e4718fddf1ebec84dbff8381c07cae67ff7768bbf19c6718e" + [[package]] name = "windows_x86_64_gnu" version = "0.52.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4e4246f76bdeff09eb48875a0fd3e2af6aada79d409d33011886d3e1581517d9" +[[package]] +name = "windows_x86_64_gnullvm" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0b7b52767868a23d5bab768e390dc5f5c55825b6d30b86c844ff2dc7414044cc" + [[package]] name = "windows_x86_64_gnullvm" version = "0.52.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "852298e482cd67c356ddd9570386e2862b5673c85bd5f88df9ab6802b334c596" +[[package]] +name = "windows_x86_64_msvc" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ed94fce61571a4006852b7389a063ab983c02eb1bb37b47f8272ce92d06d9538" + [[package]] name = "windows_x86_64_msvc" version = "0.52.5" diff --git a/Cargo.toml b/Cargo.toml index e31c65b..c70e1fd 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,16 +1,15 @@ [package] -name = "rpitest" +name = "growpi" version = "0.1.0" edition = "2021" -# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html - [dependencies] rppal = { version = "0.17", features = ["hal"] } -#rumqttc = "0.24.0" libc = "0.2" ads1x1x = "0.2" -nb = "1.1.0" -rustyline = "14.0.0" +nb = "1.1" +rustyline = "14.0" serde = { "version" = "1.0", features = ["derive"] } toml = "0.8" +axum = "0.7" +tokio = { "version" = "1.37" } diff --git a/src/actuators.rs b/src/actuators.rs index 76f2593..bfc9007 100644 --- a/src/actuators.rs +++ b/src/actuators.rs @@ -1,45 +1,41 @@ use std::{thread, time::Duration}; -use crate::{ - config::*, - error::GenericResult, - io::{Relay, RelaySwitchState}, -}; +use crate::{error::GenericResult, io::RelaySwitchState, state::ProgramState}; pub fn switch_lights( - relay: &mut Relay, state: RelaySwitchState, - config: &Configuration, + program_state: &mut ProgramState, ) -> GenericResult<()> { - relay.switch(config.relay_settings.light_pin, state, config) + program_state + .relay + .switch(program_state.config.relay_settings.light_pin, state) } -pub fn switch_fan( - relay: &mut Relay, - state: RelaySwitchState, - config: &Configuration, -) -> GenericResult<()> { - relay.switch(config.relay_settings.fan_pin, state, config) +pub fn switch_fan(state: RelaySwitchState, program_state: &mut ProgramState) -> GenericResult<()> { + program_state + .relay + .switch(program_state.config.relay_settings.fan_pin, state) } pub fn switch_water_pump( - relay: &mut Relay, state: RelaySwitchState, - config: &Configuration, + program_state: &mut ProgramState, ) -> GenericResult<()> { - relay.switch(config.relay_settings.water_pump_pin, state, config) + program_state + .relay + .switch(program_state.config.relay_settings.water_pump_pin, state) } -pub fn pump_water( - water_mass_g: u16, - relay: &mut Relay, - config: &Configuration, -) -> GenericResult<()> { - let duration_ms = water_mass_g as f32 / config.water_pump_settings.grams_per_millisecond; +pub fn pump_water(water_mass_g: u16, program_state: &mut ProgramState) -> GenericResult<()> { + let duration_ms = water_mass_g as f32 + / program_state + .config + .water_pump_settings + .grams_per_millisecond; let duration_ms = duration_ms.round() as u64; let duration = Duration::from_millis(duration_ms); - switch_water_pump(relay, RelaySwitchState::On, config)?; + switch_water_pump(RelaySwitchState::On, program_state)?; thread::sleep(duration); - switch_water_pump(relay, RelaySwitchState::Off, config)?; + switch_water_pump(RelaySwitchState::Off, program_state)?; Ok(()) } diff --git a/src/cli_mode.rs b/src/cli_mode.rs index 08c179d..ba7f942 100644 --- a/src/cli_mode.rs +++ b/src/cli_mode.rs @@ -4,29 +4,25 @@ use rustyline::{config::Configurer, error::ReadlineError, history::FileHistory}; use crate::{ actuators, - config::Configuration, - error::GenericResult, + error::{lock_err, GenericResult}, io::{self, get_input_voltage}, sensors, + state::ProgramStateShared, }; struct LoopFlags { exit: bool, } -fn process_input( - input: String, - program_state: &mut ProgramState, - config: &Configuration, -) -> GenericResult { +fn process_input(input: String, program_state: ProgramStateShared) -> GenericResult { let args = input.split(' ').collect::>(); let main_command = *args.first().ok_or("No main command found.")?; match main_command { "ana" => command_ana(&args)?, - "rel" => command_rel(&args, program_state, config)?, - "soil" => command_soil(&args, config)?, - "temp" => command_temp(&args, config)?, - "pump" => command_pump(&args, program_state, config)?, + "rel" => command_rel(&args, program_state)?, + "soil" => command_soil(&args, program_state)?, + "temp" => command_temp(&args, program_state)?, + "pump" => command_pump(&args, program_state)?, "exit" => return Ok(LoopFlags { exit: true }), _ => return Err("Unknown main command".into()), }; @@ -34,11 +30,9 @@ fn process_input( Ok(LoopFlags { exit: false }) } -fn command_pump( - args: &[&str], - program_state: &mut ProgramState, - config: &Configuration, -) -> GenericResult<()> { +fn command_pump(args: &[&str], program_state: ProgramStateShared) -> GenericResult<()> { + let mut program_state = program_state.lock().map_err(lock_err)?; + let use_grams = args .get(2) .map(|arg| matches!(*arg, "grams")) @@ -46,26 +40,27 @@ fn command_pump( if use_grams { let grams: u16 = args.get(1).ok_or("No mass specified.")?.parse()?; - actuators::pump_water(grams, &mut program_state.relay, config)?; + actuators::pump_water(grams, &mut program_state)?; return Ok(()); } let duration_ms: u64 = args.get(1).ok_or("No duration specified.")?.parse()?; let duration = Duration::from_millis(duration_ms); - actuators::switch_water_pump(&mut program_state.relay, io::RelaySwitchState::On, config)?; + actuators::switch_water_pump(io::RelaySwitchState::On, &mut program_state)?; thread::sleep(duration); - actuators::switch_water_pump(&mut program_state.relay, io::RelaySwitchState::Off, config)?; + actuators::switch_water_pump(io::RelaySwitchState::Off, &mut program_state)?; Ok(()) } -fn command_temp(args: &[&str], config: &Configuration) -> GenericResult<()> { +fn command_temp(args: &[&str], program_state: ProgramStateShared) -> GenericResult<()> { let show_loop = args .get(1) .map(|arg| matches!(*arg, "loop")) .unwrap_or(false); loop { - let temperature = sensors::get_temperature(config)?; + let program_state = program_state.lock().map_err(lock_err)?; + let temperature = sensors::get_temperature(&program_state.config)?; println!("Temperature: {}C", temperature); if !show_loop { break; @@ -75,14 +70,15 @@ fn command_temp(args: &[&str], config: &Configuration) -> GenericResult<()> { Ok(()) } -fn command_soil(args: &[&str], config: &Configuration) -> GenericResult<()> { +fn command_soil(args: &[&str], program_state: ProgramStateShared) -> GenericResult<()> { let show_loop = args .get(1) .map(|arg| matches!(*arg, "loop")) .unwrap_or(false); loop { - let humidity = sensors::get_soil_moisture(config)?; + let program_state = program_state.lock().map_err(lock_err)?; + let humidity = sensors::get_soil_moisture(&program_state.config)?; println!("Soil humidity: {}", humidity); if !show_loop { break; @@ -93,11 +89,9 @@ fn command_soil(args: &[&str], config: &Configuration) -> GenericResult<()> { Ok(()) } -fn command_rel( - args: &[&str], - program_state: &mut ProgramState, - config: &Configuration, -) -> GenericResult<()> { +fn command_rel(args: &[&str], program_state: ProgramStateShared) -> GenericResult<()> { + let mut program_state = program_state.lock().map_err(lock_err)?; + let pin = args .get(1) .ok_or("Must specify pin number.")? @@ -117,11 +111,11 @@ fn command_rel( match switch_state { Some(state) => { println!("Switching relay"); - program_state.relay.switch(pin, state?, config)? + program_state.relay.switch(pin, state?)? } None => { println!("Toggling relay"); - program_state.relay.toggle(pin, config)? + program_state.relay.toggle(pin)? } }; @@ -152,17 +146,13 @@ fn command_ana(args: &[&str]) -> GenericResult<()> { Ok(()) } -fn cli_loop( - rl: &mut CLIEditor, - program_state: &mut ProgramState, - config: &Configuration, -) -> GenericResult { +fn cli_loop(rl: &mut CLIEditor, program_state: ProgramStateShared) -> GenericResult { let readline = rl.readline("growpi>> "); match readline { Ok(line) => { rl.add_history_entry(line.as_str())?; - process_input(line, program_state, config) + process_input(line, program_state) } Err(ReadlineError::Eof) => Ok(LoopFlags { exit: true }), Err(_) => Err("No input".into()), @@ -175,29 +165,12 @@ fn init_readline() -> GenericResult { rl.set_max_history_size(10)?; Ok(rl) } -struct ProgramState { - relay: io::Relay, -} - -fn init_state(config: &Configuration) -> GenericResult { - Ok(ProgramState { - relay: io::Relay::new(config)?, - }) -} -pub fn run_cli() { +pub fn run_cli(program_state: ProgramStateShared) { let mut rl = init_readline().unwrap(); - let config = Configuration::from_file(std::path::Path::new("./growpi.toml")); - if let Err(config) = &config { - println!("Could not load config: {}", config); - } - let config = config.unwrap_or_default(); - - let mut program_state = init_state(&config).unwrap(); - 'cli_loop: loop { - match cli_loop(&mut rl, &mut program_state, &config) { + match cli_loop(&mut rl, program_state.clone()) { Ok(loop_flags) => { if loop_flags.exit { println!("Leaving CLI"); diff --git a/src/control.rs b/src/control.rs index e69de29..5f936ed 100644 --- a/src/control.rs +++ b/src/control.rs @@ -0,0 +1,3 @@ +use crate::state::ProgramStateShared; + +pub fn control_thread(program_state: ProgramStateShared) {} diff --git a/src/error.rs b/src/error.rs index 9fc7342..8f4e5aa 100644 --- a/src/error.rs +++ b/src/error.rs @@ -1,3 +1,7 @@ use std::error::Error; pub type GenericResult = Result>; + +pub fn lock_err(e: T) -> String { + format!("{}", e) +} diff --git a/src/io.rs b/src/io.rs index 482ec3e..3ea7f25 100644 --- a/src/io.rs +++ b/src/io.rs @@ -59,19 +59,14 @@ impl Relay { relay_pins: output_pins, }) } - pub fn toggle(&mut self, pin: u8, config: &Configuration) -> GenericResult<()> { - let pin = self.get_output_pin(pin, config)?; + pub fn toggle(&mut self, pin: u8) -> GenericResult<()> { + let pin = self.get_output_pin(pin)?; pin.toggle(); Ok(()) } - pub fn switch( - &mut self, - pin: u8, - state: RelaySwitchState, - config: &Configuration, - ) -> GenericResult<()> { - let pin = self.get_output_pin(pin, config)?; + pub fn switch(&mut self, pin: u8, state: RelaySwitchState) -> GenericResult<()> { + let pin = self.get_output_pin(pin)?; match state { RelaySwitchState::On => pin.set_low(), RelaySwitchState::Off => pin.set_high(), @@ -79,15 +74,11 @@ impl Relay { Ok(()) } - fn get_output_pin(&mut self, pin: u8, config: &Configuration) -> GenericResult<&mut OutputPin> { + fn get_output_pin(&mut self, pin: u8) -> GenericResult<&mut OutputPin> { Ok(self .relay_pins .get_mut(pin as usize) - .ok_or(format!( - "Pin {} not within pin array with length {}", - pin, - config.relay_settings.relay_gpio_pins.len() - ))? + .ok_or(format!("Pin {} not within pin array", pin,))? .as_mut() .ok_or("Pin not configured.")?) } diff --git a/src/main.rs b/src/main.rs index 49736fb..e5dacfb 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,20 +1,47 @@ #![allow(dead_code)] +use std::thread; + use cli_mode::run_cli; +use config::Configuration; +use server::run_server; +use state::init_state; mod actuators; mod cli_mode; mod config; +mod control; mod error; mod io; mod sensors; +mod server; +mod state; + +fn load_config() -> config::Configuration { + let config = Configuration::from_file(std::path::Path::new("./growpi.toml")); + if let Err(config) = &config { + println!("Could not load config: {}", config); + } + + config.unwrap_or_default() +} + +#[tokio::main(flavor = "current_thread")] +async fn main() { + let config = load_config(); + let program_state = init_state(config).unwrap(); + + let program_state_clone = program_state.clone(); + let control_thread_handle = thread::spawn(move || control::control_thread(program_state_clone)); -fn main() { let args = std::env::args().collect::>(); - if let Some(mode) = args.first().map(|x| x.as_str()) { - match mode { - "cli" => run_cli(), - _ => run_cli(), - } + + let mode = args.get(1).map(|x| x.as_str()); + + match mode { + Some("cli") => run_cli(program_state), + _ => run_server().await, } + + let _ = control_thread_handle.join(); } diff --git a/src/server.rs b/src/server.rs new file mode 100644 index 0000000..6c77d09 --- /dev/null +++ b/src/server.rs @@ -0,0 +1,15 @@ +use axum::{response::Html, routing::get, Router}; + +pub async fn run_server() { + let app: Router = setup_router(); + let listener = tokio::net::TcpListener::bind("0.0.0.0:2205").await.unwrap(); + axum::serve(listener, app).await.unwrap(); +} + +fn setup_router() -> Router { + Router::new().route("/", get(handler)) +} + +async fn handler() -> Html<&'static str> { + Html("hi") +} diff --git a/src/state.rs b/src/state.rs new file mode 100644 index 0000000..0c324ac --- /dev/null +++ b/src/state.rs @@ -0,0 +1,14 @@ +use std::sync::{Arc, Mutex}; + +use crate::{config::Configuration, error::GenericResult, io}; + +pub type ProgramStateShared = Arc>; +pub struct ProgramState { + pub config: Configuration, + pub relay: io::Relay, +} + +pub fn init_state(config: Configuration) -> GenericResult { + let relay = io::Relay::new(&config)?; + Ok(Arc::new(Mutex::new(ProgramState { config, relay }))) +}